-
Notifications
You must be signed in to change notification settings - Fork 1
/
RestaurantController.java
86 lines (73 loc) · 3.26 KB
/
RestaurantController.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package restvotes.rest.controller;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.rest.webmvc.RepositoryRestController;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.hateoas.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import restvotes.domain.entity.Menu;
import restvotes.domain.entity.Restaurant;
import restvotes.repository.MenuRepo;
import restvotes.rest.processor.RestaurantResourceProcessor;
import restvotes.util.LinksHelper;
import restvotes.util.MessageHelper;
import restvotes.util.exception.NotFoundException;
import static org.springframework.http.HttpStatus.CREATED;
/**
* Controller to handle query for the restaurant related menus.
* <p>An example how to deal with related objects without specifying their relations in entities classes</p>
*
* @author Cepro, 2017-01-22
*/
@Slf4j
@RequiredArgsConstructor
@RepositoryRestController
@RequestMapping("/restaurants/{id}")
public class RestaurantController {
private final @NonNull LinksHelper links;
private final @NonNull MessageHelper msgHelper;
private final @NonNull PagedResourcesAssembler<Menu> pagedResourcesAssembler;
private final @NonNull MenuRepo menuRepo;
/**
* Get {@link Menu} list of the specified Restaurant
* <p>(see a corresponding Menus link in output of {@code /api/restaurants/{id}})</p>
*
* @param restaurant the specified Restaurant
* @param pageable Pageable parameter
* @return Pageable Menu list
*/
@GetMapping("/menus")
public ResponseEntity<?> getMenus(@PathVariable("id") Restaurant restaurant, Pageable pageable) {
if (restaurant == null) {
throw new NotFoundException(msgHelper.userMessage("restaurant.not_found"));
}
Page<Menu> menus = menuRepo.findByRestaurantOrderByIdDesc(restaurant, pageable);
return ResponseEntity.ok(pagedResourcesAssembler.toResource(menus,
menu -> new Resource<>(menu, links.getMenuLinks(menu, true))));
}
/**
* Add new {@link Menu} to specified Restaurant
* <p>(see a corresponding Menus link of {@code /api/restaurants/{id}} in output)</p>
* <p>(link added in {@link RestaurantResourceProcessor})</p>
* @param restaurant the specified Restaurant
* @param menu a {@link Menu} to saved
* @return a saved {@link Menu}
*/
@Transactional
@PostMapping("/menus")
public ResponseEntity<?> addMenu(@PathVariable("id") Restaurant restaurant, @RequestBody Menu menu) {
if (restaurant == null) {
throw new NotFoundException(msgHelper.userMessage("restaurant.cannot_be_null"));
}
if (menu == null) {
throw new NotFoundException(msgHelper.userMessage("menu.cannot_be_null"));
}
Menu savedMenu = menuRepo.saveAndFlush(menu.setRestaurant(restaurant));
return new ResponseEntity<>(new Resource<>(savedMenu, links.getMenuLinks(savedMenu, true)), CREATED);
}
}