US01: Make it possible for the user to add a horse through the web UI, add additional validations

This commit is contained in:
Ivaylo Ivanov 2020-03-18 19:32:23 +01:00
parent df76acceeb
commit 01268263de
7 changed files with 16 additions and 21 deletions

View File

@ -12,6 +12,7 @@ import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import java.lang.invoke.MethodHandles;
import java.text.SimpleDateFormat;
@Service
public class SimpleHorseService implements HorseService {
@ -33,8 +34,8 @@ public class SimpleHorseService implements HorseService {
@Override
public Horse addHorse(Horse horse) throws ValidationException, DataAccessException {
LOGGER.trace("addHorse({})", horse.toString());
this.validator.validateNewHorse(horse);
LOGGER.trace("addHorse({})", horse.toString());
return horseDao.addHorse(horse);
}
}

View File

@ -16,6 +16,9 @@ public class Validator {
}
public void validateNewHorse(Horse horse) throws ValidationException {
if(horse.getName() == null || horse.getScore() == 0 || horse.getBirthday() == null || horse.getDescription() == null){
throw new ValidationException("All or some required values missing: name, description, score, birthday");
}
if(horse.getScore() > 5 || horse.getScore() < 1) {
throw new ValidationException("Score value " + horse.getScore() + " not allowed. The score must be an integer between 1 and 5");
}

View File

@ -2,12 +2,14 @@ import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import {OwnerComponent} from './component/owner/owner.component';
import { HorseComponent } from './component/horse/horse.component';
import { AddHorseComponent } from './component/add-horse/add-horse.component';
const routes: Routes = [
{path: '', redirectTo: 'owner', pathMatch: 'full'},
{path: 'owner', component: OwnerComponent},
{path: 'horse', component: HorseComponent},
{path: 'horse/add', component: AddHorseComponent},
];
@NgModule({

View File

@ -1,24 +1,28 @@
import {BrowserModule} from '@angular/platform-browser';
import {NgModule} from '@angular/core';
import { FormsModule } from '@angular/forms';
import {HttpClientModule} from '@angular/common/http';
import {AppRoutingModule} from './app-routing.module';
import {AppComponent} from './app.component';
import {HeaderComponent} from './component/header/header.component';
import {OwnerComponent} from './component/owner/owner.component';
import {HttpClientModule} from '@angular/common/http';
import { HorseComponent } from './component/horse/horse.component';
import { AddHorseComponent } from './component/add-horse/add-horse.component';
@NgModule({
declarations: [
AppComponent,
HeaderComponent,
OwnerComponent,
HorseComponent
HorseComponent,
AddHorseComponent
],
imports: [
BrowserModule,
AppRoutingModule,
HttpClientModule
HttpClientModule,
FormsModule
],
providers: [],
bootstrap: [AppComponent]

View File

@ -41,21 +41,6 @@ export class HorseComponent implements OnInit {
);
}
/**
* Creates a new horse and loads it
* @param horse
*/
private addHorse(horse: Horse) {
this.horseService.addHorse(horse).subscribe(
(horse: Horse) => {
this.horse = horse;
},
error => {
this.defaultServiceErrorHandling(error);
}
);
}
private defaultServiceErrorHandling(error: any) {
console.log(error);
this.error = true;

View File

@ -4,5 +4,5 @@ import {Injectable} from '@angular/core';
providedIn: 'root'
})
export class Globals {
readonly backendUri: string = 'http://localhost:8080/';
readonly backendUri: string = 'http://localhost:8080';
}

View File

@ -27,7 +27,7 @@ export class HorseService {
* @param horse
*/
addHorse(horse: Horse): Observable<Horse> {
console.log('Add new horse ' + horse);
console.log('Add new horse ' + JSON.stringify(horse));
return this.httpClient.post<Horse>(this.messageBaseUri, horse);
}
}