refactor: update controllers and mappers to use DTOs for brands and categories; add ID field to DTOs
This commit is contained in:
@@ -25,8 +25,11 @@ public class BrandController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<Brand> getAllBrands() {
|
||||
return brandService.getAllBrands();
|
||||
public List<BrandDTO> getAllBrands() {
|
||||
return brandService.getAllBrands()
|
||||
.stream()
|
||||
.map(brandMapper::toDto)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
|
||||
@@ -24,8 +24,11 @@ public class CategoryController {
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<Category> getAllCategories() {
|
||||
return categoryService.getAllCategories();
|
||||
public List<CategoryDTO> getAllCategories() {
|
||||
return categoryService.getAllCategories()
|
||||
.stream()
|
||||
.map(categoryMapper::toDto)
|
||||
.toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
|
||||
@@ -10,6 +10,9 @@ import lombok.NoArgsConstructor;
|
||||
@NoArgsConstructor
|
||||
public class BrandDTO {
|
||||
|
||||
@JsonProperty("id")
|
||||
private Long id;
|
||||
|
||||
@JsonProperty("name")
|
||||
private String name;
|
||||
}
|
||||
|
||||
@@ -10,6 +10,9 @@ import lombok.NoArgsConstructor;
|
||||
@NoArgsConstructor
|
||||
public class CategoryDTO {
|
||||
|
||||
@JsonProperty("id")
|
||||
private Long id;
|
||||
|
||||
@JsonProperty("name")
|
||||
private String name;
|
||||
}
|
||||
|
||||
@@ -10,6 +10,9 @@ import lombok.NoArgsConstructor;
|
||||
@NoArgsConstructor
|
||||
public class PlatformDTO {
|
||||
|
||||
@JsonProperty("id")
|
||||
private Long id;
|
||||
|
||||
@JsonProperty("name")
|
||||
private String name;
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@ public class BrandMapper {
|
||||
public Brand fromDto(BrandDTO brandDTO) {
|
||||
if (brandDTO == null) return null;
|
||||
Brand brand = new Brand();
|
||||
brand.setId(brandDTO.getId());
|
||||
brand.setName(brandDTO.getName());
|
||||
return brand;
|
||||
}
|
||||
@@ -17,6 +18,7 @@ public class BrandMapper {
|
||||
public BrandDTO toDto(Brand brand) {
|
||||
if (brand == null) return null;
|
||||
BrandDTO brandDTO = new BrandDTO();
|
||||
brandDTO.setId(brand.getId());
|
||||
brandDTO.setName(brand.getName());
|
||||
return brandDTO;
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ public class CategoryMapper {
|
||||
public Category fromDto(CategoryDTO categoryDTO) {
|
||||
if (categoryDTO == null) return null;
|
||||
Category category = new Category();
|
||||
category.setId(categoryDTO.getId());
|
||||
category.setName(categoryDTO.getName());
|
||||
return category;
|
||||
}
|
||||
@@ -17,6 +18,7 @@ public class CategoryMapper {
|
||||
public CategoryDTO toDto(Category category) {
|
||||
if (category == null) return null;
|
||||
CategoryDTO categoryDTO = new CategoryDTO();
|
||||
categoryDTO.setId(category.getId());
|
||||
categoryDTO.setName(category.getName());
|
||||
return categoryDTO;
|
||||
}
|
||||
|
||||
@@ -18,6 +18,7 @@ public class PlatformMapper {
|
||||
public Platform fromDto(PlatformDTO platformDTO) {
|
||||
if (platformDTO == null) return null;
|
||||
Platform platform = new Platform();
|
||||
platform.setId(platformDTO.getId());
|
||||
platform.setName(platformDTO.getName());
|
||||
|
||||
if (platformDTO.getBrandDTO() != null && platformDTO.getBrandDTO().getName() != null) {
|
||||
@@ -31,9 +32,10 @@ public class PlatformMapper {
|
||||
public PlatformDTO toDto(Platform platform) {
|
||||
if (platform == null) return null;
|
||||
PlatformDTO platformDTO = new PlatformDTO();
|
||||
platformDTO.setId(platform.getId());
|
||||
platformDTO.setName(platform.getName());
|
||||
if (platform.getBrand() != null) {
|
||||
platformDTO.setBrandDTO(new BrandDTO(platform.getBrand().getName()));
|
||||
platformDTO.setBrandDTO(new BrandDTO(platform.getBrand().getId(), platform.getBrand().getName()));
|
||||
} else {
|
||||
platformDTO.setBrandDTO(null);
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ public class ProductMapper {
|
||||
productDTO.setManualIncluded(product.isManualIncluded());
|
||||
|
||||
if (product.getCategory() != null) {
|
||||
productDTO.setCategoryDTO(new CategoryDTO(product.getCategory().getName()));
|
||||
productDTO.setCategoryDTO(new CategoryDTO(product.getCategory().getId(), product.getCategory().getName()));
|
||||
} else {
|
||||
productDTO.setCategoryDTO(null);
|
||||
}
|
||||
@@ -101,7 +101,7 @@ public class ProductMapper {
|
||||
platformDTO.setName(platform.getName());
|
||||
|
||||
if (platform.getBrand() != null) {
|
||||
platformDTO.setBrandDTO(new BrandDTO(platform.getBrand().getName()));
|
||||
platformDTO.setBrandDTO(new BrandDTO(platform.getBrand().getId(), platform.getBrand().getName()));
|
||||
} else {
|
||||
platformDTO.setBrandDTO(null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user