Skip to content
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
3 changes: 0 additions & 3 deletions src/main/java/com/openclassrooms/shopmanager/order/Cart.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ public double getAverageValue()
*/
public Product findProductInCartLines(Long productId)
{
// TODO implement the method
// return null;

// TO REMOVE
return cartLineList.stream().filter(cl -> cl.getProduct().getId().equals(productId)).findFirst().get().getProduct();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,42 @@
package com.openclassrooms.shopmanager.order;

import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.openclassrooms.shopmanager.product.Product;
import com.openclassrooms.shopmanager.product.ProductService;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.NoSuchElementException;
import java.util.Optional;

import javax.annotation.processing.FilerException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Controller
public class OrderController {

private OrderService orderService;


@Autowired
public OrderController( OrderService orderService)
{
this.orderService = orderService;
}



@GetMapping("/order/cart")
public String getCart(Model model)
{
Expand All @@ -27,17 +45,19 @@ public String getCart(Model model)
}

@PostMapping("/order/addToCart")
public String addToCart(@RequestParam("productId") Long productId)
public String addToCart(@RequestParam("productId") Long productId)
{

boolean success = orderService.addToCart(productId);

if (success) {
return "redirect:/order/cart";
} else {
} else {
return "redirect:/products";
}

}

@PostMapping("order/removeFromCart")
public String removeFromCart(@RequestParam Long productId)
{
Expand Down Expand Up @@ -66,4 +86,13 @@ public String createOrder(@Valid @ModelAttribute("order") Order order, BindingRe
return "order";
}
}

@ExceptionHandler({NoSuchElementException.class})
public ModelAndView handleException(NoSuchElementException exception) {
ModelAndView modelAndView = new ModelAndView("errorPage");
modelAndView.addObject("message", "The product is not anymore in our inventory");

return modelAndView;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void saveOrder(Order order)
{
orderRepository.save(order);
productService.updateProductQuantities(this.cart);

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

import javax.validation.Valid;

@Controller
Expand Down Expand Up @@ -40,13 +43,16 @@ public String productForm(Model model) {
return "product";
}

// i changed the modelAtribbute to product instead of productModel for make it works
@PostMapping("/admin/product")
public String createProduct(@Valid @ModelAttribute("productModel") ProductModel productModel, BindingResult result)
public String createProduct(@Valid @ModelAttribute("product") ProductModel productModel, BindingResult result)
{
//TODO implement form fields validation using the standard annotations in ProductModel cloass
// Business constraints for each field is commented against it
// Add proper error messages for each error and show all of them at the top of the page


List<FieldError> errors = result.getFieldErrors();
for (FieldError error : errors ) {
System.out.println (error.getObjectName() + " - " + error.getDefaultMessage());

}
if (!result.hasErrors()) {
productService.createProduct(productModel);
return "redirect:/admin/products";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,59 +1,72 @@
package com.openclassrooms.shopmanager.product;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;

public class ProductModel {

private Long id;
private String name; // Required
private String description;
private String details;
private String quantity; // Required, Integer, Greater than zero
private String price; // Required, Numeric, Greater than zero
private Long id;

@NotBlank(message = "Name must not be blank")
@Pattern(regexp = "^[^%#]*$", message = "Enter a valid name")
private String name;

private String description;
private String details;

@NotBlank(message = "Quantity must not be blank")
@Pattern(regexp = "^(1|[1-9][0-9]*)$", message = "Quantity it should be number and greater than zero")
private String quantity;

@NotBlank(message = "Price must not be blank")
@Pattern(regexp = "(1|[1-9]\\d*)?(\\.\\d+)", message = "Price should be a decimal number and greater than zero")
private String price;

public Long getId() {
return id;
}
public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}
public void setId(Long id) {
this.id = id;
}

public String getName() {
return name;
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
public void setName(String name) {
this.name = name;
}

public String getDescription() {
return description;
}
public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
public void setDescription(String description) {
this.description = description;
}

public String getDetails() {
return details;
}
public String getDetails() {
return details;
}

public void setDetails(String details) {
this.details = details;
}
public void setDetails(String details) {
this.details = details;
}

public String getQuantity() {
return quantity;
}
public String getQuantity() {
return quantity;
}

public void setQuantity(String quantity) {
this.quantity = quantity;
}
public void setQuantity(String quantity) {
this.quantity = quantity;
}

public String getPrice() {
return price;
}
public String getPrice() {
return price;
}

public void setPrice(String price) {
this.price = price;
}
public void setPrice(String price) {
this.price = price;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import com.openclassrooms.shopmanager.order.Cart;
import com.openclassrooms.shopmanager.order.CartLine;
import com.openclassrooms.shopmanager.order.OrderRepository;
import com.openclassrooms.shopmanager.order.OrderService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -15,7 +18,7 @@ public class ProductService {
private static final Logger log = LoggerFactory.getLogger(ProductService.class);

private ProductRepository productRepository;

@Autowired
public ProductService(ProductRepository repository) {
this.productRepository = repository;
Expand All @@ -37,23 +40,21 @@ public List<Product> getAllAdminProducts() {
public Product getByProductId(Long productId){
return productRepository.findById(productId).get();
}

public void createProduct(ProductModel productModel){
//change the code
public Product createProduct(ProductModel productModel){
Product product = new Product();
product.setDescription(productModel.getDescription());
product.setDetails(productModel.getDetails());
product.setName(productModel.getName());
product.setPrice(Double.parseDouble(productModel.getPrice()));
product.setQuantity(Integer.parseInt(productModel.getQuantity()));

productRepository.save(product);
return productRepository.save(product);
}

public void deleteProduct(Long productId){
// TODO what happens if a product has been added to a cart and has been later removed from the inventory ?
// delete the product form the cart by using the specific method
// => the choice is up to the student
productRepository.deleteById(productId);

}

public void updateProductQuantities(Cart cart){
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ product.details=Details
product.submit=Submit
product=Product

products.error=Back to Products page

cart.remove.from=Remove
cart.title=Your cart
cart.empty=Sorry, your cart is empty!
Expand All @@ -32,11 +34,15 @@ checkout.city=City*
checkout.zip=Zip
checkout.country=Country*

NotBlank.product.name= Name must not be blank
NotBlank.name=Please enter a name
NotBlank.address=Please enter an address
NotBlank.city=Please enter a city name
NotBlank.country=Please enter a country name

Pattern.product.quantity=Quantity it should be number and greater than zero
Pattern.product.price=Price should be a decimal number and greater than zero

completed.thanks=We are processing your order. The demo is over, click on the logo to start over.

lang.select=Select
Expand Down
7 changes: 7 additions & 0 deletions src/main/resources/static/css/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,11 @@ address {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
}

.inventory-error {
font-family: 'Exo', sans-serif;
font-size: 20px;
color:red;
text-align:center;
}
33 changes: 33 additions & 0 deletions src/main/resources/templates/errorPage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<html xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.w3.org/1999/xhtml">

<head>
<title th:text="#{products.title}">Products</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />

<link href='https://fonts.googleapis.com/css?family=Roboto'
rel='stylesheet' type='text/css' />
<link href='https://fonts.googleapis.com/css?family=Exo'
rel='stylesheet' type='text/css' />
<link th:href="@{/css/styles.css}" href="/css/styles.css" rel="stylesheet" type="text/css" />

</head>

<body>

<div th:replace="fragments :: navbar">Navigation bar fragment</div>

<div class="container">

<div class="inventory-error">Error:<p th:text="${message}"/></div>

<a href="/products" th:text="#{products.error}">Back to our Products Page</a>


<div th:replace="fragments :: footer">Footer</div>
</div>
</body>

</html>
2 changes: 1 addition & 1 deletion src/main/resources/templates/fragments.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
</form>
</li>
</ul>
</div><
</div>
</div>


Expand Down
Loading