Skip to content

Commit

Permalink
Complete crud task
Browse files Browse the repository at this point in the history
  • Loading branch information
JackKaif committed Feb 19, 2024
1 parent e6d3da4 commit b65cb91
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 2 deletions.
2 changes: 1 addition & 1 deletion java-spring-ru/crud/settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
rootProject.name = "one-to-many"
rootProject.name = "crud"
// spring.jpa.generate-ddl = true
// spring.jpa.hibernate.ddl-auto
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package exercise.controller;

import java.util.List;
import java.util.Set;

import exercise.dto.ProductCreateDTO;
import exercise.dto.ProductDTO;
import exercise.dto.ProductUpdateDTO;
import exercise.mapper.ProductMapper;
import exercise.repository.CategoryRepository;
import jakarta.validation.ConstraintViolationException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.DeleteMapping;
Expand All @@ -28,10 +31,63 @@ public class ProductsController {
@Autowired
private ProductRepository productRepository;

@Autowired
private CategoryRepository categoryRepository;

@Autowired
private ProductMapper productMapper;

// BEGIN

@GetMapping("")
public List<ProductDTO> index() {
return productRepository.findAll().stream()
.map(productMapper::map)
.toList();
}

@GetMapping("/{id}")
public ProductDTO show(@PathVariable Long id) {
var product = productRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Product with id " + id + " not found"));
return productMapper.map(product);
}

@PostMapping("")
@ResponseStatus(HttpStatus.CREATED)
public ProductDTO create(@RequestBody ProductCreateDTO newProduct) {
var product = productMapper.map(newProduct);
if (product.getCategory() == null) {
throw new ConstraintViolationException(Set.of());
}
product.getCategory().getProducts().add(product);
productRepository.save(product);
return productMapper.map(product);
}

@PutMapping("/{id}")
public ProductDTO update(@PathVariable Long id,
@RequestBody ProductUpdateDTO editedProduct) {
var product = productRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Product with id " + id + " not found"));
product.getCategory().getProducts().remove(product);
productMapper.update(editedProduct, product);
var category = categoryRepository.findById(editedProduct.getCategoryId().get())
.orElseThrow(() -> {
throw new ConstraintViolationException(Set.of());
});
product.setCategory(category);
category.getProducts().add(product);
productRepository.save(product);
return productMapper.map(product);
}

@DeleteMapping("/{id}")
@ResponseStatus(HttpStatus.NO_CONTENT)
public void delete(@PathVariable Long id) {
var product = productRepository.findById(id)
.orElseThrow(() -> new ResourceNotFoundException("Product with id " + id + " not found"));
product.getCategory().getProducts().remove(product);
productRepository.delete(product);
}
// END
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,15 @@
import org.mapstruct.ReportingPolicy;

// BEGIN
@Mapper(
uses = { JsonNullableMapper.class, ReferenceMapper.class},
nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE,
componentModel = MappingConstants.ComponentModel.SPRING,
unmappedTargetPolicy = ReportingPolicy.IGNORE
)
public abstract class CategoryMapper {
public abstract Category map(CategoryCreateDTO dto);

public abstract CategoryDTO map (Category model);
}
// END
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,21 @@
import org.mapstruct.ReportingPolicy;

// BEGIN
@Mapper(
uses = { JsonNullableMapper.class, ReferenceMapper.class},
nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE,
componentModel = MappingConstants.ComponentModel.SPRING,
unmappedTargetPolicy = ReportingPolicy.IGNORE
)
public abstract class ProductMapper {
@Mapping(target = "category", source = "categoryId")
public abstract Product map(ProductCreateDTO dto);

@Mapping(target = "categoryName", source = "category.name")
@Mapping(target = "categoryId", source = "category.id")
public abstract ProductDTO map(Product model);

public abstract void update(ProductUpdateDTO dto,
@MappingTarget Product model);
}
// END
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,15 @@
import jakarta.persistence.EntityManager;

// BEGIN
@Mapper(
componentModel = MappingConstants.ComponentModel.SPRING
)
public abstract class ReferenceMapper {
@Autowired
private EntityManager entityManager;

public <T extends BaseEntity> T toEntity(Long id, @TargetType Class<T> entityClass) {
return id != null ? entityManager.find(entityClass, id) : null;
}
}
// END

0 comments on commit b65cb91

Please sign in to comment.