Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web Pizza Service [by Alexander Nalivayko] #119

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions modules/pizza-service-alex-n/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<groupId>com.hillel.elementary.java-geeks</groupId>
<artifactId>pizza-service-alex-n</artifactId>
<version>0.0.1</version>
<packaging>war</packaging>


<parent>
Expand Down Expand Up @@ -45,6 +46,17 @@
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ private Runner() {

public static void main(String[] args) {

Context context = new DefaultPizzaServiceContext(new PizzaServiceConfig());
Context context = DefaultPizzaServiceContext.getInstance(new PizzaServiceConfig());
Controller controller = context.getBean("controller");
controller.runUserInterface();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,27 @@
public class DefaultPizzaServiceContext implements Context {

private static final Logger LOGGER = LoggerFactory.getLogger(DefaultPizzaServiceContext.class);
private final Config config;
private Config config;
private Map<String, Object> beans = new HashMap<>();
private static Context context;

public DefaultPizzaServiceContext(Config config) {
private DefaultPizzaServiceContext(Config config) {
this.config = config;
}

public static Context getInstance(Config config) {
if (context == null) {
synchronized (DefaultPizzaServiceContext.class) {
if (context == null) {
context = new DefaultPizzaServiceContext(config);
}
return context;
}
} else {
return context;
}
}

@Override
public <T> T getBean(String beanName) {
if (beanName == null || beanName.isEmpty()) {
Expand Down Expand Up @@ -70,7 +84,7 @@ private Object createNewBean(String beanName) {

try {
return constructor.newInstance(constructorArguments);
} catch (Exception e){
} catch (Exception e) {
LOGGER.error("Can't create instance of bean " + e);
throw new AppInitialisationException(e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,29 @@
public class Customer {
private Long id;
private String name;
private String password;
private String phone;

public Customer(String name) {
this.name = name;
}

public Customer(Long id, String name) {
this.id = id;
this.name = name;
}

public Customer(String name) {
public Customer(Long id, String name, String password, String phone) {
this.id = id;
this.name = name;
this.password = password;
this.phone = phone;
}

public Customer(String name, String password, String phone) {
this.name = name;
this.password = password;
this.phone = phone;
}

public Long getId() {
Expand All @@ -23,6 +38,15 @@ public String getName() {
return name;
}


public String getPassword() {
return password;
}

public String getPhone() {
return phone;
}

@Override
public String toString() {
return "Customer{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ public interface CustomerRepo {
Customer save(Customer customer);

Collection<Customer> getAll();

Customer getCustomerByName(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class InMemCustomerRepo implements CustomerRepo {

@Override
public Customer save(Customer customer) {
Customer savedCustomer = new Customer(counter, customer.getName());
Customer savedCustomer = new Customer(counter, customer.getName(), customer.getPassword(), customer.getPhone());
customers.put(counter, savedCustomer);
counter++;
return savedCustomer;
Expand All @@ -25,4 +25,14 @@ public Customer save(Customer customer) {
public Collection<Customer> getAll() {
return customers.values();
}

@Override
public Customer getCustomerByName(String name) {
for (Customer customer : customers.values()) {
if (customer.getName().equals(name)){
return customer;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ public interface CustomerService {

Customer registerCustomer(Customer customer);

Customer registerCustomer(String customerName, String pass, String phone);

Customer registerCustomer(String customerName);

Collection<Customer> getAllCustomers();

Customer signIn(String name, String pass);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.hillel.elementary.java_geeks.configs.anotations.Component;
import com.hillel.elementary.java_geeks.domain.Customer;
import com.hillel.elementary.java_geeks.repositories.CustomerRepo;
import org.apache.commons.codec.digest.DigestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -38,6 +39,26 @@ public Customer registerCustomer(Customer customer) {
return customerRepo.save(customer);
}

@Override
public Customer registerCustomer(String customerName, String pass, String phone) {
if (customerName == null ||
customerName.isEmpty() ||
pass == null ||
pass.isEmpty() ||
phone == null ||
phone.isEmpty()) {
String msg = "Can not create and save customer name:" + customerName + ",pass:" + pass + ",phone:" + phone;
LOGGER.error(msg);
return null;
} else {
if (customerRepo.getCustomerByName(customerName) != null) {
return null;
} else {
return customerRepo.save(new Customer(customerName, DigestUtils.md5Hex(pass), phone));
}
}
}

@Override
public Customer registerCustomer(String customerName) {
if (customerName == null || customerName.isEmpty() || customerName.trim().isEmpty()) {
Expand All @@ -52,4 +73,22 @@ public Customer registerCustomer(String customerName) {
public Collection<Customer> getAllCustomers() {
return customerRepo.getAll();
}


@Override
public Customer signIn(String name, String pass) {
if (name == null || pass == null || name.isEmpty() || pass.isEmpty()) {
String msg = "can't sign in user with name:" + name + ", pass:" + pass;
LOGGER.error(msg);
return null;
} else {
Customer customer = customerRepo.getCustomerByName(name);
if (customer == null) {
return null;
} else if (customer.getPassword().equals(DigestUtils.md5Hex(pass))) {
return customer;
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.hillel.elementary.java_geeks.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

public class CookieParser {

private CookieParser() {
}

public static String getCookieValue(String cookieName, HttpServletRequest req){
if (req.getCookies() != null) {
for (Cookie cookie : req.getCookies()) {
if (cookie.getName().equals(cookieName)) {
return cookie.getValue();
}
}
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.hillel.elementary.java_geeks.utils;

import com.hillel.elementary.java_geeks.domain.Pizza;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;

public class Sessions {

private Sessions() {
}

public static ArrayList<Pizza> getPizzasList(HttpSession session, String attrName) {
Object attribute = session.getAttribute(attrName);
if (attribute != null) {
return (ArrayList<Pizza>) attribute;
}
return null;
}

public static void addPizzaToSessionOrder(HttpSession session, String attrName, Pizza pizza) {
Object attribute = session.getAttribute(attrName);
ArrayList<Pizza> pizzas;
if (attribute != null) {
pizzas = (ArrayList<Pizza>) attribute;
} else {
pizzas = new ArrayList<>();
}
pizzas.add(pizza);
session.setAttribute(attrName, pizzas);
}

public static int getSessionOrderPizzasCount(HttpSession session, String attrName) {
Object attribute = session.getAttribute(attrName);
if (attribute != null) {
return ((ArrayList<Pizza>) attribute).size();
}
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.hillel.elementary.java_geeks.web;

import com.hillel.elementary.java_geeks.configs.Context;
import com.hillel.elementary.java_geeks.configs.DefaultPizzaServiceContext;
import com.hillel.elementary.java_geeks.configs.PizzaServiceConfig;
import com.hillel.elementary.java_geeks.services.PizzaService;
import com.hillel.elementary.java_geeks.utils.Sessions;

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 HomePageServlet extends HttpServlet {

private PizzaService pizzaService;
private static final String ATTR_NAME = "orderedPizza";

@Override
public void init() {
Context context = DefaultPizzaServiceContext.getInstance(new PizzaServiceConfig());
pizzaService = context.getBean("pizzaService");
}


@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setAttribute("pizzas", pizzaService.getAllPizzas());
req.getRequestDispatcher("home.jsp").forward(req, resp);
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
if (req.getParameter("add-btn") != null) {
String pizzaId = req.getParameter("pizzaId");
if (pizzaId != null) {
Sessions.addPizzaToSessionOrder(req.getSession(),
ATTR_NAME,
pizzaService.getPizzaById(Integer.parseInt(pizzaId)));
}
}
req.setAttribute("pizzas", pizzaService.getAllPizzas());
req.getRequestDispatcher("home.jsp").forward(req, resp);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.hillel.elementary.java_geeks.web;

import com.hillel.elementary.java_geeks.configs.Context;
import com.hillel.elementary.java_geeks.configs.DefaultPizzaServiceContext;
import com.hillel.elementary.java_geeks.configs.PizzaServiceConfig;
import com.hillel.elementary.java_geeks.domain.Pizza;
import com.hillel.elementary.java_geeks.services.OrderService;
import com.hillel.elementary.java_geeks.services.PizzaService;
import com.hillel.elementary.java_geeks.utils.Sessions;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


public class OrderServlet extends HttpServlet {

private static final String ATTR_NAME = "orderedPizza";
private OrderService orderService;
private PizzaService pizzaService;

@Override
public void init() throws ServletException {
Context context = DefaultPizzaServiceContext.getInstance(new PizzaServiceConfig());
pizzaService = context.getBean("pizzaService");
orderService = context.getBean("orderService");
}


@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<Pizza> pizzas = Sessions.getPizzasList(req.getSession(), ATTR_NAME);
if (pizzas != null) {
req.setAttribute("pizzas", pizzas);
req.setAttribute("count", pizzas.size());
}
req.getRequestDispatcher("order.jsp").forward(req, resp);
}


@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String userBtnValue = req.getParameter("usr-data-btn");
if (userBtnValue != null) {
if (userBtnValue.equals("delete all")) {
req.getSession().removeAttribute(ATTR_NAME);
} else if (userBtnValue.equals("create")) {
String name = req.getParameter("name");
//todo
}
}
req.getRequestDispatcher("order.jsp").forward(req, resp);
}
}
Loading