lab2 6
This commit is contained in:
parent
431abe9c99
commit
2d53d7dc86
@ -0,0 +1,71 @@
|
||||
package com.example.demo.itemOrders.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.example.demo.core.configuration.Constants;
|
||||
import com.example.demo.itemOrders.model.OrdersEntity;
|
||||
import com.example.demo.itemOrders.service.OrdersService;
|
||||
import com.example.demo.itemProducts.model.ProductsEntity;
|
||||
import com.example.demo.itemProducts.service.ProductsService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/Orders")
|
||||
public class OrdersController {
|
||||
private final OrdersService ordersService;
|
||||
private final ProductsService productsService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public OrdersController(OrdersService ordersService, ProductsService productsService, ModelMapper modelMapper) {
|
||||
this.ordersService = ordersService;
|
||||
this.productsService = productsService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private OrdersDto toDto(OrdersEntity entity) {
|
||||
return modelMapper.map(entity, OrdersDto.class);
|
||||
}
|
||||
|
||||
private OrdersEntity toEntity(OrdersDto dto) {
|
||||
final OrdersEntity entity = modelMapper.map(dto, OrdersEntity.class);
|
||||
entity.setProduct(productsService.get(dto.getProductId()));
|
||||
return entity;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<OrdersDto> getAll(@RequestParam(name = "typeId", defaultValue = "0") ProductsEntity product) {
|
||||
return ordersService.getAll(product.getId()).stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public OrdersDto get(@PathVariable(name = "id") Long id) {
|
||||
return toDto(ordersService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public OrdersDto create(@RequestBody @Valid OrdersDto dto) {
|
||||
return toDto(ordersService.create(toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public OrdersDto update(@PathVariable(name = "id") Long id, @RequestBody OrdersDto dto) {
|
||||
return toDto(ordersService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public OrdersDto delete(@PathVariable(name = "id") Long id) {
|
||||
return toDto(ordersService.delete(id));
|
||||
}
|
||||
}
|
60
src/main/java/com/example/demo/itemOrders/api/OrdersDto.java
Normal file
60
src/main/java/com/example/demo/itemOrders/api/OrdersDto.java
Normal file
@ -0,0 +1,60 @@
|
||||
package com.example.demo.itemOrders.api;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
import com.example.demo.itemProducts.model.ProductsEntity;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class OrdersDto {
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
private Long id;
|
||||
@NotNull
|
||||
private ProductsEntity product;
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Integer count;
|
||||
@NotNull
|
||||
private SimpleDateFormat date;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public ProductsEntity getProduct() {
|
||||
return product;
|
||||
}
|
||||
public void setProduct(ProductsEntity product) {
|
||||
this.product = product;
|
||||
}
|
||||
public Long getProductId() {
|
||||
return product.getId();
|
||||
}
|
||||
public void setProductId(Long productId) {
|
||||
productId = product.getId();
|
||||
}
|
||||
|
||||
public Integer getCount() {
|
||||
return count;
|
||||
}
|
||||
public void setCount(Integer count) {
|
||||
this.count = count;
|
||||
}
|
||||
public SimpleDateFormat getDate() {
|
||||
return date;
|
||||
}
|
||||
public void setDate(SimpleDateFormat date) {
|
||||
this.date = date;
|
||||
}
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
public Double getSum() {
|
||||
return count * product.getPrice();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,70 @@
|
||||
package com.example.demo.itemProducts.api;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.modelmapper.ModelMapper;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.example.demo.core.configuration.Constants;
|
||||
import com.example.demo.itemProducts.model.ProductsEntity;
|
||||
import com.example.demo.itemProducts.service.ProductsService;
|
||||
import com.example.demo.itemCategories.service.CategoriesService;
|
||||
|
||||
import jakarta.validation.Valid;
|
||||
|
||||
@RestController
|
||||
@RequestMapping(Constants.API_URL + "/Products")
|
||||
public class ProductsController {
|
||||
private final ProductsService ProductsService;
|
||||
private final CategoriesService CategoriesService;
|
||||
private final ModelMapper modelMapper;
|
||||
|
||||
public ProductsController(ProductsService ProductsService, CategoriesService CategoriesService, ModelMapper modelMapper) {
|
||||
this.ProductsService = ProductsService;
|
||||
this.CategoriesService = CategoriesService;
|
||||
this.modelMapper = modelMapper;
|
||||
}
|
||||
|
||||
private ProductsDto toDto(ProductsEntity entity) {
|
||||
return modelMapper.map(entity, ProductsDto.class);
|
||||
}
|
||||
|
||||
private ProductsEntity toEntity(ProductsDto dto) {
|
||||
final ProductsEntity entity = modelMapper.map(dto, ProductsEntity.class);
|
||||
entity.setType(CategoriesService.get(dto.getTypeId()));
|
||||
return entity;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public List<ProductsDto> getAll(@RequestParam(name = "typeId", defaultValue = "0") Long typeId) {
|
||||
return ProductsService.getAll(typeId).stream().map(this::toDto).toList();
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public ProductsDto get(@PathVariable(name = "id") Long id) {
|
||||
return toDto(ProductsService.get(id));
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public ProductsDto create(@RequestBody @Valid ProductsDto dto) {
|
||||
return toDto(ProductsService.create(toEntity(dto)));
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public ProductsDto update(@PathVariable(name = "id") Long id, @RequestBody ProductsDto dto) {
|
||||
return toDto(ProductsService.update(id, toEntity(dto)));
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public ProductsDto delete(@PathVariable(name = "id") Long id) {
|
||||
return toDto(ProductsService.delete(id));
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.example.demo.itemProducts.api;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
public class ProductsDto {
|
||||
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
|
||||
private Long id;
|
||||
@NotBlank
|
||||
private String name;
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Long typeId;
|
||||
@NotNull
|
||||
@Min(1)
|
||||
private Double price;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getTypeId() {
|
||||
return typeId;
|
||||
}
|
||||
|
||||
public void setTypeId(Long typeId) {
|
||||
this.typeId = typeId;
|
||||
}
|
||||
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
}
|
@ -24,14 +24,15 @@ public class ProductsEntity extends BaseEntity{
|
||||
public CategoriesEntity getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public void setType(CategoriesEntity type) {
|
||||
this.type = type;
|
||||
}
|
||||
public Double getPrice() {
|
||||
return price;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
@ -39,9 +40,7 @@ public class ProductsEntity extends BaseEntity{
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setPrice(Double price) {
|
||||
this.price = price;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, type, name, price);
|
||||
|
@ -1,7 +1,6 @@
|
||||
package com.example.demo.itemUsers.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
Loading…
Reference in New Issue
Block a user