Skip to content

Commit f4e7b78

Browse files
angumpangump
authored andcommitted
Refactoring + Added AllocationService
1 parent 16f6eca commit f4e7b78

File tree

5 files changed

+202
-3
lines changed

5 files changed

+202
-3
lines changed

src/main/java/guru/sfg/beer/inventory/service/repositories/BeerInventoryRepository.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,21 @@
1616
*/
1717
package guru.sfg.beer.inventory.service.repositories;
1818

19-
import guru.sfg.beer.inventory.service.domain.BeerInventory;
20-
import org.springframework.data.jpa.repository.JpaRepository;
21-
2219
import java.util.List;
2320
import java.util.UUID;
2421

22+
import org.springframework.data.jpa.repository.JpaRepository;
23+
24+
import guru.sfg.beer.inventory.service.domain.BeerInventory;
25+
26+
27+
2528
/**
2629
* Created by jt on 2019-01-26.
2730
*/
2831
public interface BeerInventoryRepository extends JpaRepository<BeerInventory, UUID> {
2932

3033
List<BeerInventory> findAllByBeerId(UUID beerId);
34+
35+
List<BeerInventory> findAllByUpc(String upc);
3136
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package guru.sfg.beer.inventory.service.services;
2+
3+
import guru.sfg.brewery.model.BeerOrderDto;
4+
5+
public interface AllocationService {
6+
7+
Boolean allocateOrder(BeerOrderDto beerOrderDto);
8+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package guru.sfg.beer.inventory.service.services;
2+
3+
import java.util.List;
4+
import java.util.concurrent.atomic.AtomicInteger;
5+
6+
import org.springframework.stereotype.Service;
7+
8+
import guru.sfg.beer.inventory.service.domain.BeerInventory;
9+
import guru.sfg.beer.inventory.service.repositories.BeerInventoryRepository;
10+
import guru.sfg.brewery.model.BeerOrderDto;
11+
import guru.sfg.brewery.model.BeerOrderLineDto;
12+
import lombok.RequiredArgsConstructor;
13+
import lombok.extern.slf4j.Slf4j;
14+
15+
@Slf4j
16+
@RequiredArgsConstructor
17+
@Service
18+
public class AllocationServiceImpl implements AllocationService {
19+
20+
private final BeerInventoryRepository beerInventoryRepository;
21+
22+
@Override
23+
public Boolean allocateOrder(BeerOrderDto beerOrderDto) {
24+
log.debug("Allocating OrderId: " + beerOrderDto.getId());
25+
26+
AtomicInteger totalOrdered = new AtomicInteger();
27+
AtomicInteger totalAllocated = new AtomicInteger();
28+
29+
beerOrderDto.getBeerOrderLines()
30+
.forEach(beerOrderLine -> {
31+
32+
if ((((beerOrderLine.getOrderQuantity() != null ? beerOrderLine.getOrderQuantity() : 0)
33+
- (beerOrderLine.getQuantityAllocated() != null ? beerOrderLine.getQuantityAllocated() : 0)) > 0)) {
34+
allocateBeerOrderLine(beerOrderLine);
35+
}
36+
totalOrdered.set(totalOrdered.get() + beerOrderLine.getOrderQuantity());
37+
totalAllocated.set(totalAllocated.get()
38+
+ (beerOrderLine.getQuantityAllocated() != null ? beerOrderLine.getQuantityAllocated() : 0));
39+
});
40+
41+
log.debug("Total Ordered: " + totalOrdered.get() + " Total Allocated: " + totalAllocated.get());
42+
43+
return totalOrdered.get() == totalAllocated.get();
44+
}
45+
46+
private void allocateBeerOrderLine(BeerOrderLineDto beerOrderLine) {
47+
List<BeerInventory> beerInventoryList = beerInventoryRepository.findAllByUpc(beerOrderLine.getUpc());
48+
49+
beerInventoryList.forEach(beerInventory -> {
50+
int inventory = (beerInventory.getQuantityOnHand() == null) ? 0 : beerInventory.getQuantityOnHand();
51+
int orderQty = (beerOrderLine.getOrderQuantity() == null) ? 0 : beerOrderLine.getOrderQuantity();
52+
int allocatedQty = (beerOrderLine.getQuantityAllocated() == null) ? 0
53+
: beerOrderLine.getQuantityAllocated();
54+
int qtyToAllocate = orderQty - allocatedQty;
55+
56+
if (inventory >= qtyToAllocate) { // full allocation
57+
inventory = inventory - qtyToAllocate;
58+
beerOrderLine.setQuantityAllocated(orderQty);
59+
beerInventory.setQuantityOnHand(inventory);
60+
61+
beerInventoryRepository.save(beerInventory);
62+
} else if (inventory > 0) { // partial allocation
63+
beerOrderLine.setQuantityAllocated(allocatedQty + inventory);
64+
beerInventory.setQuantityOnHand(0);
65+
66+
beerInventoryRepository.delete(beerInventory);
67+
}
68+
});
69+
70+
}
71+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package guru.sfg.brewery.model;
19+
20+
import java.time.OffsetDateTime;
21+
import java.util.List;
22+
import java.util.UUID;
23+
24+
import com.fasterxml.jackson.annotation.JsonFormat;
25+
import com.fasterxml.jackson.annotation.JsonProperty;
26+
27+
import lombok.AllArgsConstructor;
28+
import lombok.Builder;
29+
import lombok.Data;
30+
import lombok.NoArgsConstructor;
31+
32+
@Data
33+
@NoArgsConstructor
34+
@AllArgsConstructor
35+
@Builder
36+
public class BeerOrderDto {
37+
38+
@JsonProperty("id")
39+
private UUID id = null;
40+
41+
@JsonProperty("version")
42+
private Integer version = null;
43+
44+
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssZ", shape = JsonFormat.Shape.STRING)
45+
@JsonProperty("createdDate")
46+
private OffsetDateTime createdDate = null;
47+
48+
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssZ", shape = JsonFormat.Shape.STRING)
49+
@JsonProperty("lastModifiedDate")
50+
private OffsetDateTime lastModifiedDate = null;
51+
private UUID customerId;
52+
private String customerRef;
53+
private List<BeerOrderLineDto> beerOrderLines;
54+
private String orderStatus;
55+
private String orderStatusCallbackUrl;
56+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package guru.sfg.brewery.model;
19+
20+
import java.math.BigDecimal;
21+
import java.time.OffsetDateTime;
22+
import java.util.UUID;
23+
24+
import com.fasterxml.jackson.annotation.JsonFormat;
25+
import com.fasterxml.jackson.annotation.JsonProperty;
26+
27+
import lombok.AllArgsConstructor;
28+
import lombok.Builder;
29+
import lombok.Data;
30+
import lombok.NoArgsConstructor;
31+
32+
@Data
33+
@NoArgsConstructor
34+
@AllArgsConstructor
35+
@Builder
36+
public class BeerOrderLineDto {
37+
38+
@JsonProperty("id")
39+
private UUID id = null;
40+
41+
@JsonProperty("version")
42+
private Integer version = null;
43+
44+
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssZ", shape = JsonFormat.Shape.STRING)
45+
@JsonProperty("createdDate")
46+
private OffsetDateTime createdDate = null;
47+
48+
@JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ssZ", shape = JsonFormat.Shape.STRING)
49+
@JsonProperty("lastModifiedDate")
50+
private OffsetDateTime lastModifiedDate = null;
51+
52+
private String upc;
53+
private String beerName;
54+
private UUID beerId;
55+
private String beerStyle;
56+
private Integer orderQuantity = 0;
57+
private Integer quantityAllocated;
58+
private BigDecimal price;
59+
}

0 commit comments

Comments
 (0)