Skip to content

Commit 5a31a22

Browse files
committed
feature of associating Warehouses: business restrictions and validation checks
1 parent 1b743f2 commit 5a31a22

3 files changed

Lines changed: 30 additions & 4 deletions

File tree

src/main/java/com/fulfilment/application/monolith/rules/FulfillmentRuleRepository.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,33 @@
66
import io.quarkus.hibernate.orm.panache.PanacheRepository;
77
import jakarta.enterprise.context.ApplicationScoped;
88
import jakarta.transaction.Transactional;
9+
import jakarta.ws.rs.WebApplicationException;
10+
import jakarta.ws.rs.core.Response;
911

1012
@ApplicationScoped
1113
@Transactional
1214
public 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

src/main/java/com/fulfilment/application/monolith/rules/FulfillmentRuleResource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ public List<FulfillmentRule> listAll() {
2222

2323
@POST
2424
public FulfillmentRule create(FulfillmentRule rule) {
25-
repository.persist(rule.toDb());
25+
repository.create(rule.toDb());
2626
return rule;
2727
}
2828

2929
@DELETE
3030
@Path("{id}")
3131
public void delete(@PathParam("id") Long id) {
32-
DbFulfillmentRule rule = repository.findById(id);
32+
DbFulfillmentRule rule = repository.findById(id);
3333
if (rule == null) {
3434
throw new WebApplicationException(404);
3535
}

src/main/resources/META-INF/resources/index.html

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@
161161
}
162162

163163
function _error(response) {
164-
alert(response.data?.message || response.statusText);
164+
// Показываем `error` из тела ответа, если оно есть
165+
const msg = response.data?.error || response.statusText || 'Unknown error';
166+
alert(msg);
165167
}
166168

167169
function _clearForms() {

0 commit comments

Comments
 (0)