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;
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";
}
}

@ -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)

@ -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;
}

@ -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;

@ -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);
}