Skip to content

Commit

Permalink
create account and other minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 committed Mar 28, 2018
1 parent 2e23f85 commit ef38b40
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<User> {
const url = `http://web-message-board.192.168.42.57.nip.io/users}`;
console.log(url);
return this.http.post<User>(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;
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -31,7 +32,7 @@ import {MessageComponent} from './message/message.component';
PostMessageComponent,
MessageComponent
],
providers: [LoginService, MessageService],
providers: [LoginService, MessageService, AccountService],
bootstrap: [AppComponent]
})
export class AppModule {}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ <h2>Login</h2>
<div class="form-group">
<button [disabled]="loading" class="btn btn-primary">Login</button>
<img *ngIf="loading" src="data:image/gif;base64,R0lGODlhEAAQAPIAAP///wAAAMLCwkJCQgAAAGJiYoKCgpKSkiH/C05FVFNDQVBFMi4wAwEAAAAh/hpDcmVhdGVkIHdpdGggYWpheGxvYWQuaW5mbwAh+QQJCgAAACwAAAAAEAAQAAADMwi63P4wyklrE2MIOggZnAdOmGYJRbExwroUmcG2LmDEwnHQLVsYOd2mBzkYDAdKa+dIAAAh+QQJCgAAACwAAAAAEAAQAAADNAi63P5OjCEgG4QMu7DmikRxQlFUYDEZIGBMRVsaqHwctXXf7WEYB4Ag1xjihkMZsiUkKhIAIfkECQoAAAAsAAAAABAAEAAAAzYIujIjK8pByJDMlFYvBoVjHA70GU7xSUJhmKtwHPAKzLO9HMaoKwJZ7Rf8AYPDDzKpZBqfvwQAIfkECQoAAAAsAAAAABAAEAAAAzMIumIlK8oyhpHsnFZfhYumCYUhDAQxRIdhHBGqRoKw0R8DYlJd8z0fMDgsGo/IpHI5TAAAIfkECQoAAAAsAAAAABAAEAAAAzIIunInK0rnZBTwGPNMgQwmdsNgXGJUlIWEuR5oWUIpz8pAEAMe6TwfwyYsGo/IpFKSAAAh+QQJCgAAACwAAAAAEAAQAAADMwi6IMKQORfjdOe82p4wGccc4CEuQradylesojEMBgsUc2G7sDX3lQGBMLAJibufbSlKAAAh+QQJCgAAACwAAAAAEAAQAAADMgi63P7wCRHZnFVdmgHu2nFwlWCI3WGc3TSWhUFGxTAUkGCbtgENBMJAEJsxgMLWzpEAACH5BAkKAAAALAAAAAAQABAAAAMyCLrc/jDKSatlQtScKdceCAjDII7HcQ4EMTCpyrCuUBjCYRgHVtqlAiB1YhiCnlsRkAAAOwAAAAAAAAAAAA==" />
<a [routerLink]="['/register']" class="btn btn-link">Register</a>
<a routerLink='/register' class="btn btn-link">Register</a>
</div>
</form>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -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 '/'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
<p>
signin works!
</p>
<form name="form" (ngSubmit)="f.form.valid && createAccount()" #f="ngForm" novalidate>
<div class="form-group" [ngClass]="{ 'has-error': f.submitted && !username.valid }">
<label for="username">Username</label>
<input type="text" class="form-control" name="username" [(ngModel)]="user.userName"
#username="ngModel" required />
<div *ngIf="f.submitted && !username.valid" class="help-block">Username is required</div>
</div>
<div class="form-group">
<button [disabled]="createAccount" class="btn btn-primary">Create</button>
</div>
</form>
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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);
}

}

0 comments on commit ef38b40

Please sign in to comment.