From ef38b4087b0a227416760a5be2a2b8bc436534b5 Mon Sep 17 00:00:00 2001 From: Andrea Boriero Date: Wed, 28 Mar 2018 11:10:15 +0100 Subject: [PATCH] create account and other minor changes --- .../account/core/service/UserService.java | 3 +- .../account/repo/service/UserServiceIT.java | 10 +++++ .../repo/service/util/DeploymentUtil.java | 2 +- .../src/app/account.service.ts | 43 +++++++++++++++++++ .../src/app/app-routing.module.ts | 3 ++ .../message-board-web/src/app/app.module.ts | 3 +- .../src/app/login.service.ts | 1 - .../src/app/login/login.component.html | 2 +- .../src/app/login/login.component.ts | 4 +- .../src/app/signin/signin.component.html | 14 ++++-- .../src/app/signin/signin.component.ts | 16 ++++++- 11 files changed, 89 insertions(+), 12 deletions(-) create mode 100644 other/openshift/message-board/message-board-web/src/app/account.service.ts diff --git a/other/openshift/message-board/account-service/src/main/java/org/hibernate/demo/message/account/core/service/UserService.java b/other/openshift/message-board/account-service/src/main/java/org/hibernate/demo/message/account/core/service/UserService.java index 277fd83a..f289ee18 100644 --- a/other/openshift/message-board/account-service/src/main/java/org/hibernate/demo/message/account/core/service/UserService.java +++ b/other/openshift/message-board/account-service/src/main/java/org/hibernate/demo/message/account/core/service/UserService.java @@ -45,8 +45,9 @@ public User findByUsername(@PathParam("username") String username) { @POST @Consumes(MediaType.APPLICATION_JSON) - public void createNewUser(User user) { + public User createNewUser(User user) { repo.add( user ); + return user; } } diff --git a/other/openshift/message-board/account-service/src/test/java/org/hibernate/demo/message/account/repo/service/UserServiceIT.java b/other/openshift/message-board/account-service/src/test/java/org/hibernate/demo/message/account/repo/service/UserServiceIT.java index 32afdf49..e5ffa46b 100644 --- a/other/openshift/message-board/account-service/src/test/java/org/hibernate/demo/message/account/repo/service/UserServiceIT.java +++ b/other/openshift/message-board/account-service/src/test/java/org/hibernate/demo/message/account/repo/service/UserServiceIT.java @@ -20,6 +20,9 @@ import org.jboss.arquillian.junit.Arquillian; import org.jboss.shrinkwrap.api.spec.WebArchive; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.IsNull.notNullValue; + /** * @author Andrea Boriero */ @@ -37,4 +40,11 @@ public static WebArchive create() { public void testFindUser(){ userService.findByUsername( "NON_EXISTING_USER" ); } + + @Test + public void testCreateUser() { + User user = new User( "fabio" ); + User createdUser = userService.createNewUser( user ); + assertThat(createdUser.getId(), notNullValue()); + } } diff --git a/other/openshift/message-board/account-service/src/test/java/org/hibernate/demo/message/account/repo/service/util/DeploymentUtil.java b/other/openshift/message-board/account-service/src/test/java/org/hibernate/demo/message/account/repo/service/util/DeploymentUtil.java index 915a4086..ee295c7d 100644 --- a/other/openshift/message-board/account-service/src/test/java/org/hibernate/demo/message/account/repo/service/util/DeploymentUtil.java +++ b/other/openshift/message-board/account-service/src/test/java/org/hibernate/demo/message/account/repo/service/util/DeploymentUtil.java @@ -36,7 +36,7 @@ private static PersistenceDescriptor persistenceXml() { .jtaDataSource( "java:jboss/datasources/ExampleDS" ) .getOrCreateProperties() // We want to use the ORM from this build instead of the one coming with WildFly - .createProperty().name( "jboss.as.jpa.providerModule" ).value( "org.hibernate:" + ORM_MINOR_VERSION ).up() +// .createProperty().name( "jboss.as.jpa.providerModule" ).value( "org.hibernate:" + ORM_MINOR_VERSION ).up() .createProperty().name( "hibernate.hbm2ddl.auto" ).value( "create-drop" ).up() .up().up(); } diff --git a/other/openshift/message-board/message-board-web/src/app/account.service.ts b/other/openshift/message-board/message-board-web/src/app/account.service.ts new file mode 100644 index 00000000..580dcd91 --- /dev/null +++ b/other/openshift/message-board/message-board-web/src/app/account.service.ts @@ -0,0 +1,43 @@ +import {Injectable} from '@angular/core'; +import {HttpClient, HttpHeaders} from '@angular/common/http'; +import {Observable} from 'rxjs/Observable'; +import {catchError, map, tap} from 'rxjs/operators'; +import {of} from 'rxjs/observable/of'; +import 'rxjs/add/operator/map'; + +import {User} from './user'; + +const httpOptions = { + headers: new HttpHeaders({'Content-Type': 'application/json'}) +}; + +@Injectable() +export class AccountService { + private loginUrl = 'users'; + + private user: User; + + constructor( + private http: HttpClient) { + + } + + createAccount(user: User): Observable { + const url = `http://web-message-board.192.168.42.57.nip.io/users}`; + console.log(url); + return this.http.post(url, user, httpOptions).map( + newUser => { + console.log('user -> ' + newUser.id); + if (newUser.id == null) { + console.log('user is empty'); + Observable.throw('Username or password is incorrect'); + + } else { + console.log('a new user '); + localStorage.setItem('currentUser', JSON.stringify(user)); + } + return newUser; + } + ); + } +} \ No newline at end of file diff --git a/other/openshift/message-board/message-board-web/src/app/app-routing.module.ts b/other/openshift/message-board/message-board-web/src/app/app-routing.module.ts index c33a5bf7..3f215bf4 100644 --- a/other/openshift/message-board/message-board-web/src/app/app-routing.module.ts +++ b/other/openshift/message-board/message-board-web/src/app/app-routing.module.ts @@ -4,12 +4,15 @@ import {CommonModule} from '@angular/common'; import {MessageComponent} from './message/message.component'; import {LoginComponent} from './login/login.component'; +import {SigninComponent} from './signin/signin.component'; + const routes: Routes = [ { path: '', redirectTo: '/message', pathMatch: 'full' }, { path: 'message', component: MessageComponent }, { path: 'message/:userName', component: MessageComponent }, { path: 'login', component: LoginComponent }, + { path: 'register', component: SigninComponent}, ]; @NgModule({ diff --git a/other/openshift/message-board/message-board-web/src/app/app.module.ts b/other/openshift/message-board/message-board-web/src/app/app.module.ts index 30a74be7..491799c3 100644 --- a/other/openshift/message-board/message-board-web/src/app/app.module.ts +++ b/other/openshift/message-board/message-board-web/src/app/app.module.ts @@ -7,6 +7,7 @@ import {AppRoutingModule} from './app-routing.module'; import {LoginService} from './login.service'; import {MessageService} from './message.service'; +import {AccountService} from './account.service'; import {AppComponent} from './app.component'; import {LoginComponent} from './login/login.component'; @@ -31,7 +32,7 @@ import {MessageComponent} from './message/message.component'; PostMessageComponent, MessageComponent ], - providers: [LoginService, MessageService], + providers: [LoginService, MessageService, AccountService], bootstrap: [AppComponent] }) export class AppModule {} diff --git a/other/openshift/message-board/message-board-web/src/app/login.service.ts b/other/openshift/message-board/message-board-web/src/app/login.service.ts index 550fb199..147e9f12 100644 --- a/other/openshift/message-board/message-board-web/src/app/login.service.ts +++ b/other/openshift/message-board/message-board-web/src/app/login.service.ts @@ -30,7 +30,6 @@ export class LoginService { if (user.id == null) { console.log('user is empty'); Observable.throw('Username or password is incorrect'); - } else { console.log('a new user '); localStorage.setItem('currentUser', JSON.stringify(user)); diff --git a/other/openshift/message-board/message-board-web/src/app/login/login.component.html b/other/openshift/message-board/message-board-web/src/app/login/login.component.html index e73a3c85..8d74b7bc 100644 --- a/other/openshift/message-board/message-board-web/src/app/login/login.component.html +++ b/other/openshift/message-board/message-board-web/src/app/login/login.component.html @@ -14,7 +14,7 @@

Login

- Register + Register
\ No newline at end of file diff --git a/other/openshift/message-board/message-board-web/src/app/login/login.component.ts b/other/openshift/message-board/message-board-web/src/app/login/login.component.ts index 735ea6af..b982cea2 100644 --- a/other/openshift/message-board/message-board-web/src/app/login/login.component.ts +++ b/other/openshift/message-board/message-board-web/src/app/login/login.component.ts @@ -11,14 +11,14 @@ import {User} from '../user'; }) export class LoginComponent implements OnInit { - user: User = {'id': 0, 'userName': ''}; + user: User; loading = false; returnUrl: string; constructor( private loginService: LoginService, private route: ActivatedRoute, - private router: Router, ) { + private router: Router ) { this.loginService.logout(); // get return url from route parameters or default to '/' diff --git a/other/openshift/message-board/message-board-web/src/app/signin/signin.component.html b/other/openshift/message-board/message-board-web/src/app/signin/signin.component.html index 21a29bd0..5b80ec61 100644 --- a/other/openshift/message-board/message-board-web/src/app/signin/signin.component.html +++ b/other/openshift/message-board/message-board-web/src/app/signin/signin.component.html @@ -1,3 +1,11 @@ -

- signin works! -

+
+
+ + +
Username is required
+
+
+ +
+
diff --git a/other/openshift/message-board/message-board-web/src/app/signin/signin.component.ts b/other/openshift/message-board/message-board-web/src/app/signin/signin.component.ts index e435c43f..f6c42451 100644 --- a/other/openshift/message-board/message-board-web/src/app/signin/signin.component.ts +++ b/other/openshift/message-board/message-board-web/src/app/signin/signin.component.ts @@ -1,4 +1,8 @@ -import { Component, OnInit } from '@angular/core'; +import {Component, OnInit} from '@angular/core'; +import {Router, ActivatedRoute} from '@angular/router'; + +import {AccountService} from '../account.service'; +import {User} from '../user'; @Component({ selector: 'app-signin', @@ -7,9 +11,17 @@ import { Component, OnInit } from '@angular/core'; }) export class SigninComponent implements OnInit { - constructor() { } + user: User; + + constructor(private accountService: AccountService, + private route: ActivatedRoute, + private router: Router ) {} ngOnInit() { } + createAccount(): void { + this.accountService.createAccount(this.user); + } + }