Untitled

 avatar
unknown
plain_text
a year ago
3.9 kB
2
Indexable
//Entity 

package com.example.demo.Entity;

import jakarta.persistence.*;
import lombok.*;

@Entity
@Data
@Table(name = "Inventory")
@NoArgsConstructor
public class Inventory {
    @Id
    @Getter
    @Setter
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int Productid;

    @Getter
    @Setter
    private String Productname;

    @Getter
    @Setter
    private int Price;

    @Getter
    @Setter
    private int Quantity;

    @Getter
    @Setter
    private String Createdby;

    @Getter
    @Setter
    @OneToOne
    @JoinColumn(name = "Category_category_Id")
    private Category categoryId;

    @Getter
    @Setter
    private Boolean IsDelete;

    @Getter
    @Setter
    private String ImageRefernce;

    public Inventory(int productid, String productname, int price, int quantity, String createdby, Category categoryId, Boolean isDelete, String imageRefernce) {
        Productid = productid;
        Productname = productname;
        Price = price;
        Quantity = quantity;
        Createdby = createdby;
        this.categoryId = categoryId;
        IsDelete = isDelete;
        ImageRefernce = imageRefernce;
    }
}

//Repository

package com.example.demo.Repository;

import com.example.demo.Entity.Inventory;
import lombok.NonNull;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface InventoryRepository extends JpaRepository<Inventory, Long> {

    Inventory findBy(int ProductId);
}

//service

package com.example.demo.Service;

import com.example.demo.Entity.Inventory;
import com.example.demo.Repository.InventoryRepository;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.List;
@org.springframework.stereotype.Service
public class Service {

    @Autowired
    private InventoryRepository InventoryRepo;

    public List<Inventory> GetProducts(){
        return InventoryRepo.findAll();
    }

    public Inventory UserQuantityUpdate(int ProductId , int Quantity) throws Exception{
        Inventory temp = InventoryRepo.findBy(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");
    }
}

//Controller

package com.example.demo.Controller;

import com.example.demo.DTO.CustomerDTO;
import com.example.demo.Entity.Inventory;
import com.example.demo.Service.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping
@CrossOrigin
public class CustomerController {

    @Autowired
    private Service service;

    @GetMapping(path = "/GETPRODUCTS")
    public List<Inventory> CustomerProducts(){
        return service.GetProducts();
    }

    @PostMapping(path = "/UserQunatityUpdate")
    public Inventory PurchaseQuantity(@RequestBody CustomerDTO Dto) throws Exception{
        try {
            int productID = Dto.getProductId();
            int Quantity = Dto.getQuantity();
            return service.UserQuantityUpdate(productID, Quantity);
        }
        catch (Exception error){
            throw new Exception(error);
        }
    }
}

//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 CustomerDTO {

    @Getter
    @Setter
    private int ProductId;
    @Getter
    @Setter
    private int Quantity;

    public CustomerDTO(int productId, int quantity) {
        ProductId = productId;
        Quantity = quantity;
    }
}

Leave a Comment