Restaurant
Model.java
package com.example.restaurant.model;
import jakarta.persistence.*;
@Entity
@Table(name = "Menu_2797212")
public class Model {
@Id
@Column(name = "menuid", nullable = false, unique = true)
private String menuId;
@Column(name = "menuname", nullable = false)
private String menuName;
@Column(name = "description")
private String description;
@Column(name = "category", nullable = false)
private String category;
@Column(name = "type", nullable = false)
private String type;
@Column(name = "cost", nullable = false)
private double cost;
@Column(name = "status", nullable = false)
private String status;
// Constructors
public Model() {}
public Model(String menuId, String menuName, String description, String category, String type, double cost, String status) {
this.menuId = menuId;
this.menuName = menuName;
this.description = description;
this.category = category;
this.type = type;
this.cost = cost;
this.status = status;
}
// Getters and Setters
public String getMenuId() { return menuId; }
public void setMenuId(String menuId) { this.menuId = menuId; }
public String getMenuName() { return menuName; }
public void setMenuName(String menuName) { this.menuName = menuName; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; }
public String getCategory() { return category; }
public void setCategory(String category) { this.category = category; }
public String getType() { return type; }
public void setType(String type) { this.type = type; }
public double getCost() { return cost; }
public void setCost(double cost) { this.cost = cost; }
public String getStatus() { return status; }
public void setStatus(String status) { this.status = status; }
}
Repository.java
package com.example.restaurant.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.restaurant.model.Model;
public interface Repository extends JpaRepository<Model, String> {
}
Service.class
package com.example.restaurant.service;
import com.example.restaurant.model.Model;
import com.example.restaurant.repository.Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class Service {
@Autowired
private Repository repository;
public List<Model> getAllItems() {
return repository.findAll();
}
public Model getItemById(String menuId) {
return repository.findById(menuId).orElse(null);
}
public Model createItem(Model model) {
return repository.save(model);
}
public Model updateItem(String menuId, Model updatedModel) {
Optional<Model> optional = repository.findById(menuId);
if (optional.isPresent()) {
Model existing = optional.get();
existing.setMenuName(updatedModel.getMenuName());
existing.setDescription(updatedModel.getDescription());
existing.setCategory(updatedModel.getCategory());
existing.setType(updatedModel.getType());
existing.setCost(updatedModel.getCost());
existing.setStatus(updatedModel.getStatus());
return repository.save(existing);
}
return null;
}
public void deleteItem(String menuId) {
repository.deleteById(menuId);
}
}
Controller.java
package com.example.restaurant.controller;
import com.example.restaurant.model.Model;
import com.example.restaurant.service.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/menu")
@CrossOrigin(origins = "http://localhost:4200") // For Angular
public class Controller {
@Autowired
private Service service;
@GetMapping
public List<Model> getAllItems() {
return service.getAllItems();
}
@GetMapping("/{menuId}")
public Model getItemById(@PathVariable String menuId) {
return service.getItemById(menuId);
}
@PostMapping
public Model createItem(@RequestBody Model model) {
return service.createItem(model);
}
@PutMapping("/{menuId}")
public Model updateItem(@PathVariable String menuId, @RequestBody Model model) {
return service.updateItem(menuId, model);
}
@DeleteMapping("/{menuId}")
public void deleteItem(@PathVariable String menuId) {
service.deleteItem(menuId);
}
}
Comments
Post a Comment