66import io .quarkus .hibernate .orm .panache .PanacheRepository ;
77import jakarta .enterprise .context .ApplicationScoped ;
88import jakarta .transaction .Transactional ;
9+ import jakarta .ws .rs .WebApplicationException ;
10+ import jakarta .ws .rs .core .Response ;
911
1012@ ApplicationScoped
1113@ Transactional
1214public class FulfillmentRuleRepository implements PanacheRepository <DbFulfillmentRule > {
15+ public void create (DbFulfillmentRule rule ) {
16+ if (find ("warehouse = ?1 and store=?2 and product=?3" ,
17+ rule .warehouse , rule .store , rule .product ).count () > 0 ) {
18+ throw new WebApplicationException ("Rule already exists" , Response .Status .CONFLICT );
19+ }
20+ if (countDistinctProductByWarehouse (rule .warehouse ) >= 5 ) {
21+ throw new WebApplicationException ("Each `Warehouse` can store maximally 5 types of `Products`" ,
22+ Response .Status .PRECONDITION_FAILED );
23+ }
24+ if (countDistinctStoresByWarehouse (rule .warehouse ) >= 3 ) {
25+ throw new WebApplicationException ("Each `Store` can be fulfilled by a maximum of 3 different `Warehouses`" ,
26+ Response .Status .PRECONDITION_FAILED );
27+ }
28+ if (countDistinctWarehousesByProductAndStore (rule .store , rule .product ) >= 2 ) {
29+ throw new WebApplicationException (
30+ "Each `Product` can be fulfilled by a maximum of 2 different `Warehouses` per `Store`" ,
31+ Response .Status .PRECONDITION_FAILED );
32+ }
33+ persist (rule );
34+ }
35+
1336 public long countDistinctStoresByWarehouse (DbWarehouse warehouse ) {
1437 return find (
1538 "select count(distinct f.store.id) from " + DbFulfillmentRule .class .getSimpleName () + " f where f.warehouse = ?1" ,
@@ -19,7 +42,8 @@ public long countDistinctStoresByWarehouse(DbWarehouse warehouse) {
1942
2043 public long countDistinctProductByWarehouse (DbWarehouse warehouse ) {
2144 return find (
22- "select count(distinct f.product.id) from " + DbFulfillmentRule .class .getSimpleName () + " f where f.warehouse = ?1" ,
45+ "select count(distinct f.product.id) from " + DbFulfillmentRule .class .getSimpleName () +
46+ " f where f.warehouse = ?1" ,
2347 warehouse )
2448 .project (Long .class ).firstResult ();
2549
0 commit comments