diff --git a/src/main/java/dev/example/restaurantManager/controller/DessertController.java b/src/main/java/dev/example/restaurantManager/controller/DessertController.java new file mode 100644 index 0000000..c652767 --- /dev/null +++ b/src/main/java/dev/example/restaurantManager/controller/DessertController.java @@ -0,0 +1,69 @@ +package dev.example.restaurantManager.controller; + +import dev.example.restaurantManager.model.MenuItem.Dessert; +import dev.example.restaurantManager.service.DessertService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/api/dessert") +public class DessertController { + + @Autowired + private DessertService dessertService; + + @GetMapping + public ResponseEntity> getAllDesserts() { + List desserts = dessertService.getAllDesserts(); + return new ResponseEntity<>(desserts, HttpStatus.OK); + } + + @GetMapping("/sugarfree") + public ResponseEntity> getAllSugarFreeDesserts() { + List desserts = dessertService.getAllSugarFreeDesserts(); + return new ResponseEntity<>(desserts, HttpStatus.OK); + } + + @PostMapping + public ResponseEntity createDessert(@RequestBody Dessert dessert) { + Dessert newDessert = dessertService.createDessert(dessert); + return new ResponseEntity<>(newDessert, HttpStatus.CREATED); + } + + @GetMapping("/{id}") + public ResponseEntity getDessertById(@PathVariable String id) { + Dessert dessert = dessertService.getDessertById(id); + if (dessert != null) { + return new ResponseEntity<>(dessert, HttpStatus.OK); + } + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + @PutMapping("/{id}") + public ResponseEntity updateDessert(@PathVariable String id, @RequestBody Dessert dessertDetails) { + Dessert updatedDessert = dessertService.updateDessert(id, dessertDetails); + if (updatedDessert != null) { + return new ResponseEntity<>(updatedDessert, HttpStatus.OK); + } + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteDessert(@PathVariable String id) { + boolean deleted = dessertService.deleteDessert(id); + if (deleted) { + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + @GetMapping("/count") + public ResponseEntity countDesserts() { + long count = dessertService.countDesserts(); + return new ResponseEntity<>(count, HttpStatus.OK); + } +} \ No newline at end of file diff --git a/src/main/java/dev/example/restaurantManager/controller/MainCourseController.java b/src/main/java/dev/example/restaurantManager/controller/MainCourseController.java new file mode 100644 index 0000000..381e12d --- /dev/null +++ b/src/main/java/dev/example/restaurantManager/controller/MainCourseController.java @@ -0,0 +1,69 @@ +package dev.example.restaurantManager.controller; + +import dev.example.restaurantManager.model.MenuItem.MainCourse; +import dev.example.restaurantManager.service.MainCourseService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/api/maincourse") +public class MainCourseController { + + @Autowired + private MainCourseService mainCourseService; + + @GetMapping + public ResponseEntity> getAllMainCourses() { + List mainCourses = mainCourseService.getAllMainCourses(); + return new ResponseEntity<>(mainCourses, HttpStatus.OK); + } + + @GetMapping("/vegan") + public ResponseEntity> getAllVeganMainCourses() { + List mainCourses = mainCourseService.getAllVeganMainCourses(); + return new ResponseEntity<>(mainCourses, HttpStatus.OK); + } + + @PostMapping + public ResponseEntity createMainCourse(@RequestBody MainCourse mainCourse) { + MainCourse newMainCourse = mainCourseService.createMainCourse(mainCourse); + return new ResponseEntity<>(newMainCourse, HttpStatus.CREATED); + } + + @GetMapping("/{id}") + public ResponseEntity getMainCourseById(@PathVariable String id) { + MainCourse mainCourse = mainCourseService.getMainCourseById(id); + if (mainCourse != null) { + return new ResponseEntity<>(mainCourse, HttpStatus.OK); + } + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + @PutMapping("/{id}") + public ResponseEntity updateMainCourse(@PathVariable String id, @RequestBody MainCourse mainCourseDetails) { + MainCourse updatedMainCourse = mainCourseService.updateMainCourse(id, mainCourseDetails); + if (updatedMainCourse != null) { + return new ResponseEntity<>(updatedMainCourse, HttpStatus.OK); + } + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + @DeleteMapping("/{id}") + public ResponseEntity deleteMainCourse(@PathVariable String id) { + boolean deleted = mainCourseService.deleteMainCourse(id); + if (deleted) { + return new ResponseEntity<>(HttpStatus.NO_CONTENT); + } + return new ResponseEntity<>(HttpStatus.NOT_FOUND); + } + + @GetMapping("/count") + public ResponseEntity countMainCourses() { + long count = mainCourseService.countMainCourses(); + return new ResponseEntity<>(count, HttpStatus.OK); + } +} \ No newline at end of file diff --git a/src/main/java/dev/example/restaurantManager/controller/MenuItemController.java b/src/main/java/dev/example/restaurantManager/controller/MenuItemController.java deleted file mode 100644 index fbcc2ac..0000000 --- a/src/main/java/dev/example/restaurantManager/controller/MenuItemController.java +++ /dev/null @@ -1,63 +0,0 @@ -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.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; - -import java.util.List; - -@RestController -@RequestMapping("/api/menuitems") -public class MenuItemController { - - @Autowired - private MenuItemService menuItemService; - - @GetMapping - public ResponseEntity> getAllMenuItems() { - List menuItems = menuItemService.getAllMenuItems(); - return new ResponseEntity<>(menuItems, HttpStatus.OK); - } - - @PostMapping - public ResponseEntity createMenuItem(@RequestBody MenuItem menuItem) { - MenuItem newMenuItem = menuItemService.createMenuItem(menuItem); - return new ResponseEntity<>(newMenuItem, HttpStatus.CREATED); - } - - @GetMapping("/{id}") - public ResponseEntity getMenuItemById(@PathVariable String id) { - MenuItem menuItem = menuItemService.getMenuItemById(id); - if (menuItem != null) { - return new ResponseEntity<>(menuItem, HttpStatus.OK); - } - return new ResponseEntity<>(HttpStatus.NOT_FOUND); - } - - @PutMapping("/{id}") - public ResponseEntity updateMenuItem(@PathVariable String id, @RequestBody MenuItem menuItemDetails) { - MenuItem updatedMenuItem = menuItemService.updateMenuItem(id, menuItemDetails); - if (updatedMenuItem != null) { - return new ResponseEntity<>(updatedMenuItem, HttpStatus.OK); - } - return new ResponseEntity<>(HttpStatus.NOT_FOUND); - } - - @DeleteMapping("/{id}") - public ResponseEntity deleteMenuItem(@PathVariable String id) { - boolean deleted = menuItemService.deleteMenuItem(id); - if (deleted) { - return new ResponseEntity<>(HttpStatus.NO_CONTENT); - } - return new ResponseEntity<>(HttpStatus.NOT_FOUND); - } - - @GetMapping("/count") - public ResponseEntity countMenuItems() { - long count = menuItemService.countMenuItems(); - return new ResponseEntity<>(count, HttpStatus.OK); - } -} \ No newline at end of file diff --git a/src/main/java/dev/example/restaurantManager/model/MenuItem.java b/src/main/java/dev/example/restaurantManager/model/MenuItem.java deleted file mode 100644 index d1ba851..0000000 --- a/src/main/java/dev/example/restaurantManager/model/MenuItem.java +++ /dev/null @@ -1,30 +0,0 @@ -package dev.example.restaurantManager.model; - -import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.NoArgsConstructor; -import java.util.List; - -@Data -@AllArgsConstructor -@NoArgsConstructor -@Entity -public class MenuItem { - - @Id - private String id; - private String name; - private String description; - private double price; - - @ManyToMany(mappedBy = "menuItems") - private List menus; - - public MenuItem(String id, String name, String description, double price) { - this.id = id; - this.name = name; - this.description = description; - this.price = price; - } -} \ No newline at end of file diff --git a/src/main/java/dev/example/restaurantManager/model/MenuItem/Dessert.java b/src/main/java/dev/example/restaurantManager/model/MenuItem/Dessert.java new file mode 100644 index 0000000..b46e134 --- /dev/null +++ b/src/main/java/dev/example/restaurantManager/model/MenuItem/Dessert.java @@ -0,0 +1,28 @@ +package dev.example.restaurantManager.model.MenuItem; + +import jakarta.persistence.Entity; + + +@Entity +public class Dessert extends MenuItem{ + + private boolean sugarFree; + + public Dessert(){ + + } + + public Dessert(String id, String name, String description, double price, boolean sugarFree) { + super(id,name,description,price); + this.sugarFree = sugarFree; + } + + public boolean isSugarFree(){ + return sugarFree; + } + + public void setSugarFree(boolean sugarFree){ + this.sugarFree = sugarFree; + } + +} diff --git a/src/main/java/dev/example/restaurantManager/model/MenuItem/IMenuItem.java b/src/main/java/dev/example/restaurantManager/model/MenuItem/IMenuItem.java new file mode 100644 index 0000000..b9c1cf3 --- /dev/null +++ b/src/main/java/dev/example/restaurantManager/model/MenuItem/IMenuItem.java @@ -0,0 +1,22 @@ +package dev.example.restaurantManager.model.MenuItem; + +public interface IMenuItem { + String getId(); + String getName(); + String getDescription(); + double getPrice(); + + + void setId(String id); + void setName(String name); + void setDescription(String description); + void setPrice(double price); + + + +// private String id; +// private String name; +// private String description; +// private double price; + +} diff --git a/src/main/java/dev/example/restaurantManager/model/MenuItem/MainCourse.java b/src/main/java/dev/example/restaurantManager/model/MenuItem/MainCourse.java new file mode 100644 index 0000000..7750ddc --- /dev/null +++ b/src/main/java/dev/example/restaurantManager/model/MenuItem/MainCourse.java @@ -0,0 +1,27 @@ +package dev.example.restaurantManager.model.MenuItem; + +import jakarta.persistence.Entity; + +@Entity +public class MainCourse extends MenuItem{ + + private boolean vegan; + + public MainCourse(){ + + } + + public MainCourse(String id, String name, String description, double price, boolean vegan) { + super(id,name,description,price); + this.vegan = vegan; + } + + public boolean isVegan(){ + return vegan; + } + + public void setVegan(boolean vegan){ + this.vegan = vegan; + } + +} diff --git a/src/main/java/dev/example/restaurantManager/model/MenuItem/MenuItem.java b/src/main/java/dev/example/restaurantManager/model/MenuItem/MenuItem.java new file mode 100644 index 0000000..1a56c37 --- /dev/null +++ b/src/main/java/dev/example/restaurantManager/model/MenuItem/MenuItem.java @@ -0,0 +1,80 @@ +package dev.example.restaurantManager.model.MenuItem; + +import dev.example.restaurantManager.model.MenuRestaurant; +import jakarta.persistence.*; + +import java.util.List; + +@Entity +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +public abstract class MenuItem implements IMenuItem{ + + @Id + private String id; + private String name; + private String description; + private double price; + + @ManyToMany(mappedBy = "menuItems") + private List menus; + + public MenuItem(){ + } + + public MenuItem(String id, String name, String description, double price) { + this.id = id; + this.name = name; + this.description = description; + this.price = price; + } + + + @Override + public String getId() { + return id; + } + + @Override + public String getName() { + return name; + } + + @Override + public String getDescription() { + return description; + } + + @Override + public double getPrice() { + return price; + } + + @Override + public void setId(String id) { + this.id = id; + } + + @Override + public void setName(String name) { + this.name = name; + } + + @Override + public void setDescription(String description) { + this.description = description; + } + + @Override + public void setPrice(double price) { + this.price = price; + } + + @Override + public String toString(){ + return "id=" + this.id + + ", name=" + this.name + + ", description=" + this.description + + ", price=" + this.price; + } + +} \ No newline at end of file diff --git a/src/main/java/dev/example/restaurantManager/model/MenuRestaurant.java b/src/main/java/dev/example/restaurantManager/model/MenuRestaurant.java index f23a7ca..c1cdbc6 100644 --- a/src/main/java/dev/example/restaurantManager/model/MenuRestaurant.java +++ b/src/main/java/dev/example/restaurantManager/model/MenuRestaurant.java @@ -1,9 +1,9 @@ package dev.example.restaurantManager.model; -import com.fasterxml.jackson.annotation.JsonIgnore; +import dev.example.restaurantManager.model.MenuItem.MenuItem; import jakarta.persistence.*; import lombok.*; -import java.util.ArrayList; + import java.util.List; import java.util.Objects; diff --git a/src/main/java/dev/example/restaurantManager/repository/DessertRepository.java b/src/main/java/dev/example/restaurantManager/repository/DessertRepository.java new file mode 100644 index 0000000..8c84d8b --- /dev/null +++ b/src/main/java/dev/example/restaurantManager/repository/DessertRepository.java @@ -0,0 +1,14 @@ +package dev.example.restaurantManager.repository; + +import dev.example.restaurantManager.model.MenuItem.Dessert; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.Optional; + +public interface DessertRepository extends JpaRepository { + + // to do some test + Optional findByDescription(String description); + + +} diff --git a/src/main/java/dev/example/restaurantManager/repository/MainCourseRepository.java b/src/main/java/dev/example/restaurantManager/repository/MainCourseRepository.java new file mode 100644 index 0000000..5f6cee4 --- /dev/null +++ b/src/main/java/dev/example/restaurantManager/repository/MainCourseRepository.java @@ -0,0 +1,7 @@ +package dev.example.restaurantManager.repository; + +import dev.example.restaurantManager.model.MenuItem.MainCourse; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface MainCourseRepository extends JpaRepository { +} diff --git a/src/main/java/dev/example/restaurantManager/repository/MenuItemRepository.java b/src/main/java/dev/example/restaurantManager/repository/MenuItemRepository.java deleted file mode 100644 index cfe3f40..0000000 --- a/src/main/java/dev/example/restaurantManager/repository/MenuItemRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package dev.example.restaurantManager.repository; - -import dev.example.restaurantManager.model.MenuItem; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface MenuItemRepository extends JpaRepository { -} \ No newline at end of file diff --git a/src/main/java/dev/example/restaurantManager/service/DessertService.java b/src/main/java/dev/example/restaurantManager/service/DessertService.java new file mode 100644 index 0000000..2b885d6 --- /dev/null +++ b/src/main/java/dev/example/restaurantManager/service/DessertService.java @@ -0,0 +1,15 @@ +package dev.example.restaurantManager.service; + +import dev.example.restaurantManager.model.MenuItem.Dessert; + +import java.util.List; + +public interface DessertService { + List getAllDesserts(); + List getAllSugarFreeDesserts(); + Dessert createDessert(Dessert dessert); + Dessert getDessertById(String id); + Dessert updateDessert(String id, Dessert dessertDetails); + boolean deleteDessert(String id); + long countDesserts(); +} diff --git a/src/main/java/dev/example/restaurantManager/service/DessertServiceImpl.java b/src/main/java/dev/example/restaurantManager/service/DessertServiceImpl.java new file mode 100644 index 0000000..7fa5be1 --- /dev/null +++ b/src/main/java/dev/example/restaurantManager/service/DessertServiceImpl.java @@ -0,0 +1,68 @@ +package dev.example.restaurantManager.service; + +import dev.example.restaurantManager.model.MenuItem.Dessert; +import dev.example.restaurantManager.repository.DessertRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; + + +@Service +public class DessertServiceImpl implements DessertService{ + + @Autowired + DessertRepository dessertRepository; + + @Override + public List getAllDesserts() { + return dessertRepository.findAll(); + } + + @Override + public List getAllSugarFreeDesserts() { + return dessertRepository.findAll().stream() + .filter(Dessert::isSugarFree) + .collect(Collectors.toList()); + } + + @Override + public Dessert createDessert(Dessert dessert) { + dessert.setId(UUID.randomUUID().toString()); + return dessertRepository.save(dessert); + } + + @Override + public Dessert getDessertById(String id) { + return dessertRepository.findById(id).orElse(null); + } + + @Override + public Dessert updateDessert(String id, Dessert dessertDetails) { + Dessert dessert = getDessertById(id); + if(dessert==null){ + return null; + } + dessert.setName(dessertDetails.getName()); + dessert.setDescription(dessertDetails.getDescription()); + dessert.setPrice(dessertDetails.getPrice()); + dessert.setSugarFree(dessertDetails.isSugarFree()); + return dessertRepository.save(dessert); + } + + @Override + public boolean deleteDessert(String id) { + if(!dessertRepository.existsById(id)) { + return false; + } + dessertRepository.deleteById(id); + return true; + } + + @Override + public long countDesserts() { + return dessertRepository.count(); + } +} diff --git a/src/main/java/dev/example/restaurantManager/service/MainCourseService.java b/src/main/java/dev/example/restaurantManager/service/MainCourseService.java new file mode 100644 index 0000000..424d6a4 --- /dev/null +++ b/src/main/java/dev/example/restaurantManager/service/MainCourseService.java @@ -0,0 +1,15 @@ +package dev.example.restaurantManager.service; + +import dev.example.restaurantManager.model.MenuItem.MainCourse; + +import java.util.List; + +public interface MainCourseService { + List getAllMainCourses(); + List getAllVeganMainCourses(); + MainCourse createMainCourse(MainCourse mainCourse); + MainCourse getMainCourseById(String id); + MainCourse updateMainCourse(String id, MainCourse mainCourseDetails); + boolean deleteMainCourse(String id); + long countMainCourses(); +} diff --git a/src/main/java/dev/example/restaurantManager/service/MainCourseServiceImpl.java b/src/main/java/dev/example/restaurantManager/service/MainCourseServiceImpl.java new file mode 100644 index 0000000..f1d0f25 --- /dev/null +++ b/src/main/java/dev/example/restaurantManager/service/MainCourseServiceImpl.java @@ -0,0 +1,67 @@ +package dev.example.restaurantManager.service; + +import dev.example.restaurantManager.model.MenuItem.MainCourse; +import dev.example.restaurantManager.repository.MainCourseRepository; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; +import java.util.UUID; +import java.util.stream.Collectors; + +@Service +public class MainCourseServiceImpl implements MainCourseService{ + + @Autowired + MainCourseRepository mainCourseRepository; + + @Override + public List getAllMainCourses() { + return mainCourseRepository.findAll(); + } + + @Override + public List getAllVeganMainCourses() { + return mainCourseRepository.findAll().stream() + .filter(MainCourse::isVegan) + .collect(Collectors.toList()); + } + + @Override + public MainCourse createMainCourse(MainCourse mainCourse) { + mainCourse.setId(UUID.randomUUID().toString()); + return mainCourseRepository.save(mainCourse); + } + + @Override + public MainCourse getMainCourseById(String id) { + return mainCourseRepository.findById(id).orElse(null); + } + + @Override + public MainCourse updateMainCourse(String id, MainCourse mainCourseDetails) { + MainCourse mainCourse = getMainCourseById(id); + if(mainCourse==null){ + return null; + } + mainCourse.setName(mainCourseDetails.getName()); + mainCourse.setDescription(mainCourseDetails.getDescription()); + mainCourse.setPrice(mainCourseDetails.getPrice()); + mainCourse.setVegan(mainCourseDetails.isVegan()); + return mainCourseRepository.save(mainCourse); + } + + @Override + public boolean deleteMainCourse(String id) { + if(!mainCourseRepository.existsById(id)) { + return false; + } + mainCourseRepository.deleteById(id); + return true; + } + + @Override + public long countMainCourses() { + return mainCourseRepository.count(); + } +} diff --git a/src/main/java/dev/example/restaurantManager/service/MenuItemService.java b/src/main/java/dev/example/restaurantManager/service/MenuItemService.java deleted file mode 100644 index b45c30f..0000000 --- a/src/main/java/dev/example/restaurantManager/service/MenuItemService.java +++ /dev/null @@ -1,13 +0,0 @@ -package dev.example.restaurantManager.service; - -import dev.example.restaurantManager.model.MenuItem; -import java.util.List; - -public interface MenuItemService { - List getAllMenuItems(); - MenuItem createMenuItem(MenuItem menuItem); - MenuItem getMenuItemById(String id); - MenuItem updateMenuItem(String id, MenuItem menuItemDetails); - boolean deleteMenuItem(String id); - long countMenuItems(); -} \ No newline at end of file diff --git a/src/main/java/dev/example/restaurantManager/service/MenuItemServiceImpl.java b/src/main/java/dev/example/restaurantManager/service/MenuItemServiceImpl.java deleted file mode 100644 index ccce3d3..0000000 --- a/src/main/java/dev/example/restaurantManager/service/MenuItemServiceImpl.java +++ /dev/null @@ -1,57 +0,0 @@ -package dev.example.restaurantManager.service; - -import dev.example.restaurantManager.model.MenuItem; -import dev.example.restaurantManager.repository.MenuItemRepository; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; -import java.util.List; -import java.util.UUID; - -@Service -public class MenuItemServiceImpl implements MenuItemService { - - @Autowired - private MenuItemRepository menuItemRepository; - - @Override - public List getAllMenuItems() { - return menuItemRepository.findAll(); - } - - @Override - public MenuItem createMenuItem(MenuItem menuItem) { - menuItem.setId(UUID.randomUUID().toString()); - return menuItemRepository.save(menuItem); - } - - @Override - public MenuItem getMenuItemById(String id) { - return menuItemRepository.findById(id).orElse(null); - } - - @Override - public MenuItem updateMenuItem(String id, MenuItem menuItemDetails) { - MenuItem menuItem = menuItemRepository.findById(id).orElse(null); - if (menuItem != null) { - menuItem.setName(menuItemDetails.getName()); - menuItem.setDescription(menuItemDetails.getDescription()); - menuItem.setPrice(menuItemDetails.getPrice()); - return menuItemRepository.save(menuItem); - } - return null; - } - - @Override - public boolean deleteMenuItem(String id) { - if (menuItemRepository.existsById(id)) { - menuItemRepository.deleteById(id); - return true; - } - return false; - } - - @Override - public long countMenuItems() { - return menuItemRepository.count(); - } -} \ No newline at end of file diff --git a/src/main/java/dev/example/restaurantManager/service/MenuRestaurantServiceImpl.java b/src/main/java/dev/example/restaurantManager/service/MenuRestaurantServiceImpl.java index 2162bfd..882d7db 100644 --- a/src/main/java/dev/example/restaurantManager/service/MenuRestaurantServiceImpl.java +++ b/src/main/java/dev/example/restaurantManager/service/MenuRestaurantServiceImpl.java @@ -1,9 +1,10 @@ package dev.example.restaurantManager.service; import dev.example.restaurantManager.model.MenuRestaurant; -import dev.example.restaurantManager.model.MenuItem; +import dev.example.restaurantManager.model.MenuItem.MenuItem; +import dev.example.restaurantManager.repository.DessertRepository; +import dev.example.restaurantManager.repository.MainCourseRepository; import dev.example.restaurantManager.repository.MenuRestaurantRepository; -import dev.example.restaurantManager.repository.MenuItemRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @@ -16,7 +17,10 @@ public class MenuRestaurantServiceImpl implements MenuRestaurantService { private MenuRestaurantRepository menuRestaurantRepository; @Autowired - private MenuItemRepository menuItemRepository; + private MainCourseRepository mainCourseRepository; + + @Autowired + private DessertRepository dessertRepository; @Override public List getAllMenus() { @@ -59,10 +63,21 @@ public long countMenus() { return menuRestaurantRepository.count(); } + + private MenuItem getMenuItem(String menuItemId){ + if(mainCourseRepository.existsById(menuItemId)){ + return mainCourseRepository.findById(menuItemId).orElse(null); + } + if(dessertRepository.existsById(menuItemId)){ + return dessertRepository.findById(menuItemId).orElse(null); + } + return null; + } + @Override public MenuRestaurant addMenuItemToMenu(String menuId, String menuItemId) { + MenuItem menuItem = getMenuItem(menuItemId); MenuRestaurant menu = menuRestaurantRepository.findById(menuId).orElse(null); - MenuItem menuItem = menuItemRepository.findById(menuItemId).orElse(null); if (menu != null && menuItem != null) { menu.getMenuItems().add(menuItem); return menuRestaurantRepository.save(menu); diff --git a/src/main/java/dev/example/restaurantManager/utilities/DataLoader.java b/src/main/java/dev/example/restaurantManager/utilities/DataLoader.java index 760cf2a..d82cd14 100644 --- a/src/main/java/dev/example/restaurantManager/utilities/DataLoader.java +++ b/src/main/java/dev/example/restaurantManager/utilities/DataLoader.java @@ -2,11 +2,13 @@ import com.github.javafaker.Faker; import dev.example.restaurantManager.model.*; +import dev.example.restaurantManager.model.MenuItem.Dessert; +import dev.example.restaurantManager.model.MenuItem.MainCourse; +import dev.example.restaurantManager.model.MenuItem.MenuItem; import dev.example.restaurantManager.repository.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import java.time.LocalDate; import java.util.*; import java.util.concurrent.TimeUnit; @@ -18,7 +20,9 @@ public class DataLoader { @Autowired private TableRestaurantRepository tableRepository; @Autowired - private MenuItemRepository menuItemRepository; + private MainCourseRepository mainCourseRepository; + @Autowired + private DessertRepository dessertRepository; @Autowired private MenuRestaurantRepository menuRepository; @Autowired @@ -44,7 +48,8 @@ public void loadAllData() { // then create relationships between them createCustomers(); createTables(); - createMenuItems(); + createMainCourses(); + createDesserts(); // create and assign menu items createMenusAndAssignMenuItems(); @@ -94,17 +99,33 @@ private void createTables() { } } - // we are going to create 25 menu items + // we are going to create 25 mainCourses // and save them in the H2 local database - private void createMenuItems() { + private void createMainCourses() { for (int i = 0; i < 25; i++) { - MenuItem menuItem = new MenuItem( + MainCourse mainCourse = new MainCourse( UUID.randomUUID().toString(), faker.food().dish(), faker.food().ingredient() + " " + faker.food().ingredient() , - faker.number().randomDouble(2, 5, 30) + faker.number().randomDouble(2, 5, 30), + faker.bool().bool() + ); + mainCourseRepository.save(mainCourse); + } + } + + // we are going to create 25 desserts + // and save them in the H2 local database + private void createDesserts() { + for (int i = 0; i < 25; i++) { + Dessert dessert = new Dessert( + UUID.randomUUID().toString(), + faker.food().fruit(), + faker.food().ingredient() + " " + faker.food().ingredient() , + faker.number().randomDouble(2, 5, 30), + faker.bool().bool() ); - menuItemRepository.save(menuItem); + dessertRepository.save(dessert); } } @@ -112,16 +133,22 @@ private void createMenuItems() { // and assign 5 to 10 menu items to each menu // to create a many-to-many relationship private void createMenusAndAssignMenuItems() { - List allItems = menuItemRepository.findAll(); + List allMainCourses = mainCourseRepository.findAll(); + List allDesserts = dessertRepository.findAll(); for (int i = 0; i < 15; i++) { MenuRestaurant menu = new MenuRestaurant( UUID.randomUUID().toString(), " Menu-00" + (i + 1), faker.lorem().paragraph() ); - List menuItems = new ArrayList<>(faker.random().nextInt(5, 10)); - for (int j = 0; j < faker.random().nextInt(5, 10); j++) { - menuItems.add(allItems.get(faker.random().nextInt(allItems.size()))); + int mainCourses = faker.random().nextInt(5, 10); + int desserts = faker.random().nextInt(2, 5); + List menuItems = new ArrayList<>(mainCourses+desserts); + for (int j = 0; j < mainCourses; j++) { + menuItems.add(allMainCourses.get(faker.random().nextInt(allMainCourses.size()))); + } + for (int j = 0; j < desserts; j++) { + menuItems.add(allDesserts.get(faker.random().nextInt(allDesserts.size()))); } menu.setMenuItems(menuItems); menuRepository.save(menu); diff --git a/src/main/resources/application-local.properties b/src/main/resources/application-local.properties index 89a2791..4dacbdc 100644 --- a/src/main/resources/application-local.properties +++ b/src/main/resources/application-local.properties @@ -2,17 +2,25 @@ # H2 LOCAL DB SERVER -spring.datasource.url=jdbc:h2:/home/albert/MyProjects/DataBase/restaurantManager/restaurantManagerDB -#spring.datasource.url=jdbc:h2:/home/albert/MyProjects/DataBase/restaurantManager/restaurantManagerDBTest -spring.datasource.username=albert -spring.datasource.password=1234 +spring.datasource.url=jdbc:h2:/home/jc/Documents/restaurant-db/db-pra05 +spring.datasource.username=jc +spring.datasource.password= + +#spring.datasource.url=jdbc:h2:/home/albert/MyProjects/DataBase/restaurantManager/restaurantManagerDB +##spring.datasource.url=jdbc:h2:/home/albert/MyProjects/DataBase/restaurantManager/restaurantManagerDBTest +#spring.datasource.username=albert +#spring.datasource.password=1234 + +# see sql in console +#spring.jpa.show-sql=true +#spring.jpa.properties.hibernate.format_sql=true # DDL OPTIONS: create-drop, create, update, none, validate -spring.jpa.hibernate.ddl-auto=create +spring.jpa.hibernate.ddl-auto=none # create ? Hibernate first drops existing tables and then creates new tables. # update ? The object model created based on the mappings # (annotations or XML) is compared with the existing schema, # and then Hibernate updates the schema according to the diff. # It never deletes the existing tables or columns # even if they are no longer required by the application. -# https://albertprofe.dev/springboot/sblab0-5.html#ddl \ No newline at end of file +# https://albertprofe.dev/springboot/sblab0-5.html#ddl diff --git a/src/test/java/dev/example/restaurantManager/menuItem/DessertRepositoryTest.java b/src/test/java/dev/example/restaurantManager/menuItem/DessertRepositoryTest.java new file mode 100644 index 0000000..d4614a2 --- /dev/null +++ b/src/test/java/dev/example/restaurantManager/menuItem/DessertRepositoryTest.java @@ -0,0 +1,87 @@ +package dev.example.restaurantManager.menuItem; + +import dev.example.restaurantManager.model.MenuItem.Dessert; +import dev.example.restaurantManager.repository.DessertRepository; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest +public class DessertRepositoryTest { + + @Autowired + DessertRepository dessertRepository; + + private final String description = "This dessert is only for test"; + private Dessert dessert; + + @BeforeEach + public void setup(){ + dessert = new Dessert("ID-will-change-on-insert-to-db","Dessert test",description,6.66,true); + } + + @AfterEach + public void deleteFakeInsertedDessert(){ + Optional foundDessert = dessertRepository.findByDescription(description); + foundDessert.ifPresent(value -> dessertRepository.delete(value)); + } + + @Test + public void insertDessert(){ + Dessert insertedDessert = dessertRepository.save(dessert); + assertThat(insertedDessert).isNotNull(); + assertThat(insertedDessert.getDescription()).isEqualTo(dessert.getDescription()); + } + + @Test + public void deleteDessert(){ + Dessert insertedDessert = dessertRepository.save(dessert); + assertThat(insertedDessert).isNotNull(); + dessertRepository.deleteById(insertedDessert.getId()); + Optional deletedDessert = dessertRepository.findById(insertedDessert.getId()); + assertThat(deletedDessert).isNotPresent(); + } + + @Test + public void updateDessert(){ + Dessert insertedDessert = dessertRepository.save(dessert); + assertThat(insertedDessert).isNotNull(); + String newDescription = "This is a new description for our fake dessert"; + assertThat(insertedDessert.getDescription()).isNotEqualTo(newDescription); + insertedDessert.setDescription(newDescription); + Dessert updatedDessert = dessertRepository.save(insertedDessert); + assertThat(updatedDessert).isNotNull(); + assertThat(updatedDessert.getId()).isEqualTo(insertedDessert.getId()); + assertThat(updatedDessert.getDescription()).isEqualTo(newDescription); + } + + @Test + public void findAll(){ + long nDessertsBeforeInsert = dessertRepository.findAll().size(); + Dessert insertedDessert = dessertRepository.save(dessert); + long nDessertsAfterInsert = dessertRepository.findAll().size(); + assertThat(nDessertsAfterInsert).isEqualTo(nDessertsBeforeInsert+1); + } + + @Test + public void findById(){ + Dessert insertedDessert = dessertRepository.save(dessert); + assertThat(insertedDessert).isNotNull(); + Optional foundDessert = dessertRepository.findById(insertedDessert.getId()); + assertThat(foundDessert).isPresent(); + } + + @Test + public void findByIdNonexistentDessert(){ + Optional foundDessert = dessertRepository.findById("Non existent dessert ID"); + assertThat(foundDessert).isNotPresent(); + } + +} diff --git a/src/test/java/dev/example/restaurantManager/menuItem/DessertServiceTest.java b/src/test/java/dev/example/restaurantManager/menuItem/DessertServiceTest.java new file mode 100644 index 0000000..999adfa --- /dev/null +++ b/src/test/java/dev/example/restaurantManager/menuItem/DessertServiceTest.java @@ -0,0 +1,91 @@ +package dev.example.restaurantManager.menuItem; + +import dev.example.restaurantManager.model.MenuItem.Dessert; +import dev.example.restaurantManager.repository.DessertRepository; +import dev.example.restaurantManager.service.DessertService; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.List; +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest +public class DessertServiceTest { + + @Autowired + DessertService dessertService; + + @Autowired + DessertRepository dessertRepository; + + private final String description = "This dessert is only for test"; + private Dessert dessert; + + @BeforeEach + public void setup(){ + dessert = new Dessert("ID-will-change-on-insert-to-db","Dessert test",description,6.66,true); + } + + @AfterEach + public void deleteFakeInsertedDessert(){ + Optional foundDessert = dessertRepository.findByDescription(description); + foundDessert.ifPresent(value -> dessertRepository.delete(value)); + } + + @Test + public void createDessert(){ + Dessert newDessert = dessertService.createDessert(dessert); + assertThat(newDessert).isNotNull(); + assertThat(newDessert.getDescription()).isEqualTo(dessert.getDescription()); + } + + @Test + public void deleteDessert(){ + Dessert insertedDessert = dessertRepository.save(dessert); + assertThat(insertedDessert).isNotNull(); + boolean deleted = dessertService.deleteDessert(insertedDessert.getId()); + assertThat(deleted).isEqualTo(true); + } + + @Test + public void updateDessert(){ + Dessert insertedDessert = dessertRepository.save(dessert); + assertThat(insertedDessert).isNotNull(); + String newDescription = "This is a new description for our fake dessert"; + dessert.setDescription(newDescription); + Dessert updatedDessert = dessertService.updateDessert(insertedDessert.getId(),dessert); + assertThat(updatedDessert).isNotNull(); + assertThat(updatedDessert.getId()).isEqualTo(insertedDessert.getId()); + assertThat(updatedDessert.getDescription()).isEqualTo(newDescription); + + } + + @Test + public void findAll(){ + long nDesserts = dessertRepository.count(); + List desserts = dessertService.getAllDesserts(); + assertThat(desserts.size()).isEqualTo(nDesserts); + } + + @Test + public void findById(){ + Dessert insertedDessert = dessertRepository.save(dessert); + assertThat(insertedDessert).isNotNull(); + Dessert foundDessert = dessertService.getDessertById(insertedDessert.getId()); + assertThat(foundDessert).isNotNull(); + assertThat(foundDessert.getDescription()).isEqualTo(insertedDessert.getDescription()); + } + + @Test + public void findByIdNonexistentDessert(){ + Dessert foundDessert = dessertService.getDessertById("Non existent dessert ID"); + assertThat(foundDessert).isNull(); + } + + +} diff --git a/src/test/java/dev/example/restaurantManager/menuItem/DessertTest.java b/src/test/java/dev/example/restaurantManager/menuItem/DessertTest.java new file mode 100644 index 0000000..feeb64b --- /dev/null +++ b/src/test/java/dev/example/restaurantManager/menuItem/DessertTest.java @@ -0,0 +1,50 @@ +package dev.example.restaurantManager.menuItem; + +import dev.example.restaurantManager.model.MenuItem.Dessert; +import dev.example.restaurantManager.repository.DessertRepository; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.assertj.core.api.Assertions.assertThat; + + +@SpringBootTest +public class DessertTest { + + @Autowired + private DessertRepository dessertRepository; + + @Test + void newInterfaceDessert_InMemory(){ + String id = "MI001"; + String name = "item 1"; + String description = "item 1 description"; + double price = 20.0; + boolean sugarFree = true; + Dessert dessert = new Dessert(); + dessert.setId(id); + dessert.setName(name); + dessert.setDescription(description); + dessert.setPrice(price); + dessert.setSugarFree(sugarFree); + + assertThat(dessert.getId()).isEqualTo(id); + assertThat(dessert.getName()).isEqualTo(name); + assertThat(dessert.getDescription()).isEqualTo(description); + assertThat(dessert.getPrice()).isEqualTo(price); + assertThat(dessert.isSugarFree()).isEqualTo(sugarFree); + } + + @Test + void newInterfaceDessert_DB(){ + Dessert dessert = dessertRepository.findAll().getFirst(); + + assertThat(dessert.getId()).isNotNull(); + assertThat(dessert.getName()).isNotNull(); + assertThat(dessert.getDescription()).isNotNull(); + assertThat(dessert.getPrice()).isNotNull(); + assertThat(dessert.isSugarFree()).isNotNull(); + } + +} diff --git a/src/test/java/dev/example/restaurantManager/menuItem/MainCourseTest.java b/src/test/java/dev/example/restaurantManager/menuItem/MainCourseTest.java new file mode 100644 index 0000000..bcbe4e9 --- /dev/null +++ b/src/test/java/dev/example/restaurantManager/menuItem/MainCourseTest.java @@ -0,0 +1,51 @@ +package dev.example.restaurantManager.menuItem; + +import dev.example.restaurantManager.model.MenuItem.MainCourse; +import dev.example.restaurantManager.repository.MainCourseRepository; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +import static org.assertj.core.api.Assertions.assertThat; + +@SpringBootTest +public class MainCourseTest { + + @Autowired + private MainCourseRepository mainCourseRepository; + + @Test + void newInterfaceMainCourse_InMemory(){ + String id = "MI001"; + String name = "item 1"; + String description = "item 1 description"; + double price = 20.0; + boolean vegan = false; + MainCourse mainCourse = new MainCourse(); + mainCourse.setId(id); + mainCourse.setName(name); + mainCourse.setDescription(description); + mainCourse.setPrice(price); + mainCourse.setVegan(vegan); + + assertThat(mainCourse.getId()).isEqualTo(id); + assertThat(mainCourse.getName()).isEqualTo(name); + assertThat(mainCourse.getDescription()).isEqualTo(description); + assertThat(mainCourse.getPrice()).isEqualTo(price); + assertThat(mainCourse.isVegan()).isEqualTo(vegan); + } + + @Test + void newInterfaceMainCourse_DB(){ + MainCourse mainCourse = mainCourseRepository.findAll().getFirst(); + + assertThat(mainCourse.getId()).isNotNull(); + assertThat(mainCourse.getName()).isNotNull(); + assertThat(mainCourse.getDescription()).isNotNull(); + assertThat(mainCourse.getPrice()).isNotNull(); + assertThat(mainCourse.isVegan()).isNotNull(); + } +} + + +