Skip to content

Commit

Permalink
Add builder to model entities
Browse files Browse the repository at this point in the history
  • Loading branch information
isbel8ai committed Apr 11, 2024
1 parent 22bd117 commit b9400d3
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 166 deletions.
9 changes: 3 additions & 6 deletions src/main/java/com/i8ai/training/storeapi/model/Lot.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;

import java.util.Date;

@Entity
@Getter
@Setter
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Lot {
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/i8ai/training/storeapi/model/Pack.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;

import java.util.Date;

@Entity
@Getter
@Setter
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Pack {
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/i8ai/training/storeapi/model/Product.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;

@Entity
@Getter
@Setter
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Product {
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/i8ai/training/storeapi/model/Sale.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,13 @@

import jakarta.persistence.*;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;

import java.util.Date;

@Entity
@Getter
@Setter
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Sale {
Expand Down
9 changes: 3 additions & 6 deletions src/main/java/com/i8ai/training/storeapi/model/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.*;

@Entity
@Getter
@Setter
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Shop {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.i8ai.training.storeapi.model.Product;
import com.i8ai.training.storeapi.model.Shop;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@AllArgsConstructor
@Builder
public class Balance {
Double spent;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import com.i8ai.training.storeapi.model.Product;
import com.i8ai.training.storeapi.model.Shop;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@AllArgsConstructor
@Builder
public class Existence {
private Double amount;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,99 +29,92 @@ public BalanceServiceImpl(ShopService shopService, ProductService productService

@Override
public Balance getNetBalance(Date start, Date end) {
return new Balance(
saleService.getNetSalesExpenses(start, end),
saleService.getNetSalesIncome(start, end),
null,
null
);
return Balance.builder()
.spent(saleService.getNetSalesExpenses(start, end))
.income(saleService.getNetSalesIncome(start, end))
.build();
}

@Override
public List<Balance> getBalancesPerProduct(Date start, Date end) {
return productService.getAllProducts().stream()
.map(product -> new Balance(
saleService.getSalesExpensesByProduct(product.getId(), start, end),
saleService.getSalesIncomeByProduct(product.getId(), start, end),
product,
null
)).toList();
return productService.getAllProducts().stream().map(product -> Balance.builder()
.spent(saleService.getSalesExpensesByProduct(product.getId(), start, end))
.income(saleService.getSalesIncomeByProduct(product.getId(), start, end))
.product(product)
.build()
).toList();
}

@Override
public List<Balance> getBalancesPerShop(Date start, Date end) {
return shopService.getAllShops().stream()
.map(shop -> new Balance(
saleService.getSalesExpensesByShop(shop.getId(), start, end),
saleService.getSalesIncomeByShop(shop.getId(), start, end),
null,
shop
)).toList();
return shopService.getAllShops().stream().map(shop -> Balance.builder()
.spent(saleService.getSalesExpensesByShop(shop.getId(), start, end))
.income(saleService.getSalesIncomeByShop(shop.getId(), start, end))
.shop(shop)
.build()
).toList();
}

@Override
public List<Balance> getBalancesPerProductPerShop(Date start, Date end) {
List<Product> products = productService.getAllProducts();
return shopService.getAllShops().stream().flatMap(shop ->
products.stream().map(product ->
new Balance(
saleService.getSalesExpensesByProductAndShop(product.getId(), shop.getId(), start, end),
saleService.getSalesIncomeByProductAndShop(product.getId(), shop.getId(), start, end),
product,
shop
)
)).toList();
return shopService.getAllShops().stream().flatMap(shop -> products.stream().map(product -> Balance.builder()
.spent(saleService.getSalesExpensesByProductAndShop(product.getId(), shop.getId(), start, end))
.income(saleService.getSalesIncomeByProductAndShop(product.getId(), shop.getId(), start, end))
.product(product)
.shop(shop)
.build()
)).toList();
}

@Override
public Balance getBalanceByProduct(Long productId, Date start, Date end) {
return new Balance(
saleService.getSalesExpensesByProduct(productId, start, end),
saleService.getSalesIncomeByProduct(productId, start, end),
productService.getProduct(productId),
null
);
return Balance.builder()
.spent(saleService.getSalesExpensesByProduct(productId, start, end))
.income(saleService.getSalesIncomeByProduct(productId, start, end))
.product(productService.getProduct(productId))
.build();
}

@Override
public Balance getBalanceByShop(Long shopId, Date start, Date end) {
return new Balance(
saleService.getSalesExpensesByShop(shopId, start, end),
saleService.getSalesIncomeByShop(shopId, start, end),
null,
shopService.getShop(shopId)
);
return Balance.builder()
.spent(saleService.getSalesExpensesByShop(shopId, start, end))
.income(saleService.getSalesIncomeByShop(shopId, start, end))
.shop(shopService.getShop(shopId))
.build();
}

@Override
public List<Balance> getBalancesByProductPerShop(Long productId, Date start, Date end) {
Product product = productService.getProduct(productId);
return shopService.getAllShops().stream().map(shop -> new Balance(
saleService.getSalesExpensesByProductAndShop(productId, shop.getId(), start, end),
saleService.getSalesIncomeByProductAndShop(productId, shop.getId(), start, end),
product,
shop
)).toList();
return shopService.getAllShops().stream().map(shop -> Balance.builder()
.spent(saleService.getSalesExpensesByProductAndShop(productId, shop.getId(), start, end))
.income(saleService.getSalesIncomeByProductAndShop(productId, shop.getId(), start, end))
.product(product)
.shop(shop)
.build()
).toList();
}

@Override
public List<Balance> getBalancesByShopPerProduct(Long shopId, Date start, Date end) {
Shop shop = shopService.getShop(shopId);
return productService.getAllProducts().stream().map(product -> new Balance(
saleService.getSalesExpensesByProductAndShop(product.getId(), shopId, start, end),
saleService.getSalesIncomeByProductAndShop(product.getId(), shopId, start, end),
product,
shop
)).toList();
return productService.getAllProducts().stream().map(product -> Balance.builder()
.spent(saleService.getSalesExpensesByProductAndShop(product.getId(), shopId, start, end))
.income(saleService.getSalesIncomeByProductAndShop(product.getId(), shopId, start, end))
.product(product)
.shop(shop).build()
).toList();
}

@Override
public Balance getBalanceByProductAndShop(Long productId, Long shopId, Date start, Date end) {
return new Balance(
saleService.getSalesExpensesByProductAndShop(productId, shopId, start, end),
saleService.getSalesIncomeByProductAndShop(productId, shopId, start, end),
productService.getProduct(productId),
shopService.getShop(shopId)
);
return Balance.builder()
.spent(saleService.getSalesExpensesByProductAndShop(productId, shopId, start, end))
.income(saleService.getSalesIncomeByProductAndShop(productId, shopId, start, end))
.product(productService.getProduct(productId))
.shop(shopService.getShop(shopId))
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ public Existence getExistenceByProductInMain(Product product) {
Double amount = lotService.getProductReceivedAmount(product.getId()) -
packService.getProductDeliveredAmount(product.getId());

return new Existence(amount, product, null);
return Existence.builder().amount(amount).product(product).build();
}

private Existence getExistenceByProductAndShop(Product product, Shop shop) {
Double amount = packService.getProductDeliveredToShopAmount(product.getId(), shop.getId()) -
saleService.getSoldAmountByProductAndShop(product.getId(), shop.getId());

return new Existence(amount, product, shop);
return Existence.builder().amount(amount).product(product).shop(shop).build();
}
}
Loading

0 comments on commit b9400d3

Please sign in to comment.