Skip to content

Instantly share code, notes, and snippets.

@madsunrise
Created December 10, 2018 07:22
Show Gist options
  • Save madsunrise/9b07929cd66e5db22b4923dfa7e35833 to your computer and use it in GitHub Desktop.
Save madsunrise/9b07929cd66e5db22b4923dfa7e35833 to your computer and use it in GitHub Desktop.
package com.rv150.bestbeforeserver.main;
import com.google.gson.Gson;
import com.rv150.bestbeforeserver.dao.BarcodeDAO;
import com.rv150.bestbeforeserver.dao.ProductDAO;
import com.rv150.bestbeforeserver.dao.RequestsDAO;
import com.rv150.bestbeforeserver.dao.UserDAO;
import com.rv150.bestbeforeserver.exception.AlreadyExistException;
import com.rv150.bestbeforeserver.model.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
/**
* Created by Rudnev on 20.10.2016.
*/
@SuppressWarnings("ALL")
@Controller
@RequestMapping(path = "/v1")
public class BarcodeController {
private static final Logger LOGGER = LoggerFactory.getLogger(BarcodeController.class.getName());
private final UserDAO userDAO;
private final BarcodeDAO barcodeDAO;
private final ProductDAO productDAO;
private final RequestsDAO requestsDAO;
// Just for fun
private int barcodeHits = 0;
public BarcodeController(UserDAO userDAO, BarcodeDAO barcodeDAO, ProductDAO productDAO, RequestsDAO requestsDAO) {
this.userDAO = userDAO;
this.barcodeDAO = barcodeDAO;
this.productDAO = productDAO;
this.requestsDAO = requestsDAO;
}
@RequestMapping(path = "/hits", method = RequestMethod.GET)
public ResponseEntity getBarcodesHist() {
return ResponseEntity.ok(barcodeHits);
}
@RequestMapping(path = "/transfer_finished", method = RequestMethod.GET)
public String markTransferFinished() {
return "okay";
}
@RequestMapping(path = "/barcode", method = RequestMethod.GET)
public ResponseEntity getBarcodes(@RequestParam String barcode) {
LOGGER.info("Requesting product with code = {}", barcode);
Product product = productDAO.getByCode(barcode);
if (product == null) {
LOGGER.info("Ooops! Not found!");
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(new BarcodeResponse());
}
else {
barcodeHits++;
LOGGER.info("Yes! This code belongs to {}", product.getName());
return ResponseEntity.ok(new BarcodeResponse(barcode, product.getName()));
}
}
@RequestMapping(path = "/barcode", method = RequestMethod.POST)
public ResponseEntity addBarcodes(@RequestBody String inputData) {
LOGGER.info("Handled upload data request");
BarcodeRequest request = new Gson().fromJson(inputData, BarcodeRequest.class);
User user = userDAO.getOrInsert(request.getDeviceId());
List<Product> products = new ArrayList<>();
for (ClientBarcodeModel model: request.getBarcodeList()) {
Product product = new Product();
product.setAuthor(user.getId());
product.setName(model.name);
product.getBarcodes().add(new Barcode(model.code));
products.add(product);
}
List<Product> conflictingProducts = new ArrayList<>();
for (Product product : products) {
try {
if (productDAO.insert(product)) {
LOGGER.info("New barcode ({}) was successful added by user {}!", product.getName(), product.getAuthor());
} else {
LOGGER.error("Failed to insert barcode!");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(new ArrayList<>());
}
} catch (AlreadyExistException ex) {
LOGGER.info("Barcode with code {} already exists! Checking author...", product.getBarcodes().get(0).getCode());
Product existing = productDAO.getByCode(product.getBarcodes().get(0).getCode());
if (existing.getAuthor() == product.getAuthor()) {
LOGGER.info("You have grants to change this barcode's name to {}" + product.getName());
product.setId(existing.getId());
productDAO.update(product);
}
else {
conflictingProducts.add(existing);
}
}
}
return ResponseEntity.ok(conflictingProducts);
}
@RequestMapping(path = "/user_status", method = RequestMethod.GET)
public ResponseEntity getMyProductsCount(@RequestParam(name = "device_id") String deviceId) {
LOGGER.info("Requesting user status for deviceID = " + deviceId);
User user = userDAO.getByDeviceId(deviceId);
int productsCount;
if (user == null) {
user = userDAO.insert(deviceId);
LOGGER.info("This is new user!", deviceId);
productsCount = 0;
}
else {
productsCount = productDAO.getUserProductsCount(user.getId());
LOGGER.info("User {} already exists and he has {} products", deviceId, productsCount);
}
long joined = Calendar.getInstance().getTimeInMillis(); // disabling trial
return ResponseEntity.ok(new UserStatusResponse(0, joined));
}
public static class BarcodeRequest {
private String deviceId;
private List<ClientBarcodeModel> barcodeList;
public BarcodeRequest() {
}
public String getDeviceId() {
return deviceId;
}
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
public List<ClientBarcodeModel> getBarcodeList() {
return barcodeList;
}
public void setBarcodeList(List<ClientBarcodeModel> barcodeList) {
this.barcodeList = barcodeList;
}
}
public static class ClientBarcodeModel {
private String code;
private String name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment