196 lines
7.0 KiB
Java
196 lines
7.0 KiB
Java
/**
|
|
* license: GPLv3
|
|
edu.inventorym
|
|
*/
|
|
package edu.inventorym;
|
|
|
|
import java.time.ZonedDateTime;
|
|
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
import edu.inventorym.model.Customer;
|
|
import edu.inventorym.model.CustomerManager;
|
|
import edu.inventorym.model.Drawing;
|
|
import edu.inventorym.model.InventoryList;
|
|
import edu.inventorym.model.InventoryManager;
|
|
import edu.inventorym.model.InventoryPiece;
|
|
import edu.inventorym.model.Painting;
|
|
import edu.inventorym.model.Print;
|
|
import edu.inventorym.model.Sculpture;
|
|
import edu.inventorym.model.Transaction;
|
|
import edu.inventorym.model.TransactionList;
|
|
import edu.inventorym.model.TransactionManager;
|
|
import io.swagger.v3.oas.annotations.Operation;
|
|
import io.swagger.v3.oas.annotations.responses.ApiResponse;
|
|
|
|
@RestController
|
|
@RequestMapping("/api")
|
|
public class ApiController { // SINGLETON
|
|
|
|
@Operation(summary = "Get status Inventory Loaded", description = "Returns status Inventory Loaded.")
|
|
@ApiResponse(responseCode = "200", description = "loaded")
|
|
@GetMapping
|
|
public static ResponseEntity<String> getInventory() {
|
|
/* Inventory pieces */
|
|
InventoryManager.getInstance().load();
|
|
if (InventoryManager.getInstance().INVENTORY.isEmpty()) {
|
|
/* Add Inventory */
|
|
InventoryManager.getInstance().add(make(new Drawing()));
|
|
InventoryManager.getInstance().add(make(new Print()));
|
|
InventoryManager.getInstance().add(make(new Painting()));
|
|
InventoryManager.getInstance().add(make(new Sculpture()));
|
|
InventoryManager.getInstance().add(make(new Sculpture()));
|
|
InventoryManager.getInstance().add(make(new Sculpture()));
|
|
|
|
InventoryManager.getInstance().save();
|
|
}
|
|
|
|
/* Customers */
|
|
CustomerManager.getInstance().load();
|
|
if (CustomerManager.getInstance().CUSTOMERS.isEmpty()) {
|
|
/* Add Customer */
|
|
CustomerManager.getInstance().add(new Customer("Kate", "Demsey", "310-676-4844", "kate@museum.com"));
|
|
CustomerManager.getInstance().add(new Customer("Jim", "Gumbly", "203-676-4844", "jim@homedeco.com"));
|
|
CustomerManager.getInstance().add(new Customer("Evan", "Hao", "203-676-8943", "evan@homedeco.com"));
|
|
|
|
/* Update Customer List */
|
|
CustomerManager.getInstance().save();
|
|
|
|
InventoryList customerCart = new InventoryList();
|
|
customerCart.add(InventoryManager.getInstance().INVENTORY.getFirst());
|
|
customerCart.add(InventoryManager.getInstance().INVENTORY.getLast());
|
|
|
|
enterTransaction(CustomerManager.getInstance().findByEmail("kate@museum.com"), customerCart);
|
|
}
|
|
|
|
InventoryManager imgr = InventoryManager.getInstance();
|
|
|
|
return new ResponseEntity<>("Resource loaded! \n" + String.format("Art Pieces in INVENTORY: count %d", imgr.INVENTORY.size())
|
|
, HttpStatus.OK);
|
|
}
|
|
|
|
static void enterTransaction(Customer customer, InventoryList customerCart){
|
|
|
|
TransactionManager tmgr = TransactionManager.getInstance();
|
|
tmgr.load();
|
|
/*
|
|
* 3. Add a new transaction object to the list manager is handling
|
|
*/
|
|
Transaction tran = tmgr.TransactRequest(customer, customerCart);
|
|
/*
|
|
* 4. Calculate and return transaction price
|
|
* 5. Complete transaction
|
|
|
|
*/
|
|
tmgr.compute(tran);
|
|
tmgr.complete(tran);
|
|
/*
|
|
* 9. Update inventory file(s) on local system
|
|
*/
|
|
InventoryManager.getInstance().save();
|
|
/*
|
|
* 10. Update transaction file(s) on local system
|
|
*/
|
|
TransactionManager.getInstance().save();
|
|
}
|
|
|
|
static void callerInterface() {
|
|
// @Comment
|
|
/*
|
|
* 1. Add new art object to the inventory. Your manager must check that the art
|
|
* is not already in the inventory before adding
|
|
*/
|
|
|
|
/*
|
|
* 2. Remove art object from the inventory
|
|
*/
|
|
InventoryManager imgr = InventoryManager.getInstance();
|
|
System.out.println(String.format("Art Pieces in INVENTORY: count %d", imgr.INVENTORY.size()));
|
|
InventoryPiece art = imgr.INVENTORY.getLast();
|
|
InventoryPiece piece = imgr.find(art.getId());
|
|
imgr.Remove(piece);
|
|
imgr.save();
|
|
System.out.println(String.format("Removed 1 Art Pieces from INVENTORY: count %d", imgr.INVENTORY.size()));
|
|
/*
|
|
* 3. Return a list of all art objects from inventory
|
|
*/
|
|
System.out.println("============= Inventory List ===============");
|
|
imgr.INVENTORY.forEach(System.out::println);
|
|
System.out.println("\n");
|
|
|
|
if (imgr.INVENTORY.isEmpty()) {
|
|
System.out.println(String.format("%s: No Inventoried Art Piecies Available !!!. ", ApiController.class));
|
|
return;
|
|
}
|
|
|
|
Transaction tran = null;
|
|
TransactionManager tmgr = null;
|
|
|
|
//* 6. Remove transaction object
|
|
// tmgr.remove(tran);
|
|
/*
|
|
* 7. Format and return transaction data in print format
|
|
*/
|
|
System.out.println(tran);
|
|
/*
|
|
* 8. Retrieve and return transactions based on:
|
|
* customer email
|
|
*/
|
|
Customer customer = CustomerManager.getInstance().findByEmail("kate@museum.com");
|
|
TransactionList foundTrans = tmgr.findByCustomerEmail(customer.getEmail());
|
|
/*
|
|
* transaction date
|
|
*/
|
|
foundTrans = tmgr.findByTransactionDate(tran.getCompleted());
|
|
/*
|
|
* transaction id
|
|
*/
|
|
foundTrans = tmgr.findByTransactionId(tran.getId());
|
|
/*
|
|
* art id
|
|
*/
|
|
foundTrans = tmgr.findByTransactionId(tran.getId());
|
|
}
|
|
|
|
static Drawing make(Drawing o) {
|
|
o.setTitle("Windy Rowing");
|
|
o.setDescription("East Market Square find. Local Author.");
|
|
o.setAuthor("Dave Janson");
|
|
o.setPrice(92.0f);
|
|
o.setCreated(ZonedDateTime.now());
|
|
return o;
|
|
}
|
|
|
|
static Painting make(Painting o) {
|
|
o.setTitle("Walking by the beach.");
|
|
o.setDescription("East Market Square find. Local Author.");
|
|
o.setAuthor("Dave Janson");
|
|
o.setPrice(51.0f);
|
|
o.setCreated(ZonedDateTime.now());
|
|
return o;
|
|
}
|
|
|
|
static Print make(Print o) {
|
|
o.setTitle("Different day same thing");
|
|
o.setDescription("East Market Square find. Local Author.");
|
|
o.setAuthor("Dave Janson");
|
|
o.setPrice(102.0f);
|
|
o.setCreated(ZonedDateTime.now());
|
|
return o;
|
|
}
|
|
|
|
static Sculpture make(Sculpture o) {
|
|
o.setTitle("Jolly Romp");
|
|
o.setDescription("East Market Square find. Local Author.");
|
|
o.setAuthor("Dave Janson");
|
|
o.setPrice(32.0f);
|
|
o.setWeight(4.0F);
|
|
o.setCreated(ZonedDateTime.now());
|
|
return o;
|
|
}
|
|
}
|