Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

import com.rms.domain.core.ProductCategory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface ProductCategoryRepository extends JpaRepository<ProductCategory, Integer> {
@Query(value = "select pc from ProductCategory pc where pc.name= :name" )
Optional<ProductCategory> findByName(@Param("name")String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
import com.rms.domain.investor.Transaction;
import com.rms.rest.dto.ProductCategoryDto;
import com.rms.rest.dto.common.PaginatedResultDto;
import com.rms.rest.exception.ErrorCodes;
import com.rms.rest.exception.ResourceNotFoundException;
import com.rms.rest.exception.ResourceRelatedException;
import com.rms.rest.exception.Response;
import com.rms.rest.exception.*;
import com.rms.rest.modelmapper.ProductCategoryMapper;
import com.rms.rest.modelmapper.common.PaginationMapper;
import com.rms.service.ProductCategoryService;
Expand All @@ -18,6 +15,7 @@
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

@Component
@AllArgsConstructor
Expand All @@ -43,16 +41,31 @@ public ResponseEntity<ProductCategoryDto> getById(Integer id) {
}

public ResponseEntity<ProductCategoryDto> save(ProductCategoryDto productCategoryDto) {
Optional<ProductCategory>productCategoryexist=productCategoryService.findByName(productCategoryDto.getName());

if(productCategoryexist.isPresent())
{

throw new ResourceAlreadyExistsException(ProductCategory.class.getSimpleName(),
"product category name",productCategoryDto.getName(),ErrorCodes.DUPLICATE_RESOURCE.getCode());
}
ProductCategory productCategory = mapper.toEntity(productCategoryDto);
productCategoryService.save(productCategory);
ProductCategoryDto dto = mapper.toDto(productCategory);
return ResponseEntity.ok(dto);
}

public ResponseEntity<ProductCategoryDto> update(ProductCategoryDto productCategoryDto, Integer id) {
Optional<ProductCategory>productCategoryexist=productCategoryService.findByName(productCategoryDto.getName());
if(productCategoryexist.isPresent()&&!productCategoryexist.get().getId().equals(id))
{
throw new ResourceAlreadyExistsException(ProductCategory.class.getSimpleName(),
"product category name",productCategoryDto.getName(),ErrorCodes.DUPLICATE_RESOURCE.getCode());
}
ProductCategory productCategory = productCategoryService.getById(id)
.orElseThrow(() -> new ResourceNotFoundException(ProductCategory.class.getSimpleName(), id));
mapper.updateEntityFromDto(productCategoryDto, productCategory);
productCategory.setId(id);
productCategoryService.save(productCategory);
ProductCategoryDto dto = mapper.toDto(productCategory);
return ResponseEntity.ok(dto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public Optional<ProductCategory> getById(Integer id) {
return productCategoryRepository.findById(id);
}

public Optional<ProductCategory>findByName(String name)
{
return productCategoryRepository.findByName(name);
}

public ProductCategory save(ProductCategory category) {
return productCategoryRepository.save(category);
}
Expand Down