refactor: update controllers and mappers to use DTOs for brands and categories; add ID field to DTOs

This commit is contained in:
Vincent Guillet
2025-11-01 17:56:52 +01:00
parent 55a1d54c70
commit 2fe52830d8
9 changed files with 28 additions and 7 deletions

View File

@@ -25,8 +25,11 @@ public class BrandController {
} }
@GetMapping @GetMapping
public List<Brand> getAllBrands() { public List<BrandDTO> getAllBrands() {
return brandService.getAllBrands(); return brandService.getAllBrands()
.stream()
.map(brandMapper::toDto)
.toList();
} }
@GetMapping("/{id}") @GetMapping("/{id}")

View File

@@ -24,8 +24,11 @@ public class CategoryController {
} }
@GetMapping @GetMapping
public List<Category> getAllCategories() { public List<CategoryDTO> getAllCategories() {
return categoryService.getAllCategories(); return categoryService.getAllCategories()
.stream()
.map(categoryMapper::toDto)
.toList();
} }
@GetMapping("/{id}") @GetMapping("/{id}")

View File

@@ -10,6 +10,9 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor @NoArgsConstructor
public class BrandDTO { public class BrandDTO {
@JsonProperty("id")
private Long id;
@JsonProperty("name") @JsonProperty("name")
private String name; private String name;
} }

View File

@@ -10,6 +10,9 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor @NoArgsConstructor
public class CategoryDTO { public class CategoryDTO {
@JsonProperty("id")
private Long id;
@JsonProperty("name") @JsonProperty("name")
private String name; private String name;
} }

View File

@@ -10,6 +10,9 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor @NoArgsConstructor
public class PlatformDTO { public class PlatformDTO {
@JsonProperty("id")
private Long id;
@JsonProperty("name") @JsonProperty("name")
private String name; private String name;

View File

@@ -10,6 +10,7 @@ public class BrandMapper {
public Brand fromDto(BrandDTO brandDTO) { public Brand fromDto(BrandDTO brandDTO) {
if (brandDTO == null) return null; if (brandDTO == null) return null;
Brand brand = new Brand(); Brand brand = new Brand();
brand.setId(brandDTO.getId());
brand.setName(brandDTO.getName()); brand.setName(brandDTO.getName());
return brand; return brand;
} }
@@ -17,6 +18,7 @@ public class BrandMapper {
public BrandDTO toDto(Brand brand) { public BrandDTO toDto(Brand brand) {
if (brand == null) return null; if (brand == null) return null;
BrandDTO brandDTO = new BrandDTO(); BrandDTO brandDTO = new BrandDTO();
brandDTO.setId(brand.getId());
brandDTO.setName(brand.getName()); brandDTO.setName(brand.getName());
return brandDTO; return brandDTO;
} }

View File

@@ -10,6 +10,7 @@ public class CategoryMapper {
public Category fromDto(CategoryDTO categoryDTO) { public Category fromDto(CategoryDTO categoryDTO) {
if (categoryDTO == null) return null; if (categoryDTO == null) return null;
Category category = new Category(); Category category = new Category();
category.setId(categoryDTO.getId());
category.setName(categoryDTO.getName()); category.setName(categoryDTO.getName());
return category; return category;
} }
@@ -17,6 +18,7 @@ public class CategoryMapper {
public CategoryDTO toDto(Category category) { public CategoryDTO toDto(Category category) {
if (category == null) return null; if (category == null) return null;
CategoryDTO categoryDTO = new CategoryDTO(); CategoryDTO categoryDTO = new CategoryDTO();
categoryDTO.setId(category.getId());
categoryDTO.setName(category.getName()); categoryDTO.setName(category.getName());
return categoryDTO; return categoryDTO;
} }

View File

@@ -18,6 +18,7 @@ public class PlatformMapper {
public Platform fromDto(PlatformDTO platformDTO) { public Platform fromDto(PlatformDTO platformDTO) {
if (platformDTO == null) return null; if (platformDTO == null) return null;
Platform platform = new Platform(); Platform platform = new Platform();
platform.setId(platformDTO.getId());
platform.setName(platformDTO.getName()); platform.setName(platformDTO.getName());
if (platformDTO.getBrandDTO() != null && platformDTO.getBrandDTO().getName() != null) { if (platformDTO.getBrandDTO() != null && platformDTO.getBrandDTO().getName() != null) {
@@ -31,9 +32,10 @@ public class PlatformMapper {
public PlatformDTO toDto(Platform platform) { public PlatformDTO toDto(Platform platform) {
if (platform == null) return null; if (platform == null) return null;
PlatformDTO platformDTO = new PlatformDTO(); PlatformDTO platformDTO = new PlatformDTO();
platformDTO.setId(platform.getId());
platformDTO.setName(platform.getName()); platformDTO.setName(platform.getName());
if (platform.getBrand() != null) { if (platform.getBrand() != null) {
platformDTO.setBrandDTO(new BrandDTO(platform.getBrand().getName())); platformDTO.setBrandDTO(new BrandDTO(platform.getBrand().getId(), platform.getBrand().getName()));
} else { } else {
platformDTO.setBrandDTO(null); platformDTO.setBrandDTO(null);
} }

View File

@@ -90,7 +90,7 @@ public class ProductMapper {
productDTO.setManualIncluded(product.isManualIncluded()); productDTO.setManualIncluded(product.isManualIncluded());
if (product.getCategory() != null) { if (product.getCategory() != null) {
productDTO.setCategoryDTO(new CategoryDTO(product.getCategory().getName())); productDTO.setCategoryDTO(new CategoryDTO(product.getCategory().getId(), product.getCategory().getName()));
} else { } else {
productDTO.setCategoryDTO(null); productDTO.setCategoryDTO(null);
} }
@@ -101,7 +101,7 @@ public class ProductMapper {
platformDTO.setName(platform.getName()); platformDTO.setName(platform.getName());
if (platform.getBrand() != null) { if (platform.getBrand() != null) {
platformDTO.setBrandDTO(new BrandDTO(platform.getBrand().getName())); platformDTO.setBrandDTO(new BrandDTO(platform.getBrand().getId(), platform.getBrand().getName()));
} else { } else {
platformDTO.setBrandDTO(null); platformDTO.setBrandDTO(null);
} }