From e1dc998cffc00d267b71db08e34d17bc997e9553 Mon Sep 17 00:00:00 2001 From: deez nuts Date: Wed, 28 Aug 2024 10:31:41 +0200 Subject: [PATCH] update your mom --- Tests.http | 0 .../webshop/controller/SupplierController.java | 16 +++++++++++++++- .../de/szut/webshop/model/ArticleEntity.java | 8 ++++++-- .../de/szut/webshop/model/ContactEntity.java | 10 +++++++++- .../de/szut/webshop/model/SupplierEntity.java | 9 +++++---- .../de/szut/webshop/service/SupplierService.java | 13 ++----------- 6 files changed, 37 insertions(+), 19 deletions(-) create mode 100644 Tests.http diff --git a/Tests.http b/Tests.http new file mode 100644 index 0000000..e69de29 diff --git a/src/main/java/de/szut/webshop/controller/SupplierController.java b/src/main/java/de/szut/webshop/controller/SupplierController.java index c0a6585..952019d 100644 --- a/src/main/java/de/szut/webshop/controller/SupplierController.java +++ b/src/main/java/de/szut/webshop/controller/SupplierController.java @@ -1,6 +1,8 @@ package de.szut.webshop.controller; import de.szut.webshop.dto.AddSupplierDto; +import de.szut.webshop.model.ContactEntity; +import de.szut.webshop.model.SupplierEntity; import de.szut.webshop.service.SupplierService; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -18,7 +20,19 @@ public class SupplierController { @PostMapping public String createSupplier(@RequestBody AddSupplierDto dto) { - supplierService.createSupplier(dto); + SupplierEntity supplier = new SupplierEntity(); + ContactEntity contact = new ContactEntity(); + + contact.setCity(dto.getCity()); + contact.setPostcode(dto.getPostcode()); + contact.setStreet(dto.getStreet()); + contact.setPhone(dto.getPhone()); + + supplier.setName(dto.getName()); + supplier.setContact(contact); + + supplierService.createSupplier(supplier); + return "Supplier created"; } } diff --git a/src/main/java/de/szut/webshop/model/ArticleEntity.java b/src/main/java/de/szut/webshop/model/ArticleEntity.java index 0cea4cc..45fe9c7 100644 --- a/src/main/java/de/szut/webshop/model/ArticleEntity.java +++ b/src/main/java/de/szut/webshop/model/ArticleEntity.java @@ -1,6 +1,8 @@ package de.szut.webshop.model; import jakarta.persistence.*; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; @@ -15,14 +17,16 @@ import java.time.LocalDateTime; public class ArticleEntity { @Id - @GeneratedValue + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @NotBlank(message = "Designation is mandatory") private String designation; + @NotNull private double price; - @Column(name = "name", nullable = false) + @Column(name = "created_at", nullable = false) private LocalDateTime createdAt = LocalDateTime.now(); @Column(name = "updated_at", nullable = false) diff --git a/src/main/java/de/szut/webshop/model/ContactEntity.java b/src/main/java/de/szut/webshop/model/ContactEntity.java index b399585..c36161c 100644 --- a/src/main/java/de/szut/webshop/model/ContactEntity.java +++ b/src/main/java/de/szut/webshop/model/ContactEntity.java @@ -2,6 +2,8 @@ package de.szut.webshop.model; import jakarta.persistence.*; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; import lombok.Data; @Data @@ -12,15 +14,21 @@ public class ContactEntity { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long cid; + @NotBlank(message = "Street is mandatory") + @Size(max = 50, message = "Street must not exceed 50 characters") private String street; @Column(name="postcode") + @NotBlank(message = "Postcode is mandatory") + @Size(min = 5, max = 5, message = "Postcode must have 5 characters") private String postcode; + @NotBlank(message = "City is mandatory") + @Size(max = 50, message = "City must not exceed 50 characters") private String city; private String phone; - @OneToOne(fetch = FetchType.LAZY, mappedBy = "contact", cascade = CascadeType.ALL) + @OneToOne(mappedBy = "contact", cascade = CascadeType.ALL) private SupplierEntity supplier; } \ No newline at end of file diff --git a/src/main/java/de/szut/webshop/model/SupplierEntity.java b/src/main/java/de/szut/webshop/model/SupplierEntity.java index fc15eaf..649d143 100644 --- a/src/main/java/de/szut/webshop/model/SupplierEntity.java +++ b/src/main/java/de/szut/webshop/model/SupplierEntity.java @@ -1,6 +1,8 @@ package de.szut.webshop.model; import jakarta.persistence.*; +import jakarta.validation.constraints.NotNull; +import jakarta.validation.constraints.Size; import lombok.Data; import lombok.Getter; import lombok.Setter; @@ -14,14 +16,13 @@ import java.util.Set; @Table(name = "Supplier") public class SupplierEntity { @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @NotNull + @Size(max = 50, message = "Name must be less than 50 characters") private String name; - private String address; - - private String phone; - @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) private ContactEntity contact; diff --git a/src/main/java/de/szut/webshop/service/SupplierService.java b/src/main/java/de/szut/webshop/service/SupplierService.java index ab3f998..a719fa2 100644 --- a/src/main/java/de/szut/webshop/service/SupplierService.java +++ b/src/main/java/de/szut/webshop/service/SupplierService.java @@ -1,6 +1,5 @@ package de.szut.webshop.service; -import de.szut.webshop.dto.AddSupplierDto; import de.szut.webshop.model.SupplierEntity; import de.szut.webshop.repository.SupplierRepository; import org.springframework.stereotype.Service; @@ -18,22 +17,14 @@ public class SupplierService { return supplierRepository.findById(id).orElseThrow(); } - public void createSupplier(AddSupplierDto supplier) { - SupplierEntity supplierEntity = new SupplierEntity(); - - supplierEntity.setName(supplier.getName()); - supplierEntity.setAddress(supplier.getStreet()); - supplierEntity.setPhone(supplier.getPhone()); - - supplierRepository.save(supplierEntity); + public void createSupplier(SupplierEntity supplier) { + supplierRepository.save(supplier); } public SupplierEntity updateSupplier(Long id, SupplierEntity supplier) { SupplierEntity supplierToUpdate = supplierRepository.findById(id).orElseThrow(); supplierToUpdate.setName(supplier.getName()); - supplierToUpdate.setAddress(supplier.getAddress()); - supplierToUpdate.setPhone(supplier.getPhone()); return supplierRepository.save(supplierToUpdate); }