Skip to content

Commit

Permalink
Recheck the functionality of every modlue
Browse files Browse the repository at this point in the history
  • Loading branch information
sanajitjana committed Nov 13, 2022
1 parent 824d1bf commit a95b3da
Show file tree
Hide file tree
Showing 14 changed files with 283 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ public class FoodCartController {

@PostMapping("/addtocart/{customerId}")
public ResponseEntity<FoodCart> addItemToCartHandler(@RequestParam(required = false) String key,
@PathVariable("customerId") Integer customerId, @RequestBody ItemDTO itemDTO)
@PathVariable("customerId") Integer customerId, @RequestParam(required = false) Integer itemId)
throws ItemException, CustomerException, LoginException {
FoodCart foodCart = foodCartService.addItemToCart(key, customerId, itemDTO);
FoodCart foodCart = foodCartService.addItemToCart(key, customerId, itemId);
return new ResponseEntity<FoodCart>(foodCart, HttpStatus.OK);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.foodyexpress.exception.CategoryException;
import com.foodyexpress.exception.ItemException;
import com.foodyexpress.exception.LoginException;
import com.foodyexpress.model.Category;
import com.foodyexpress.model.CategoryDTO;
import com.foodyexpress.model.Item;
import com.foodyexpress.model.ItemDTO;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.foodyexpress.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.foodyexpress.exception.CustomerException;
import com.foodyexpress.exception.LoginException;
import com.foodyexpress.exception.OrderHistoryException;
import com.foodyexpress.exception.RestaurantException;
import com.foodyexpress.model.OrderHistory;
import com.foodyexpress.service.OrderHistoryService;

@RestController
@RequestMapping("/order-history")
public class OrderHistoryController {

@Autowired
private OrderHistoryService orderHisService;

@GetMapping("/get")
public ResponseEntity<OrderHistory> OrderHistoryById(@RequestParam(required = false) String key,
@RequestParam(required = false) Integer orderHisId)
throws RestaurantException, LoginException, OrderHistoryException {
OrderHistory orderHistory = orderHisService.getOrderHistoryById(key, orderHisId);
return new ResponseEntity<OrderHistory>(orderHistory, HttpStatus.ACCEPTED);
}

@GetMapping("/customer-id")
public ResponseEntity<List<OrderHistory>> OrderHistoryByCustomerId(@RequestParam(required = false) String key,
@RequestParam(required = false) Integer customerId)
throws RestaurantException, LoginException, OrderHistoryException, CustomerException {
List<OrderHistory> orderHistoryList = orderHisService.getOrderHistoryByCustomerId(key, customerId);
return new ResponseEntity<List<OrderHistory>>(orderHistoryList, HttpStatus.ACCEPTED);
}

@GetMapping("/all")
public ResponseEntity<List<OrderHistory>> getAllOrderHistory(@RequestParam(required = false) String key)
throws RestaurantException, LoginException, OrderHistoryException, CustomerException {
List<OrderHistory> orderHistoryList = orderHisService.getAllOrderHistory(key);
return new ResponseEntity<List<OrderHistory>>(orderHistoryList, HttpStatus.ACCEPTED);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.foodyexpress.exception;

public class OrderHistoryException extends Exception {

public OrderHistoryException() {
// TODO Auto-generated constructor stub
}

public OrderHistoryException(String message) {
// TODO Auto-generated constructor stub
super(message);
}

}
11 changes: 10 additions & 1 deletion FoodyExpress/src/main/java/com/foodyexpress/model/Bill.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.validation.constraints.NotNull;

import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.AllArgsConstructor;
import lombok.Data;
Expand All @@ -24,10 +27,16 @@ public class Bill {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer billId;

@NotNull(message = "Date time required")
private LocalDateTime billDate;

@NotNull(message = "Total cost required")
private Double totalCost;

@NotNull(message = "Total item required")
private Integer totalItem;

@OneToOne
private OrderDetails order;

Expand Down
4 changes: 2 additions & 2 deletions FoodyExpress/src/main/java/com/foodyexpress/model/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public class Item {
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer itemId;

@NotNull(message = "Name is rqquire")
@NotNull(message = "Name is require")
private String itemName;

@NotNull(message = "Category is rqquire")
@NotNull(message = "Category is require")
@ManyToOne
private Category category;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.foodyexpress.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class ItemCreateDTO {
private String itemName;
private String catergoryName;
private Double cost;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.foodyexpress.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.validation.constraints.NotNull;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

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

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer orderHistoryId;

@NotNull(message = "Customer id required")
private Integer customerId;

@OneToOne(targetEntity = Bill.class)
private Bill bill;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.foodyexpress.repository;

import java.util.List;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.foodyexpress.model.OrderHistory;

@Repository
public interface OrderHistoryRepo extends JpaRepository<OrderHistory, Integer> {

public List<OrderHistory> findByCustomerId(Integer customerId);

}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.foodyexpress.service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

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

import com.foodyexpress.exception.BillException;
import com.foodyexpress.exception.CustomerException;
import com.foodyexpress.exception.ItemException;
import com.foodyexpress.exception.LoginException;
import com.foodyexpress.exception.OrderDetailsException;
import com.foodyexpress.model.Bill;
Expand All @@ -17,17 +17,21 @@
import com.foodyexpress.model.FoodCart;
import com.foodyexpress.model.Item;
import com.foodyexpress.model.OrderDetails;
import com.foodyexpress.model.OrderHistory;
import com.foodyexpress.repository.BillRepo;
import com.foodyexpress.repository.CurrentUserSessionRepo;
import com.foodyexpress.repository.CustomerRepo;
import com.foodyexpress.repository.FoodCartRepo;
import com.foodyexpress.repository.ItemRepo;
import com.foodyexpress.repository.OrderDetailsRepo;

import antlr.collections.List;
import com.foodyexpress.repository.OrderHistoryRepo;

@Service
public class BillServiceImpl implements BillService {

@Autowired
private ItemRepo itemRepo;

@Autowired
private BillRepo billRepo;

Expand All @@ -43,41 +47,63 @@ public class BillServiceImpl implements BillService {
@Autowired
private CurrentUserSessionRepo currSession;

@Autowired
private OrderHistoryRepo orderHistoryRepo;

@Override
public Bill generateBill(String key, Integer customerId, Integer orderDetailId)
throws CustomerException, OrderDetailsException, LoginException {
throws CustomerException, OrderDetailsException, LoginException, BillException {

// user validation
CurrentUserSession currSess = currSession.findByPrivateKey(key);
if (currSess == null)
throw new LoginException("Login required");

// order validation
Optional<OrderDetails> opt = orderDetailRepo.findById(orderDetailId);
if (opt.isEmpty())
throw new OrderDetailsException("order details not found ...");

// existing generate bill check
if (opt.get().getOrderStatus().equals("completed"))
throw new BillException("Bill already generated for this order id");

// customer validation
Optional<Customer> customerOpt = cusDAO.findById(customerId);
if (customerOpt.isEmpty())
throw new CustomerException("customer does not exist");
Bill bill = new Bill();

OrderDetails orderDetails = opt.get();
FoodCart foodCart = orderDetails.getFoodCart();

Double totalCost = 0D;
Integer totalItems = 0;
for (int i = 0; i < foodCart.getItemList().size(); i++) {
Item items = foodCart.getItemList().get(i);
totalCost = totalCost + (items.getQuantity() * items.getCost());
totalItems = totalItems + items.getQuantity();
totalCost += (items.getQuantity() * items.getCost());
totalItems += items.getQuantity();
items.setQuantity(1);
itemRepo.save(items);
}

Bill bill = new Bill();
bill.setTotalCost(totalCost);
bill.setTotalItem(totalItems);
bill.setBillDate(LocalDateTime.now());
bill.setOrder(orderDetails);
billRepo.save(bill);
orderDetails.setOrderStatus("completed");
foodCart.getItemList().clear();
// foodCartRepo.save(foodCart);

return bill;
// transfer bill to order history list
OrderHistory orderHis = new OrderHistory();
orderHis.setBill(bill);
orderHis.setCustomerId(customerId);
orderHistoryRepo.save(orderHis);

orderDetails.setOrderStatus("completed");
orderDetailRepo.save(orderDetails);

foodCartRepo.save(foodCart);
return bill;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

public interface FoodCartService {

public FoodCart addItemToCart(String key, Integer customerId, ItemDTO itemDTO) throws ItemException, CustomerException, LoginException;
public FoodCart addItemToCart(String key, Integer customerId, Integer itemId) throws ItemException, CustomerException, LoginException;

public FoodCart increaseItemQuantity(String key, Integer cartId, Integer quantity, Integer itemId) throws FoodCartException, ItemException, LoginException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class FoodCartServiceImpl implements FoodCartService {
private CurrentUserSessionRepo currSession;

@Override
public FoodCart addItemToCart(String key, Integer customerId, ItemDTO itemDTO) throws ItemException, CustomerException, LoginException {
public FoodCart addItemToCart(String key, Integer customerId, Integer itemId) throws ItemException, CustomerException, LoginException {

CurrentUserSession currSess = currSession.findByPrivateKey(key);
if (currSess == null)
Expand All @@ -47,7 +47,7 @@ public FoodCart addItemToCart(String key, Integer customerId, ItemDTO itemDTO) t
if (opt.isEmpty())
throw new CustomerException("Customer not found!");

Optional<Item> itemOpt = itemRepo.findById(itemDTO.getItemId());
Optional<Item> itemOpt = itemRepo.findById(itemId);
if (itemOpt.isEmpty())
throw new ItemException("Item not found!");

Expand All @@ -59,7 +59,7 @@ public FoodCart addItemToCart(String key, Integer customerId, ItemDTO itemDTO) t
for(int i=0;i<itemList.size();i++)
{
Item element=itemList.get(i);
if(element.getItemId()==itemDTO.getItemId())
if(element.getItemId()==itemId)
{
element.setQuantity(element.getQuantity()+1);
flag=false;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.foodyexpress.service;

import java.util.List;

import com.foodyexpress.exception.CustomerException;
import com.foodyexpress.exception.LoginException;
import com.foodyexpress.exception.OrderHistoryException;
import com.foodyexpress.model.OrderHistory;

public interface OrderHistoryService {

public OrderHistory getOrderHistoryById(String key, Integer orderHisId)
throws OrderHistoryException, LoginException;

public List<OrderHistory> getOrderHistoryByCustomerId(String key, Integer customerId)
throws OrderHistoryException, LoginException, CustomerException;

public List<OrderHistory> getAllOrderHistory(String key) throws OrderHistoryException, LoginException;

}
Loading

0 comments on commit a95b3da

Please sign in to comment.