Implement First Draft of Supplier API
This commit is contained in:
11
supplier/src/main/java/org/hso/ecommerce/supplier/App.java
Normal file
11
supplier/src/main/java/org/hso/ecommerce/supplier/App.java
Normal file
@ -0,0 +1,11 @@
|
||||
package org.hso.ecommerce.supplier;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(App.class, args);
|
||||
}
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package org.hso.ecommerce.supplier;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.hso.ecommerce.supplier.data.Supplier;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class ConfigurationReader {
|
||||
|
||||
public static List<Supplier> read() throws IOException {
|
||||
File dir = new File("./config/");
|
||||
|
||||
System.out.println("Loading Config in " + dir.getAbsolutePath());
|
||||
ArrayList<Supplier> ret = Files.list(dir.toPath()).map(path -> {
|
||||
|
||||
System.out.println("Iterating over; " + path);
|
||||
if (path.toString().endsWith(".json")) {
|
||||
try {
|
||||
String jsonData = Files.readString(path, StandardCharsets.UTF_8);
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
Supplier sup = (Supplier)objectMapper.readValue(jsonData, Supplier.class);
|
||||
System.out.println("Loaded " + sup.id);
|
||||
return sup;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
System.out.println("Skipping because of file extension.");
|
||||
}
|
||||
return null;
|
||||
}).collect(Collectors.toCollection(ArrayList::new));
|
||||
|
||||
ret.removeIf(Objects::isNull);
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,88 @@
|
||||
package org.hso.ecommerce.supplier;
|
||||
|
||||
import org.hso.ecommerce.supplier.data.Article;
|
||||
import org.hso.ecommerce.supplier.data.Order;
|
||||
import org.hso.ecommerce.supplier.data.OrderConfirmation;
|
||||
import org.hso.ecommerce.supplier.data.Supplier;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
public class RequestController {
|
||||
|
||||
private final HashMap<String, Integer> dailySalesVolumeCent = new HashMap<>();
|
||||
private final HashMap<String, Supplier> knownSuppliers = new HashMap<>();
|
||||
private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
|
||||
|
||||
@PostConstruct
|
||||
public void init() throws IOException {
|
||||
for (Supplier s : ConfigurationReader.read()) {
|
||||
knownSuppliers.put(s.id, s);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@GetMapping("/")
|
||||
public List<Supplier> index() {
|
||||
return new ArrayList<>(knownSuppliers.values());
|
||||
}
|
||||
|
||||
@GetMapping("/{supplier}/")
|
||||
public Supplier supplier(HttpServletResponse res, @PathVariable("supplier") String supplierName) {
|
||||
Supplier s = knownSuppliers.get(supplierName);
|
||||
if(s == null) {
|
||||
res.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
@PostMapping("/{supplier}/order")
|
||||
public OrderConfirmation order(HttpServletResponse res, @PathVariable("supplier") String supplierName, @RequestBody Order order) {
|
||||
Supplier s = knownSuppliers.get(supplierName);
|
||||
if(s == null) {
|
||||
res.setStatus(HttpServletResponse.SC_NOT_FOUND);
|
||||
return null;
|
||||
}
|
||||
|
||||
String dateKey = simpleDateFormat.format(new Date());
|
||||
int dailyVolume = dailySalesVolumeCent.getOrDefault(dateKey,0);
|
||||
|
||||
Article a = s.findArticle(order.manufacturer, order.articleNumber);
|
||||
if(a == null) {
|
||||
res.setStatus(HttpServletResponse.SC_BAD_REQUEST);
|
||||
return null;
|
||||
}
|
||||
|
||||
int priceNet = a.pricePerUnitNet * order.quantity;
|
||||
int discount = 0;
|
||||
if(dailyVolume >= s.discount.minimumDailySalesVolumeNetCent) {
|
||||
discount = (priceNet * s.discount.percentDiscount) / 100;
|
||||
}
|
||||
|
||||
OrderConfirmation confirmation = new OrderConfirmation();
|
||||
confirmation.articleNumber = order.articleNumber;
|
||||
confirmation.discountNetCent = discount;
|
||||
confirmation.pricePerUnitNetCent = a.pricePerUnitNet;
|
||||
confirmation.manufacturer = a.manufacturer;
|
||||
confirmation.quantity = order.quantity;
|
||||
confirmation.totalPriceNetCharged = priceNet - discount;
|
||||
|
||||
if (confirmation.totalPriceNetCharged > order.maxTotalPriceCentNet) {
|
||||
res.setStatus(HttpServletResponse.SC_EXPECTATION_FAILED);
|
||||
return null;
|
||||
}
|
||||
|
||||
dailyVolume += confirmation.totalPriceNetCharged;
|
||||
dailySalesVolumeCent.put(dateKey, dailyVolume);
|
||||
|
||||
return confirmation;
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
package org.hso.ecommerce.supplier.data;
|
||||
|
||||
public class Article {
|
||||
public String title;
|
||||
public String manufacturer;
|
||||
public String articleNumber;
|
||||
|
||||
public int vatPercent;
|
||||
public int pricePerUnitNet;
|
||||
public boolean shouldBeAdvertised;
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package org.hso.ecommerce.supplier.data;
|
||||
|
||||
public class Order {
|
||||
public String manufacturer;
|
||||
public String articleNumber;
|
||||
|
||||
public int quantity;
|
||||
public int maxTotalPriceCentNet;
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
package org.hso.ecommerce.supplier.data;
|
||||
|
||||
public class OrderConfirmation {
|
||||
public String manufacturer;
|
||||
public String articleNumber;
|
||||
|
||||
public int quantity;
|
||||
|
||||
public int pricePerUnitNetCent;
|
||||
public int discountNetCent;
|
||||
public int totalPriceNetCharged;
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package org.hso.ecommerce.supplier.data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Supplier {
|
||||
|
||||
public String id;
|
||||
public String name;
|
||||
public SupplierDiscount discount;
|
||||
public List<Article> articles;
|
||||
|
||||
public Article findArticle(String manufacturer, String articleNumber) {
|
||||
for(Article a : articles) {
|
||||
if(a.manufacturer.equals(manufacturer) && a.articleNumber.equals(articleNumber)) {
|
||||
return a;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package org.hso.ecommerce.supplier.data;
|
||||
|
||||
public class SupplierDiscount {
|
||||
public int minimumDailySalesVolumeNetCent;
|
||||
public int percentDiscount;
|
||||
}
|
2
supplier/src/main/resources/application.properties
Normal file
2
supplier/src/main/resources/application.properties
Normal file
@ -0,0 +1,2 @@
|
||||
server.address=::1
|
||||
server.port=8081
|
Reference in New Issue
Block a user