diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index 6774fba..c8b4141 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -11,4 +11,7 @@ + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 0000000..e96534f --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,124 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index 39b72bc..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,58 +0,0 @@ - - - - - - - - - - - - - - - - - - - - { - "keyToString": { - "RunOnceActivity.OpenProjectViewOnStart": "true", - "RunOnceActivity.ShowReadmeOnStart": "true", - "SHARE_PROJECT_CONFIGURATION_FILES": "true" - } -} - - - - - 1653433046840 - - - - - - - \ No newline at end of file diff --git a/README.md b/README.md index 7db84fe..3108a54 100644 --- a/README.md +++ b/README.md @@ -1 +1,42 @@ -This is maxwell moord's read me page, its not done \ No newline at end of file +# Project 1: Pi 2 a 1000 Places + +# Desctiption +Our Gourmet Pizza Restaurant app was developed for a small restaurant that is looking to help streamline the ordering process for customers to help reduce costs. Customers can enjoy an seamless means to order directly with an account of their own at the restaurant in question, where Admins are capable of adding and updating the menu items. This RESTful application leverages a tomcat server to handle incoming requests to thoroughly tested services that must be persisted using Hibernate and Azure SQL services. This project includes a front-end that will be developed using ReactJS to handle user requests. + +# User Stories + +## As A Admin: + +- [ ] Add items to the menu +- [ ] Update items to the menu +- [ ] Delete items to the menu + + +## As A: Customer + +- [ ] View all items on the menu without needing to Register or Login +- [ ] Register/Update/Delete an account +- [ ] Add/Update/Delete a credit card to be saved to my account +- [ ] Make an order for a specific menu item +- [ ] Add a comment to the order to request a change, if it is substitutable +- [ ] Favorite an order +- [ ] View past orders by date +- [ ] Pay off your balance with your credit card + + +## Tech Stack + +- [ ] Java 8 +- [ ] JUnit +- [ ] Mockito +- [ ] Apache Maven +- [ ] Hibernate +- [ ] Jackson library (for JSON marshalling/unmarshalling) +- [ ] Java EE Servlet API (v4.0+) +- [ ] Azure SQL +- [ ] Git SCM (on GitHub) + +## Collaborators +- Maxwell Moord +- Tenyce Melrose +- Rodney Lewis diff --git a/pom.xml b/pom.xml index 94caf7b..488f681 100644 --- a/pom.xml +++ b/pom.xml @@ -14,6 +14,7 @@ 8 + @@ -32,6 +33,8 @@ + + @@ -56,7 +59,6 @@ test - javax.servlet @@ -86,7 +88,26 @@ 5.4.32.Final + + + com.microsoft.sqlserver + mssql-jdbc + 10.2.1.jre8 + + + org.junit.jupiter + junit-jupiter-api + 5.8.2 + test + + + + org.junit.jupiter + junit-jupiter-api + 5.8.2 + test + - + - \ No newline at end of file + diff --git a/src/main/java/com/revature/Pi2a1000Places/auth/AuthServlet.java b/src/main/java/com/revature/Pi2a1000Places/auth/AuthServlet.java new file mode 100644 index 0000000..c94ea8c --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/auth/AuthServlet.java @@ -0,0 +1,63 @@ +package com.revature.Pi2a1000Places.auth; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.revature.Pi2a1000Places.customer.Customer; +import com.revature.Pi2a1000Places.customer.CustomerServices; +import com.revature.Pi2a1000Places.util.exceptions.AuthenticationException; +import com.revature.Pi2a1000Places.util.exceptions.InvalidRequestException; +import com.revature.Pi2a1000Places.util.interfaces.Authable; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.io.IOException; + + +public class AuthServlet extends HttpServlet { + + private final CustomerServices customerServices; + + private final ObjectMapper mapper; + + public AuthServlet(CustomerServices customerServices, ObjectMapper mapper){ + this.customerServices = customerServices; + this.mapper = mapper; + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { + + try { + + LoginCreds loginCreds = mapper.readValue(req.getInputStream(), LoginCreds.class); + + Customer authCustomer = customerServices.authenticateCustomer(loginCreds.getUsername(), loginCreds.getPassword()); + + + + HttpSession httpSession = req.getSession(true); + httpSession.setAttribute("authCustomer", authCustomer); + + + resp.getWriter().write("You have successfully logged in!"); + } catch (AuthenticationException | InvalidRequestException e){ + resp.setStatus(404); + resp.getWriter().write(e.getMessage()); + } catch (Exception e){ + resp.setStatus(409); + resp.getWriter().write(e.getMessage()); + } + } + + @Override + protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.addHeader("Access-Control-Allow-Origin", "*"); + resp.addHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE"); + resp.addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization"); + req.getSession().invalidate(); + resp.getWriter().write("User has logged out!"); + } + +} diff --git a/src/main/java/com/revature/Pi2a1000Places/auth/LoginCreds.java b/src/main/java/com/revature/Pi2a1000Places/auth/LoginCreds.java new file mode 100644 index 0000000..3e1e50c --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/auth/LoginCreds.java @@ -0,0 +1,25 @@ +package com.revature.Pi2a1000Places.auth; + +public class LoginCreds { + + private static String username; + private static String password; + + // JACKSON REQUIRES A NO ARG CONSTRUCTOR + + public static String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public static String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } +} diff --git a/src/main/java/com/revature/Pi2a1000Places/creditcard/CreditCard.java b/src/main/java/com/revature/Pi2a1000Places/creditcard/CreditCard.java new file mode 100644 index 0000000..f5f6717 --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/creditcard/CreditCard.java @@ -0,0 +1,102 @@ +package com.revature.Pi2a1000Places.creditcard; + +public class CreditCard { + //CC value setup - initializing + private String ccNumber; + private String ccName; + private int cvv; + private String expDate; + private int zip; + private int limit; + private String userName; + +//Creating Constructors for the CC Class + + + public CreditCard(String ccNumber, String ccName, int cvv, String expDate, int zip, int limit, String userName) { + this.ccNumber = ccNumber; + this.ccName = ccName; + this.cvv = cvv; + this.expDate = expDate; + this.zip = zip; + this.limit = limit; + this.userName = userName; + } + + public CreditCard() { + + } + + //Generate Getters/Setters + public String getCCNumber() { + return ccNumber; + } + + public void setCCNumber(String ccNumber) { + this.ccNumber = ccNumber; + } + + public String getCCName() { + return ccName; + } + + public void setCCName(String ccName) { + this.ccName = ccName; + } + + public int getCvv() { + return cvv; + } + + public void setCvv(int cvv) { + this.cvv = cvv; + } + + public String getExpDate() { + return expDate; + } + + public void setExpDate(String expDate) { + this.expDate = expDate; + } + + public int getZip() { + return zip; + } + + public void setZip(int zip) { + this.zip = zip; + } + + public int getLimit() { + return limit; + } + + public void setLimit(int limit) { + this.limit = limit; + } + + public String getUserName() { + return userName; + } + + public void setUserName(String userName) { + this.userName = userName; + } + + @Override + public String toString() { + return "CreditCard{" + + "ccNumber='" + ccNumber + '\'' + + ", ccName='" + ccName + '\'' + + ", cvv='" + cvv + '\'' + + ", expDate='" + expDate + '\'' + + ", zip='" + zip + '\'' + + ", limit='" + limit + '\'' + + ", userName='" + userName + '\'' + + '}'; + } + + public void setCCardNumber(String s) { + } +} \ No newline at end of file diff --git a/src/main/java/com/revature/Pi2a1000Places/creditcard/CreditCardDao.java b/src/main/java/com/revature/Pi2a1000Places/creditcard/CreditCardDao.java new file mode 100644 index 0000000..0eb0255 --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/creditcard/CreditCardDao.java @@ -0,0 +1,168 @@ +//package com.revature.Pi2a1000Places.creditcard; +// +//import com.revature.Pi2a1000Places.creditcard.CreditCardServices; +//import com.revature.Pi2a1000Places.util.ConnectionFactory; +//import com.revature.Pi2a1000Places.util.HibernateUtil; +//import org.hibernate.HibernateException; +//import org.hibernate.Session; +//import org.hibernate.SessionFactory; +//import org.hibernate.Transaction; +// +//import com.revature.Pi2a1000Places.util.HibernateUtil; +// +//import java.io.IOException; +// +//public class CreditCardDao extends CreditCardServices { +// //Create creditCard +// public static CreditCard create(Object CreditCard) { +// +// CreditCard creditcard = new CreditCard(); +// creditcard.setCCNumber("7878"); +// creditcard.setCCName("Test"); +// creditcard.setCvv(Integer.parseInt("888")); +// creditcard.setExpDate("12/12/28"); +// creditcard.setZip(Integer.parseInt("64646")); +// creditcard.setLimit(Integer.parseInt("55000")); +// creditcard.setUserName("Name"); +// +// try { +// SessionFactory sessionFactory = HibernateUtil.getSession().getSessionFactory(); +// Session session = sessionFactory.openSession(); +// session.beginTransaction(); +// session.save(CreditCard); +// return new CreditCard(); +// session.getTransaction().commit(); +// }catch (HibernateException){ +// } catch (IOException e) { +// throw new RuntimeException(e); +// } +// System.out.println("CC Added"); +// return null; +// session.close(); +// sessionFactory.close(); +// +// { +// +//// //Update creditCard +//// public boolean update(CreditCard updatedCreditCard){ +//// +//// Session session; +//// try { +//// sessionFactory = HibernateUtil.getSession().getSessionFactory(); +//// session = sessionFactory.openSession(); +//// session.beginTransaction(); +//// session.update(creditcard); +//// return updatedCreditCard; +//// //creditCard.setCCName() ("Test") +//// session.getTransaction().commit(); +//// } catch (HibernateException) { +//// } catch (IOException e) { +//// throw new RuntimeException(e); +//// +//// System.out.println("CC Updated"); +//// return CreditCard; +//// session.close(); +//// sessionFactory.close(); +//// //where does return statement go? +// +// } +// +// //Delete creditCard +// public Boolean delete (String ccNumber){ +// try { +// Session session = HibernateUtil.getSession(); +// Transaction transaction = session.beginTransaction(); +// CreditCard creditCard = session.load(CreditCard.class,"creditCard"); +// session.delete(creditCard); +// transaction.commit(); +// } catch (HibernateException | IOException e) { +// System.out.println("CC Deleted"); +// } finally { +// HibernateUtil.closeSession(); +// } +// } +// +// +//} + +// MVP a. Create - ADD Card +// public CreditCard addCC(String ccNumber, String ccName, String cvv, String expDate, String zip, String limits, String customerUsername) { +// try(Connection conn = ConnectionFactory.getInstance().getConnection();){ +// String sql = "insert into credit_card values (?, ?, ?, ?, ?, ?, ?)"; +// PreparedStatement ps = conn.prepareStatement(sql); +// +// ps.setString(1,ccNumber); +// ps.setString(2, ccName); +// ps.setString(3, cvv); +// ps.setString(4, expDate); +// ps.setString(5, zip); +// ps.setString(6, limits); +// ps.setString(7, customerUsername); +// +// int checkInsert = ps.executeUpdate(); +// +// if (checkInsert == 0){ +// throw new RuntimeException(); +// } +// +// }catch (SQLException e){ +// e.printStackTrace(); +// return null; +// } +// return null; +// } +// +// +// //// MVP c. Delete - DELETE Card +// public boolean deleteByCCNumber(String ccNumber) { +// Connection conn = ConnectionFactory.getInstance().getConnection(); +// { +// String sql = "delete from credit_card where cc_number = ?"; +// +// try { +// PreparedStatement ps = conn.prepareStatement(sql); +// ps.setString(1, ccNumber); +// +// int checkInsert = ps.executeUpdate(); +// +// if (checkInsert == 0) { +// throw new RuntimeException(); +// } +// +// return true; +// } catch (SQLException e) { +// e.printStackTrace(); +// } +// +// } +// return false; +// } +//} +// +//// d. Pay off your balance with your credit card > Update (to the limit) +////4.5 (for credit card update and update limit see do get in trainer servlet ) +//} + +// public void update () { +// } +// +// public void update () { +// } +// +// public void update () { +// } +// +// public static void create () { +// } +// +// public static void createCC () { +// } +// +// public static void createCC () { +// } +// +// public static void createCC () { +// } +// +// public static void createCC () { +// } \ No newline at end of file diff --git a/src/main/java/com/revature/Pi2a1000Places/creditcard/CreditCardDto.java b/src/main/java/com/revature/Pi2a1000Places/creditcard/CreditCardDto.java new file mode 100644 index 0000000..3e08c20 --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/creditcard/CreditCardDto.java @@ -0,0 +1,70 @@ +package com.revature.Pi2a1000Places.creditcard; + +public class CreditCardDto { + //Generate Pass-in values + + + private String ccNumber; + private String ccName; + private String cvv; + private String expDate; + private String zip; + private String limits; + private String customerUsername; + + public String getCcNumber() { + return ccNumber; + } + + public void setCcNumber(String ccNumber) { + this.ccNumber = ccNumber; + } + + public String getCcName() { + return ccName; + } + + public void setCcName(String ccName) { + this.ccName = ccName; + } + + public String getCvv() { + return cvv; + } + + public void setCvv(String cvv) { + this.cvv = cvv; + } + + public String getExpDate() { + return expDate; + } + + public void setExpDate(String expDate) { + this.expDate = expDate; + } + + public String getZip() { + return zip; + } + + public void setZip(String zip) { + this.zip = zip; + } + + public String getLimits() { + return limits; + } + + public void setLimits(String limits) { + this.limits = limits; + } + + public String getCustomerUsername() { + return customerUsername; + } + + public void setCustomerUsername(String customerUsername) { + this.customerUsername = customerUsername; + } +} diff --git a/src/main/java/com/revature/Pi2a1000Places/creditcard/CreditCardServices.java b/src/main/java/com/revature/Pi2a1000Places/creditcard/CreditCardServices.java new file mode 100644 index 0000000..eb8f416 --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/creditcard/CreditCardServices.java @@ -0,0 +1,54 @@ +//package com.revature.Pi2a1000Places.creditcard; +// +//import javax.persistence.Entity; +//import javax.persistence.Table; +// +//import java.io.Serializable; +// +//import javax.persistence.*; +// +//public class CreditCardServices { +// private final CreditCardDao creditCardDao; +// +// @Id +// @Column(name="CCNUMBER") +// @GeneratedValue(strategy=GenerationType.AUTO) +// private int ccNumber; +// +// //@Override +// public CreditCard create(CreditCard newCreditCard){ +// return creditCardDao.create(new CreditCard()); +// } +// //@Override +// public CreditCard update(CreditCard updatedCreditCard) { +// if (creditCardDao.update(updatedCreditCard)) return null; +// +// return updatedCreditCard; +// } +// +// //Override +// public boolean delete() { +// return delete(null); +// } +// +// // Override +// public boolean delete(String ccNumber){ +// +// return true; +// } +// +// +// @Column(name="CCNAME") +// private String ccName; +// @Column(name="CVV") +// private int cVv; +// @Column(name="EXPDATE") +// private String expDate; +// @Column(name="ZIP") +// private int zip; +// @Column(name="LIMIT") +// private String limit; +// @Column(name="USERNAME") +// private String userName; +// +//} \ No newline at end of file diff --git a/src/main/java/com/revature/Pi2a1000Places/creditcard/CreditCardServlet.java b/src/main/java/com/revature/Pi2a1000Places/creditcard/CreditCardServlet.java new file mode 100644 index 0000000..aea895f --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/creditcard/CreditCardServlet.java @@ -0,0 +1,78 @@ +//package com.revature.Pi2a1000Places.creditcard; +// +//import com.fasterxml.jackson.databind.ObjectMapper; +//import com.revature.Pi2a1000Places.auth.LoginCreds; +// +//import javax.servlet.ServletException; +//import javax.servlet.http.HttpServlet; +//import javax.servlet.http.HttpServletRequest; +//import javax.servlet.http.HttpServletResponse; +//import java.io.IOException; +// +//public class CreditCardServlet extends HttpServlet{ +// Private final ObjectMapper mapper; +// private final CreditCardDao creditCardDao; +// +// public CreditCardServlet(ObjectMapper mapper) { +// this.mapper = mapper; +// } +// +// protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { +// super.doOptions(req, resp); +// resp.addHeader("Access-Control-Allow-Origin", "*"); +// resp.addHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE"); +// resp.addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization"); +// +// } +// +// //CREATE +// protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException { +//// addHeads(req, resp); +// resp.addHeader("Access-Control-Allow-Origin", "*"); +// resp.addHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE"); +// resp.addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization"); +// CreditCardDto pass = mapper.readValue(req.getInputStream(), CreditCardDto.class); +// CreditCard firstResult = CreditCardDao.createCC(pass.getCcNumber(), pass.getCcName(), pass.getCvv(), pass.getExpDate(), pass.getZip(), pass.getLimits(), pass.getCustomerUsername()); +// CreditCard theObject = CreditCardDao.followUpCreateCreditCard(pass.getCcNumber()); +// +// String payload = mapper.writeValueAsString(theObject); +// +// resp.getWriter().write("Credit Card Added \n"); +// resp.getWriter().write(payload); +// resp.setStatus(201); +// } +// +// // UPDATE +// protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws IOException { +// resp.addHeader("Access-Control-Allow-Origin", "*"); +// resp.addHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE"); +// resp.addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization"); +// CreditCardDto pass = mapper.readValue(req.getInputStream(), CreditCardDto.class); +// +// CreditCard firstResult = creditCardDao.update(pass.getTableSelection(), pass.getNewCellName(), pass.getCcNumber()); +// CreditCard theObject = creditCardDao.update(pass.getCcNumber()); +// +// String payload = mapper.writeValueAsString(theObject); +// +// resp.getWriter().write("Credit Card Updated \n"); +// resp.getWriter().write(payload); +// resp.setStatus(201); +// } +// +// //DELETE +// protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { +// resp.addHeader("Access-Control-Allow-Origin", "*"); +// resp.addHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE"); +// resp.addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization"); CreditCardDto pass = mapper.readValue(req.getInputStream(), CreditCardDto.class); +// +// boolean deleteTrue = creditCardDao.delete(pass.getCcNumber()); +// +// String payload = mapper.writeValueAsString(deleteTrue); +// +// resp.getWriter().write("Credit Card deleted \n"); +// resp.getWriter().write(payload); +// resp.setStatus(201); +// } +// +// +//} diff --git a/src/main/java/com/revature/Pi2a1000Places/customer/Customer.java b/src/main/java/com/revature/Pi2a1000Places/customer/Customer.java new file mode 100644 index 0000000..0271da4 --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/customer/Customer.java @@ -0,0 +1,69 @@ +package com.revature.Pi2a1000Places.customer; + +import javax.persistence.*; + +@Entity +@Table(name = "customer") +public class Customer { + + @Id @GeneratedValue + @Column(name = "username") + private String username; + @Column(name = "fname") + private String fname; + @Column(name = "lname") + private String lname; + @Column(name = "password") + private String password; + @Column(name = "balance") + private String balance; + + public Customer( String username, String password, String fname,String lname, String balance){ + super(); + this.fname = fname; + this.lname = lname; + this.balance = balance; + this.username = username; + this.password = password; + } + + public Customer(){ + + } + + //Getters And Setters + public String getFname(){return fname;} + + public void setFname(String fname){this.fname = fname;} + + public String getLname(){return lname;} + + public void setLname(String lname){this.lname = lname;} + + public String getBalance(){return balance;} + + public void setBalance(String balance){this.balance = balance;} + + public String getUsername(){return username;} + + public void setUsername(String username){this.username = username;} + + public String getPassword(){return password;} + + public void setPassword(String password){this.password = password;} + + + + + + @Override + public String toString() { + return "User Info{" + + "First Name ='" + fname + '\'' + + ", Last Name ='" + lname + '\'' + + '}'; + } + +} + + diff --git a/src/main/java/com/revature/Pi2a1000Places/customer/CustomerDao.java b/src/main/java/com/revature/Pi2a1000Places/customer/CustomerDao.java new file mode 100644 index 0000000..1f73a46 --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/customer/CustomerDao.java @@ -0,0 +1,169 @@ +package com.revature.Pi2a1000Places.customer; + +import com.revature.Pi2a1000Places.util.ConnectionFactory; +import com.revature.Pi2a1000Places.util.HibernateUtil; +import org.hibernate.HibernateException; +import org.hibernate.Session; +import org.hibernate.Transaction; + +import java.io.IOException; +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +public class CustomerDao { + + public static Customer create(Customer newUser) { + + System.out.println("Here is the newUser as it enters our DAO layer: " + newUser); // What happens here? Java knows to invoke the toString() method when printing the object to the terminal + + try (Connection conn = ConnectionFactory.getInstance().getConnection();) { + + String sql = "insert into customer (username, fname, lname, password) values (?, ?, ?, ?)"; + + PreparedStatement ps = conn.prepareStatement(sql); + + + ps.setString(1, newUser.getUsername()); + ps.setString(2, newUser.getFname()); + ps.setString(3, newUser.getLname()); + ps.setString(4, newUser.getPassword()); + + int checkInsert = ps.executeUpdate(); + + if (checkInsert == 0) { + throw new RuntimeException(); + } + + } catch (SQLException | RuntimeException e) { + e.printStackTrace(); + return null; + } + return newUser; + } + + + public Boolean pullUsernames(String username) { + try { + //System.out.println("Before Hibernate"); + Session session = HibernateUtil.getSession(); + //System.out.println("After Get Session"); + Transaction transaction = session.beginTransaction(); + + // System.out.println("After Transaction"); + Customer customer = session.get(Customer.class,username); + //System.out.println("After Session.get"); + transaction.commit(); + // System.out.println("After Transaction Commit"); + + if(customer !=null){ + return true; + }else return false; + + } catch (HibernateException | IOException e) { + e.printStackTrace(); + return null; + } finally { + HibernateUtil.closeSession(); + } + + } + + + + public Customer authenticateCustomer(String username, String password) { + + try (Connection conn = ConnectionFactory.getInstance().getConnection()) { + String sql = "select * from customer where username = ? and password = ?"; + PreparedStatement ps = conn.prepareStatement(sql); + ps.setString(1, username); + ps.setString(2, password); + + ResultSet rs = ps.executeQuery(); + + if (!rs.next()) { + return null; + } + + Customer customer = new Customer(); + + + customer.setUsername(rs.getString("username")); + customer.setFname(rs.getString("fname")); + customer.setLname(rs.getString("lname")); + customer.setPassword(rs.getString("password")); + + + return customer; + + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + + + } + + public String deleteCustomer(String username) { + + try (Connection conn = ConnectionFactory.getInstance().getConnection();) { + String sql = "delete from \"order\" where customer_username = ?"; + PreparedStatement ps = conn.prepareStatement(sql); + ps.setString(1, username); + + + int rs = ps.executeUpdate(); + System.out.println(rs); + + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + + try (Connection conn = ConnectionFactory.getInstance().getConnection();) { + String sql = "delete from customer where username = ?"; + PreparedStatement ps = conn.prepareStatement(sql); + ps.setString(1, username); + + + int rs = ps.executeUpdate(); + System.out.println(rs); + + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + String deletedAccount; + return deletedAccount = ("Account of " + username + " has been deleted \n"); + } + + public Customer updateCustomer(Customer customerToUpdate){ + String username = customerToUpdate.getUsername(); + String password = customerToUpdate.getPassword(); + + try { + Session session = HibernateUtil.getSession(); + + Transaction transaction = session.beginTransaction(); + session.update(username, customerToUpdate); + transaction.commit(); + + session.beginTransaction(); + Customer customer = session.get(Customer.class,username); + transaction.commit(); + + } catch (HibernateException | IOException e) { + e.printStackTrace(); + return null; + } finally { + HibernateUtil.closeSession(); + } + return customerToUpdate; + } + + + } + + + diff --git a/src/main/java/com/revature/Pi2a1000Places/customer/CustomerServices.java b/src/main/java/com/revature/Pi2a1000Places/customer/CustomerServices.java new file mode 100644 index 0000000..c56bce4 --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/customer/CustomerServices.java @@ -0,0 +1,85 @@ +package com.revature.Pi2a1000Places.customer; + +import com.revature.Pi2a1000Places.util.exceptions.AuthenticationException; +import com.revature.Pi2a1000Places.util.exceptions.InvalidRequestException; + +public class CustomerServices { + + private CustomerDao customerDao = new CustomerDao(); + private Customer customer = new Customer(); + + public CustomerServices(CustomerDao customerDao){this.customerDao = customerDao;} + + public CustomerServices() { + + } + + + //TODO:Exceptions to yell at the user + public boolean validateUserInput(Customer newUser) throws InvalidRequestException { + System.out.println("Validating User: " + newUser); + if(newUser == null) return false; + if(newUser.getFname() == null || newUser.getFname().trim().equals("")){ + throw new InvalidRequestException("First Name cannot be blank");} + if(newUser.getLname() == null || newUser.getLname().trim().equals("")){ + throw new InvalidRequestException("Last Name cannot be blank");} + if(newUser.getUsername() == null || newUser.getUsername().trim().equals("")){ + throw new InvalidRequestException("Username cannot be blank");} + if(newUser.getPassword() == null || newUser.getPassword().trim().equals("")){ + throw new InvalidRequestException("Password cannot be blank");} + System.out.println("The User Has been Validated"); + if (verifyNewUsername(newUser.getUsername()) == true) { + + throw new InvalidRequestException("That username has already been taken"); + }else{ + createNewUser(newUser); + return true; + } + } + + public boolean verifyNewUsername(String username){ + return customerDao.pullUsernames(username); + } + + + public Customer createNewUser(Customer newUser){ + System.out.println("New user being created " + newUser); + + return CustomerDao.create(newUser); + } + + + //Tells the authServlet that everything is good + public Customer authenticateCustomer(String username, String password){ + + if(password == null || password.trim().equals("") || password == null || password.trim().equals("")) { + throw new InvalidRequestException("Either username or password is an invalid entry. Please try logging in again"); + } + + Customer authenticatedCustomer = customerDao.authenticateCustomer(username, password); + + if (authenticatedCustomer == null){ + throw new AuthenticationException("Unauthenticated user, information provided was not consistent with our database."); + } + + return authenticatedCustomer; + } + + public String deleteCustomer(Customer customerToDelete){ + String fname = customerToDelete.getFname(); + String lname = customerToDelete.getLname(); + String username = customerToDelete.getUsername(); + String password = customerToDelete.getPassword(); + String deleteStatement = customerDao.deleteCustomer(username); + + + return deleteStatement ; + } + + public Customer updateCustomer(Customer customerToUpdate){ + return customerDao.updateCustomer(customerToUpdate);} + + } + + + diff --git a/src/main/java/com/revature/Pi2a1000Places/customer/CustomerServlet.java b/src/main/java/com/revature/Pi2a1000Places/customer/CustomerServlet.java new file mode 100644 index 0000000..5d19fd2 --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/customer/CustomerServlet.java @@ -0,0 +1,85 @@ +package com.revature.Pi2a1000Places.customer; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.revature.Pi2a1000Places.auth.LoginCreds; +import com.revature.Pi2a1000Places.util.exceptions.AuthenticationException; +import com.revature.Pi2a1000Places.util.exceptions.InvalidRequestException; +import com.revature.Pi2a1000Places.util.interfaces.Authable; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +public class CustomerServlet extends HttpServlet { + + private final CustomerServices customerServices; + private final ObjectMapper mapper; + + public CustomerServlet(CustomerServices customerServices, ObjectMapper mapper) { + this.customerServices = customerServices; + this.mapper = mapper; + } + + @Override + protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doOptions(req, resp); + resp.addHeader("Access-Control-Allow-Origin", "*"); + resp.addHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE"); + resp.addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization"); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + resp.addHeader("Access-Control-Allow-Origin", "*"); + resp.addHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE"); + resp.addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization"); + Customer newUser = mapper.readValue(req.getInputStream(), Customer.class); + + if(!Authable.checkAuth(req, resp)){ + try { + resp.getWriter().write("\nAttempting to Create Account\n"); + customerServices.validateUserInput(newUser); + resp.getWriter().write("This user has been created " + newUser); + } catch (InvalidRequestException e) { + resp.setStatus(409); + resp.getWriter().write(e.getMessage()); + } + } else if (Authable.checkAuth(req, resp)) { + try { + if (newUser.getUsername().equals(LoginCreds.getUsername()) && newUser.getPassword().equals(LoginCreds.getPassword())) { + newUser = customerServices.updateCustomer(newUser); + String payload = mapper.writeValueAsString(newUser); + resp.getWriter().write(payload); + } else { + throw new AuthenticationException("The username and password of the current user does not match the one to be deleted"); + } + }catch (AuthenticationException e){ + resp.setStatus(409); + resp.getWriter().write(e.getMessage()); + } + } + } + + @Override + protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + + resp.addHeader("Access-Control-Allow-Origin", "*"); + resp.addHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE"); + resp.addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization"); + + if (Authable.checkAuth(req, resp)) { + Customer customerToDelete = mapper.readValue(req.getInputStream(), Customer.class); + Customer customer = new Customer(); + + if (customerToDelete.getUsername().equals(LoginCreds.getUsername()) && customerToDelete.getPassword().equals(LoginCreds.getPassword())) { + resp.getWriter().write( customerServices.deleteCustomer(customerToDelete)); + req.getSession().invalidate(); + }else{throw new AuthenticationException("The username and password of the current user does not match the one to be deleted"); + } + } + } + + +} diff --git a/src/main/java/com/revature/Pi2a1000Places/menu/Menu.java b/src/main/java/com/revature/Pi2a1000Places/menu/Menu.java new file mode 100644 index 0000000..0a8238c --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/menu/Menu.java @@ -0,0 +1,8 @@ +package com.revature.Pi2a1000Places.menu; + +public class Menu { + public static class MenuServices { + + } + } + diff --git a/src/main/java/com/revature/Pi2a1000Places/order/Order.java b/src/main/java/com/revature/Pi2a1000Places/order/Order.java new file mode 100644 index 0000000..f0bbc09 --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/order/Order.java @@ -0,0 +1,80 @@ +package com.revature.Pi2a1000Places.order; + +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Table; + +@Entity +@Table(name = "order") +public class Order { + + @Id + private String id; + private String menuItem; + private String comment; + private String isFavorite; + private String orderDate; + private String customerUsername; + + + public Order( String id, String menuItem, String comment,String isFavorite, String orderDate, String customerUsername){ + super(); + this.id = id; + this.menuItem = menuItem; + this.comment = comment; + this.isFavorite = isFavorite; + this.orderDate = orderDate; + this.customerUsername = customerUsername; + } + + public Order(){ + + } + + //Getters And Setters + public String getId(){return id;} + + public void setId(String id){this.id = id;} + + public String getMenuItem(){return menuItem;} + + public void setMenuItem(String menuItem){this.menuItem = menuItem;} + + public String getComment(){return comment;} + + public void setComment(String comment){this.comment = comment;} + + public String getIsFavorite(){return isFavorite;} + + public void setIsFavorite(String isFavorite){this.isFavorite = isFavorite;} + + public String getOrderDate(){return orderDate;} + + public void setOrderDate(String orderDate){this.orderDate = orderDate;} + + public String getCustomerUsername(){return customerUsername;} + + public void setCustomerUsername(String customerUsername){this.customerUsername = customerUsername;} + + + + + + @Override + public String toString() { + return "User Info{" + + "Order ID ='" + id + '\'' + + ", Menu Item ='" + menuItem + '\'' + + ", Comment ='" + comment + '\'' + + ", Favorite ='" + isFavorite + '\'' + + ", Order Date ='" + orderDate + '\'' + + '}'; + } + + } + + + + + + diff --git a/src/main/java/com/revature/Pi2a1000Places/order/OrderDao.java b/src/main/java/com/revature/Pi2a1000Places/order/OrderDao.java new file mode 100644 index 0000000..3a8441a --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/order/OrderDao.java @@ -0,0 +1,64 @@ +package com.revature.Pi2a1000Places.order; + +import com.revature.Pi2a1000Places.util.ConnectionFactory; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Calendar; +import java.util.Date; +import java.util.Random; + +public class OrderDao { + + + public Order createOrder(String username, String menuItem, int id, int date) { + + + System.out.println("Persisting Order"); + + Order order = new Order(); + try (Connection conn = ConnectionFactory.getInstance().getConnection()) { + String sql = "insert into \"order\" values (?, ?, null, default, ?, ? );"; + PreparedStatement ps = conn.prepareStatement(sql); + ps.setInt(1,id); + ps.setString(2, menuItem); + ps.setInt(3, date); + ps.setString(4, username); + + int rs = ps.executeUpdate(); + System.out.println(rs); + + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + + try (Connection conn = ConnectionFactory.getInstance().getConnection();) { + String sql = "select * from \"order\" where customer_username = ? and menu_item = ?;"; + PreparedStatement ps = conn.prepareStatement(sql); + ps.setString(1, username); + ps.setString(2, menuItem); + + ResultSet rs = ps.executeQuery(); + + while (rs.next()) { + + + order.setId(rs.getString("id")); + order.setMenuItem(rs.getString("menu_item")); + order.setCustomerUsername(rs.getString("customer_username")); + + } + } catch (SQLException e) { + e.printStackTrace(); + return null; + } + + System.out.println("Order Persisted"); + return order; + } + + +} diff --git a/src/main/java/com/revature/Pi2a1000Places/order/OrderServices.java b/src/main/java/com/revature/Pi2a1000Places/order/OrderServices.java new file mode 100644 index 0000000..cbf6aa5 --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/order/OrderServices.java @@ -0,0 +1,30 @@ +package com.revature.Pi2a1000Places.order; + +import com.revature.Pi2a1000Places.util.exceptions.InvalidRequestException; + +import java.util.Calendar; +import java.util.Random; + +public class OrderServices { + + OrderDao orderDao = new OrderDao(); + private final Calendar calendar = Calendar.getInstance(); + + public Order createOrder(Order orderToCreate) { + + System.out.println("Adding order"); + Random random = new Random(); + int id = Math.abs(random.nextInt()); + int date = calendar.get(Calendar.DAY_OF_MONTH); + + String menuItem = orderToCreate.getMenuItem(); + String username = orderToCreate.getCustomerUsername(); + + if(username == null){throw new InvalidRequestException("Username cannot be null"); + }else if(menuItem == null) {throw new InvalidRequestException("Menu Item cannot be null"); + }else { + return orderDao.createOrder(username, menuItem, id, date); + } + + } +} diff --git a/src/main/java/com/revature/Pi2a1000Places/order/OrderServlet.java b/src/main/java/com/revature/Pi2a1000Places/order/OrderServlet.java new file mode 100644 index 0000000..413744f --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/order/OrderServlet.java @@ -0,0 +1,63 @@ +package com.revature.Pi2a1000Places.order; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.revature.Pi2a1000Places.customer.Customer; +import com.revature.Pi2a1000Places.order.*; +import com.revature.Pi2a1000Places.auth.LoginCreds; +import com.revature.Pi2a1000Places.util.exceptions.AuthenticationException; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +import static com.revature.Pi2a1000Places.util.interfaces.Authable.checkAuth; + +public class OrderServlet extends HttpServlet { + + private final OrderServices orderServices; + private final ObjectMapper mapper; + + + + public OrderServlet(OrderServices orderServices, ObjectMapper mapper) { + this.orderServices = orderServices; + this.mapper = mapper; + } + + @Override + protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + super.doOptions(req, resp); + resp.addHeader("Access-Control-Allow-Origin", "*"); + resp.addHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE"); + resp.addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization"); + } + + @Override + protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + + resp.addHeader("Access-Control-Allow-Origin", "*"); + resp.addHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE"); + resp.addHeader("Access-Control-Allow-Headers", "Content-Type, Authorization"); + + if (checkAuth(req, resp)) { + System.out.println("Creating Order"); + Order orderToCreate = mapper.readValue(req.getInputStream(), Order.class); + Order order = new Order(); + System.out.println(LoginCreds.getUsername()); + if (orderToCreate.getCustomerUsername().equals(LoginCreds.getUsername())) { + //creating + order = orderServices.createOrder(orderToCreate); + String payload = mapper.writeValueAsString(order); + resp.getWriter().write(payload); + } + + + } + } + + + + +} diff --git a/src/main/java/com/revature/Pi2a1000Places/util/ConnectionFactory.java b/src/main/java/com/revature/Pi2a1000Places/util/ConnectionFactory.java index 387cdf2..d690b17 100644 --- a/src/main/java/com/revature/Pi2a1000Places/util/ConnectionFactory.java +++ b/src/main/java/com/revature/Pi2a1000Places/util/ConnectionFactory.java @@ -1,5 +1,6 @@ package com.revature.Pi2a1000Places.util; + import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; @@ -11,7 +12,7 @@ public class ConnectionFactory { private static final ConnectionFactory connectionFactory = new ConnectionFactory(); private Properties prop = new Properties(); - private ConnectionFactory(){ + private ConnectionFactory() { try { ClassLoader loader = Thread.currentThread().getContextClassLoader(); prop.load(loader.getResourceAsStream("db.properties")); @@ -29,7 +30,7 @@ private ConnectionFactory(){ } } - public static ConnectionFactory getInstance(){ + public static ConnectionFactory getInstance() { return connectionFactory; } @@ -44,5 +45,7 @@ public Connection getConnection() { } return conn; } - } + + + diff --git a/src/main/java/com/revature/Pi2a1000Places/util/Crudable.java b/src/main/java/com/revature/Pi2a1000Places/util/Crudable.java new file mode 100644 index 0000000..d8a9a22 --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/util/Crudable.java @@ -0,0 +1,13 @@ +package com.revature.Pi2a1000Places.util; + +public interface Crudable { +//CRUD: Create, Read, Update, Delete + + T create(T newObject); + + T readById(String userName); + + T findById(String username); + boolean update(T updatedObject); + boolean delete(String username); +} //or CreditCard? diff --git a/src/main/java/com/revature/Pi2a1000Places/util/HibernateUtil.java b/src/main/java/com/revature/Pi2a1000Places/util/HibernateUtil.java new file mode 100644 index 0000000..9765bb1 --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/util/HibernateUtil.java @@ -0,0 +1,72 @@ +package com.revature.Pi2a1000Places.util; + +import com.revature.Pi2a1000Places.customer.Customer; +import com.revature.Pi2a1000Places.creditcard.CreditCard; +import com.revature.Pi2a1000Places.menu.Menu; +import com.revature.Pi2a1000Places.order.Order; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; +import org.hibernate.cfg.Configuration; +import org.hibernate.service.ServiceRegistry; + +import java.io.IOException; +import java.util.Properties; + +public class HibernateUtil { + + private static SessionFactory sessionFactory; + private static Session session; + + public static Session getSession() throws IOException { + if(sessionFactory == null) { + Configuration configuration = new Configuration(); + Properties props = new Properties(); + + + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + props.load(loader.getResourceAsStream("hibernate.properties")); + + configuration.setProperties(props); + + configuration.addAnnotatedClass(Customer.class); + + + //configuration.addAnnotatedClass(CreditCard.class); + //configuration.addAnnotatedClass(Menu.class); + //configuration.addAnnotatedClass(Order.class); + + + // ServiceRegistry + ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() + .applySettings(configuration.getProperties()).build(); + + sessionFactory = configuration.buildSessionFactory(serviceRegistry); + + + + } + + if(session == null) { + session = sessionFactory.openSession(); + } + + return session; + } + + public static void closeSession() { + session.close(); + session = null; + + } +} + +// hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect +// hibernate.connection.driver_class=org.postgresql.Driver +// hibernate.connection.url=jdbc:postgresql://localhost:5432/postgres?currentSchema=restaurant +// hibernate.connection.username=postgres +// hibernate.connection.password= +// hibernate.show_sql=true +// #Create once and update thereafter + diff --git a/src/main/java/com/revature/Pi2a1000Places/util/exceptions/AuthenticationException.java b/src/main/java/com/revature/Pi2a1000Places/util/exceptions/AuthenticationException.java new file mode 100644 index 0000000..5b09e62 --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/util/exceptions/AuthenticationException.java @@ -0,0 +1,8 @@ +package com.revature.Pi2a1000Places.util.exceptions; + +public class AuthenticationException extends RuntimeException { + + public AuthenticationException(String message) { + super(message); + } +} diff --git a/src/main/java/com/revature/Pi2a1000Places/util/exceptions/InvalidRequestException.java b/src/main/java/com/revature/Pi2a1000Places/util/exceptions/InvalidRequestException.java new file mode 100644 index 0000000..b73cd02 --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/util/exceptions/InvalidRequestException.java @@ -0,0 +1,7 @@ +package com.revature.Pi2a1000Places.util.exceptions; + +public class InvalidRequestException extends RuntimeException{ + public InvalidRequestException(String message) { + super(message); + } +} diff --git a/src/main/java/com/revature/Pi2a1000Places/util/exceptions/ResourcePersistenceException.java b/src/main/java/com/revature/Pi2a1000Places/util/exceptions/ResourcePersistenceException.java new file mode 100644 index 0000000..cdb3a80 --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/util/exceptions/ResourcePersistenceException.java @@ -0,0 +1,8 @@ +package com.revature.Pi2a1000Places.util.exceptions; + +public class ResourcePersistenceException extends RuntimeException{ + + public ResourcePersistenceException(String message) { + super(message); + } +} diff --git a/src/main/java/com/revature/Pi2a1000Places/util/interfaces/Authable.java b/src/main/java/com/revature/Pi2a1000Places/util/interfaces/Authable.java new file mode 100644 index 0000000..634344f --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/util/interfaces/Authable.java @@ -0,0 +1,24 @@ +package com.revature.Pi2a1000Places.util.interfaces; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; +import java.io.IOException; + +public interface Authable { + + static boolean checkAuth(HttpServletRequest req, HttpServletResponse resp) throws IOException { + HttpSession httpSession = req.getSession(); + if(httpSession.getAttribute("authCustomer") == null){ + resp.getWriter().write("Unauthorized request - not log in as registered user"); + resp.setStatus(401); // Unauthorized + return false; + } + return true; + } + + void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException; + + void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException; +} diff --git a/src/main/java/com/revature/Pi2a1000Places/util/logging/Logger.java b/src/main/java/com/revature/Pi2a1000Places/util/logging/Logger.java new file mode 100644 index 0000000..d6e4a6b --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/util/logging/Logger.java @@ -0,0 +1,56 @@ +package com.revature.Pi2a1000Places.util.logging; + +import java.io.FileWriter; +import java.io.IOException; +import java.io.Writer; +import java.net.URL; +import java.time.LocalDateTime; + +public class Logger { + + private static Logger logger; + private final boolean printToConsole; + + private Logger(boolean printToConsole) {//So the singleton is because we're privatizing its constructor and there's only ever going to be a single instance of the logger. + this.printToConsole = printToConsole; + } + + public static Logger getLogger(boolean printToConsole) { + // logger is being lazily instantiated + if (logger == null) { + logger = new Logger(printToConsole); + } + + return logger; + } + + public static Logger getLogger() { + // logger is being lazily instantiated + if (logger == null) { + logger = new Logger(true); + } + + return logger; + } + + public void log(String message) { + ClassLoader loader = Thread.currentThread().getContextClassLoader(); + URL file = loader.getResource("pi2a1000.log"); + + try (Writer logWriter = new FileWriter(String.valueOf(file).split(":")[1], true);) { + logWriter.write(LocalDateTime.now().toString() + " LOG: " + message + "\n"); + + if (printToConsole) { + System.out.println(LocalDateTime.now().toString() + " LOG: " + message); + } + + } catch (IOException e) { + e.printStackTrace(); + } + + + } + + public void info(String message) { + } +} \ No newline at end of file diff --git a/src/main/java/com/revature/Pi2a1000Places/util/web/ContextLoaderListener.java b/src/main/java/com/revature/Pi2a1000Places/util/web/ContextLoaderListener.java new file mode 100644 index 0000000..6bc02bb --- /dev/null +++ b/src/main/java/com/revature/Pi2a1000Places/util/web/ContextLoaderListener.java @@ -0,0 +1,44 @@ +package com.revature.Pi2a1000Places.util.web; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.revature.Pi2a1000Places.auth.AuthServlet; +import com.revature.Pi2a1000Places.customer.CustomerDao; +import com.revature.Pi2a1000Places.customer.CustomerServices; +import com.revature.Pi2a1000Places.customer.CustomerServlet; +import com.revature.Pi2a1000Places.order.OrderServices; +import com.revature.Pi2a1000Places.order.OrderServlet; + +import javax.servlet.ServletContext; +import javax.servlet.ServletContextEvent; +import javax.servlet.ServletContextListener; +import javax.servlet.annotation.WebListener; + +@WebListener +public class ContextLoaderListener implements ServletContextListener { + + @Override + public void contextInitialized(ServletContextEvent sce) { + ObjectMapper mapper = new ObjectMapper(); + CustomerDao customerDao = new CustomerDao(); + CustomerServices customerServices = new CustomerServices(); + OrderServices orderServices = new OrderServices(); + + AuthServlet authServlet = new AuthServlet(customerServices, mapper); + CustomerServlet customerServlet = new CustomerServlet(customerServices, mapper); + OrderServlet orderServlet = new OrderServlet(orderServices, mapper); + + + ServletContext context = sce.getServletContext(); + context.addServlet("AuthServlet", authServlet).addMapping("/auth"); + context.addServlet("CustomerServlet", customerServlet).addMapping("/customers/*"); + context.addServlet("OrderServlet", orderServlet ).addMapping("/orders/*"); + + + } + + @Override + public void contextDestroyed(ServletContextEvent sce) { + ServletContextListener.super.contextDestroyed(sce); + } +} + diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..7ec00d2 --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,11 @@ + + + Hello, Servlets! + + + + \ No newline at end of file diff --git a/src/main/webapp/index.html b/src/main/webapp/index.html new file mode 100644 index 0000000..47a68a3 --- /dev/null +++ b/src/main/webapp/index.html @@ -0,0 +1,30 @@ + + + + + + + Document + + + +

Maxwell’s Magnificent Minecraft Modpack Maker

+


+

User Story

+



+ As a user without an account I can ...

+ + See all the available mods
+ Filter mods by who created them
+ Register an account
+


+

As a user with an account I can ...

+ Log in as a user
+ Update a mod I own
+ Create a mod that doesn't already exist
+ Create a modpack
+ Add mods to the modpack
+ +

+ + \ No newline at end of file diff --git a/src/main/webapp/style.css b/src/main/webapp/style.css new file mode 100644 index 0000000..cd3d0d3 --- /dev/null +++ b/src/main/webapp/style.css @@ -0,0 +1,17 @@ +body{ + text-align: center; + background-color: rgb(18, 18, 18); + color: white; + font-size: large; + font-family: 'Trebuchet MS', 'Lucida Sans Unicode', 'Lucida Grande', 'Lucida Sans', Arial, sans-serif; +} +.comic{ + font-family: Comic Sans MS, Comic Sans, cursive; +} +.large{ + font-size: x-large; +} + +.title{ + color: rgb(64,224,208) +} \ No newline at end of file diff --git a/src/test/java/org/example/AppTest.java b/src/test/java/org/example/AppTest.java deleted file mode 100644 index 6a1d2d7..0000000 --- a/src/test/java/org/example/AppTest.java +++ /dev/null @@ -1,20 +0,0 @@ -package org.example; - -import static org.junit.Assert.assertTrue; - -import org.junit.Test; - -/** - * Unit test for simple App. - */ -public class AppTest -{ - /** - * Rigorous Test :-) - */ - @Test - public void shouldAnswerWithTrue() - { - assertTrue( true ); - } -} diff --git a/src/test/java/testSuite/ConnectionFactoryTestSuite.java b/src/test/java/testSuite/ConnectionFactoryTestSuite.java new file mode 100644 index 0000000..03ed7c1 --- /dev/null +++ b/src/test/java/testSuite/ConnectionFactoryTestSuite.java @@ -0,0 +1,25 @@ +package testSuite; + +import com.revature.Pi2a1000Places.util.ConnectionFactory; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Assertions; + +import java.sql.Connection; + +public class ConnectionFactoryTestSuite { + + @Test + public void test_getConnection_givenProvidedCredentials_returnValidConnection() { + + try (Connection conn = ConnectionFactory.getInstance().getConnection()) { + System.out.println(conn); + + Assertions.assertNotNull(conn); + } catch (Exception e) { + e.printStackTrace(); + } + + } + + +} diff --git a/src/test/java/testSuite/creditCard/CreditCardDaoCreateMethodTestSuite.java b/src/test/java/testSuite/creditCard/CreditCardDaoCreateMethodTestSuite.java new file mode 100644 index 0000000..3490b1c --- /dev/null +++ b/src/test/java/testSuite/creditCard/CreditCardDaoCreateMethodTestSuite.java @@ -0,0 +1,22 @@ +//package testSuite.creditCard; +// +//import com.revature.Pi2a1000Places.creditcard.CreditCard; +//import com.revature.Pi2a1000Places.creditcard.CreditCardDao; +//import org.junit.jupiter.api.BeforeEach; +//import org.junit.jupiter.api.Test; +// +//public class CreditCardDaoCreateMethodTestSuite { +// +// CreditCardDao ccDao; +// +// @BeforeEach +// public void testPrep() { +// ccDao = new CreditCardDao(); +// } +// +// @Test +// public void ccDaoTestForCreateMethod_returnCreditCard(){ +// CreditCard newlyAddedCreditCard= ccDao.addCC("5555","1212", "Test", "333", "12/12/22", "94949", "test"); +// System.out.println(newlyAddedCreditCard); +// } +//} diff --git a/src/test/java/testSuite/creditCard/CreditCardDaoDeleteMethodTestSuite.java b/src/test/java/testSuite/creditCard/CreditCardDaoDeleteMethodTestSuite.java new file mode 100644 index 0000000..4641774 --- /dev/null +++ b/src/test/java/testSuite/creditCard/CreditCardDaoDeleteMethodTestSuite.java @@ -0,0 +1,22 @@ +//package testSuite.creditCard; +// +//import com.revature.Pi2a1000Places.creditcard.CreditCardDao; +//import org.junit.jupiter.api.BeforeEach; +//import org.junit.jupiter.api.Test; +// +//public class CreditCardDaoDeleteMethodTestSuite { +// +// CreditCardDao ccDao; +// +// +// @BeforeEach +// public void testPrep(){ccDao= new CreditCardDao(); +// +// } +// @Test +// public void ccDaoTestForTheDeleteMethod_returnTrue(){ +// boolean result= ccDao.deleteByCCNumber("1111"); +// System.out.println(result); +// } +// +//} diff --git a/src/test/java/testSuite/creditCard/CreditCardServicesDaoTestSuite.java b/src/test/java/testSuite/creditCard/CreditCardServicesDaoTestSuite.java new file mode 100644 index 0000000..3c3f383 --- /dev/null +++ b/src/test/java/testSuite/creditCard/CreditCardServicesDaoTestSuite.java @@ -0,0 +1,14 @@ +package testSuite.creditCard; + +//import com.revature.Pi2a1000Places.creditcard.CreditCardDao; +//import com.revature.Pi2a1000Places.creditcard.CreditCardServices; +//import org.junit.jupiter.api.Test; +// +//public class CreditCardServicesDaoTestSuite { +// +// CreditCardServices sut = new CreditCardServices (new CreditCardDao); + + // @Test + +// public void get test +//} \ No newline at end of file diff --git a/src/test/java/testSuite/creditCard/CreditCardServicesTestSuite.java b/src/test/java/testSuite/creditCard/CreditCardServicesTestSuite.java new file mode 100644 index 0000000..bf63066 --- /dev/null +++ b/src/test/java/testSuite/creditCard/CreditCardServicesTestSuite.java @@ -0,0 +1,4 @@ +package testSuite.creditCard; + +public class CreditCardServicesTestSuite { +}