Skip to content
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package dev.example.restaurantManager.controller;

import dev.example.restaurantManager.model.MenuItem;
import dev.example.restaurantManager.service.MenuItemService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/menu-items")
public class MenuItemController {
private final MenuItemService menuItemService;

public MenuItemController(MenuItemService menuItemService) {
this.menuItemService = menuItemService;
}

@PostMapping
public ResponseEntity<MenuItem> createMenuItem(@RequestBody MenuItem menuItem) {
MenuItem created = menuItemService.createMenuItem(menuItem);
return new ResponseEntity<>(created, HttpStatus.CREATED);
}

@GetMapping("/{id}")
public ResponseEntity<MenuItem> getMenuItem(@PathVariable String id) {
return menuItemService.getMenuItemById(id)
.map(item -> new ResponseEntity<>(item, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}

@GetMapping
public ResponseEntity<List<MenuItem>> getAllMenuItems() {
List<MenuItem> items = menuItemService.getAllMenuItems();
return new ResponseEntity<>(items, HttpStatus.OK);
}

@PutMapping("/{id}")
public ResponseEntity<MenuItem> updateMenuItem(@PathVariable String id, @RequestBody MenuItem menuItem) {
menuItem.setId(Long.valueOf(id));
MenuItem updated = menuItemService.updateMenuItem(menuItem);
return new ResponseEntity<>(updated, HttpStatus.OK);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteMenuItem(@PathVariable String id) {
menuItemService.deleteMenuItem(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package dev.example.restaurantManager.controller;

import dev.example.restaurantManager.model.MenuRestaurant;
import dev.example.restaurantManager.model.MenuItem;
import dev.example.restaurantManager.service.MenuRestaurantService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/menus-restaurant")
public class MenuRestaurantController {
private final MenuRestaurantService menuRestaurantService;

public MenuRestaurantController(MenuRestaurantService menuRestaurantService) {
this.menuRestaurantService = menuRestaurantService;
}

@PostMapping
public ResponseEntity<MenuRestaurant> createMenu(@RequestBody MenuRestaurant menuRestaurant) {
MenuRestaurant created = menuRestaurantService.createMenu(menuRestaurant);
return new ResponseEntity<>(created, HttpStatus.CREATED);
}

@GetMapping("/{id}")
public ResponseEntity<MenuRestaurant> getMenu(@PathVariable String id) {
return menuRestaurantService.getMenuById(id)
.map(menu -> new ResponseEntity<>(menu, HttpStatus.OK))
.orElse(new ResponseEntity<>(HttpStatus.NOT_FOUND));
}

@GetMapping
public ResponseEntity<List<MenuRestaurant>> getAllMenus() {
List<MenuRestaurant> menus = menuRestaurantService.getAllMenus();
return new ResponseEntity<>(menus, HttpStatus.OK);
}

@PutMapping("/{id}")
public ResponseEntity<MenuRestaurant> updateMenu(@PathVariable String id, @RequestBody MenuRestaurant menuRestaurant) {
menuRestaurant.setId(id);
MenuRestaurant updated = menuRestaurantService.updateMenu(menuRestaurant);
return new ResponseEntity<>(updated, HttpStatus.OK);
}

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteMenu(@PathVariable String id) {
menuRestaurantService.deleteMenu(id);
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}

@PostMapping("/{menuId}/items")
public ResponseEntity<Void> addMenuItemToMenu(@PathVariable String menuId, @RequestBody MenuItem menuItem) {
menuRestaurantService.addMenuItemToMenu(menuId, menuItem);
return new ResponseEntity<>(HttpStatus.CREATED);
}

@GetMapping("/{menuId}/items")
public ResponseEntity<List<MenuItem>> getMenuItems(@PathVariable String menuId) {
List<MenuItem> items = menuRestaurantService.getMenuItems(menuId);
return new ResponseEntity<>(items, HttpStatus.OK);
}
}
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
}
80 changes: 80 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,80 @@
package dev.example.restaurantManager.model;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.GenericGenerator;

import java.awt.*;
import java.util.*;
import java.util.List;

@Entity
@Data
@NoArgsConstructor
@AllArgsConstructor
public class MenuItem {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

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

@Enumerated(EnumType.STRING)
private CourseType courseType;

@ManyToMany(mappedBy = "menuItems", fetch = FetchType.LAZY)
private List<MenuRestaurant> menus = new ArrayList<>();

// Constructor
public MenuItem(String name, String description, boolean isSpicy, boolean hasGluten, boolean isAvailable, CourseType courseType) {
this.name = name;
this.description = description;
this.isSpicy = isSpicy;
this.hasGluten = hasGluten;
this.isAvailable = isAvailable;
this.courseType = courseType;
}


// Constructors, getters, and setters
@Override
public String toString() {
return "MenuItem{" +
"id=" + id +
", name='" + name + '\'' +
", description='" + description + '\'' +
", isSpicy=" + isSpicy +
", hasGluten=" + hasGluten +
", isAvailable=" + isAvailable +
", courseType=" + courseType +
// ", menus=" + menus +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MenuItem menuItem = (MenuItem) o;
return isSpicy == menuItem.isSpicy &&
hasGluten == menuItem.hasGluten &&
isAvailable == menuItem.isAvailable &&
Objects.equals(id, menuItem.id) &&
Objects.equals(name, menuItem.name) &&
Objects.equals(description, menuItem.description) &&
courseType == menuItem.courseType;
}

@Override
public int hashCode() {
return Objects.hash(id, name, description, isSpicy, hasGluten, isAvailable, courseType);
}

}

Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
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.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Data
@AllArgsConstructor
@NoArgsConstructor
@AllArgsConstructor
@Entity
public class MenuRestaurant {
public class MenuRestaurant {

@Id
private String id;
Expand All @@ -27,16 +27,47 @@ public class MenuRestaurant {
@ManyToMany(mappedBy = "menus", fetch = FetchType.LAZY)
private List<OrderRestaurant> orders = new ArrayList<>();

@Getter
@ManyToMany(fetch = FetchType.EAGER)
@JoinTable(
name = "menu_restaurant_menu_item",
joinColumns = @JoinColumn(name = "menu_restaurant_id"),
inverseJoinColumns = @JoinColumn(name = "menu_item_id")
)
private List<MenuItem> menuItems = new ArrayList<>();

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.orders = new ArrayList<>();
this.menuItems = new ArrayList<>();
}

public void addMenuItem(MenuItem menuItem) {
this.menuItems.add(menuItem);
menuItem.getMenus().add(this);
}


@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MenuRestaurant that = (MenuRestaurant) o;
return Objects.equals(id, that.id) &&
Objects.equals(name, that.name);
}

//We might want to exclude 'orders' from toString() to avoid circular references
@Override
public int hashCode() {
return Objects.hash(id, name);
}

// We might want to exclude 'orders' from toString() to avoid circular references
@Override
public String toString() {
return "MenuRestaurant{" +
Expand All @@ -47,9 +78,10 @@ public String toString() {
", active=" + active +
", water=" + water +
", ordersCount=" + (orders != null ? orders.size() : 0) +
", orders=" + orders +
", orders=" + orders + '\'' +
", menuItemsCount=" + + (menuItems != null ? menuItems.size() : 0) +
// ", menuItems=" + menuItems +
'}';
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
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,18 @@
package dev.example.restaurantManager.service;

import dev.example.restaurantManager.model.MenuItem;

import java.util.List;
import java.util.Optional;

public interface MenuItemService {
MenuItem createMenuItem(MenuItem menuItem);

Optional<MenuItem> getMenuItemById(String id);

List<MenuItem> getAllMenuItems();

MenuItem updateMenuItem(MenuItem menuItem);

void deleteMenuItem(String id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package dev.example.restaurantManager.service.impl;

import dev.example.restaurantManager.model.MenuItem;
import dev.example.restaurantManager.repository.MenuItemRepository;
import dev.example.restaurantManager.service.MenuItemService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class MenuItemServiceImpl implements MenuItemService {

@Autowired
private MenuItemRepository menuItemRepository;

@Override
public MenuItem createMenuItem(MenuItem menuItem) {
return menuItemRepository.save(menuItem);
}

@Override
public Optional<MenuItem> getMenuItemById(String id) {
return menuItemRepository.findById(id);
}

@Override
public List<MenuItem> getAllMenuItems() {
return menuItemRepository.findAll();
}

@Override
public MenuItem updateMenuItem(MenuItem menuItem) {
return menuItemRepository.save(menuItem);
}

@Override
public void deleteMenuItem(String id) {
menuItemRepository.deleteById(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package dev.example.restaurantManager.service;

import dev.example.restaurantManager.model.MenuRestaurant;
import dev.example.restaurantManager.model.MenuItem;

import java.util.List;
import java.util.Optional;

public interface MenuRestaurantService {
MenuRestaurant createMenu(MenuRestaurant menuRestaurant);

Optional<MenuRestaurant> getMenuById(String id);

List<MenuRestaurant> getAllMenus();

MenuRestaurant updateMenu(MenuRestaurant menuRestaurant);

void deleteMenu(String id);

void addMenuItemToMenu(String menuId, MenuItem menuItem);

List<MenuItem> getMenuItems(String menuId);

}
Loading