DEV Community

Saravanan Lakshmanan
Saravanan Lakshmanan

Posted on

Spring Boot Learning Series - Creating APIs

ProductDAO.java

package com.company.sms.dao;

import java.util.List;

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

import com.company.sms.entity.Product;
import com.company.sms.repository.ProductRepo;

@Repository
public class ProductDAO {//CRUD Operations

    @Autowired
    ProductRepo repo;

    public void addProduct(Product product){
        repo.save(product);        
    }

    public List<Product> viewProduct(){
            List<Product> list = repo.findAll();
    return list;
    }

    public void updateProduct(Product product){
        repo.save(product);
    }

    public void deleteProduct(int id){
        repo.deleteById(id);
    }


}

Enter fullscreen mode Exit fullscreen mode

ProductService.java

package com.company.sms.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;

import com.company.sms.dao.ProductDAO;
import com.company.sms.entity.Product;
import com.company.sms.repository.ProductRepo;

@Service
public class ProductService {

    @Autowired
    ProductDAO dao;

//  @PostMapping
    public void addProduct(Product product){
        dao.addProduct(product);
    }

//  @GetMapping("/students")
    public List<Product> viewProduct(){
        return dao.viewProduct();
    }

//  @PutMapping
    public void updateProduct(Product product){
        dao.updateProduct(product);
    }

//    @DeleteMapping
    public void deleteProduct(int id){
        dao.deleteProduct(id);
    }

    public Product findTopPrice(){
        List<Product> arr = dao.viewProduct();
        Product topPrice = arr.get(0);

        for(Product product : arr){
            if(topPrice.getPrice() < product.getPrice()){
                 topPrice = product;
            }
        }
    return topPrice;
    }

    public ArrayList<Product> findByCategory(String category){
        List<Product> arr = dao.viewProduct();
        ArrayList<Product> arrCategory= new ArrayList();

        for(Product product : arr){
            if(product.getCategory().equals(category)){
                arrCategory.add(product);
            }
        }
    return arrCategory;
    }

    public HashSet<String> findUniqueCategory(){
        List<Product> arr = dao.viewProduct();
        //container to add only unique category to avoid duplication
        HashSet<String> set = new HashSet();

        for(Product product : arr){
            set.add(product.getCategory());
        }
    return set;
    }

    public HashSet<String> findUniqueDuplicateCategory(){

        List<Product> arr = dao.viewProduct();
        HashSet<String> set = new HashSet();

        for(Product product : arr){
            if(set.add(product.getCategory())){
                System.out.println("Unique Category: "+ product.getCategory());
            }else{
                System.out.println("Duplicate Category: "+ product.getCategory());
            }
        }
    return set;
    }

    public HashSet<String> findDuplicateUniqueCategory(){

        List<Product> arr = dao.viewProduct();
        HashSet<String> uniqueSet = new HashSet();
        HashSet<String> duplicateSet = new HashSet();

        for(Product product : arr){
            boolean unique = uniqueSet.add(product.getCategory());
            if(!unique){
                duplicateSet.add(product.getCategory());
            }
        }
    return duplicateSet;
    }

    public Product getById(int id){

        List<Product> arr = dao.viewProduct();
        HashMap<Integer, Product> map = new HashMap<>();

        for(Product product : arr){
            map.put(product.getId(), product);
        }
    return map.get(id);
    }

//    public Product getById(int id){
//        HashMap<Integer, Student> map = this.getMap();
//        return map.get(id);
//    }

    public HashMap<String, Integer> countByCategory(){
        List<Product> arr = dao.viewProduct();
        HashMap<String, Integer> map = new HashMap<>();

        for(Product product : arr){
            if(map.containsKey(product.getCategory())){
                map.put(product.getCategory(), map.get(product.getCategory()) + 1);
            }else{
                map.put(product.getCategory(),1);
            }
        }
    return map;

    }

    public HashMap<String, Integer> countCategory(){

        List<Product> arr = dao.viewProduct();
        HashMap<String, Integer> map = new HashMap<>();

        for(Product product : arr){
            map.put(product.getCategory(),map.getOrDefault(product.getCategory(),0) + 1);
        }
    return map;
    }


}

Enter fullscreen mode Exit fullscreen mode

ProductController.java

package com.company.sms.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.company.sms.entity.Product;
import com.company.sms.service.ProductService;

@RestController
@RequestMapping("/products")
public class ProductController {


@Autowired
ProductService service;

@PostMapping
public void addProduct(@RequestBody Product product) {
    service.addProduct(product);
}

@GetMapping
public List<Product> viewProduct() {
    return service.viewProduct();
}

@PutMapping
public void updateProduct(@RequestBody Product product) {
    service.updateProduct(product);
}

@DeleteMapping("/{id}")
public void deleteProduct(@PathVariable int id) {
    service.deleteProduct(id);
}
}

Enter fullscreen mode Exit fullscreen mode

Output:

[
    {
        "id": 1,
        "name": "Pant",
        "category": "Clothing",
        "price": 2000
    },
    {
        "id": 3,
        "name": "TV",
        "category": "Electronic",
        "price": 30000
    },
    {
        "id": 4,
        "name": "AC",
        "category": "ELectronic",
        "price": 50000
    },
    {
        "id": 5,
        "name": "Jeans",
        "category": "CLothing",
        "price": 2000
    }
]
Enter fullscreen mode Exit fullscreen mode

Top comments (0)