-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathMenuRestaurantController.java
More file actions
82 lines (68 loc) · 3.48 KB
/
MenuRestaurantController.java
File metadata and controls
82 lines (68 loc) · 3.48 KB
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
package dev.example.restaurantManager.controller;
import dev.example.restaurantManager.model.MenuRestaurant;
import dev.example.restaurantManager.service.MenuRestaurantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.List;
@RequestMapping("/api/v1/menu")
@RestController
public class MenuRestaurantController {
@Autowired
private MenuRestaurantService menuRestaurantService;
@GetMapping("/allMenus")
public ResponseEntity<List<MenuRestaurant>> getAllMenus( ) {
List<MenuRestaurant> menuRestaurants = menuRestaurantService.getAllMenuRestaurants();
HttpHeaders headers = getCommonHeaders("Get all menus");
return menuRestaurants != null && !menuRestaurants.isEmpty()
? new ResponseEntity<>(menuRestaurants, headers, HttpStatus.OK)
: new ResponseEntity<>(headers, HttpStatus.NOT_FOUND);
}
@PostMapping("/add")
public ResponseEntity<MenuRestaurant> createMenu(@RequestBody MenuRestaurant menuRestaurant) {
MenuRestaurant createdMenu = menuRestaurantService.createMenuRestaurants(menuRestaurant);
HttpHeaders headers = getCommonHeaders("Create a new menu");
return createdMenu != null
? new ResponseEntity<>(createdMenu, headers, HttpStatus.CREATED)
: new ResponseEntity<>(headers, HttpStatus.BAD_REQUEST);
}
@PutMapping("/update/{id}")
public ResponseEntity<MenuRestaurant> updateMenu(@PathVariable String id, @RequestBody MenuRestaurant menuRestaurantDetails) {
MenuRestaurant updatedMenu = menuRestaurantService.updateMenuRestaurants(id, menuRestaurantDetails);
HttpHeaders headers = getCommonHeaders("Update a menu");
return updatedMenu != null
? new ResponseEntity<>(updatedMenu, headers, HttpStatus.OK)
: new ResponseEntity<>(headers, HttpStatus.NOT_FOUND);
}
@DeleteMapping("/delete/{id}")
public ResponseEntity<Void> deleteMenu(@PathVariable String id) {
boolean deleted = menuRestaurantService.deleteMenuRestaurants(id);
HttpHeaders headers = getCommonHeaders("Delete a menu");
headers.add("deleted", String.valueOf(deleted));
return deleted
? new ResponseEntity<>(headers, HttpStatus.NO_CONTENT)
: new ResponseEntity<>(headers, HttpStatus.NOT_FOUND);
}
@GetMapping("/get/{id}")
public ResponseEntity<MenuRestaurant> getMenuById(@PathVariable String id) {
MenuRestaurant menu = menuRestaurantService.getMenuRestaurantsById(id);
HttpHeaders headers = getCommonHeaders("Get a menu");
return menu != null
? new ResponseEntity<>(menu, headers, HttpStatus.OK)
: new ResponseEntity<>(headers, HttpStatus.NOT_FOUND);
}
private HttpHeaders getCommonHeaders(String description) {
HttpHeaders headers = new HttpHeaders();
headers.add("desc", description);
headers.add("content-type", "application/json");
headers.add("date", new Date().toString());
headers.add("server", "H2 Database");
headers.add("version", "1.0.0");
headers.add("menuRestaurant-count", String.valueOf(menuRestaurantService.countMenuRestaurant()));
headers.add("object", "customers");
return headers;
}
}