11package ru .javawebinar .topjava .web .meal ;
22
3- import org .springframework .format .annotation .DateTimeFormat ;
43import org .springframework .http .HttpStatus ;
54import org .springframework .http .MediaType ;
5+ import org .springframework .http .ResponseEntity ;
66import org .springframework .lang .Nullable ;
7+ import org .springframework .validation .BindingResult ;
78import org .springframework .web .bind .annotation .*;
89import ru .javawebinar .topjava .model .Meal ;
910import ru .javawebinar .topjava .to .MealTo ;
11+ import ru .javawebinar .topjava .util .ValidationUtil ;
1012
13+ import javax .validation .Valid ;
1114import java .time .LocalDate ;
12- import java .time .LocalDateTime ;
1315import java .time .LocalTime ;
1416import java .util .List ;
1517
@@ -23,6 +25,12 @@ public List<MealTo> getAll() {
2325 return super .getAll ();
2426 }
2527
28+ @ Override
29+ @ GetMapping ( "/{id}" )
30+ public Meal get (@ PathVariable int id ) {
31+ return super .get (id );
32+ }
33+
2634 @ Override
2735 @ DeleteMapping ("/{id}" )
2836 @ ResponseStatus (HttpStatus .NO_CONTENT )
@@ -32,10 +40,16 @@ public void delete(@PathVariable int id) {
3240
3341 @ PostMapping
3442 @ ResponseStatus (HttpStatus .NO_CONTENT )
35- public void create (@ RequestParam @ DateTimeFormat (iso = DateTimeFormat .ISO .DATE_TIME ) LocalDateTime dateTime ,
36- @ RequestParam String description ,
37- @ RequestParam int calories ) {
38- super .create (new Meal (null , dateTime , description , calories ));
43+ public ResponseEntity <String > createOrUpdate (@ Valid Meal meal , BindingResult result ) {
44+ if (result .hasErrors ()) {
45+ return ValidationUtil .getErrorResponse (result );
46+ }
47+ if (meal .isNew ()) {
48+ super .create (meal );
49+ } else {
50+ super .update (meal , meal .getId ());
51+ }
52+ return ResponseEntity .ok ().build ();
3953 }
4054
4155 @ Override
0 commit comments