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

Fix order-line api

parent 10e22f38
package com.example.testj.domain; 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.Column;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue; import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id; import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table; import jakarta.persistence.Table;
import jakarta.validation.constraints.Digits;
import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotEmpty;
@Entity @Entity
...@@ -30,16 +21,17 @@ public class Goods { ...@@ -30,16 +21,17 @@ public class Goods {
@NotEmpty @NotEmpty
@Column(name = "price") @Column(name = "price")
private BigDecimal price; private double price;
// @JsonIgnore // @JsonIgnore
// @OneToMany(mappedBy= "goods", cascade = CascadeType.ALL, orphanRemoval = true) // @OneToMany(mappedBy= "goods", cascade = CascadeType.ALL, orphanRemoval =
// true)
// private List<OrderLine> orderLine; // private List<OrderLine> orderLine;
public Goods() { public Goods() {
} }
public Goods(Long id, String name, BigDecimal price) { public Goods(Long id, String name, Double price) {
this.id = id; this.id = id;
this.name = name; this.name = name;
this.price = price; this.price = price;
...@@ -66,11 +58,11 @@ public class Goods { ...@@ -66,11 +58,11 @@ public class Goods {
this.name = name; this.name = name;
} }
public BigDecimal getPrice() { public Double getPrice() {
return price; return price;
} }
public void setPrice(BigDecimal price) { public void setPrice(Double price) {
this.price = price; this.price = price;
} }
......
...@@ -4,8 +4,6 @@ import java.util.List; ...@@ -4,8 +4,6 @@ import java.util.List;
import org.springframework.format.annotation.DateTimeFormat; import org.springframework.format.annotation.DateTimeFormat;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.CascadeType; import jakarta.persistence.CascadeType;
import jakarta.persistence.Column; import jakarta.persistence.Column;
import jakarta.persistence.Entity; import jakarta.persistence.Entity;
......
...@@ -5,25 +5,47 @@ import org.springframework.stereotype.Service; ...@@ -5,25 +5,47 @@ import org.springframework.stereotype.Service;
import com.example.testj.domain.OrderLine; import com.example.testj.domain.OrderLine;
import com.example.testj.exception.ResourceNotFoundException; import com.example.testj.exception.ResourceNotFoundException;
import com.example.testj.repository.OrderLineRepository; 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.OrderLineDto;
import com.example.testj.service.dto.OrderLineRequestDto;
import com.example.testj.utils.MappingUtils; import com.example.testj.utils.MappingUtils;
@Service @Service
public class OrderLineService { public class OrderLineService {
private final MappingUtils mappingUtils; private final MappingUtils mappingUtils;
private final OrderLineRepository orderLineRepository; 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.orderLineRepository = orderLineRepository;
this.mappingUtils = mappingUtils; 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)); 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 { 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) { public void deleteOrderLine(Long id) {
......
package com.example.testj.service.dto; package com.example.testj.service.dto;
import java.io.Serializable; import java.io.Serializable;
import java.math.BigDecimal;
import com.example.testj.domain.Goods; import com.example.testj.domain.Goods;
...@@ -11,7 +10,7 @@ import com.example.testj.domain.Goods; ...@@ -11,7 +10,7 @@ import com.example.testj.domain.Goods;
public class GoodsDto implements Serializable { public class GoodsDto implements Serializable {
private Long id; private Long id;
private String name; private String name;
private BigDecimal price; private double price;
public GoodsDto() { public GoodsDto() {
} }
...@@ -38,11 +37,11 @@ public class GoodsDto implements Serializable { ...@@ -38,11 +37,11 @@ public class GoodsDto implements Serializable {
this.name = name; this.name = name;
} }
public BigDecimal getPrice() { public double getPrice() {
return price; return price;
} }
public void setPrice(BigDecimal price) { public void setPrice(double price) {
this.price = 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 { ...@@ -31,6 +31,8 @@ public class MappingUtils {
public OrderLine toOrderLine(OrderLineDto orderLineDto) { public OrderLine toOrderLine(OrderLineDto orderLineDto) {
OrderLine orderLine = new OrderLine(); OrderLine orderLine = new OrderLine();
orderLine.setGoods(orderLineDto.getGoods());
orderLine.setOrder(orderLineDto.getOrder());
orderLine.setCount(orderLineDto.getCount()); orderLine.setCount(orderLineDto.getCount());
return orderLine; return orderLine;
} }
......
package com.example.testj.web.rest; 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.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
...@@ -9,8 +12,10 @@ import org.springframework.web.bind.annotation.RequestBody; ...@@ -9,8 +12,10 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import com.example.testj.exception.ResourceNotFoundException;
import com.example.testj.service.OrderLineService; import com.example.testj.service.OrderLineService;
import com.example.testj.service.dto.OrderLineDto; import com.example.testj.service.dto.OrderLineDto;
import com.example.testj.service.dto.OrderLineRequestDto;
import com.example.testj.utils.MappingUtils; import com.example.testj.utils.MappingUtils;
import jakarta.validation.Valid; import jakarta.validation.Valid;
...@@ -33,15 +38,23 @@ public class OrderLineController { ...@@ -33,15 +38,23 @@ public class OrderLineController {
} }
@PostMapping @PostMapping
public OrderLineDto createOrder(@Valid @RequestBody OrderLineDto orderLine) { public OrderLineDto create(@Valid @RequestBody OrderLineRequestDto orderLineRequest) {
return orderLineService.createOrUpdateOrderLine(mappingUtils.toOrderLine(orderLine)); return orderLineService.createOrderLine(orderLineRequest);
} }
@PutMapping("/{id}") @PutMapping("/{id}")
public OrderLineDto updateOrder(@PathVariable Long id, @Valid @RequestBody OrderLineDto orderLine) { public OrderLineDto updateOrder(@PathVariable Long id, @Valid @RequestBody OrderLineDto orderLine) {
OrderLineDto updateOrderLine = orderLineService.getOrderLine(id); return orderLineService.updateOrderLine(mappingUtils.toOrderLine(orderLine), id);
updateOrderLine.setCount(orderLine.getCount()); }
return orderLineService.createOrUpdateOrderLine(mappingUtils.toOrderLine(updateOrderLine));
@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"> <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 <button
type="button" type="button"
class="btn-close" class="btn-close"
...@@ -64,3 +64,8 @@ ...@@ -64,3 +64,8 @@
} }
<ng-template #loading> loading... </ng-template> <ng-template #loading> loading... </ng-template>
</div> </div>
<div class="modal-footer">
<button class="btn btn-outline-secondary" (click)="modal?.close()">
Close
</button>
</div>
...@@ -34,7 +34,7 @@ ...@@ -34,7 +34,7 @@
class="btn btn-outline-secondary me-2" class="btn btn-outline-secondary me-2"
(click)="openAddProductModal()" (click)="openAddProductModal()"
> >
Add products Edit products
</button> </button>
</div> </div>
......
...@@ -97,6 +97,7 @@ export class OrderPageComponent ...@@ -97,6 +97,7 @@ export class OrderPageComponent
orderModalRef.componentInstance.orderLineChange orderModalRef.componentInstance.orderLineChange
.pipe( .pipe(
takeUntil(this.destroy$),
concatMap((v: UpdateCount) => { concatMap((v: UpdateCount) => {
const orderLineId = this.order.orderLines.find( const orderLineId = this.order.orderLines.find(
(ol) => ol.goods.id === v.goods_id, (ol) => ol.goods.id === v.goods_id,
...@@ -116,8 +117,15 @@ export class OrderPageComponent ...@@ -116,8 +117,15 @@ export class OrderPageComponent
}); });
}), }),
) )
.subscribe((resp: any) => { .subscribe({
console.dir(resp); 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