Commit 592e3a75 authored by Vladimir Trubachoff's avatar Vladimir Trubachoff

Fix order-line api

parent 10e22f38
package com.example.testj.domain;
import java.math.BigDecimal;
import java.util.List;
import com.example.testj.service.dto.GoodsDto;
import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.validation.constraints.Digits;
import jakarta.validation.constraints.NotEmpty;
@Entity
......@@ -30,16 +21,17 @@ public class Goods {
@NotEmpty
@Column(name = "price")
private BigDecimal price;
private double price;
// @JsonIgnore
// @OneToMany(mappedBy= "goods", cascade = CascadeType.ALL, orphanRemoval = true)
// @OneToMany(mappedBy= "goods", cascade = CascadeType.ALL, orphanRemoval =
// true)
// private List<OrderLine> orderLine;
public Goods() {
}
public Goods(Long id, String name, BigDecimal price) {
public Goods(Long id, String name, Double price) {
this.id = id;
this.name = name;
this.price = price;
......@@ -66,19 +58,19 @@ public class Goods {
this.name = name;
}
public BigDecimal getPrice() {
public Double getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
public void setPrice(Double price) {
this.price = price;
}
// public List<OrderLine> getOrderLine() {
// return orderLine;
// return orderLine;
// }
// public void setOrderLine(List<OrderLine> orderLine) {
// this.orderLine = orderLine;
// this.orderLine = orderLine;
// }
}
......@@ -4,8 +4,6 @@ import java.util.List;
import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
......
......@@ -5,25 +5,47 @@ import org.springframework.stereotype.Service;
import com.example.testj.domain.OrderLine;
import com.example.testj.exception.ResourceNotFoundException;
import com.example.testj.repository.OrderLineRepository;
import com.example.testj.service.dto.GoodsDto;
import com.example.testj.service.dto.OrderDto;
import com.example.testj.service.dto.OrderLineDto;
import com.example.testj.service.dto.OrderLineRequestDto;
import com.example.testj.utils.MappingUtils;
@Service
public class OrderLineService {
private final MappingUtils mappingUtils;
private final OrderLineRepository orderLineRepository;
private final OrderService orderService;
private final GoodsService goodsService;
public OrderLineService(OrderLineRepository orderLineRepository, MappingUtils mappingUtils) {
public OrderLineService(OrderLineRepository orderLineRepository, MappingUtils mappingUtils,
OrderService orderService, GoodsService goodsService) {
this.orderLineRepository = orderLineRepository;
this.mappingUtils = mappingUtils;
this.orderService = orderService;
this.goodsService = goodsService;
}
public OrderLineDto createOrUpdateOrderLine(OrderLine orderLine) {
public OrderLineDto createOrderLine(OrderLineRequestDto orderLineRequest) {
OrderDto order = orderService.getOrder(orderLineRequest.getOrderId());
GoodsDto goods = goodsService.getGoods(orderLineRequest.getGoodsId());
OrderLine orderLine = new OrderLine();
orderLine.setOrder(mappingUtils.toOrder(order));
orderLine.setGoods(mappingUtils.toGoods(goods));
orderLine.setCount(orderLineRequest.getCount());
return mappingUtils.toOrderLineDto(orderLineRepository.save(orderLine));
}
public OrderLineDto updateOrderLine(OrderLine orderLine, Long id) {
OrderLine updatedOrderLine = orderLineRepository.findById(id).orElseThrow(ResourceNotFoundException::new);
updatedOrderLine.setCount(orderLine.getCount());
return mappingUtils.toOrderLineDto(orderLineRepository.save(updatedOrderLine));
}
public OrderLineDto getOrderLine(Long id) throws ResourceNotFoundException {
return mappingUtils.toOrderLineDto(orderLineRepository.findById(id).orElseThrow(ResourceNotFoundException::new));
return mappingUtils
.toOrderLineDto(orderLineRepository.findById(id).orElseThrow(ResourceNotFoundException::new));
}
public void deleteOrderLine(Long id) {
......
package com.example.testj.service.dto;
import java.io.Serializable;
import java.math.BigDecimal;
import com.example.testj.domain.Goods;
......@@ -11,7 +10,7 @@ import com.example.testj.domain.Goods;
public class GoodsDto implements Serializable {
private Long id;
private String name;
private BigDecimal price;
private double price;
public GoodsDto() {
}
......@@ -38,11 +37,11 @@ public class GoodsDto implements Serializable {
this.name = name;
}
public BigDecimal getPrice() {
public double getPrice() {
return price;
}
public void setPrice(BigDecimal price) {
public void setPrice(double price) {
this.price = price;
}
}
\ No newline at end of file
package com.example.testj.service.dto;
public class OrderLineRequestDto {
private Long order_id;
private Long goods_id;
private int count;
public Long getOrderId() {
return order_id;
}
public void setOrderId(Long orderId) {
order_id = orderId;
}
public Long getGoodsId() {
return goods_id;
}
public void setGoodsId(Long goodsId) {
goods_id = goodsId;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
......@@ -31,6 +31,8 @@ public class MappingUtils {
public OrderLine toOrderLine(OrderLineDto orderLineDto) {
OrderLine orderLine = new OrderLine();
orderLine.setGoods(orderLineDto.getGoods());
orderLine.setOrder(orderLineDto.getOrder());
orderLine.setCount(orderLineDto.getCount());
return orderLine;
}
......
package com.example.testj.web.rest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
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;
......@@ -9,8 +12,10 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.testj.exception.ResourceNotFoundException;
import com.example.testj.service.OrderLineService;
import com.example.testj.service.dto.OrderLineDto;
import com.example.testj.service.dto.OrderLineRequestDto;
import com.example.testj.utils.MappingUtils;
import jakarta.validation.Valid;
......@@ -33,15 +38,23 @@ public class OrderLineController {
}
@PostMapping
public OrderLineDto createOrder(@Valid @RequestBody OrderLineDto orderLine) {
return orderLineService.createOrUpdateOrderLine(mappingUtils.toOrderLine(orderLine));
public OrderLineDto create(@Valid @RequestBody OrderLineRequestDto orderLineRequest) {
return orderLineService.createOrderLine(orderLineRequest);
}
@PutMapping("/{id}")
public OrderLineDto updateOrder(@PathVariable Long id, @Valid @RequestBody OrderLineDto orderLine) {
OrderLineDto updateOrderLine = orderLineService.getOrderLine(id);
updateOrderLine.setCount(orderLine.getCount());
return orderLineService.createOrUpdateOrderLine(mappingUtils.toOrderLine(updateOrderLine));
return orderLineService.updateOrderLine(mappingUtils.toOrderLine(orderLine), id);
}
@DeleteMapping("/{id}")
public ResponseEntity<HttpStatus> delete(@PathVariable Long id) throws ResourceNotFoundException {
try {
orderLineService.deleteOrderLine(id);
return new ResponseEntity<>(HttpStatus.OK);
} catch (ResourceNotFoundException e) {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
}
<div class="modal-header">
<h4 class="modal-title" id="modal-basic-title">Add product</h4>
<h4 class="modal-title" id="modal-basic-title">Edit products</h4>
<button
type="button"
class="btn-close"
......@@ -64,3 +64,8 @@
}
<ng-template #loading> loading... </ng-template>
</div>
<div class="modal-footer">
<button class="btn btn-outline-secondary" (click)="modal?.close()">
Close
</button>
</div>
......@@ -34,7 +34,7 @@
class="btn btn-outline-secondary me-2"
(click)="openAddProductModal()"
>
Add products
Edit products
</button>
</div>
......
......@@ -97,6 +97,7 @@ export class OrderPageComponent
orderModalRef.componentInstance.orderLineChange
.pipe(
takeUntil(this.destroy$),
concatMap((v: UpdateCount) => {
const orderLineId = this.order.orderLines.find(
(ol) => ol.goods.id === v.goods_id,
......@@ -116,8 +117,15 @@ export class OrderPageComponent
});
}),
)
.subscribe((resp: any) => {
console.dir(resp);
.subscribe({
next: (res: any) => {
this.getData(this.order.id);
},
error: (err: any) => {
if (DEV) {
console.dir(`Error: ${err}`);
}
},
});
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment