Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package dev.example.restaurantManager.controller;

import dev.example.restaurantManager.model.MenuRestaurant;
import dev.example.restaurantManager.service.MenuService;
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/menus")
@RestController
public class MenuController {

@Autowired
MenuService menuService;

// manage request by ResponseEntity with all menus
@GetMapping("/allMenus")
public ResponseEntity<List<MenuRestaurant>> getAllMenus( ) {
List<MenuRestaurant> menus = menuService.getAllMenus();
HttpHeaders headers = getCommonHeaders("Get all menus");

return menus != null && !menus.isEmpty()
? new ResponseEntity<>(menus, headers, HttpStatus.OK)
: new ResponseEntity<>(headers, HttpStatus.NOT_FOUND);
}

@PostMapping
public ResponseEntity<MenuRestaurant> createMenu(@RequestBody MenuRestaurant menu) {
MenuRestaurant createdMenu = menuService.createMenu(menu);
HttpHeaders headers = getCommonHeaders("Create a new menu");

return createdMenu != null
? new ResponseEntity<>(createdMenu, headers, HttpStatus.CREATED)
: new ResponseEntity<>(headers, HttpStatus.BAD_REQUEST);
}

@PutMapping("/{id}")
public ResponseEntity<MenuRestaurant> updateMenu(@PathVariable String id, @RequestBody MenuRestaurant menuDetails) {
MenuRestaurant updatedMenu = menuService.updateMenu(id, menuDetails);
HttpHeaders headers = getCommonHeaders("Update a menu");

return updatedMenu != null
? new ResponseEntity<>(updatedMenu, headers, HttpStatus.OK)
: new ResponseEntity<>(headers, HttpStatus.NOT_FOUND);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteMenu(@PathVariable String id) {
menuService.deleteMenu(id);
HttpHeaders headers = getCommonHeaders("Delete a menu");
boolean deleted = menuService.getMenuById(id) == null;
headers.add("deleted", String.valueOf(deleted));

return deleted
? new ResponseEntity<>(headers, HttpStatus.NO_CONTENT)
: new ResponseEntity<>(headers, HttpStatus.NOT_FOUND);
}

@GetMapping("/{id}")
public ResponseEntity<MenuRestaurant> getMenuById(@PathVariable String id) {
MenuRestaurant menu = menuService.getMenuById(id);
HttpHeaders headers = getCommonHeaders("Get a menu by Id");

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("menus-count", String.valueOf(menuService.countMenus()));
headers.add("object", "menus");
return headers;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package dev.example.restaurantManager.controller;

import dev.example.restaurantManager.model.MenuItem;
import dev.example.restaurantManager.service.MenuItemService;
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/items")
@RestController
public class MenuItemController {

@Autowired
MenuItemService itemService;

// manage request by ResponseEntity with all menus
@GetMapping("/allItems")
public ResponseEntity<List<MenuItem>> getAllItems( ) {
List<MenuItem> menus = itemService.getAllItems();
HttpHeaders headers = getCommonHeaders("Get all items");

return menus != null && !menus.isEmpty()
? new ResponseEntity<>(menus, headers, HttpStatus.OK)
: new ResponseEntity<>(headers, HttpStatus.NOT_FOUND);
}

@PostMapping
public ResponseEntity<MenuItem> createMenu(@RequestBody MenuItem item) {
MenuItem createdItem = itemService.createItem(item);
HttpHeaders headers = getCommonHeaders("Create a new item");

return createdItem != null
? new ResponseEntity<>(createdItem, headers, HttpStatus.CREATED)
: new ResponseEntity<>(headers, HttpStatus.BAD_REQUEST);
}

@PutMapping("/{id}")
public ResponseEntity<MenuItem> updateItem(@PathVariable String id, @RequestBody MenuItem itemDetails) {
MenuItem updatedItem = itemService.updateItem(id, itemDetails);
HttpHeaders headers = getCommonHeaders("Update an item");

return updatedItem != null
? new ResponseEntity<>(updatedItem, headers, HttpStatus.OK)
: new ResponseEntity<>(headers, HttpStatus.NOT_FOUND);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteItem(@PathVariable String id) {
itemService.deleteItem(id);
HttpHeaders headers = getCommonHeaders("Delete an item");
boolean deleted = itemService.getItemById(id) == null;
headers.add("deleted", String.valueOf(deleted));

return deleted
? new ResponseEntity<>(headers, HttpStatus.NO_CONTENT)
: new ResponseEntity<>(headers, HttpStatus.NOT_FOUND);
}

@GetMapping("/{id}")
public ResponseEntity<MenuItem> getItemById(@PathVariable String id) {
MenuItem item = itemService.getItemById(id);
HttpHeaders headers = getCommonHeaders("Get an item by Id");

return item != null
? new ResponseEntity<>(item, 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("items-count", String.valueOf(itemService.countItems()));
headers.add("object", "items");
return headers;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.example.restaurantManager.model;

public enum CourseType {
STARTER,
MAIN,
DESSERT
}
76 changes: 76 additions & 0 deletions src/main/java/dev/example/restaurantManager/model/MenuItem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package dev.example.restaurantManager.model;

import jakarta.persistence.*;
import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Data
@Entity
public class MenuItem {

@Id
private String id;

private String name;
private String description;
private boolean isSpicy;
private boolean hasGluten;
private boolean isAvailable;

@Enumerated(EnumType.STRING)
private CourseType courseType;

@ManyToMany(mappedBy = "items")
private ArrayList<MenuRestaurant> menus;

public void addMenus(List<MenuRestaurant> newMenus){
if(newMenus== null){
return;
}
if(this.menus == null){
this.menus = new ArrayList<>();
}
for(MenuRestaurant menu:newMenus){
if(!this.menus.contains(menu)){
menu.setItems(new ArrayList<>(Arrays.asList(this)));
this.menus.add(menu);
}
}
}


// Default constructor
public MenuItem() {
this.id = UUID.randomUUID().toString();
}

// Constructors, getters, and setters

@Override
public boolean equals(Object other){
if(this==other){
return true;
}

if(other == null || !(other instanceof MenuItem) ){
return false;
}

MenuItem that = (MenuItem)other;

return this.id.equals(that.id) &&
this.name.equals(that.name) &&
this.description.equals(that.description) &&
this.isSpicy == that.isSpicy &&
this.hasGluten == that.hasGluten &&
this.isAvailable == that.isAvailable &&
this.courseType == that.courseType
;
}

}

Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package dev.example.restaurantManager.model;

import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Id;
import jakarta.persistence.ManyToMany;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Data
Expand All @@ -27,13 +25,43 @@ public class MenuRestaurant {
@ManyToMany(mappedBy = "menus", fetch = FetchType.LAZY)
private List<OrderRestaurant> orders = new ArrayList<>();

@ManyToMany
@JoinTable(name = "ITEMS_ON_MENUS",
joinColumns = @JoinColumn(name = "MENU_ID", referencedColumnName="id"),
inverseJoinColumns = @JoinColumn(name = "ITEM_ID", referencedColumnName="id"))
private List<MenuItem> items;

public void addMenuItems(List<MenuItem> newItems){
if(newItems== null){
return;
}
if(this.items == null){
this.items = new ArrayList<>();
}
for(MenuItem menuItem:newItems){
if(!this.items.contains(menuItem)){
menuItem.setMenus(new ArrayList<>(Arrays.asList(this)));
this.items.add(menuItem);
}
}
}

public void removeMenuItems(List<MenuItem> newItems){
if(this.items == null || newItems== null || newItems.size() == 0){
return;
}
for(MenuItem menuItem:newItems){
if(this.items.contains(menuItem)){
this.items.remove(menuItem);
}
}
}

public MenuRestaurant(String id, String name, Double price, String content, boolean active, boolean water,List<OrderRestaurant> orders) {
this(id,name,price,content,active,water,orders,null);
}
public MenuRestaurant(String id, String name, Double price, String content, boolean active, boolean water) {
this.id = id;
this.name = name;
this.price = price;
this.content = content;
this.active = active;
this.water = water;
this(id,name,price,content,active,water,null,null);
}

//We might want to exclude 'orders' from toString() to avoid circular references
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.example.restaurantManager.repository;

import dev.example.restaurantManager.model.MenuItem;
import org.springframework.data.jpa.repository.JpaRepository;

public interface MenuItemRepository extends JpaRepository<MenuItem,String> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package dev.example.restaurantManager.service;

import dev.example.restaurantManager.model.MenuItem;

import java.util.List;

public interface MenuItemService {

List<MenuItem> getAllItems();
MenuItem createItem(MenuItem item);
MenuItem getItemById(String id);
MenuItem updateItem(String id,MenuItem itemDetails);
void deleteItem(String id);
long countItems();

}
Loading