Skip to content

Commit

Permalink
[#12] Create required DTOs for controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
isbel8ai committed Oct 26, 2024
1 parent cd2efba commit 873dd23
Show file tree
Hide file tree
Showing 42 changed files with 585 additions and 335 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/i8ai/training/store/model/Lot.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ public class Lot {

@NotNull
@Column(nullable = false)
private Double amount;
private Double acquiredAmount;

@NotNull
@Column(nullable = false)
private Double cost;
private Double costPerUnit;

@NotNull
@Column(nullable = false)
Expand All @@ -38,7 +38,7 @@ public class Lot {
private Product product;

public Double getCurrentAmount() {
if (deliveredAmount == null) return amount;
return amount - deliveredAmount;
if (deliveredAmount == null) return acquiredAmount;
return acquiredAmount - deliveredAmount;
}
}
6 changes: 3 additions & 3 deletions src/main/java/com/i8ai/training/store/model/Pack.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class Pack {

@NotNull
@Column(nullable = false)
private Double amount;
private Double receivedAmount;

@NotNull
@Column(nullable = false)
Expand All @@ -38,7 +38,7 @@ public class Pack {
private Shop shop;

public Double getCurrentAmount() {
if (soldAmount == null) return amount;
return amount - soldAmount;
if (soldAmount == null) return receivedAmount;
return receivedAmount - soldAmount;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/i8ai/training/store/model/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Product {

@NotBlank
@Column(nullable = false)
private String measure;
private String measureUnit;

@Column
private String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ public interface LotRepository extends JpaRepository<Lot, Long> {

List<Lot> findAllByReceivedAtBetweenAndProductId(Date start, Date end, Long productId);

@Query("select sum(l.amount) from Lot l where l.product.id = :productId")
@Query("select sum(l.acquiredAmount) from Lot l where l.product.id = :productId")
Double getAmountArrivedByProductId(Long productId);

@Modifying
@Query("update Lot o set o.deliveredAmount = (select sum(p.amount) from Pack p where p.lot.id = :lotId)")
@Query("update Lot o set o.deliveredAmount = (select sum(p.receivedAmount) from Pack p where p.lot.id = :lotId) " +
"where o.id = :lotId")
void updateDeliveredAmountById(Long lotId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@
@Repository
public interface OfferRepository extends JpaRepository<Offer, Long> {

@Query("select o from Offer o where o.pack.soldAmount = null or o.pack.amount - o.pack.soldAmount > 0 " +
@Query("select o from Offer o where (o.pack.soldAmount is null or o.pack.receivedAmount - o.pack.soldAmount > 0) " +
"order by o.createdAt desc")
List<Offer> findAllAvailable();

@Query("select o from Offer o where (o.pack.soldAmount = null or o.pack.amount - o.pack.soldAmount > 0) " +
@Query("select o from Offer o where (o.pack.soldAmount is null or o.pack.receivedAmount - o.pack.soldAmount > 0) " +
"and o.pack.shop.id = :shopId order by o.createdAt desc")
List<Offer> findAllAvailableByShopId(Long shopId);

@Query("select o from Offer o where (o.pack.soldAmount = null or o.pack.amount - o.pack.soldAmount > 0) " +
@Query("select o from Offer o where (o.pack.soldAmount is null or o.pack.receivedAmount - o.pack.soldAmount > 0) " +
"and o.pack.lot.product.id = :productId order by o.createdAt desc")
List<Offer> findAllAvailableByProductId(Long productId);

@Query("select o from Offer o where (o.pack.soldAmount = null or o.pack.amount - o.pack.soldAmount > 0) " +
@Query("select o from Offer o where (o.pack.soldAmount is null or o.pack.receivedAmount - o.pack.soldAmount > 0) " +
"and o.pack.shop.id = :shopId and o.pack.lot.product.id = :productId order by o.createdAt desc")
List<Offer> findAllAvailableByShopIdAndProductId(Long shopId, Long productId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,21 @@ public interface PackRepository extends JpaRepository<Pack, Long> {

List<Pack> findAllByDeliveredAtBetweenAndLotProductIdAndShopId(Date start, Date end, Long productId, Long shopId);

@Query("select p from Pack p where (p.soldAmount is null or p.amount - p.soldAmount > 0) " +
@Query("select p from Pack p where (p.soldAmount is null or p.receivedAmount - p.soldAmount > 0) " +
"and p.shop.id = :shopId and p.lot.product.id = :productId order by p.deliveredAt limit 1")
Optional<Pack> findAvailableByShopIdAndProductId(Long shopId, Long productId);

@Query("select sum(d.amount) from Pack d where d.lot.id = ?1")
@Query("select sum(d.receivedAmount) from Pack d where d.lot.id = ?1")
Double getDeliveredAmountByLotId(Long lotId);

@Query("select sum(d.amount) from Pack d where d.lot.product.id = ?1")
@Query("select sum(d.receivedAmount) from Pack d where d.lot.product.id = ?1")
Double getDeliveredAmountByProductId(Long productId);

@Query("select sum(d.amount) from Pack d where d.lot.product.id = ?1 and d.shop.id = ?2")
@Query("select sum(d.receivedAmount) from Pack d where d.lot.product.id = ?1 and d.shop.id = ?2")
Double getDeliveredAmountByProductIdAndShopId(Long productId, Long shopId);

@Modifying
@Query("update Pack a set a.soldAmount = (select sum(s.amount) from Sale s where s.offer.pack.id = :packId)")
@Query("update Pack p set p.soldAmount = (select sum(s.amount) from Sale s where s.offer.pack.id = :packId) " +
"where p.id = :packId")
void updateSoldAmountById(Long packId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ List<Sale> findAllByRegisteredAtBetweenAndOfferPackLotProductIdAndOfferPackShopI
"and s.offer.pack.shop.id = :shopId and s.registeredAt between :start and :end")
Double getIncomeByProductIdAndShopId(Date start, Date end, Long productId, Long shopId);

@Query("select sum(s.amount * s.offer.pack.lot.cost) from Sale s where s.registeredAt between :start and :end")
@Query("select sum(s.amount * s.offer.pack.lot.costPerUnit) from Sale s where s.registeredAt between :start and :end")
Double getNetSalesExpenses(Date start, Date end);

@Query("select sum(s.amount * s.offer.pack.lot.cost) from Sale s where s.offer.pack.lot.product.id = :productId " +
@Query("select sum(s.amount * s.offer.pack.lot.costPerUnit) from Sale s where s.offer.pack.lot.product.id = :productId " +
"and s.registeredAt between :start and :end")
Double getSaleExpensesByProductId(Date start, Date end, Long productId);

@Query("select sum(s.amount * s.offer.pack.lot.cost) from Sale s where s.offer.pack.shop.id = :shopId " +
@Query("select sum(s.amount * s.offer.pack.lot.costPerUnit) from Sale s where s.offer.pack.shop.id = :shopId " +
"and s.registeredAt between :start and :end")
Double getSaleExpensesByShopId(Date start, Date end, Long shopId);

@Query("select sum(s.amount * s.offer.pack.lot.cost) from Sale s where s.offer.pack.lot.product.id = :productId " +
@Query("select sum(s.amount * s.offer.pack.lot.costPerUnit) from Sale s where s.offer.pack.lot.product.id = :productId " +
"and s.offer.pack.shop.id = :shopId and s.registeredAt between :start and :end")
Double getSaleExpensesByProductIdAndShopId(Date start, Date end, Long productId, Long shopId);
}
16 changes: 8 additions & 8 deletions src/main/java/com/i8ai/training/store/rest/LotController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.i8ai.training.store.rest;

import com.i8ai.training.store.model.Lot;
import com.i8ai.training.store.rest.dto.LotDto;
import com.i8ai.training.store.service.LotService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
Expand All @@ -16,16 +16,16 @@ public class LotController {

private final LotService lotService;

@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public LotDto registerLot(@RequestBody LotDto newLot) {
return new LotDto(lotService.registerLot(newLot));
}

@GetMapping
public List<Lot> getLots(@RequestParam(required = false) Long productId,
public List<LotDto> getLots(@RequestParam(required = false) Long productId,
@RequestParam(required = false) Date start,
@RequestParam(required = false) Date end) {
return lotService.getLots(productId, start, end);
}

@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public Lot registerLot(@RequestBody Lot newLot) {
return lotService.registerLot(newLot);
return lotService.getLots(productId, start, end).stream().map(LotDto::new).toList();
}

@DeleteMapping("{lotId}")
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/com/i8ai/training/store/rest/OfferController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.i8ai.training.store.rest;

import com.i8ai.training.store.model.Offer;
import com.i8ai.training.store.rest.dto.OfferDto;
import com.i8ai.training.store.service.OfferService;
import lombok.RequiredArgsConstructor;
Expand All @@ -19,21 +18,21 @@ public class OfferController {
private final OfferService offerService;

@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public Offer createOffer(@RequestBody OfferDto offerDto) {
return offerService.createOffer(offerDto);
public OfferDto createOffer(@RequestBody OfferDto offerDto) {
return new OfferDto(offerService.createOffer(offerDto));
}

@GetMapping
public List<Offer> getCurrentOffers(@RequestParam(required = false) Long shopId,
public List<OfferDto> getCurrentOffers(@RequestParam(required = false) Long shopId,
@RequestParam(required = false) Long productId) {
return offerService.getCurrentOffers(shopId, productId);
return offerService.getCurrentOffers(shopId, productId).stream().map(OfferDto::new).toList();
}

@GetMapping("history")
public List<Offer> getOffersHistory(@RequestParam(required = false) Long shopId,
public List<OfferDto> getOffersHistory(@RequestParam(required = false) Long shopId,
@RequestParam(required = false) Long productId,
@RequestParam(required = false) Date start,
@RequestParam(required = false) Date end) {
return offerService.getOffersHistory(shopId, productId, start, end);
return offerService.getOffersHistory(shopId, productId, start, end).stream().map(OfferDto::new).toList();
}
}
16 changes: 8 additions & 8 deletions src/main/java/com/i8ai/training/store/rest/PackController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.i8ai.training.store.rest;

import com.i8ai.training.store.model.Pack;
import com.i8ai.training.store.rest.dto.PackDto;
import com.i8ai.training.store.service.PackService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
Expand All @@ -15,17 +15,17 @@
public class PackController {
private final PackService packService;

@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public PackDto registerPack(@RequestBody PackDto packDto) {
return new PackDto(packService.registerPack(packDto));
}

@GetMapping
public List<Pack> getPacks(@RequestParam(required = false) Long productId,
public List<PackDto> getPacks(@RequestParam(required = false) Long productId,
@RequestParam(required = false) Long shopId,
@RequestParam(required = false) Date start,
@RequestParam(required = false) Date end) {
return packService.getPacks(productId, shopId, start, end);
}

@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public Pack registerPack(@RequestBody Pack newPack) {
return packService.registerPack(newPack);
return packService.getPacks(productId, shopId, start, end).stream().map(PackDto::new).toList();
}

@DeleteMapping(value = "{packId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ public class ProductController {

private final ProductService productService;

@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public Product createProduct(@Valid @RequestBody Product newProduct) {
return productService.createProduct(newProduct);
}

@GetMapping
public List<Product> getAllProducts() {
return productService.getAllProducts();
}

@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public Product addProduct(@Valid @RequestBody Product newProduct) {
return productService.createProduct(newProduct);
}

@GetMapping("{productId}")
public Product getProduct(@PathVariable Long productId) {
return productService.getProduct(productId);
Expand Down
14 changes: 9 additions & 5 deletions src/main/java/com/i8ai/training/store/rest/SaleController.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.i8ai.training.store.rest;

import com.i8ai.training.store.model.Sale;
import com.i8ai.training.store.rest.dto.SaleDto;
import com.i8ai.training.store.service.SaleService;
import lombok.RequiredArgsConstructor;
Expand All @@ -17,15 +16,20 @@ public class SaleController {
private final SaleService saleService;

@PostMapping
public Sale registerSale(@RequestBody SaleDto saleDto) {
return saleService.registerSale(saleDto);
public SaleDto registerSale(@RequestBody SaleDto saleDto) {
return new SaleDto(saleService.registerSale(saleDto));
}

@GetMapping
public List<Sale> getSales(@RequestParam(required = false) Date start,
public List<SaleDto> getSales(@RequestParam(required = false) Date start,
@RequestParam(required = false) Date end,
@RequestParam(required = false) Long productId,
@RequestParam(required = false) Long shopId) {
return saleService.getSales(start, end, productId, shopId);
return saleService.getSales(start, end, productId, shopId).stream().map(SaleDto::new).toList();
}

@DeleteMapping("{saleId}")
public void deleteSale(@PathVariable Long saleId) {
saleService.deleteSale(saleId);
}
}
14 changes: 5 additions & 9 deletions src/main/java/com/i8ai/training/store/rest/ShopController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,29 @@
import com.i8ai.training.store.model.Shop;
import com.i8ai.training.store.service.ShopService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("shops")
public class ShopController {

private final ShopService shopService;

@Autowired
public ShopController(ShopService shopService) {
this.shopService = shopService;
@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public Shop createShop(@Valid @RequestBody Shop newShop) {
return shopService.createShop(newShop);
}

@GetMapping
public List<Shop> getAllShops() {
return shopService.getAllShops();
}

@PostMapping(consumes = MediaType.APPLICATION_JSON_VALUE)
public Shop addShop(@Valid @RequestBody Shop newShop) {
return shopService.createShop(newShop);
}

@GetMapping("{shopId}")
public Shop getShop(@PathVariable Long shopId) {
return shopService.getShop(shopId);
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/i8ai/training/store/rest/dto/LotDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.i8ai.training.store.rest.dto;

import com.i8ai.training.store.model.Lot;

import java.util.Date;

public record LotDto(
Long id,
Long productId,
Double acquiredAmount,
Double costPerUnit,
Double currentAmount,
Date receivedAt
) {
public LotDto(Lot lot) {
this(
lot.getId(),
lot.getProduct().getId(),
lot.getAcquiredAmount(),
lot.getCostPerUnit(),
lot.getCurrentAmount(),
lot.getReceivedAt()
);
}
}
14 changes: 13 additions & 1 deletion src/main/java/com/i8ai/training/store/rest/dto/OfferDto.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.i8ai.training.store.rest.dto;

import com.i8ai.training.store.model.Offer;

import java.util.Date;

public record OfferDto(
Expand All @@ -8,6 +10,16 @@ public record OfferDto(
Long productId,
Double price,
Double discount,
Date created
Date createdAt
) {
public OfferDto(Offer offer) {
this(
offer.getId(),
offer.getPack().getShop().getId(),
offer.getPack().getLot().getProduct().getId(),
offer.getPrice(),
offer.getDiscount(),
offer.getCreatedAt()
);
}
}
Loading

0 comments on commit 873dd23

Please sign in to comment.