update your mom

This commit is contained in:
deez nuts 2024-08-28 10:31:41 +02:00
parent 46f9baab3e
commit e1dc998cff
6 changed files with 37 additions and 19 deletions

0
Tests.http Normal file

@ -1,6 +1,8 @@
package de.szut.webshop.controller; package de.szut.webshop.controller;
import de.szut.webshop.dto.AddSupplierDto; 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 de.szut.webshop.service.SupplierService;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
@ -18,7 +20,19 @@ public class SupplierController {
@PostMapping @PostMapping
public String createSupplier(@RequestBody AddSupplierDto dto) { 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"; return "Supplier created";
} }
} }

@ -1,6 +1,8 @@
package de.szut.webshop.model; package de.szut.webshop.model;
import jakarta.persistence.*; import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter; import lombok.Getter;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import lombok.Setter; import lombok.Setter;
@ -15,14 +17,16 @@ import java.time.LocalDateTime;
public class ArticleEntity { public class ArticleEntity {
@Id @Id
@GeneratedValue @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private Long id;
@NotBlank(message = "Designation is mandatory")
private String designation; private String designation;
@NotNull
private double price; private double price;
@Column(name = "name", nullable = false) @Column(name = "created_at", nullable = false)
private LocalDateTime createdAt = LocalDateTime.now(); private LocalDateTime createdAt = LocalDateTime.now();
@Column(name = "updated_at", nullable = false) @Column(name = "updated_at", nullable = false)

@ -2,6 +2,8 @@ package de.szut.webshop.model;
import jakarta.persistence.*; import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Data; import lombok.Data;
@Data @Data
@ -12,15 +14,21 @@ public class ContactEntity {
@GeneratedValue(strategy = GenerationType.IDENTITY) @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long cid; private Long cid;
@NotBlank(message = "Street is mandatory")
@Size(max = 50, message = "Street must not exceed 50 characters")
private String street; private String street;
@Column(name="postcode") @Column(name="postcode")
@NotBlank(message = "Postcode is mandatory")
@Size(min = 5, max = 5, message = "Postcode must have 5 characters")
private String postcode; private String postcode;
@NotBlank(message = "City is mandatory")
@Size(max = 50, message = "City must not exceed 50 characters")
private String city; private String city;
private String phone; private String phone;
@OneToOne(fetch = FetchType.LAZY, mappedBy = "contact", cascade = CascadeType.ALL) @OneToOne(mappedBy = "contact", cascade = CascadeType.ALL)
private SupplierEntity supplier; private SupplierEntity supplier;
} }

@ -1,6 +1,8 @@
package de.szut.webshop.model; package de.szut.webshop.model;
import jakarta.persistence.*; import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Data; import lombok.Data;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@ -14,14 +16,13 @@ import java.util.Set;
@Table(name = "Supplier") @Table(name = "Supplier")
public class SupplierEntity { public class SupplierEntity {
@Id @Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id; private Long id;
@NotNull
@Size(max = 50, message = "Name must be less than 50 characters")
private String name; private String name;
private String address;
private String phone;
@OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY) @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
private ContactEntity contact; private ContactEntity contact;

@ -1,6 +1,5 @@
package de.szut.webshop.service; package de.szut.webshop.service;
import de.szut.webshop.dto.AddSupplierDto;
import de.szut.webshop.model.SupplierEntity; import de.szut.webshop.model.SupplierEntity;
import de.szut.webshop.repository.SupplierRepository; import de.szut.webshop.repository.SupplierRepository;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -18,22 +17,14 @@ public class SupplierService {
return supplierRepository.findById(id).orElseThrow(); return supplierRepository.findById(id).orElseThrow();
} }
public void createSupplier(AddSupplierDto supplier) { public void createSupplier(SupplierEntity supplier) {
SupplierEntity supplierEntity = new SupplierEntity(); supplierRepository.save(supplier);
supplierEntity.setName(supplier.getName());
supplierEntity.setAddress(supplier.getStreet());
supplierEntity.setPhone(supplier.getPhone());
supplierRepository.save(supplierEntity);
} }
public SupplierEntity updateSupplier(Long id, SupplierEntity supplier) { public SupplierEntity updateSupplier(Long id, SupplierEntity supplier) {
SupplierEntity supplierToUpdate = supplierRepository.findById(id).orElseThrow(); SupplierEntity supplierToUpdate = supplierRepository.findById(id).orElseThrow();
supplierToUpdate.setName(supplier.getName()); supplierToUpdate.setName(supplier.getName());
supplierToUpdate.setAddress(supplier.getAddress());
supplierToUpdate.setPhone(supplier.getPhone());
return supplierRepository.save(supplierToUpdate); return supplierRepository.save(supplierToUpdate);
} }