Skip to content

Commit

Permalink
[#16] Add offers endpoint and respective service, repository and entity
Browse files Browse the repository at this point in the history
  • Loading branch information
isbel8ai committed Oct 18, 2024
1 parent f484922 commit cd2efba
Show file tree
Hide file tree
Showing 55 changed files with 792 additions and 490 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {
public class ProductStoreApplication {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
SpringApplication.run(ProductStoreApplication.class, args);
}
}
15 changes: 12 additions & 3 deletions src/main/java/com/i8ai/training/store/model/Lot.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,32 @@
@NoArgsConstructor
@AllArgsConstructor
public class Lot {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

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

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

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

@Column
private Double deliveredAmount;

@ManyToOne
@JoinColumn(nullable = false, updatable = false)
private Product product;

public Double getCurrentAmount() {
if (deliveredAmount == null) return amount;
return amount - deliveredAmount;
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/i8ai/training/store/model/Offer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.i8ai.training.store.model;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.*;

import java.util.Date;

@Entity
@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Offer {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@Column(nullable = false)
private Double price;


@NotNull
@Column(nullable = false)
private Date createdAt;

@Column
private Double discount;

@ManyToOne
@JoinColumn(nullable = false, updatable = false)
private Pack pack;
}
13 changes: 11 additions & 2 deletions src/main/java/com/i8ai/training/store/model/Pack.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,21 @@
@NoArgsConstructor
@AllArgsConstructor
public class Pack {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

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

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

@Column
private Double soldAmount;

@ManyToOne
@JoinColumn(nullable = false, updatable = false)
Expand All @@ -32,4 +36,9 @@ public class Pack {
@ManyToOne
@JoinColumn(nullable = false, updatable = false)
private Shop shop;

public Double getCurrentAmount() {
if (soldAmount == null) return amount;
return amount - 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 @@ -28,6 +28,6 @@ public class Product {
@Column(nullable = false)
private String measure;

@Column
private String description;

}
9 changes: 3 additions & 6 deletions src/main/java/com/i8ai/training/store/model/Sale.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,20 @@
@NoArgsConstructor
@AllArgsConstructor
public class Sale {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotNull
@Column(nullable = false)
private Date registered;

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

@NotNull
@Column(nullable = false)
private Double price;
private Date registeredAt;

@ManyToOne
@JoinColumn(nullable = false, updatable = false)
private Pack pack;
private Offer offer;
}
2 changes: 1 addition & 1 deletion src/main/java/com/i8ai/training/store/model/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public class Shop {
@Column(nullable = false)
private String address;

@Column
private String description;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.i8ai.training.store.model.Lot;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

Expand All @@ -10,10 +11,14 @@

@Repository
public interface LotRepository extends JpaRepository<Lot, Long> {
List<Lot> findAllByReceivedBetween(Date start, Date end);
List<Lot> findAllByReceivedAtBetween(Date start, Date end);

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

@Query("select sum(l.amount) from Lot l where l.product.id = ?1")
@Query("select sum(l.amount) 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)")
void updateDeliveredAmountById(Long lotId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.i8ai.training.store.repository;

import com.i8ai.training.store.model.Offer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@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 " +
"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) " +
"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) " +
"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) " +
"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);

List<Offer> findAllByCreatedAtBetween(Date start, Date end);

List<Offer> findAllByCreatedAtBetweenAndPackShopId(Date start, Date end, Long shopId);

List<Offer> findAllByCreatedAtBetweenAndPackLotProductId(Date start, Date end, Long productId);

List<Offer> findAllByCreatedAtBetweenAndPackShopIdAndPackLotProductId(
Date start, Date end, Long shopId, Long productId
);
}


Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,28 @@

import com.i8ai.training.store.model.Pack;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Optional;

@Repository
public interface PackRepository extends JpaRepository<Pack, Long> {
List<Pack> findAllByDeliveredBetweenAndLotProductIdAndShopId(Date start, Date end, Long productId, Long shopId);

List<Pack> findAllByDeliveredBetweenAndLotProductId(Date start, Date end, Long productId);
List<Pack> findAllByDeliveredAtBetween(Date start, Date end);

List<Pack> findAllByDeliveredBetweenAndShopId(Date start, Date end, Long shopId);
List<Pack> findAllByDeliveredAtBetweenAndShopId(Date start, Date end, Long shopId);

List<Pack> findAllByDeliveredBetween(Date start, Date end);
List<Pack> findAllByDeliveredAtBetweenAndLotProductId(Date start, Date end, Long productId);

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) " +
"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")
Double getDeliveredAmountByLotId(Long lotId);
Expand All @@ -26,4 +33,8 @@ public interface PackRepository extends JpaRepository<Pack, Long> {

@Query("select sum(d.amount) 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)")
void updateSoldAmountById(Long packId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,49 @@

@Repository
public interface SaleRepository extends JpaRepository<Sale, Long> {
List<Sale> findAllByRegisteredBetween(Date start, Date end);
List<Sale> findAllByRegisteredAtBetween(Date start, Date end);

List<Sale> findAllByRegisteredBetweenAndPackShopId(Date start, Date end, Long shopId);
List<Sale> findAllByRegisteredAtBetweenAndOfferPackShopId(Date start, Date end, Long shopId);

List<Sale> findAllByRegisteredBetweenAndPackLotProductId(Date start, Date end, Long productId);
List<Sale> findAllByRegisteredAtBetweenAndOfferPackLotProductId(Date start, Date end, Long productId);

List<Sale> findAllByRegisteredBetweenAndPackLotProductIdAndPackShopId(Date start, Date end, Long productId, Long shopId);
List<Sale> findAllByRegisteredAtBetweenAndOfferPackLotProductIdAndOfferPackShopId(
Date start, Date end, Long productId, Long shopId);

@Query("select sum(s.amount) from Sale s where s.pack.id = :packId")
@Query("select sum(s.amount) from Sale s where s.offer.pack.id = :packId")
Double getSoldAmountByPackId(Long packId);

@Query("select sum(s.amount) from Sale s where s.pack.lot.product.id = :productId and s.pack.shop.id = :shopId")
@Query("select sum(s.amount) from Sale s where s.offer.pack.lot.product.id = :productId " +
"and s.offer.pack.shop.id = :shopId")
Double getSoldAmountByProductIdAndShopId(Long productId, Long shopId);

@Query("select sum(s.amount * s.price) from Sale s where s.registered between :start and :end")
@Query("select sum(s.amount * s.offer.price) from Sale s where s.registeredAt between :start and :end")
Double getNetSalesIncome(Date start, Date end);

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

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

@Query("select sum(s.amount * s.price) from Sale s where s.pack.lot.product.id = :productId " +
"and s.pack.shop.id = :shopId and s.registered between :start and :end")
@Query("select sum(s.amount * s.offer.price) 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 getIncomeByProductIdAndShopId(Date start, Date end, Long productId, Long shopId);

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

@Query("select sum(s.amount * s.pack.lot.cost) from Sale s where s.pack.lot.product.id = :productId " +
"and s.registered between :start and :end")
@Query("select sum(s.amount * s.offer.pack.lot.cost) 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.pack.lot.cost) from Sale s where s.pack.shop.id = :shopId " +
"and s.registered between :start and :end")
@Query("select sum(s.amount * s.offer.pack.lot.cost) 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.pack.lot.cost) from Sale s where s.pack.lot.product.id = :productId " +
"and s.pack.shop.id = :shopId and s.registered between :start and :end")
@Query("select sum(s.amount * s.offer.pack.lot.cost) 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);
}
Loading

0 comments on commit cd2efba

Please sign in to comment.