From 2a12ded47678a9f64314fd57f2ac829d77ff9fbc Mon Sep 17 00:00:00 2001 From: Snoweuph Date: Wed, 28 Aug 2024 10:34:43 +0200 Subject: [PATCH] 11 --- .../java/de/szut/store/article/Article.java | 4 -- .../dto/GetAllArticlesBySupplierIdDTO.java | 12 +++++ .../szut/store/article/dto/GetArticleDTO.java | 11 +++++ .../java/de/szut/store/contact/Contact.java | 10 +--- .../de/szut/store/mapping/MappingService.java | 33 +++++++++++++ .../java/de/szut/store/supplier/Supplier.java | 1 - .../store/supplier/SupplierController.java | 48 +++++++++++++++++++ .../store/supplier/dto/AddSupplierDTO.java | 20 ++++++++ .../store/supplier/dto/GetSupplierDTO.java | 13 +++++ 9 files changed, 138 insertions(+), 14 deletions(-) create mode 100644 src/main/java/de/szut/store/article/dto/GetAllArticlesBySupplierIdDTO.java create mode 100644 src/main/java/de/szut/store/article/dto/GetArticleDTO.java create mode 100644 src/main/java/de/szut/store/mapping/MappingService.java create mode 100644 src/main/java/de/szut/store/supplier/dto/AddSupplierDTO.java create mode 100644 src/main/java/de/szut/store/supplier/dto/GetSupplierDTO.java diff --git a/src/main/java/de/szut/store/article/Article.java b/src/main/java/de/szut/store/article/Article.java index 277c2b2..2e4df57 100644 --- a/src/main/java/de/szut/store/article/Article.java +++ b/src/main/java/de/szut/store/article/Article.java @@ -19,11 +19,7 @@ public class Article { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - - @NotBlank private String destination; - - @NotBlank private Double price; @NotNull diff --git a/src/main/java/de/szut/store/article/dto/GetAllArticlesBySupplierIdDTO.java b/src/main/java/de/szut/store/article/dto/GetAllArticlesBySupplierIdDTO.java new file mode 100644 index 0000000..5c236c4 --- /dev/null +++ b/src/main/java/de/szut/store/article/dto/GetAllArticlesBySupplierIdDTO.java @@ -0,0 +1,12 @@ +package de.szut.store.article.dto; + +import lombok.Data; + +import java.util.Set; + +@Data +public class GetAllArticlesBySupplierIdDTO { + private Long supplierId; + private String name; + private Set articles; +} diff --git a/src/main/java/de/szut/store/article/dto/GetArticleDTO.java b/src/main/java/de/szut/store/article/dto/GetArticleDTO.java new file mode 100644 index 0000000..a2cf0db --- /dev/null +++ b/src/main/java/de/szut/store/article/dto/GetArticleDTO.java @@ -0,0 +1,11 @@ +package de.szut.store.article.dto; + +import jakarta.validation.constraints.NotBlank; +import lombok.Data; + +@Data +public class GetArticleDTO { + private Long id; + private String destination; + private String price; +} diff --git a/src/main/java/de/szut/store/contact/Contact.java b/src/main/java/de/szut/store/contact/Contact.java index 7798673..2919122 100644 --- a/src/main/java/de/szut/store/contact/Contact.java +++ b/src/main/java/de/szut/store/contact/Contact.java @@ -13,19 +13,11 @@ public class Contact { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; - - @NotBlank private String street; - - @NotBlank - @Size(min = 3, max = 7) private String zipcode; - - @NotBlank private String city; + private String phone; @OneToOne(mappedBy = "contact", cascade = CascadeType.ALL) private Supplier supplier; - - private String phone; } diff --git a/src/main/java/de/szut/store/mapping/MappingService.java b/src/main/java/de/szut/store/mapping/MappingService.java new file mode 100644 index 0000000..445682b --- /dev/null +++ b/src/main/java/de/szut/store/mapping/MappingService.java @@ -0,0 +1,33 @@ +package de.szut.store.mapping; + +import de.szut.store.contact.Contact; +import de.szut.store.supplier.Supplier; +import de.szut.store.supplier.dto.AddSupplierDTO; +import de.szut.store.supplier.dto.GetSupplierDTO; +import org.springframework.stereotype.Service; + +@Service +public class MappingService { + public Supplier mapAddSupplierDTOToSupplier(AddSupplierDTO dto) { + Supplier supplier = new Supplier(); + supplier.setName(dto.getName()); + Contact contact = new Contact(); + contact.setStreet(dto.getStreet()); + contact.setZipcode(dto.getZipcode()); + contact.setCity(dto.getCity()); + contact.setPhone(dto.getPhone()); + supplier.setContact(contact); + contact.setSupplier(supplier); + return supplier; + } + public GetSupplierDTO mapSupplierToGetSupplierDTO(Supplier supplier) { + GetSupplierDTO dto = new GetSupplierDTO(); + dto.setId(supplier.getId()); + dto.setName(supplier.getName()); + dto.setStreet(supplier.getContact().getStreet()); + dto.setZipcode(supplier.getContact().getZipcode()); + dto.setCity(supplier.getContact().getCity()); + dto.setPhone(supplier.getContact().getPhone()); + return dto; + } +} diff --git a/src/main/java/de/szut/store/supplier/Supplier.java b/src/main/java/de/szut/store/supplier/Supplier.java index d337bc5..845ab41 100644 --- a/src/main/java/de/szut/store/supplier/Supplier.java +++ b/src/main/java/de/szut/store/supplier/Supplier.java @@ -18,7 +18,6 @@ public class Supplier { private Long id; @NotBlank - @Size(min = 3, max = 50) private String name; @OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) diff --git a/src/main/java/de/szut/store/supplier/SupplierController.java b/src/main/java/de/szut/store/supplier/SupplierController.java index 7fc7f0f..27b3ac1 100644 --- a/src/main/java/de/szut/store/supplier/SupplierController.java +++ b/src/main/java/de/szut/store/supplier/SupplierController.java @@ -1,4 +1,52 @@ package de.szut.store.supplier; +import de.szut.store.mapping.MappingService; +import de.szut.store.supplier.dto.AddSupplierDTO; +import de.szut.store.supplier.dto.GetSupplierDTO; +import jakarta.validation.Valid; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; + +@RestController +@RequestMapping("/store/supplier") public class SupplierController { + @Autowired + private SupplierService supplierService; + @Autowired + private MappingService mappingService; + @Autowired + private ValidationAutoConfiguration validationAutoConfiguration; + + @PostMapping + public ResponseEntity createSupplier(@Valid @RequestBody final AddSupplierDTO dto){ + Supplier supplier = this.mappingService.mapAddSupplierDTOToSupplier(dto); + supplier = this.supplierService.create(supplier); + final GetSupplierDTO request = this.mappingService.mapSupplierToGetSupplierDTO(supplier); + return new ResponseEntity<>(request, HttpStatus.CREATED); + } + + @GetMapping + public ResponseEntity> getAllSuppliers(){ + List suppliers = this.supplierService.readAll(); + List dtos = new LinkedList<>(); + for (Supplier supplier : suppliers) { + dtos.add(this.mappingService.mapSupplierToGetSupplierDTO(supplier)); + } + return new ResponseEntity<>(dtos, HttpStatus.OK); + } + + @GetMapping("/{id}") + public ResponseEntity getSupplier(@PathVariable final Long id){ + final Supplier supplier = this.supplierService.readById(id); + final GetSupplierDTO dto = this.mappingService.mapSupplierToGetSupplierDTO(supplier); + return new ResponseEntity<>(dto, HttpStatus.OK); + } } diff --git a/src/main/java/de/szut/store/supplier/dto/AddSupplierDTO.java b/src/main/java/de/szut/store/supplier/dto/AddSupplierDTO.java new file mode 100644 index 0000000..ef92c72 --- /dev/null +++ b/src/main/java/de/szut/store/supplier/dto/AddSupplierDTO.java @@ -0,0 +1,20 @@ +package de.szut.store.supplier.dto; + +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; +import lombok.Data; + +@Data +public class AddSupplierDTO { + @NotBlank + @Size(min = 3, max = 50) + private String name; + @NotBlank + private String street; + @NotBlank + @Size(min = 5, max = 7) + private String zipcode; + @NotBlank + private String city; + private String phone; +} diff --git a/src/main/java/de/szut/store/supplier/dto/GetSupplierDTO.java b/src/main/java/de/szut/store/supplier/dto/GetSupplierDTO.java new file mode 100644 index 0000000..8092deb --- /dev/null +++ b/src/main/java/de/szut/store/supplier/dto/GetSupplierDTO.java @@ -0,0 +1,13 @@ +package de.szut.store.supplier.dto; + +import lombok.Data; + +@Data +public class GetSupplierDTO { + private Long id; + private String name; + private String street; + private String zipcode; + private String city; + private String phone; +}