Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package br.ufc.mandacaru5.service;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Optional;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import br.ufc.mandacaru5.dto.ProductDTO;
- import br.ufc.mandacaru5.model.Product;
- import br.ufc.mandacaru5.repository.ProductRepository;
- @Service
- public class ProductService {
- @Autowired
- ProductRepository productRepository;
- public void save(int id, ProductDTO entity) {
- Product product = new Product();
- if (id != 0) {
- product = findProduct(id);
- }
- product.setName(entity.getName());
- product.setPrice(entity.getPrice());
- productRepository.save(product);
- }
- public void delete(int id) {
- Product product = findProduct(id);
- productRepository.delete(product);
- }
- public ProductDTO find(int id) {
- if (id < 1) {
- return null;
- }
- Optional<Product> product = productRepository.findById(id);
- if (product.isPresent()) {
- ProductDTO dto = new ProductDTO();
- dto.setName(product.get().getName());
- dto.setPrice(product.get().getPrice());
- return dto;
- }
- return null;
- }
- private Product findProduct(int id) {
- if (id < 1) {
- return null;
- }
- Optional<Product> product = productRepository.findById(id);
- if (product.isPresent()) {
- return product.get();
- }
- return null;
- }
- public List<ProductDTO> findAll() {
- List<Product> list = productRepository.findAll();
- List<ProductDTO> listDTO = new ArrayList<ProductDTO>();
- for (Product product : list) {
- ProductDTO dto = new ProductDTO();
- dto.setName(product.getName());
- dto.setPrice(product.getPrice());
- listDTO.add(dto);
- }
- return listDTO;
- }
- public ProductDTO findByName(String str) {
- if (str.length() < 3) {
- return null;
- }
- Product product = productRepository.findFirstByName(str);
- if (product != null) {
- ProductDTO dto = new ProductDTO();
- dto.setName(product.getName());
- dto.setPrice(product.getPrice());
- return dto;
- }
- return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement