Commit d71a3831 authored by 1516160's avatar 1516160

Upload New File

parent 05b0d0a5
package com.altimetrik.playground.candidate.assessment.service;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.altimetrik.playground.candidate.assessment.entity.Customer;
import com.altimetrik.playground.candidate.assessment.entity.CustomerCart;
import com.altimetrik.playground.candidate.assessment.entity.CustomerCartID;
import com.altimetrik.playground.candidate.assessment.entity.Product;
import com.altimetrik.playground.candidate.assessment.repository.CustomerCartRepo;
import com.altimetrik.playground.candidate.assessment.repository.CustomerRepo;
import com.altimetrik.playground.candidate.assessment.repository.ProductRepo;
import com.altimetrik.playground.candidate.assessment.request.CustomerReq;
import com.altimetrik.playground.candidate.assessment.request.ProductReq;
@Service
public class AdminDbConsoleService implements IAdminDbConsole {
@Autowired
private CustomerRepo repo;
@Autowired
private CustomerCartRepo ccRepo;
@Autowired
private ProductRepo pRepo;
@Override
public String newCustomer(CustomerReq request) {
if(repo.findById(request.getEmail()).isPresent()) {
return "Already Customer available";
} else {
Customer cust = new Customer();
cust.setAddress(request.getAddress());
cust.setAge(request.getAge());
cust.setEmail(request.getEmail());
cust.setGender(request.getGender());
cust.setName(request.getName());
cust.setPassword(request.getPassword());
repo.save(cust);
}
return "Customer Created";
}
@Override
public String addProduct(ProductReq request) {
CustomerCartID id = new CustomerCartID();
id.setEmail(request.getEmail());
id.setProdId(request.getProdId());
if(ccRepo.findById(id).isPresent()) {
return "Product already Added";
} else {
CustomerCart cc = new CustomerCart();
cc.setCheckOutStatus('N');
cc.setCustomerName(repo.findById(request.getEmail()).get().getName());
cc.setPrice(request.getPrice());
cc.setProdname(request.getProdname());
cc.setQuantity(request.getQuantity());
cc.setId(id);
ccRepo.save(cc);
}
return "Product added in the cart";
}
@Override
public String removeItem(ProductReq request) {
CustomerCartID id = new CustomerCartID();
id.setEmail(request.getEmail());
id.setProdId(request.getProdId());
ccRepo.deleteById(id);
return "Successfully removed the item from the Cart";
}
@Override
public String updateProduct(ProductReq request) {
CustomerCartID id = new CustomerCartID();
id.setEmail(request.getEmail());
id.setProdId(request.getProdId());
CustomerCart cc = new CustomerCart();
cc.setCheckOutStatus('N');
cc.setCustomerName(repo.findById(request.getEmail()).get().getName());
cc.setPrice(request.getPrice());
cc.setProdname(request.getProdname());
cc.setQuantity(request.getQuantity());
cc.setId(id);
ccRepo.save(cc);
return "Cart Item is updated";
}
@Override
public List<ProductReq> getCartdetails(String email) {
List<CustomerCart> list = ccRepo.findByIdEmailAndCheckOutStatus(email,'N');
List<ProductReq> res = new ArrayList<ProductReq>();
for (CustomerCart cc : list) {
ProductReq prod = new ProductReq();
prod.setEmail(email);
prod.setPrice(cc.getPrice());
prod.setProdId(cc.getId().getProdId());
prod.setProdname(cc.getProdname());
prod.setQuantity(cc.getQuantity());
res.add(prod);
}
return res;
}
@Override
public String doCheckOut(List<ProductReq> request) {
String email = request.get(0).getEmail();
List<Integer> prodIds = request.stream().map(i -> i.getProdId()).distinct().collect(Collectors.toList());
ccRepo.updateCheckOutstatus('Y', email,prodIds);
return null;
}
@Override
public List<ProductReq> getProduct(String prodName) {
List<Product> product = pRepo.getProduct(prodName);
List<ProductReq> res = new LinkedList<ProductReq>();
for (Product p : product) {
ProductReq prod = new ProductReq();
prod.setPrice(p.getPrice());
prod.setProdId(p.getProdId());
prod.setProdname(p.getProdname());
res.add(prod);
}
return res;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment