Skip to content

Commit

Permalink
Merge pull request #44 from 3296Fall2020/tp-springBoot-sneaker-endpoint
Browse files Browse the repository at this point in the history
Tp spring boot sneaker endpoint
  • Loading branch information
tuc56947 authored Nov 17, 2020
2 parents 41586f3 + 2b49ab5 commit c78e3be
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SneakerController {
public class HelloController {

@RequestMapping(value="/", method= RequestMethod.GET)
public String hello(){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static void main(String[] args) { // main used for testing
//System.out.println(database.editSize("tony",11.5,1)); // true if done
//System.out.println(database.editPrice("tony",120,1)); // true if done
//System.out.println(database.editDate("tony","2020/12/25",1)); // true if done YYYY/MM/DD
System.out.println(database.editForm(1,"Tims",null,"123shoe",11,null,101.67,"Tony"));
//System.out.println(database.editForm(1,"Tims",null,"123shoe",11,null,101.67,"Tony"));
//--------------------------------------------------------------
database.closeConnection(); // log off
}
Expand Down Expand Up @@ -123,9 +123,8 @@ public boolean addUser(String userID, String password) {
" shoeName VARCHAR(255) not NULL, " +
" sku VARCHAR(255) not NULL, " +
" size VARCHAR(255) not NULL, " +
" date VARCHAR(255) not NULL, " +
" price VARCHAR(255) not NULL, " +
" user_id VARCHAR(20)," +
" user_id VARCHAR(20) not NULL," +
" PRIMARY KEY (index_id),"+
" FOREIGN KEY ( user_id ) REFERENCES user_table (user_id))");
} catch(SQLException throwables){ throwables.printStackTrace();return false;} // this should never happen
Expand Down Expand Up @@ -210,33 +209,41 @@ public String querySessionID(String userID){
}
//---------------------------------------------------------------------------- inventory functions

public boolean insertData(String shoeName, String sku, String size , String date, String price, String userID) // first entry needs to be entered via this method
public boolean insertData(String shoeName, String sku, String size , String price, String userID) // first entry needs to be entered via this method
{
String tableName = userID.toLowerCase() + "_inventory";
try {
statement = connect.createStatement();
statement.executeUpdate("INSERT INTO "+ tableName + " "+
"VALUES (" +shoeName+"','" +sku+"',"+size+",'"+date+"', "+price+",'"+userID+"')" );
statement.executeUpdate("INSERT INTO "+ tableName + " (shoeName, sku, size, price, user_id) "+
"VALUES ('" +shoeName+"', '" +sku+"', '"+size+"', '"+price+"', '"+userID+"' )" );


}catch(SQLException throwables){
throwables.printStackTrace();
return false;}
return true;
}

boolean editForm(int index, String brand, String model, String sku, double size , String date, double price, String userID) // edits rows that are already created, insert null/0 for unwanted string/number changes
public boolean editForm(String index, String shoeName, String sku, String size, String price, String userID){
String tableName = userID.toLowerCase() + "_inventory";
try {
statement = connect.createStatement();
statement.executeUpdate("UPDATE "+ tableName + " SET shoeName = '" + shoeName + "', sku = '" + sku + "', size = '" + size
+ "', price = '" + price + "' WHERE index_id = '" + index + "'");
}catch(SQLException throwables){
throwables.printStackTrace();
return false;}
return true;
}

/*public boolean editForm(String index, String sneakerName, String sku, String size , String date, double price, String userID) // edits rows that are already created, insert null/0 for unwanted string/number changes
{
int isDone = 0; //false
if(brand != null){
editBrand(userID,brand,index);
isDone++;
}

if(model != null){
editModel(userID,model,index);
isDone++;
}

if(sku != null){
editSKU(userID,sku,index);
isDone++;
Expand All @@ -259,9 +266,9 @@ boolean editForm(int index, String brand, String model, String sku, double size
if(isDone>0){return true;}
else {return false;}
}
}*/

boolean editBrand( String userID, String brandName, int index) {
boolean editBrand( String userID, String brandName, String index) {
String tableName = userID.toLowerCase() + "_inventory";
try {
statement = connect.createStatement();
Expand All @@ -276,7 +283,7 @@ boolean editBrand( String userID, String brandName, int index) {

}

boolean editModel(String userID, String model, int index){
boolean editModel(String userID, String model, String index){
String tableName = userID.toLowerCase() + "_inventory";
try {
statement = connect.createStatement();
Expand All @@ -290,7 +297,7 @@ boolean editModel(String userID, String model, int index){
}
}

boolean editSKU(String userID, String sku, int index){
boolean editSKU(String userID, String sku, String index){
String tableName = userID.toLowerCase() + "_inventory";
try {
statement = connect.createStatement();
Expand All @@ -304,7 +311,7 @@ boolean editSKU(String userID, String sku, int index){
}
}

boolean editSize(String userID, double size, int index){
boolean editSize(String userID, String size, String index){
String tableName = userID.toLowerCase() + "_inventory";
try {
statement = connect.createStatement();
Expand All @@ -318,7 +325,7 @@ boolean editSize(String userID, double size, int index){
}
}

boolean editDate(String userID, String date, int index){
boolean editDate(String userID, String date, String index){
String tableName = userID.toLowerCase() + "_inventory";
try {
statement = connect.createStatement();
Expand All @@ -331,7 +338,7 @@ boolean editDate(String userID, String date, int index){
return false;
}}

boolean editPrice(String userID, double price, int index){
boolean editPrice(String userID, double price, String index){
String tableName = userID.toLowerCase() + "_inventory";
try {
statement = connect.createStatement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,6 @@ public LoginResponse sessionResponse(@RequestBody String userID) throws SQLExcep
return new LoginResponse(operation.querySessionID("NaN"));
}

@ResponseBody
@RequestMapping(value = "/addSneaker", method = RequestMethod.POST)
public LoginResponse sessionResponse(@RequestBody Sneaker sneaker){
DatabaseOperation operation = new DatabaseOperation();
operation.createConnect();
boolean createSneaker = operation.insertData(sneaker.getSneakerName(), sneaker.getSku(), sneaker.getSize(), sneaker.getDate(), sneaker.getPrice(), sneaker.getUserID());
if (createSneaker){
return new LoginResponse("SessionID");
}
return new LoginResponse("NaN");
}



Expand Down
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
package com.company.sneakerinvetory.sneaker;

public class Sneaker {
String userID;
String SneakerName;
String Sku;
String index;
String id;
String shoeName;
String sku;
String size;
String date;
String price;

public Sneaker(String userID, String sneakerName, String Sku, String size, String price){
this.userID = userID;
this.SneakerName = sneakerName;
this.Sku = Sku;
public Sneaker(String id, String shoeName, String sku, String size, String price){
this.id = id;
this.shoeName = shoeName;
this.sku = sku;
this.size = size;
this.price = price;
}

public String getUserID() {
return userID;
public String getIndex() {
return index;
}

public void setUserID(String userID) {
this.userID = userID;
public void setIndex(String index) {
this.index = index;
}

public String getSneakerName() {
return SneakerName;
public String getid() {
return id;
}

public void setSneakerName(String sneakerName) {
SneakerName = sneakerName;
public void setId(String id) {
this.id = id;
}

public String getSku() {
return Sku;
public String getShoeName() {
return shoeName;
}

public void setSku(String sku) {
Sku = sku;
public void setShoeName(String sneakerName) {
shoeName = sneakerName;
}

public String getSize() {
return size;
public String getSku() {
return sku;
}

public String getDate() {
return date;
public void setSku(String sku) {
sku = sku;
}

public void setDate(String date) {
this.date = date;
public String getSize() {
return size;
}

public void setSize(String size) {
Expand All @@ -63,4 +63,17 @@ public String getPrice() {
public void setPrice(String price) {
this.price = price;
}

public static boolean validateAddSneaker(Sneaker sneaker) {
return sneaker.getid() != null && sneaker.getPrice() != null && sneaker.getSize() != null && sneaker.getSku() != null && sneaker.getShoeName() != null;

}
public static boolean validateEditSneaker(Sneaker sneaker){
return sneaker.getIndex() != null && sneaker.getid() != null && sneaker.getPrice() != null && sneaker.getSize() != null && sneaker.getSku() != null && sneaker.getShoeName() != null;
}

public static void printSneaker(Sneaker sneaker){
System.out.println(sneaker.getIndex() + "\t" + sneaker.getid() + "\t" + sneaker.getShoeName() + "\t" + sneaker.getSku() + "\t" +
sneaker.getSize() + "\t" + sneaker.getPrice());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.company.sneakerinvetory.sneaker;

import com.company.sneakerinvetory.MySQLConnction.DatabaseOperation;
import com.company.sneakerinvetory.login.LoginResponse;
import org.springframework.web.bind.annotation.*;

//curl -i -H "Accept: application/json" -H "Content-Type:application/json" -X POST --data '{ "index": "1", "id" : "tester", "SneakerName" : "yeezy 350 v2", "Sku" : "fu9006", "size" : "11", "price" : "$350" }' "http://localhost:8080/editSneaker"
@RestController
public class SneakerController {

@ResponseBody
@RequestMapping(value = "/addSneaker", method = RequestMethod.POST)
public LoginResponse handleAddSneaker(@RequestBody Sneaker sneaker){
DatabaseOperation operation = new DatabaseOperation();
operation.createConnect();
boolean valid = Sneaker.validateAddSneaker(sneaker);
if (valid) {
boolean createSneaker = operation.insertData(sneaker.getShoeName(), sneaker.getSku(), sneaker.getSize(), sneaker.getPrice(), sneaker.getid());
if (createSneaker) {
return new LoginResponse("SessionID");
}
}
return new LoginResponse("NaN");
}

@ResponseBody
@RequestMapping(value = "/editSneaker", method = RequestMethod.POST)
public LoginResponse handleEditSneaker(@RequestBody Sneaker sneaker){
DatabaseOperation operation = new DatabaseOperation();
operation.createConnect();
boolean valid = Sneaker.validateEditSneaker(sneaker);
if (valid) {
boolean createSneaker = operation.editForm(sneaker.getIndex(), sneaker.getShoeName(), sneaker.getSku(), sneaker.getSize(), sneaker.getPrice(), sneaker.getid());
if (createSneaker) {

return new LoginResponse("SessionID");
}
}
return new LoginResponse("NaN");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.company.sneakerinvetory.sneaker;

public class SneakerResponse {

}

0 comments on commit c78e3be

Please sign in to comment.