From eb2dce399600899390897d69554e6df47050591e Mon Sep 17 00:00:00 2001 From: "hamza.bouslama" <hamza.bouslama@marketingconfort.com> Date: Fri, 20 Dec 2024 04:56:21 +0100 Subject: [PATCH] API changes for unique / optinal columns - add isdefault category --- pom.xml | 4 +- .../config/SecurityConfiguration.java | 1 - .../mydressin/constants/Paths.java | 3 ++ .../controller/CategoryController.java | 30 +++++++++-- .../controller/ProductController.java | 3 -- .../mydressin/controller/TagController.java | 19 ++++--- .../controller/TermChoiceController.java | 49 +++++++++++++++--- .../controller/TermColorController.java | 48 +++++++++++++++--- .../controller/TermImageController.java | 50 ++++++++++++++++--- .../controller/TermLabelController.java | 48 +++++++++++++++--- .../mydressin/dto/CategoryDTO.java | 2 + .../mydressin/dto/TagDTO.java | 1 + .../mydressin/mappers/CategoryMapper.java | 5 +- .../mydressin/mappers/TagMapper.java | 7 ++- .../repositories/TermChoiceRepository.java | 12 +++++ .../repositories/TermColorRepository.java | 10 ++++ .../repositories/TermImageRepository.java | 13 +++++ .../repositories/TermLabelRepository.java | 13 +++++ .../mydressin/services/CategoryService.java | 4 ++ .../mydressin/services/TermChoiceService.java | 17 ++++++- .../mydressin/services/TermColorService.java | 17 ++++++- .../mydressin/services/TermImageService.java | 16 +++++- .../mydressin/services/TermLabelService.java | 17 ++++++- .../implementations/CategoryServiceImpl.java | 30 +++++++++++ .../TermChoiceServiceImpl.java | 29 ++++++++--- .../implementations/TermColorServiceImpl.java | 28 ++++++++--- .../implementations/TermImageServiceImpl.java | 27 +++++++--- .../implementations/TermLabelServiceImpl.java | 29 ++++++++--- 28 files changed, 451 insertions(+), 81 deletions(-) diff --git a/pom.xml b/pom.xml index 6d9618f..1f8518e 100644 --- a/pom.xml +++ b/pom.xml @@ -19,7 +19,7 @@ <dependency> <groupId>com.marketingconfort</groupId> <artifactId>mydressin-common</artifactId> - <version>1.0.152-RELEASE</version> + <version>1.0.156-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> @@ -50,4 +50,4 @@ </plugin> </plugins> </build> -</project> +</project> \ No newline at end of file diff --git a/src/main/java/com/marketingconfort/mydressin/config/SecurityConfiguration.java b/src/main/java/com/marketingconfort/mydressin/config/SecurityConfiguration.java index ff0fc45..9fcd8d6 100644 --- a/src/main/java/com/marketingconfort/mydressin/config/SecurityConfiguration.java +++ b/src/main/java/com/marketingconfort/mydressin/config/SecurityConfiguration.java @@ -28,5 +28,4 @@ public class SecurityConfiguration { return http.build(); } - } \ No newline at end of file diff --git a/src/main/java/com/marketingconfort/mydressin/constants/Paths.java b/src/main/java/com/marketingconfort/mydressin/constants/Paths.java index e0f355c..9db44ee 100644 --- a/src/main/java/com/marketingconfort/mydressin/constants/Paths.java +++ b/src/main/java/com/marketingconfort/mydressin/constants/Paths.java @@ -34,7 +34,10 @@ public class Paths { public static final String DELETE = "/delete/{id}"; public static final String DELETE_BULK = "/delete-bulk"; + public static final String DELETE_TERM_BY_ATTRIBUTE = "/{attribute_id}/delete/{term_id}"; + public static final String DELETE_TERM_BULK_BY_ATTRIBUTE = "/{attribute_id}/delete-bulk/{term_id}"; + public static final String DELETE_CATEGORY_AFTER_PRODUCT = "{category_id}/delete-category-after-product/{default_category_id}"; public static final String PRODUCT_DETAILS_BY_ID = "/details/product/{id}"; public static final String PRODUCT_DETAILS_BY_NAME_URI = "/details/name-uri/{nameUri}"; diff --git a/src/main/java/com/marketingconfort/mydressin/controller/CategoryController.java b/src/main/java/com/marketingconfort/mydressin/controller/CategoryController.java index 9c456a5..5329ba3 100644 --- a/src/main/java/com/marketingconfort/mydressin/controller/CategoryController.java +++ b/src/main/java/com/marketingconfort/mydressin/controller/CategoryController.java @@ -63,9 +63,9 @@ public class CategoryController { try { CategoryDTO savedCategory = categoryService.save(categoryDTO); return ResponseEntity.status(HttpStatus.CREATED).body(savedCategory); - } catch (DataIntegrityViolationException ex) { + }catch (DataIntegrityViolationException ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body("A category with this name already exists."); + .body("A category with this information already exists"); } catch (Exception ex) { // Attrape toutes les autres exceptions qui n'ont pas été gérées spécifiquement return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body("An unexpected error occurred: " + ex.getMessage()); @@ -77,12 +77,12 @@ public class CategoryController { try { CategoryDTO updatedCategory = categoryService.update(id, categoryDTO); return ResponseEntity.ok(updatedCategory); + }catch (DataIntegrityViolationException ex) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body("A category with this information already exists"); } catch (ResourceNotFoundException ex) { return ResponseEntity.status(HttpStatus.NOT_FOUND) .body("Category not found with ID: " + id); - } catch (DataIntegrityViolationException ex) { - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body("A category with this name already exists."); } catch (Exception ex) { // Attrape toutes les autres exceptions qui n'ont pas été gérées spécifiquement return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body("An unexpected error occurred: " + ex.getMessage()); @@ -116,6 +116,26 @@ public class CategoryController { .body("Impossible de supprimer les catégories. Veuillez d'abord supprimer les associations avec les produits existants."); } } + + @DeleteMapping(Paths.DELETE_CATEGORY_AFTER_PRODUCT) + public ResponseEntity<?> deleteCategoryAfterProduct( + @PathVariable("category_id") Long categoryId, + @PathVariable("default_category_id") Long defaultCategoryId) { + try { + categoryService.transferProductsAndDeleteCategory(categoryId, defaultCategoryId); + return ResponseEntity.ok().body("Category transferred and deleted successfully."); + } catch (ResourceNotFoundException ex) { + return ResponseEntity.status(HttpStatus.NOT_FOUND) + .body(ex.getMessage()); + } catch (DataIntegrityViolationException ex) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body("Data integrity violation: " + ex.getMessage()); + } catch (Exception ex) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("An unexpected error occurred: " + ex.getMessage()); + } + } + @GetMapping(Paths.CATEGORY_HIERARCHY) public List<CategoryDTO> getHierarchicalCategories() { return categoryService.findAllHierarchical(); diff --git a/src/main/java/com/marketingconfort/mydressin/controller/ProductController.java b/src/main/java/com/marketingconfort/mydressin/controller/ProductController.java index d5299d3..1838e32 100644 --- a/src/main/java/com/marketingconfort/mydressin/controller/ProductController.java +++ b/src/main/java/com/marketingconfort/mydressin/controller/ProductController.java @@ -61,9 +61,6 @@ public class ProductController { return ResponseEntity.ok(productDTO); } - - - ///////////////////////////////// //Get product from database @GetMapping(Paths.PRODUCT_DETAILS_BY_ID) diff --git a/src/main/java/com/marketingconfort/mydressin/controller/TagController.java b/src/main/java/com/marketingconfort/mydressin/controller/TagController.java index afe0ece..74f84a4 100644 --- a/src/main/java/com/marketingconfort/mydressin/controller/TagController.java +++ b/src/main/java/com/marketingconfort/mydressin/controller/TagController.java @@ -41,14 +41,12 @@ public class TagController { @PostMapping(Paths.TAGS_ADD) public ResponseEntity<?> createTag(@RequestBody TagDTO tagDTO) { - try { TagDTO createdTagDTO = tagService.saveTag(tagDTO); return ResponseEntity.status(HttpStatus.CREATED).body(createdTagDTO); - } catch (DataIntegrityViolationException ex) { return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body("A tag with this name already exists."); + .body("A tag with this information already exists"); } catch (Exception ex) { // Attrape toutes les autres exceptions qui n'ont pas été gérées spécifiquement return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body("An unexpected error occurred: " + ex.getMessage()); @@ -64,13 +62,14 @@ public class TagController { } else { return ResponseEntity.status(HttpStatus.NOT_FOUND).body("Tag not found with ID: " + id); } - } catch (DataIntegrityViolationException ex) { - return ResponseEntity.status(HttpStatus.BAD_REQUEST) - .body("A tag with this name already exists."); - } catch (Exception ex) { // Attrape toutes les autres exceptions qui n'ont pas été gérées spécifiquement - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) - .body("An unexpected error occurred: " + ex.getMessage()); - }} + } catch (DataIntegrityViolationException ex) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body("A tag with this information already exists"); + } catch (Exception ex) { // Attrape toutes les autres exceptions qui n'ont pas été gérées spécifiquement + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("An unexpected error occurred: " + ex.getMessage()); + } + } @DeleteMapping(Paths.DELETE) public ResponseEntity<?> deleteTag(@PathVariable Long id) { diff --git a/src/main/java/com/marketingconfort/mydressin/controller/TermChoiceController.java b/src/main/java/com/marketingconfort/mydressin/controller/TermChoiceController.java index e61d5d9..10c6c5c 100644 --- a/src/main/java/com/marketingconfort/mydressin/controller/TermChoiceController.java +++ b/src/main/java/com/marketingconfort/mydressin/controller/TermChoiceController.java @@ -2,6 +2,7 @@ package com.marketingconfort.mydressin.controller; import com.marketingconfort.mydressin.constants.Paths; import com.marketingconfort.mydressin.dto.TermChoiceDTO; +import com.marketingconfort.mydressin.exceptions.ResourceNotFoundException; import com.marketingconfort.mydressin.services.TermChoiceService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; @@ -63,13 +64,49 @@ public class TermChoiceController { } } - @DeleteMapping(Paths.DELETE) - public void deleteTermChoix(@PathVariable Long id) { - termChoiceService.deleteById(id); + /** + * Delete a single TermChoice by Attribute ID and TermChoice ID + */ + @DeleteMapping(Paths.DELETE_TERM_BY_ATTRIBUTE) + public ResponseEntity<?> deleteTermByAttribute( + @PathVariable("attribute_id") Long attributeId, + @PathVariable("term_id") Long termId) { + try { + termChoiceService.deleteTermByAttribute(attributeId, termId); + return ResponseEntity.noContent().build(); // 204 No Content + } catch (ResourceNotFoundException ex) { + return ResponseEntity.status(HttpStatus.NOT_FOUND) + .body(ex.getMessage()); + } catch (IllegalArgumentException ex) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(ex.getMessage()); + } catch (Exception ex) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("An unexpected error occurred: " + ex.getMessage()); + } } - @DeleteMapping(Paths.DELETE_BULK) - public void deleteTermChoices(@RequestBody List<Long> ids) { - termChoiceService.deleteByIds(ids); + /** + * Bulk Delete TermChoices by Attribute ID + * Expects a list of Term IDs in the request body + */ + @DeleteMapping(Paths.DELETE_TERM_BULK_BY_ATTRIBUTE) + public ResponseEntity<?> deleteTermsBulkByAttribute( + @PathVariable("attribute_id") Long attributeId, + @RequestBody List<Long> termIds) { + try { + termChoiceService.deleteTermsBulkByAttribute(attributeId, termIds); + return ResponseEntity.noContent().build(); // 204 No Content + } catch (ResourceNotFoundException ex) { + return ResponseEntity.status(HttpStatus.NOT_FOUND) + .body(ex.getMessage()); + } catch (IllegalArgumentException ex) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(ex.getMessage()); + } catch (Exception ex) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("An unexpected error occurred: " + ex.getMessage()); + } } + } diff --git a/src/main/java/com/marketingconfort/mydressin/controller/TermColorController.java b/src/main/java/com/marketingconfort/mydressin/controller/TermColorController.java index 64aad7a..67dfb6a 100644 --- a/src/main/java/com/marketingconfort/mydressin/controller/TermColorController.java +++ b/src/main/java/com/marketingconfort/mydressin/controller/TermColorController.java @@ -2,6 +2,7 @@ package com.marketingconfort.mydressin.controller; import com.marketingconfort.mydressin.constants.Paths; import com.marketingconfort.mydressin.dto.TermColorDTO; +import com.marketingconfort.mydressin.exceptions.ResourceNotFoundException; import com.marketingconfort.mydressin.services.TermColorService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; @@ -64,13 +65,48 @@ public class TermColorController { } - @DeleteMapping(Paths.DELETE) - public void deleteTermColor(@PathVariable Long id) { - termColorService.deleteById(id); + /** + * Delete a single TermChoice by Attribute ID and TermChoice ID + */ + @DeleteMapping(Paths.DELETE_TERM_BY_ATTRIBUTE) + public ResponseEntity<?> deleteTermByAttribute( + @PathVariable("attribute_id") Long attributeId, + @PathVariable("term_id") Long termId) { + try { + termColorService.deleteTermByAttribute(attributeId, termId); + return ResponseEntity.noContent().build(); // 204 No Content + } catch (ResourceNotFoundException ex) { + return ResponseEntity.status(HttpStatus.NOT_FOUND) + .body(ex.getMessage()); + } catch (IllegalArgumentException ex) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(ex.getMessage()); + } catch (Exception ex) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("An unexpected error occurred: " + ex.getMessage()); + } } - @DeleteMapping(Paths.DELETE_BULK) - public void deleteTermColors(@RequestBody List<Long> ids) { - termColorService.deleteByIds(ids); + /** + * Bulk Delete TermChoices by Attribute ID + * Expects a list of Term IDs in the request body + */ + @DeleteMapping(Paths.DELETE_TERM_BULK_BY_ATTRIBUTE) + public ResponseEntity<?> deleteTermsBulkByAttribute( + @PathVariable("attribute_id") Long attributeId, + @RequestBody List<Long> termIds) { + try { + termColorService.deleteTermsBulkByAttribute(attributeId, termIds); + return ResponseEntity.noContent().build(); // 204 No Content + } catch (ResourceNotFoundException ex) { + return ResponseEntity.status(HttpStatus.NOT_FOUND) + .body(ex.getMessage()); + } catch (IllegalArgumentException ex) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(ex.getMessage()); + } catch (Exception ex) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("An unexpected error occurred: " + ex.getMessage()); + } } } diff --git a/src/main/java/com/marketingconfort/mydressin/controller/TermImageController.java b/src/main/java/com/marketingconfort/mydressin/controller/TermImageController.java index b570874..64255f0 100644 --- a/src/main/java/com/marketingconfort/mydressin/controller/TermImageController.java +++ b/src/main/java/com/marketingconfort/mydressin/controller/TermImageController.java @@ -4,6 +4,7 @@ import com.marketingconfort.mydressin.constants.Paths; import com.marketingconfort.mydressin.dto.TermChoiceDTO; import com.marketingconfort.mydressin.dto.TermImageDTO; +import com.marketingconfort.mydressin.exceptions.ResourceNotFoundException; import com.marketingconfort.mydressin.services.TermImageService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; @@ -60,14 +61,49 @@ public class TermImageController { .body("An unexpected error occurred: " + ex.getMessage()); }} - - @DeleteMapping(Paths.DELETE) - public void deleteTermImage(@PathVariable Long id) { - termImageService.deleteById(id); + /** + * Delete a single TermChoice by Attribute ID and TermChoice ID + */ + @DeleteMapping(Paths.DELETE_TERM_BY_ATTRIBUTE) + public ResponseEntity<?> deleteTermByAttribute( + @PathVariable("attribute_id") Long attributeId, + @PathVariable("term_id") Long termId) { + try { + termImageService.deleteTermByAttribute(attributeId, termId); + return ResponseEntity.noContent().build(); // 204 No Content + } catch (ResourceNotFoundException ex) { + return ResponseEntity.status(HttpStatus.NOT_FOUND) + .body(ex.getMessage()); + } catch (IllegalArgumentException ex) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(ex.getMessage()); + } catch (Exception ex) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("An unexpected error occurred: " + ex.getMessage()); + } } - @DeleteMapping(Paths.DELETE_BULK) - public void deleteTermImages(@RequestBody List<Long> ids) { - termImageService.deleteByIds(ids); + /** + * Bulk Delete TermChoices by Attribute ID + * Expects a list of Term IDs in the request body + */ + @DeleteMapping(Paths.DELETE_TERM_BULK_BY_ATTRIBUTE) + public ResponseEntity<?> deleteTermsBulkByAttribute( + @PathVariable("attribute_id") Long attributeId, + @RequestBody List<Long> termIds) { + try { + termImageService.deleteTermsBulkByAttribute(attributeId, termIds); + return ResponseEntity.noContent().build(); // 204 No Content + } catch (ResourceNotFoundException ex) { + return ResponseEntity.status(HttpStatus.NOT_FOUND) + .body(ex.getMessage()); + } catch (IllegalArgumentException ex) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(ex.getMessage()); + } catch (Exception ex) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("An unexpected error occurred: " + ex.getMessage()); + } } + } diff --git a/src/main/java/com/marketingconfort/mydressin/controller/TermLabelController.java b/src/main/java/com/marketingconfort/mydressin/controller/TermLabelController.java index 90f5d48..e2530a7 100644 --- a/src/main/java/com/marketingconfort/mydressin/controller/TermLabelController.java +++ b/src/main/java/com/marketingconfort/mydressin/controller/TermLabelController.java @@ -2,6 +2,7 @@ package com.marketingconfort.mydressin.controller; import com.marketingconfort.mydressin.constants.Paths; import com.marketingconfort.mydressin.dto.TermLabelDTO; +import com.marketingconfort.mydressin.exceptions.ResourceNotFoundException; import com.marketingconfort.mydressin.services.TermLabelService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataIntegrityViolationException; @@ -62,13 +63,48 @@ public class TermLabelController { } } - @DeleteMapping(Paths.DELETE) - public void deleteTermLabel(@PathVariable Long id) { - termLabelService.deleteById(id); + /** + * Delete a single TermChoice by Attribute ID and TermChoice ID + */ + @DeleteMapping(Paths.DELETE_TERM_BY_ATTRIBUTE) + public ResponseEntity<?> deleteTermByAttribute( + @PathVariable("attribute_id") Long attributeId, + @PathVariable("term_id") Long termId) { + try { + termLabelService.deleteTermByAttribute(attributeId, termId); + return ResponseEntity.noContent().build(); // 204 No Content + } catch (ResourceNotFoundException ex) { + return ResponseEntity.status(HttpStatus.NOT_FOUND) + .body(ex.getMessage()); + } catch (IllegalArgumentException ex) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(ex.getMessage()); + } catch (Exception ex) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("An unexpected error occurred: " + ex.getMessage()); + } } - @DeleteMapping(Paths.DELETE_BULK) - public void deleteTermLabels(@RequestBody List<Long> ids) { - termLabelService.deleteByIds(ids); + /** + * Bulk Delete TermChoices by Attribute ID + * Expects a list of Term IDs in the request body + */ + @DeleteMapping(Paths.DELETE_TERM_BULK_BY_ATTRIBUTE) + public ResponseEntity<?> deleteTermsBulkByAttribute( + @PathVariable("attribute_id") Long attributeId, + @RequestBody List<Long> termIds) { + try { + termLabelService.deleteTermsBulkByAttribute(attributeId, termIds); + return ResponseEntity.noContent().build(); // 204 No Content + } catch (ResourceNotFoundException ex) { + return ResponseEntity.status(HttpStatus.NOT_FOUND) + .body(ex.getMessage()); + } catch (IllegalArgumentException ex) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(ex.getMessage()); + } catch (Exception ex) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body("An unexpected error occurred: " + ex.getMessage()); + } } } diff --git a/src/main/java/com/marketingconfort/mydressin/dto/CategoryDTO.java b/src/main/java/com/marketingconfort/mydressin/dto/CategoryDTO.java index 709eec1..3f3ba60 100644 --- a/src/main/java/com/marketingconfort/mydressin/dto/CategoryDTO.java +++ b/src/main/java/com/marketingconfort/mydressin/dto/CategoryDTO.java @@ -24,6 +24,7 @@ public class CategoryDTO { private Long parentId; private String typeAffichage; // Ajout du type d'affichage private List<CategoryDTO> children; + private boolean isdefaultcategory; public static CategoryDTO fromEntity(Category category) { if (category == null) { @@ -35,6 +36,7 @@ public class CategoryDTO { .slug(category.getSlug()) .description(category.getDescription()) .image(category.getImage()) + .isdefaultcategory(category.isIsdefaultcategory()) .build(); } } \ No newline at end of file diff --git a/src/main/java/com/marketingconfort/mydressin/dto/TagDTO.java b/src/main/java/com/marketingconfort/mydressin/dto/TagDTO.java index 2eec9ee..d501924 100644 --- a/src/main/java/com/marketingconfort/mydressin/dto/TagDTO.java +++ b/src/main/java/com/marketingconfort/mydressin/dto/TagDTO.java @@ -14,4 +14,5 @@ public class TagDTO { private String slug; private String description; private Long total; // Nombre total de produits associés à ce tag + private boolean isdefaulttags; } diff --git a/src/main/java/com/marketingconfort/mydressin/mappers/CategoryMapper.java b/src/main/java/com/marketingconfort/mydressin/mappers/CategoryMapper.java index 2f6f8e4..6f3fb2f 100644 --- a/src/main/java/com/marketingconfort/mydressin/mappers/CategoryMapper.java +++ b/src/main/java/com/marketingconfort/mydressin/mappers/CategoryMapper.java @@ -31,6 +31,7 @@ public class CategoryMapper { category.setSlug(categoryDTO.getSlug() == null || categoryDTO.getSlug().isEmpty() ? slugify(categoryDTO.getNom()) : slugify(categoryDTO.getSlug())); category.setDescription(categoryDTO.getDescription()); category.setImage(categoryDTO.getImage()); + category.setIsdefaultcategory(categoryDTO.isIsdefaultcategory()); // Conversion de la chaîne en énumération if (categoryDTO.getTypeAffichage() != null) { @@ -62,15 +63,17 @@ public class CategoryMapper { .image(category.getImage()) .total(total) .typeAffichage(category.getTypeAffichage() != null ? category.getTypeAffichage().name() : null) + .isdefaultcategory(category.isIsdefaultcategory()) .parentId(category.getParent() != null ? category.getParent().getId() : null) .build(); } - + public static void updateCategoryFromDTO(Category category, CategoryDTO categoryDTO) { category.setNom(categoryDTO.getNom()); category.setSlug(categoryDTO.getSlug() == null || categoryDTO.getSlug().isEmpty() ? slugify(categoryDTO.getNom()) : slugify(categoryDTO.getSlug())); category.setDescription(categoryDTO.getDescription()); category.setImage(categoryDTO.getImage()); + category.setIsdefaultcategory(categoryDTO.isIsdefaultcategory()); category.setTypeAffichage(TypeAffichage.valueOf(categoryDTO.getTypeAffichage())); if (categoryDTO.getParentId() != null) { Category parentCategory = new Category(); diff --git a/src/main/java/com/marketingconfort/mydressin/mappers/TagMapper.java b/src/main/java/com/marketingconfort/mydressin/mappers/TagMapper.java index 198a558..b6f6f51 100644 --- a/src/main/java/com/marketingconfort/mydressin/mappers/TagMapper.java +++ b/src/main/java/com/marketingconfort/mydressin/mappers/TagMapper.java @@ -22,6 +22,7 @@ public class TagMapper { .slug(tag.getSlug()) .description(tag.getDescription()) .total(total) + .isdefaulttags(tag.isIsdefaulttags()) .build(); } @@ -40,6 +41,7 @@ public class TagMapper { tag.setNom(tagDTO.getNom()); tag.setSlug(slug); tag.setDescription(tagDTO.getDescription()); + tag.setIsdefaulttags(tagDTO.isIsdefaulttags()); return tag; } public void updateTagFromMap(Tag tag, Map<String, Object> updates) { @@ -58,7 +60,8 @@ public class TagMapper { if (updates.containsKey("description") && updates.get("description") != null) { tag.setDescription(updates.get("description").toString()); } + if (updates.containsKey("isdefaulttags") && updates.get("isdefaulttags") != null) { + tag.setIsdefaulttags(Boolean.parseBoolean(updates.get("isdefaulttags").toString())); + } } - - } diff --git a/src/main/java/com/marketingconfort/mydressin/repositories/TermChoiceRepository.java b/src/main/java/com/marketingconfort/mydressin/repositories/TermChoiceRepository.java index 86ea1b6..fb55190 100644 --- a/src/main/java/com/marketingconfort/mydressin/repositories/TermChoiceRepository.java +++ b/src/main/java/com/marketingconfort/mydressin/repositories/TermChoiceRepository.java @@ -2,9 +2,13 @@ package com.marketingconfort.mydressin.repositories; import com.marketingconfort.mydressin.common.stockmanagement.models.Attribute; import com.marketingconfort.mydressin.common.stockmanagement.models.TermChoix; +import com.marketingconfort.mydressin.common.stockmanagement.models.TermColor; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; import java.util.List; @@ -21,4 +25,12 @@ public interface TermChoiceRepository extends JpaRepository<TermChoix, Long> { List<TermChoix> findByAttributeOrderById(Attribute attribute); List<TermChoix> findByAttribute_Id(Long attributeId); long countByAttribute_Id(Long attributeId); + + @Query("SELECT tc FROM TermChoix tc WHERE tc.id IN :termIds AND tc.attribute.id = :attributeId") + List<TermChoix> findByIdInAndAttributeId(@Param("termIds") List<Long> termIds, @Param("attributeId") Long attributeId); + + @Modifying + @Transactional + @Query("DELETE FROM TermChoix tc WHERE tc.id = :termId AND tc.attribute.id = :attributeId") + void deleteByTermIdAndAttributeId(@Param("termId") Long termId, @Param("attributeId") Long attributeId); } diff --git a/src/main/java/com/marketingconfort/mydressin/repositories/TermColorRepository.java b/src/main/java/com/marketingconfort/mydressin/repositories/TermColorRepository.java index 86cc710..201e4ed 100644 --- a/src/main/java/com/marketingconfort/mydressin/repositories/TermColorRepository.java +++ b/src/main/java/com/marketingconfort/mydressin/repositories/TermColorRepository.java @@ -2,9 +2,12 @@ package com.marketingconfort.mydressin.repositories; import com.marketingconfort.mydressin.common.stockmanagement.models.TermColor; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import com.marketingconfort.mydressin.common.stockmanagement.models.Attribute; +import org.springframework.transaction.annotation.Transactional; import java.util.List; @@ -21,4 +24,11 @@ public interface TermColorRepository extends JpaRepository<TermColor, Long> { List<TermColor> findByAttribute_Id(Long attributeId); long countByAttribute_Id(Long attributeId); + @Query("SELECT tc FROM TermColor tc WHERE tc.id IN :termIds AND tc.attribute.id = :attributeId") + List<TermColor> findByIdInAndAttributeId(@Param("termIds") List<Long> termIds, @Param("attributeId") Long attributeId); + + @Modifying + @Transactional + @Query("DELETE FROM TermColor tc WHERE tc.id = :termId AND tc.attribute.id = :attributeId") + void deleteByTermIdAndAttributeId(@Param("termId") Long termId, @Param("attributeId") Long attributeId); } diff --git a/src/main/java/com/marketingconfort/mydressin/repositories/TermImageRepository.java b/src/main/java/com/marketingconfort/mydressin/repositories/TermImageRepository.java index 4939684..d7eb44f 100644 --- a/src/main/java/com/marketingconfort/mydressin/repositories/TermImageRepository.java +++ b/src/main/java/com/marketingconfort/mydressin/repositories/TermImageRepository.java @@ -1,10 +1,15 @@ package com.marketingconfort.mydressin.repositories; import com.marketingconfort.mydressin.common.stockmanagement.models.Attribute; +import com.marketingconfort.mydressin.common.stockmanagement.models.TermChoix; +import com.marketingconfort.mydressin.common.stockmanagement.models.TermColor; import com.marketingconfort.mydressin.common.stockmanagement.models.TermImage; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; import java.util.List; @@ -19,4 +24,12 @@ public interface TermImageRepository extends JpaRepository<TermImage, Long> { List<TermImage> findByAttributeOrderById(Attribute attribute); List<TermImage> findByAttribute_Id(Long attributeId); long countByAttribute_Id(Long attributeId); + + @Query("SELECT tc FROM TermImage tc WHERE tc.id IN :termIds AND tc.attribute.id = :attributeId") + List<TermImage> findByIdInAndAttributeId(@Param("termIds") List<Long> termIds, @Param("attributeId") Long attributeId); + + @Modifying + @Transactional + @Query("DELETE FROM TermImage tc WHERE tc.id = :termId AND tc.attribute.id = :attributeId") + void deleteByTermIdAndAttributeId(@Param("termId") Long termId, @Param("attributeId") Long attributeId); } diff --git a/src/main/java/com/marketingconfort/mydressin/repositories/TermLabelRepository.java b/src/main/java/com/marketingconfort/mydressin/repositories/TermLabelRepository.java index ef4da04..2cf1979 100644 --- a/src/main/java/com/marketingconfort/mydressin/repositories/TermLabelRepository.java +++ b/src/main/java/com/marketingconfort/mydressin/repositories/TermLabelRepository.java @@ -1,10 +1,15 @@ package com.marketingconfort.mydressin.repositories; import com.marketingconfort.mydressin.common.stockmanagement.models.Attribute; +import com.marketingconfort.mydressin.common.stockmanagement.models.TermChoix; +import com.marketingconfort.mydressin.common.stockmanagement.models.TermImage; import com.marketingconfort.mydressin.common.stockmanagement.models.TermLabel; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; import java.util.List; @@ -19,4 +24,12 @@ public interface TermLabelRepository extends JpaRepository<TermLabel, Long> { List<TermLabel> findByAttributeOrderById(Attribute attribute); List<TermLabel> findByAttribute_Id(Long attributeId); long countByAttribute_Id(Long attributeId); + + @Query("SELECT tc FROM TermLabel tc WHERE tc.id IN :termIds AND tc.attribute.id = :attributeId") + List<TermLabel> findByIdInAndAttributeId(@Param("termIds") List<Long> termIds, @Param("attributeId") Long attributeId); + + @Modifying + @Transactional + @Query("DELETE FROM TermLabel tc WHERE tc.id = :termId AND tc.attribute.id = :attributeId") + void deleteByTermIdAndAttributeId(@Param("termId") Long termId, @Param("attributeId") Long attributeId); } diff --git a/src/main/java/com/marketingconfort/mydressin/services/CategoryService.java b/src/main/java/com/marketingconfort/mydressin/services/CategoryService.java index f49db2f..22acdd3 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/CategoryService.java +++ b/src/main/java/com/marketingconfort/mydressin/services/CategoryService.java @@ -2,6 +2,8 @@ package com.marketingconfort.mydressin.services; import com.marketingconfort.mydressin.dto.CategoryDTO; import com.marketingconfort.mydressin.dto.CategoryWithSubcategoriesDto; +import com.marketingconfort.mydressin.exceptions.ResourceNotFoundException; +import org.springframework.dao.DataIntegrityViolationException; import org.springframework.web.multipart.MultipartFile; @@ -16,6 +18,8 @@ public interface CategoryService { CategoryDTO save(CategoryDTO categoryDTO); void deleteById(Long id); void deleteByIds(List<Long> ids); + void transferProductsAndDeleteCategory(Long sourceCategoryId, Long defaultCategoryId) + throws ResourceNotFoundException, DataIntegrityViolationException; List<CategoryDTO> findAllHierarchical(); Map<String, Boolean> importCategories(MultipartFile file) throws Exception ; List<CategoryWithSubcategoriesDto> getAllCategoriesWithSubcategories(); diff --git a/src/main/java/com/marketingconfort/mydressin/services/TermChoiceService.java b/src/main/java/com/marketingconfort/mydressin/services/TermChoiceService.java index 169425d..52d5ff4 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/TermChoiceService.java +++ b/src/main/java/com/marketingconfort/mydressin/services/TermChoiceService.java @@ -9,6 +9,19 @@ public interface TermChoiceService { List<TermChoiceDTO> findByAttribute(Long attributeId); TermChoiceDTO findById(Long id); TermChoiceDTO save(TermChoiceDTO termChoiceDTO); - void deleteById(Long id); - void deleteByIds(List<Long> ids); + /** + * Deletes a single TermChoice associated with an Attribute. + * + * @param attributeId The ID of the Attribute. + * @param termId The ID of the TermChoice to delete. + */ + void deleteTermByAttribute(Long attributeId, Long termId); + + /** + * Bulk deletes TermChoices associated with an Attribute. + * + * @param attributeId The ID of the Attribute. + * @param termIds The list of TermChoice IDs to delete. + */ + void deleteTermsBulkByAttribute(Long attributeId, List<Long> termIds); } diff --git a/src/main/java/com/marketingconfort/mydressin/services/TermColorService.java b/src/main/java/com/marketingconfort/mydressin/services/TermColorService.java index 9acd698..66fad22 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/TermColorService.java +++ b/src/main/java/com/marketingconfort/mydressin/services/TermColorService.java @@ -8,6 +8,19 @@ public interface TermColorService { List<TermColorDTO> findAllByAttributeId(Long attributeId); TermColorDTO findById(Long id); TermColorDTO save(TermColorDTO termColorDTO); - void deleteById(Long id); - void deleteByIds(List<Long> ids); + /** + * Deletes a single TermChoice associated with an Attribute. + * + * @param attributeId The ID of the Attribute. + * @param termId The ID of the TermChoice to delete. + */ + void deleteTermByAttribute(Long attributeId, Long termId); + + /** + * Bulk deletes TermChoices associated with an Attribute. + * + * @param attributeId The ID of the Attribute. + * @param termIds The list of TermChoice IDs to delete. + */ + void deleteTermsBulkByAttribute(Long attributeId, List<Long> termIds); } diff --git a/src/main/java/com/marketingconfort/mydressin/services/TermImageService.java b/src/main/java/com/marketingconfort/mydressin/services/TermImageService.java index e330ec5..a1b1856 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/TermImageService.java +++ b/src/main/java/com/marketingconfort/mydressin/services/TermImageService.java @@ -8,7 +8,19 @@ public interface TermImageService { List<TermImageDTO> findByAttribute(Long attributeId); TermImageDTO findById(Long id); TermImageDTO save(TermImageDTO termImageDTO); - void deleteById(Long id); - void deleteByIds(List<Long> ids); + /** + * Deletes a single TermChoice associated with an Attribute. + * + * @param attributeId The ID of the Attribute. + * @param termId The ID of the TermChoice to delete. + */ + void deleteTermByAttribute(Long attributeId, Long termId); + /** + * Bulk deletes TermChoices associated with an Attribute. + * + * @param attributeId The ID of the Attribute. + * @param termIds The list of TermChoice IDs to delete. + */ + void deleteTermsBulkByAttribute(Long attributeId, List<Long> termIds); } diff --git a/src/main/java/com/marketingconfort/mydressin/services/TermLabelService.java b/src/main/java/com/marketingconfort/mydressin/services/TermLabelService.java index 68c94b6..88ab540 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/TermLabelService.java +++ b/src/main/java/com/marketingconfort/mydressin/services/TermLabelService.java @@ -10,6 +10,19 @@ public interface TermLabelService { TermLabelDTO findById(Long id); TermLabelDTO save(TermLabelDTO termLabelDTO); - void deleteById(Long id); - void deleteByIds(List<Long> ids); + /** + * Deletes a single TermChoice associated with an Attribute. + * + * @param attributeId The ID of the Attribute. + * @param termId The ID of the TermChoice to delete. + */ + void deleteTermByAttribute(Long attributeId, Long termId); + + /** + * Bulk deletes TermChoices associated with an Attribute. + * + * @param attributeId The ID of the Attribute. + * @param termIds The list of TermChoice IDs to delete. + */ + void deleteTermsBulkByAttribute(Long attributeId, List<Long> termIds); } diff --git a/src/main/java/com/marketingconfort/mydressin/services/implementations/CategoryServiceImpl.java b/src/main/java/com/marketingconfort/mydressin/services/implementations/CategoryServiceImpl.java index af6a3bf..8a8cd0b 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/implementations/CategoryServiceImpl.java +++ b/src/main/java/com/marketingconfort/mydressin/services/implementations/CategoryServiceImpl.java @@ -2,12 +2,14 @@ package com.marketingconfort.mydressin.services.implementations; import com.marketingconfort.mydressin.common.stockmanagement.enumurations.TypeAffichage; import com.marketingconfort.mydressin.common.stockmanagement.models.Category; +import com.marketingconfort.mydressin.common.stockmanagement.models.Product; import com.marketingconfort.mydressin.dto.CategoryDTO; import com.marketingconfort.mydressin.dto.CategoryWithSubcategoriesDto; import com.marketingconfort.mydressin.dto.ImportCategoryRequestDTO; import com.marketingconfort.mydressin.exceptions.ResourceNotFoundException; import com.marketingconfort.mydressin.mappers.CategoryMapper; import com.marketingconfort.mydressin.repositories.CategoryRepository; +import com.marketingconfort.mydressin.repositories.ProductRepository; import com.marketingconfort.mydressin.services.CategoryService; import com.opencsv.CSVReader; @@ -27,6 +29,7 @@ import java.util.stream.Collectors; public class CategoryServiceImpl implements CategoryService { private final CategoryRepository categoryRepository; + private final ProductRepository productRepository; // Ensure this repository exists @Override public List<CategoryDTO> findAll() { @@ -99,6 +102,33 @@ public class CategoryServiceImpl implements CategoryService { return CategoryMapper.fromEntity(savedCategory, total); } + @Override + @Transactional + public void transferProductsAndDeleteCategory(Long sourceCategoryId, Long defaultCategoryId) + throws ResourceNotFoundException, DataIntegrityViolationException { + Category sourceCategory = categoryRepository.findById(sourceCategoryId) + .orElseThrow(() -> new ResourceNotFoundException("Source category not found with ID: " + sourceCategoryId)); + + Category defaultCategory = categoryRepository.findById(defaultCategoryId) + .orElseThrow(() -> new ResourceNotFoundException("Default category not found with ID: " + defaultCategoryId)); + Set<Product> productsToTransfer = sourceCategory.getProducts(); + + for (Product product : productsToTransfer) { + product.getCategory().remove(sourceCategory); + product.getCategory().add(defaultCategory); + productRepository.save(product); + } + + sourceCategory.getProducts().clear(); + categoryRepository.save(sourceCategory); + + try { + categoryRepository.delete(sourceCategory); + } catch (DataIntegrityViolationException ex) { + throw new DataIntegrityViolationException("Cannot delete category with ID: " + sourceCategoryId + ". It may be associated with existing products.", ex); + } + } + @Override public void deleteById(Long id) { try { diff --git a/src/main/java/com/marketingconfort/mydressin/services/implementations/TermChoiceServiceImpl.java b/src/main/java/com/marketingconfort/mydressin/services/implementations/TermChoiceServiceImpl.java index dd1e14b..1ed9a74 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/implementations/TermChoiceServiceImpl.java +++ b/src/main/java/com/marketingconfort/mydressin/services/implementations/TermChoiceServiceImpl.java @@ -12,6 +12,7 @@ import com.marketingconfort.mydressin.repositories.TermChoiceRepository; import com.marketingconfort.mydressin.services.TermChoiceService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.stream.Collectors; @@ -73,15 +74,31 @@ public class TermChoiceServiceImpl implements TermChoiceService { } @Override - public void deleteById(Long id) { - if (!termChoiceRepository.existsById(id)) { - throw new ResourceNotFoundException("TermChoix not found with id: " + id); + public void deleteTermByAttribute(Long attributeId, Long termId) { + // Validate existence of Attribute and TermColor + if (!attributeRepository.existsById(attributeId)) { + throw new ResourceNotFoundException("Attribute not found with id: " + attributeId); } - termChoiceRepository.deleteById(id); + if (!termChoiceRepository.existsById(termId)) { + throw new ResourceNotFoundException("TermColor not found with id: " + termId); + } + + // Perform the delete operation + termChoiceRepository.deleteByTermIdAndAttributeId(termId, attributeId); } @Override - public void deleteByIds(List<Long> ids) { - termChoiceRepository.deleteAllById(ids); + @Transactional + public void deleteTermsBulkByAttribute(Long attributeId, List<Long> termIds) { + Attribute attribute = attributeRepository.findById(attributeId) + .orElseThrow(() -> new ResourceNotFoundException("Attribute not found with id: " + attributeId)); + + List<TermChoix> termsToDelete = termChoiceRepository.findByIdInAndAttributeId(termIds, attributeId); + if (termsToDelete.size() != termIds.size()) { + throw new IllegalArgumentException("Some TermChoices do not belong to Attribute with id " + attributeId); + } + termChoiceRepository.deleteAll(termsToDelete); } + + } diff --git a/src/main/java/com/marketingconfort/mydressin/services/implementations/TermColorServiceImpl.java b/src/main/java/com/marketingconfort/mydressin/services/implementations/TermColorServiceImpl.java index 1da23f8..17316c8 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/implementations/TermColorServiceImpl.java +++ b/src/main/java/com/marketingconfort/mydressin/services/implementations/TermColorServiceImpl.java @@ -1,6 +1,7 @@ package com.marketingconfort.mydressin.services.implementations; import com.marketingconfort.mydressin.common.stockmanagement.enumurations.TypeAttribute; +import com.marketingconfort.mydressin.common.stockmanagement.models.TermChoix; import com.marketingconfort.mydressin.exceptions.ResourceNotFoundException; import com.marketingconfort.mydressin.exceptions.InvalidAttributeTypeException; import com.marketingconfort.mydressin.mappers.TermColorMapper; @@ -14,6 +15,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.stream.Collectors; @@ -77,15 +79,29 @@ public class TermColorServiceImpl implements TermColorService { } @Override - public void deleteById(Long id) { - if (!termColorRepository.existsById(id)) { - throw new ResourceNotFoundException("TermColor not found with id: " + id); + public void deleteTermByAttribute(Long attributeId, Long termId) { + // Validate existence of Attribute and TermColor + if (!attributeRepository.existsById(attributeId)) { + throw new ResourceNotFoundException("Attribute not found with id: " + attributeId); } - termColorRepository.deleteById(id); + if (!termColorRepository.existsById(termId)) { + throw new ResourceNotFoundException("TermColor not found with id: " + termId); + } + + // Perform the delete operation + termColorRepository.deleteByTermIdAndAttributeId(termId, attributeId); } @Override - public void deleteByIds(List<Long> ids) { - termColorRepository.deleteAllById(ids); + @Transactional + public void deleteTermsBulkByAttribute(Long attributeId, List<Long> termIds) { + Attribute attribute = attributeRepository.findById(attributeId) + .orElseThrow(() -> new ResourceNotFoundException("Attribute not found with id: " + attributeId)); + + List<TermColor> termsToDelete = termColorRepository.findByIdInAndAttributeId(termIds, attributeId); + if (termsToDelete.size() != termIds.size()) { + throw new IllegalArgumentException("Some termColor do not belong to Attribute with id " + attributeId); + } + termColorRepository.deleteAll(termsToDelete); } } diff --git a/src/main/java/com/marketingconfort/mydressin/services/implementations/TermImageServiceImpl.java b/src/main/java/com/marketingconfort/mydressin/services/implementations/TermImageServiceImpl.java index c863ae6..84d9848 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/implementations/TermImageServiceImpl.java +++ b/src/main/java/com/marketingconfort/mydressin/services/implementations/TermImageServiceImpl.java @@ -2,6 +2,7 @@ package com.marketingconfort.mydressin.services.implementations; import com.marketingconfort.mydressin.common.stockmanagement.enumurations.TypeAttribute; import com.marketingconfort.mydressin.common.stockmanagement.models.Attribute; +import com.marketingconfort.mydressin.common.stockmanagement.models.TermChoix; import com.marketingconfort.mydressin.common.stockmanagement.models.TermImage; import com.marketingconfort.mydressin.dto.TermImageDTO; import com.marketingconfort.mydressin.exceptions.InvalidAttributeTypeException; @@ -12,6 +13,7 @@ import com.marketingconfort.mydressin.repositories.TermImageRepository; import com.marketingconfort.mydressin.services.TermImageService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.stream.Collectors; @@ -65,17 +67,30 @@ public class TermImageServiceImpl implements TermImageService { } @Override - public void deleteById(Long id) { - if(!termImageRepository.existsById(id)){ - throw new ResourceNotFoundException("TermImage not found with: "+ id); + public void deleteTermByAttribute(Long attributeId, Long termId) { + // Validate existence of Attribute and TermColor + if (!attributeRepository.existsById(attributeId)) { + throw new ResourceNotFoundException("Attribute not found with id: " + attributeId); + } + if (!termImageRepository.existsById(termId)) { + throw new ResourceNotFoundException("TermColor not found with id: " + termId); } - termImageRepository.deleteById(id); + // Perform the delete operation + termImageRepository.deleteByTermIdAndAttributeId(termId, attributeId); } + @Override - public void deleteByIds(List<Long> ids) { - termImageRepository.deleteAllById(ids); + @Transactional + public void deleteTermsBulkByAttribute(Long attributeId, List<Long> termIds) { + Attribute attribute = attributeRepository.findById(attributeId) + .orElseThrow(() -> new ResourceNotFoundException("Attribute not found with id: " + attributeId)); + List<TermImage> termsToDelete = termImageRepository.findByIdInAndAttributeId(termIds, attributeId); + if (termsToDelete.size() != termIds.size()) { + throw new IllegalArgumentException("Some TermImages do not belong to Attribute with id " + attributeId); + } + termImageRepository.deleteAll(termsToDelete); } } diff --git a/src/main/java/com/marketingconfort/mydressin/services/implementations/TermLabelServiceImpl.java b/src/main/java/com/marketingconfort/mydressin/services/implementations/TermLabelServiceImpl.java index 7e6301d..fd14dde 100644 --- a/src/main/java/com/marketingconfort/mydressin/services/implementations/TermLabelServiceImpl.java +++ b/src/main/java/com/marketingconfort/mydressin/services/implementations/TermLabelServiceImpl.java @@ -2,6 +2,7 @@ package com.marketingconfort.mydressin.services.implementations; import com.marketingconfort.mydressin.common.stockmanagement.enumurations.TypeAttribute; import com.marketingconfort.mydressin.common.stockmanagement.models.Attribute; +import com.marketingconfort.mydressin.common.stockmanagement.models.TermChoix; import com.marketingconfort.mydressin.common.stockmanagement.models.TermLabel; import com.marketingconfort.mydressin.dto.TermLabelDTO; import com.marketingconfort.mydressin.exceptions.InvalidAttributeTypeException; @@ -12,6 +13,7 @@ import com.marketingconfort.mydressin.repositories.TermLabelRepository; import com.marketingconfort.mydressin.services.TermLabelService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; import java.util.stream.Collectors; @@ -69,15 +71,30 @@ public class TermLabelServiceImpl implements TermLabelService { } @Override - public void deleteById(Long id) { - if (!termLabelRepository.existsById(id)) { - throw new ResourceNotFoundException("TermLabel not found with id: " + id); + public void deleteTermByAttribute(Long attributeId, Long termId) { + // Validate existence of Attribute and TermColor + if (!attributeRepository.existsById(attributeId)) { + throw new ResourceNotFoundException("Attribute not found with id: " + attributeId); } - termLabelRepository.deleteById(id); + if (!termLabelRepository.existsById(termId)) { + throw new ResourceNotFoundException("TermColor not found with id: " + termId); + } + + // Perform the delete operation + termLabelRepository.deleteByTermIdAndAttributeId(termId, attributeId); } + @Override - public void deleteByIds(List<Long> ids) { - termLabelRepository.deleteAllById(ids); + @Transactional + public void deleteTermsBulkByAttribute(Long attributeId, List<Long> termIds) { + Attribute attribute = attributeRepository.findById(attributeId) + .orElseThrow(() -> new ResourceNotFoundException("Attribute not found with id: " + attributeId)); + + List<TermLabel> termsToDelete = termLabelRepository.findByIdInAndAttributeId(termIds, attributeId); + if (termsToDelete.size() != termIds.size()) { + throw new IllegalArgumentException("Some termLabel do not belong to Attribute with id " + attributeId); + } + termLabelRepository.deleteAll(termsToDelete); } } -- GitLab