This commit is contained in:
Snoweuph 2024-08-28 10:34:43 +02:00
parent 46f9ef70d1
commit 2a12ded476
Signed by: Snoweuph
GPG key ID: A494330694B208EF
9 changed files with 138 additions and 14 deletions

View file

@ -19,11 +19,7 @@ public class Article {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank
private String destination;
@NotBlank
private Double price;
@NotNull

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -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<GetSupplierDTO> 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<List<GetSupplierDTO>> getAllSuppliers(){
List<Supplier> suppliers = this.supplierService.readAll();
List<GetSupplierDTO> dtos = new LinkedList<>();
for (Supplier supplier : suppliers) {
dtos.add(this.mappingService.mapSupplierToGetSupplierDTO(supplier));
}
return new ResponseEntity<>(dtos, HttpStatus.OK);
}
@GetMapping("/{id}")
public ResponseEntity<GetSupplierDTO> 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);
}
}

View file

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

View file

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