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
@@ -1,13 +1,20 @@
package dev.example.restaurantManager;

import dev.example.restaurantManager.utilities.DataLoader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class RestaurantManagerApplication {


public static void main(String[] args) {
SpringApplication.run(RestaurantManagerApplication.class, args);
}



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

import dev.example.restaurantManager.model.Dessert;
import dev.example.restaurantManager.model.MainCourse;
import dev.example.restaurantManager.model.MenuItem;
import dev.example.restaurantManager.service.MenuItemService;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -16,17 +18,22 @@ public class MenuItemController {
@Autowired
private MenuItemService menuItemService;

// Get all MenuItems (including MainCourse and Dessert)

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

// Create a MenuItem (can be a MainCourse, Dessert, or other MenuItem)

@PostMapping
public ResponseEntity<MenuItem> createMenuItem(@RequestBody MenuItem menuItem) {
MenuItem newMenuItem = menuItemService.createMenuItem(menuItem);
return new ResponseEntity<>(newMenuItem, HttpStatus.CREATED);
}
// Get a MenuItem by ID (including MainCourse and Dessert)

@GetMapping("/{id}")
public ResponseEntity<MenuItem> getMenuItemById(@PathVariable String id) {
Expand All @@ -36,6 +43,7 @@ public ResponseEntity<MenuItem> getMenuItemById(@PathVariable String id) {
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
// Update an existing MenuItem

@PutMapping("/{id}")
public ResponseEntity<MenuItem> updateMenuItem(@PathVariable String id, @RequestBody MenuItem menuItemDetails) {
Expand All @@ -45,6 +53,7 @@ public ResponseEntity<MenuItem> updateMenuItem(@PathVariable String id, @Request
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
// Delete a MenuItem by ID

@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteMenuItem(@PathVariable String id) {
Expand All @@ -54,10 +63,25 @@ public ResponseEntity<Void> deleteMenuItem(@PathVariable String id) {
}
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
// Get the count of all MenuItems

@GetMapping("/count")
public ResponseEntity<Long> countMenuItems() {
long count = menuItemService.countMenuItems();
return new ResponseEntity<>(count, HttpStatus.OK);
}

// Endpoint to create a MainCourse
@PostMapping("/maincourse")
public ResponseEntity<MainCourse> createMainCourse(@RequestBody MainCourse mainCourse){
MainCourse newMainCourse = menuItemService.createMainCourse(mainCourse);
return new ResponseEntity<>(newMainCourse, HttpStatus.CREATED);
}

@PostMapping("/dessert")
public ResponseEntity<Dessert> createDessert (@RequestBody Dessert dessert){
Dessert newDessert = menuItemService.createDessert(dessert);
return new ResponseEntity<>(newDessert,HttpStatus.CREATED);
}

}
42 changes: 42 additions & 0 deletions src/main/java/dev/example/restaurantManager/model/Dessert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package dev.example.restaurantManager.model;


import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@Entity
@DiscriminatorValue("DESSERT")

public class Dessert extends MenuItem {

private boolean isGlutenFree; // indicates if the dessert is gluten-free
private String flavor; //Flavor of dessert
private int sweetnessLevel; //Sweetness Level on a scale from 1 to 10;

//Constructor that inherits from MenuItem and initializes dessert specific attributes.

public Dessert (String id, String name, String description, double price, boolean isGlutenFree, String flavor, int sweetnessLevel){
super(id, name, description, price); // Calls the constructor of the parent class (MenuItem)
this.isGlutenFree = isGlutenFree; // Initializes the gluten-free attribute
this.flavor = flavor; // Initializes the flavor attribute
this.sweetnessLevel = sweetnessLevel; // Initializes the sweetness level attribute

}

//method to get a summary of dessert
public String getDessertSummary() {
return String.format("%s - %s (Price: %.2f, Gluten Free: %b, Flavor: %s, Sweetness Level: %d)",
getName(), getDescription(), getPrice(), isGlutenFree, flavor, sweetnessLevel);
}

public boolean isSuitableForDiet(String dietaryNeed) {
if(dietaryNeed.equalsIgnoreCase("gluten-free")){
return isGlutenFree; //Checks if the dessert is gluten-free
}
return false; //Default case
}
}
38 changes: 38 additions & 0 deletions src/main/java/dev/example/restaurantManager/model/MainCourse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dev.example.restaurantManager.model;

import jakarta.persistence.DiscriminatorValue;
import jakarta.persistence.Entity;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@Entity
@DiscriminatorValue("MAIN_COURSE")
public class MainCourse extends MenuItem {

private String sideDish; // "chips" or "salad"
private int calories; // calories content of the main course
private boolean chefRecommendation;// Indicates if it's a chef's special recommendation

//constructor inheriting from MenuItem and initializing MainCourse - specific attributes.
public MainCourse(String id, String name, String description, double price, String sideDish, int calories, boolean chefRecommendation){
super(id, name, description, price); // Calls the constructor of MenuItem
this.sideDish = sideDish; // Sets the side dish
this.calories = calories; // Sets the calories
this.chefRecommendation = chefRecommendation; // Marks if it's a chef's recommendation
}

public String getFullDescription() {
return String.format("%s - %s (Price: %.2f, Side Dish: %s, Calories: %d, Chef Recommended: %b)",
getName(), getDescription(), getPrice(), sideDish, calories, chefRecommendation);
}

public boolean isHealthy() {
return calories < 500; // Example threshold for healthy
}

public int comparePrice(MainCourse other) {
return Double.compare(this.getPrice(), other.getPrice()); // Compares prices of two main courses
}
}
17 changes: 16 additions & 1 deletion src/main/java/dev/example/restaurantManager/model/MenuItem.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.example.restaurantManager.model;

import dev.example.restaurantManager.model.interfaces.IMenuItem;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand All @@ -10,9 +11,13 @@
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class MenuItem {
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "item_type", discriminatorType = DiscriminatorType.STRING)

public abstract class MenuItem implements IMenuItem {

@Id

private String id;
private String name;
private String description;
Expand All @@ -27,4 +32,14 @@ public MenuItem(String id, String name, String description, double price) {
this.description = description;
this.price = price;
}

@Override
public String getItemType() {
// You can return the class type as the item type
return this.getClass().getSimpleName();
}
@Override
public String getSummary() {
return String.format("%s - %s (Price: %.2f)", name, description, price);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dev.example.restaurantManager.model.interfaces;

public interface IMenuItem {

//Method to get the name of the menu item
String getName();

//Method to get the description of the menu item
String getDescription();

//Method to get the price of the menu item
double getPrice();

//Method to get the item type
String getItemType();

//Method to provide a summary of the menu item
String getSummary();
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
package dev.example.restaurantManager.repository;

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

import java.util.List;

public interface MenuItemRepository extends JpaRepository<MenuItem, String> {

// Búsqueda solo de main courses
@Query("SELECT m FROM MainCourse m")
List<MainCourse> findAllMainCourses();

// Búsqueda solo de desserts
@Query("SELECT d FROM Dessert d")
List<Dessert> findAllDesserts();

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

import dev.example.restaurantManager.model.Dessert;
import dev.example.restaurantManager.model.MainCourse;
import dev.example.restaurantManager.model.MenuItem;
import java.util.List;

Expand All @@ -10,4 +12,8 @@ public interface MenuItemService {
MenuItem updateMenuItem(String id, MenuItem menuItemDetails);
boolean deleteMenuItem(String id);
long countMenuItems();

// New methods for MainCourse and Dessert
MainCourse createMainCourse(MainCourse mainCourse);
Dessert createDessert(Dessert dessert);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.example.restaurantManager.service;

import dev.example.restaurantManager.model.Dessert;
import dev.example.restaurantManager.model.MainCourse;
import dev.example.restaurantManager.model.MenuItem;
import dev.example.restaurantManager.repository.MenuItemRepository;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -13,22 +15,39 @@ public class MenuItemServiceImpl implements MenuItemService {
@Autowired
private MenuItemRepository menuItemRepository;

//Retrieve all MenuItems including subtypes
@Override
public List<MenuItem> getAllMenuItems() {
return menuItemRepository.findAll();
}

//Save a generic Menu item
@Override
public MenuItem createMenuItem(MenuItem menuItem) {
menuItem.setId(UUID.randomUUID().toString());
return menuItemRepository.save(menuItem);
}

//Save a MainCourse
public MainCourse createMainCourse(MainCourse mainCourse){
mainCourse.setId(UUID.randomUUID().toString());
return menuItemRepository.save(mainCourse);
}

//Save Dessert

public Dessert createDessert(Dessert dessert){
dessert.setId(UUID.randomUUID().toString());
return menuItemRepository.save(dessert);
}

//Retrieve a MenuItem by ID
@Override
public MenuItem getMenuItemById(String id) {
return menuItemRepository.findById(id).orElse(null);
}

//Update an existing MenuItem
@Override
public MenuItem updateMenuItem(String id, MenuItem menuItemDetails) {
MenuItem menuItem = menuItemRepository.findById(id).orElse(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,48 @@ private void createTables() {
// we are going to create 25 menu items
// and save them in the H2 local database
private void createMenuItems() {

// Listas predefinidas para los valores de sideDish, salad y dessert
List<String> sideDishes = Arrays.asList("Fries", "Rice", "Mashed Potatoes", "Grilled Vegetables", "Garlic Bread");
List<String> flavors = Arrays.asList("Chocolate", "Vanilla", "Strawberry", "Lemon", "Banana");
List<String> desserts = Arrays.asList("Chocolate Cake", "Ice Cream", "Apple Pie", "Cheesecake", "Tiramisu");

// Crear 25 elementos de menú de tipo MainCourse o Dessert
for (int i = 0; i < 25; i++) {
MenuItem menuItem = new MenuItem(
UUID.randomUUID().toString(),
faker.food().dish(),
faker.food().ingredient() + " " + faker.food().ingredient() ,
faker.number().randomDouble(2, 5, 30)
);
menuItemRepository.save(menuItem);

// Decidir aleatoriamente si el menú será un MainCourse o un Dessert
boolean isMainCourse = faker.random().nextBoolean();

if (isMainCourse) {
// Crear un MainCourse
MainCourse mainCourse = new MainCourse(
UUID.randomUUID().toString(),
faker.food().dish(),
faker.food().ingredient() + " " + faker.food().ingredient(),
faker.number().randomDouble(2, 5, 30),
sideDishes.get(faker.random().nextInt(sideDishes.size())), // Selecciona un side dish aleatorio
faker.number().numberBetween(200, 800), // Calorías
faker.random().nextBoolean() // Chef Recommendation
);

// Guardar el MainCourse
menuItemRepository.save(mainCourse);

} else {
// Crear un Dessert
Dessert dessert = new Dessert(
UUID.randomUUID().toString(),
desserts.get(faker.random().nextInt(desserts.size())),
faker.food().ingredient() + " " + faker.food().ingredient(),
faker.number().randomDouble(2, 3, 15),
faker.random().nextBoolean(), // Gluten Free
flavors.get(faker.random().nextInt(flavors.size())), // Flavor
faker.number().numberBetween(1, 10) // Sweetness Level
);

// Guardar el Dessert
menuItemRepository.save(dessert);
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
spring.application.name=restaurantManager

# application.properties
spring.profiles.active=local
spring.profiles.active=memory



Expand Down
Loading