Untitled
unknown
plain_text
2 years ago
4.7 kB
11
Indexable
service
package com.example.demo.Service;
import com.example.demo.DTO.AdminDTO;
import com.example.demo.Entity.Inventory;
import com.example.demo.Repository.InventoryRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class AdminServiceimpl {
@Autowired
private InventoryRepository InventoryRepo;
public List<Inventory> GetProductsAdmin(){
return InventoryRepo.findByIsDelete(true);
}
public Inventory AdminQuantityUpdate(int ProductId , int Quantity) throws Exception{
Inventory temp = InventoryRepo.findByProductid(ProductId);
if (temp != null){
int OldQuantity = temp.getQuantity();
int newQuantity = OldQuantity + Quantity;
temp.setQuantity(newQuantity);
return InventoryRepo.save(temp);
}
throw new Exception("ProductId Doesnot Exist");
}
public Inventory UpdateRecord(Inventory inventory) throws Exception {
if (inventory == null ) {
throw new Exception("InventoryRecord or Id must not be null");
}
Optional<Inventory> optionalinventory = Optional.ofNullable(InventoryRepo.findByProductid(inventory.getProductid()));
if (optionalinventory.isEmpty()) {
throw new Exception("Product with Id : " + inventory.getProductid() + "Does not exist");
}
Inventory newInventoryRecord = optionalinventory.get();
newInventoryRecord.setProductid(inventory.getProductid());
newInventoryRecord.setProductname(inventory.getProductname());
newInventoryRecord.setPrice(inventory.getPrice());
newInventoryRecord.setQuantity(inventory.getQuantity());
newInventoryRecord.setCreatedby("pavan");
newInventoryRecord.setIsDelete(true);
return InventoryRepo.save(newInventoryRecord);
}
public Inventory Addnew(AdminDTO adminDTO) {
Inventory inventory = new Inventory();
inventory.setQuantity(adminDTO.getQuantity());
inventory.setProductname(adminDTO.getProductname());
inventory.setPrice(adminDTO.getPrice());
inventory.setCategoryId(adminDTO.getCategoryId());
inventory.setCreatedby("pavan");
inventory.setIsDelete(true);
return InventoryRepo.save(inventory);
}
}
dto
package com.example.demo.DTO;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import lombok.Getter;
import lombok.Setter;
@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class AdminDTO {
@Getter
@Setter
private String Productname;
@Getter
@Setter
private int price;
@Getter
@Setter
private int Quantity;
@Getter
@Setter
private int CategoryId;
public AdminDTO(String productname, int price, int quantity, int categoryId) {
Productname = productname;
this.price = price;
Quantity = quantity;
CategoryId = categoryId;
}
}
Controller
package com.example.demo.Controller;
import com.example.demo.DTO.AdminDTO;
import com.example.demo.DTO.CustomerDTO;
import com.example.demo.Entity.Inventory;
import com.example.demo.Service.AdminServiceimpl;
import com.example.demo.Service.CustomerServiceimpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@CrossOrigin
@RequestMapping
public class AdminController {
@Autowired
private AdminServiceimpl service;
@GetMapping(path = "/GETPRODUCTSAdmin")
public List<Inventory> AdminProducts(){
return service.GetProductsAdmin();
}
@PostMapping(path = "/AdminQunatityUpdate")
public Inventory AdminQuantity(@RequestBody CustomerDTO Dto) throws Exception{
try {
int productID = Dto.getProductId();
int Quantity = Dto.getQuantity();
return service.AdminQuantityUpdate(productID, Quantity);
}
catch (Exception error){
throw new Exception(error);
}
}
@PostMapping(path = "/Updateproduct")
public Inventory update(@RequestBody Inventory inventory) throws Exception{
try{
return service.UpdateRecord(inventory);
}
catch (Exception error){
throw new Exception(error);
}
}
@PostMapping(path = "/AddnewProduct")
public Inventory Addnew(@RequestBody AdminDTO inventory) throws Exception{
try{
return service.Addnew(inventory);
}
catch (Exception error){
throw new Exception(error);
}
}
}
Editor is loading...
Leave a Comment