11
This commit is contained in:
parent
46f9ef70d1
commit
2a12ded476
9 changed files with 138 additions and 14 deletions
|
@ -19,11 +19,7 @@ public class Article {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@NotBlank
|
|
||||||
private String destination;
|
private String destination;
|
||||||
|
|
||||||
@NotBlank
|
|
||||||
private Double price;
|
private Double price;
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
11
src/main/java/de/szut/store/article/dto/GetArticleDTO.java
Normal file
11
src/main/java/de/szut/store/article/dto/GetArticleDTO.java
Normal 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;
|
||||||
|
}
|
|
@ -13,19 +13,11 @@ public class Contact {
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@NotBlank
|
|
||||||
private String street;
|
private String street;
|
||||||
|
|
||||||
@NotBlank
|
|
||||||
@Size(min = 3, max = 7)
|
|
||||||
private String zipcode;
|
private String zipcode;
|
||||||
|
|
||||||
@NotBlank
|
|
||||||
private String city;
|
private String city;
|
||||||
|
private String phone;
|
||||||
|
|
||||||
@OneToOne(mappedBy = "contact", cascade = CascadeType.ALL)
|
@OneToOne(mappedBy = "contact", cascade = CascadeType.ALL)
|
||||||
private Supplier supplier;
|
private Supplier supplier;
|
||||||
|
|
||||||
private String phone;
|
|
||||||
}
|
}
|
||||||
|
|
33
src/main/java/de/szut/store/mapping/MappingService.java
Normal file
33
src/main/java/de/szut/store/mapping/MappingService.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -18,7 +18,6 @@ public class Supplier {
|
||||||
private Long id;
|
private Long id;
|
||||||
|
|
||||||
@NotBlank
|
@NotBlank
|
||||||
@Size(min = 3, max = 50)
|
|
||||||
private String name;
|
private String name;
|
||||||
|
|
||||||
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
@OneToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
|
||||||
|
|
|
@ -1,4 +1,52 @@
|
||||||
package de.szut.store.supplier;
|
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 {
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
20
src/main/java/de/szut/store/supplier/dto/AddSupplierDTO.java
Normal file
20
src/main/java/de/szut/store/supplier/dto/AddSupplierDTO.java
Normal 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;
|
||||||
|
}
|
13
src/main/java/de/szut/store/supplier/dto/GetSupplierDTO.java
Normal file
13
src/main/java/de/szut/store/supplier/dto/GetSupplierDTO.java
Normal 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;
|
||||||
|
}
|
Loading…
Reference in a new issue