diff --git a/FinalProject/finalproject.mwb b/FinalProject/finalproject.mwb new file mode 100644 index 00000000..2c29e243 Binary files /dev/null and b/FinalProject/finalproject.mwb differ diff --git a/FinalProject/pom.xml b/FinalProject/pom.xml new file mode 100644 index 00000000..31094d7a --- /dev/null +++ b/FinalProject/pom.xml @@ -0,0 +1,68 @@ + + + 4.0.0 + + + org.springframework.boot + spring-boot-starter-parent + 1.2.1.RELEASE + + + + UTF-8 + com.javabootcamp.finalproject.data.jpa.main.FinalProjectBootcamp + 1.8 + + + FinalProject + com.javabootcamp + Java bootcamp final project + Spring Boot Data JPA Sample + 1.0-SNAPSHOT + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + mysql + mysql-connector-java + + + org.hibernate + hibernate-entitymanager + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework + spring-web + jar + + + org.springframework + spring-webmvc + jar + + + org.springframework.boot + spring-boot-starter-tomcat + + + org.springframework.boot + spring-boot-starter-actuator + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + diff --git a/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/FinalProjectBootcamp.java b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/FinalProjectBootcamp.java new file mode 100644 index 00000000..bd4bf55d --- /dev/null +++ b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/FinalProjectBootcamp.java @@ -0,0 +1,18 @@ + +package com.javabootcamp.finalproject.data.jpa; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +/** + * Class with main method, starts the application + * @author Santiago + */ +@SpringBootApplication +public class FinalProjectBootcamp { + + public static void main(String[] args) throws Exception { + SpringApplication.run(FinalProjectBootcamp.class, args); + + } + +} diff --git a/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/domain/Itemtobuy.java b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/domain/Itemtobuy.java new file mode 100644 index 00000000..dfc3f28a --- /dev/null +++ b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/domain/Itemtobuy.java @@ -0,0 +1,63 @@ + +package com.javabootcamp.finalproject.data.jpa.domain; + +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; +import javax.persistence.PrimaryKeyJoinColumn; +import org.hibernate.annotations.NaturalId; + +@Entity +public class Itemtobuy implements Serializable { + + @Id + @GeneratedValue + private Long iditem; + + @NaturalId + @PrimaryKeyJoinColumn(referencedColumnName = "idshoppingcart") + private Long idcart; + + @NaturalId + @PrimaryKeyJoinColumn(referencedColumnName = "idproduct") + private Long idprod; + + @Column(nullable = false) + private int quantity; + + protected Itemtobuy(){} + + public Itemtobuy (Long idcart, Long idprod, int quantity){ + this.idcart = idcart; + this.idprod = idprod; + this.quantity = quantity; + } + + public Long getIdcart() { + return idcart; + } + + public void setIdcart(Long ids) { + this.idcart = ids; + } + + public Long getIdprod() { + return idprod; + } + + public void setIdprod(Long idprod) { + this.idprod = idprod; + } + + public int getQuantity() { + return quantity; + } + + public void setQuantity(int quantity) { + this.quantity = quantity; + } + + +} diff --git a/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/domain/Product.java b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/domain/Product.java new file mode 100644 index 00000000..68e51906 --- /dev/null +++ b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/domain/Product.java @@ -0,0 +1,68 @@ + +package com.javabootcamp.finalproject.data.jpa.domain; + +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class Product implements Serializable { + + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue + @Column(name="idproduct") + private Long idproduct; + + @Column(nullable=false) + private String name; + + @Column(nullable=false) + private String category; + + @Column(nullable=false) + private double price; + + protected Product(){} + + public Product(String name, String category){ + this.name = name; + this.category = category; + } + + public Long getId() { + return idproduct; + } + + public String getName() { + return name; + } + + public String getCategory() { + return category; + } + + @Override + public String toString() { + return "Product{" + "id=" + idproduct + ", name=" + name + ", category=" + category + '}'; + } + + public void setName(String name) { + this.name = name; + } + + public void setCategory(String category) { + this.category = category; + } + + public void setPrice(double p){ + this.price = p; + } + + public double getPrice(){ + return price; + } +} diff --git a/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/domain/Shoppingcart.java b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/domain/Shoppingcart.java new file mode 100644 index 00000000..275a88f6 --- /dev/null +++ b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/domain/Shoppingcart.java @@ -0,0 +1,46 @@ + +package com.javabootcamp.finalproject.data.jpa.domain; + +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.PrimaryKeyJoinColumn; + + +@Entity +public class Shoppingcart implements Serializable { + + @Id + @PrimaryKeyJoinColumn(referencedColumnName = "iduser") + private Long idshoppingcart; + + @Column(nullable = false) + private double total; + + protected Shoppingcart(){} + + public Shoppingcart(Long id, double total){ + this.idshoppingcart = id; + this.total = total; + } + + public Long getUserid() { + return idshoppingcart; + } + + public void setUserid(Long userid) { + this.idshoppingcart = userid; + } + + + public double getTotal() { + return total; + } + + public void setTotal(double num) { + this.total = num; + } + + +} diff --git a/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/domain/User.java b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/domain/User.java new file mode 100644 index 00000000..3a8e7930 --- /dev/null +++ b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/domain/User.java @@ -0,0 +1,50 @@ + +package com.javabootcamp.finalproject.data.jpa.domain; + +import java.io.Serializable; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.Id; + +@Entity +public class User implements Serializable { + + private static final long serialVersionUID = 1L; + + @Id + @GeneratedValue + @Column( name = "iduser") + private Long iduser; + + @Column(nullable = false) + private String username; + + @Column(nullable = false) + private String password; + + protected User(){} + + public User(String username, String password){ + this.username = username; + this.password = password; + } + + @Override + public String toString() { + return "User{" + ", username=" + username + ", password=" + password + ", administrator=" +'}'; + } + + public Long getId() { + return iduser; + } + + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } + +} diff --git a/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ItemToBuyRepository.java b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ItemToBuyRepository.java new file mode 100644 index 00000000..0fd696c9 --- /dev/null +++ b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ItemToBuyRepository.java @@ -0,0 +1,25 @@ + +package com.javabootcamp.finalproject.data.jpa.service; + +import com.javabootcamp.finalproject.data.jpa.domain.Itemtobuy; +import java.util.List; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; + +public interface ItemToBuyRepository extends CrudRepository { + + @Query("Select p.name, i.quantity from Product p, Itemtobuy i where i.idprod = ?1 and i.idprod = p.idproduct" ) + public String findAllProductsById(Long id); + + @Query("from Itemtobuy i where idcart = ?1 and idprod = ?2 ") + public Itemtobuy findByShopId(Long id, Long idprod); + + @Query("Select i.idprod from Itemtobuy i where i.idcart = ?1") + public List getIdItemsInCart(Long id); + + @Modifying + @Query("Delete from Itemtobuy i where i.idcart = ?1") + public void deleteAllById(Long id); + +} diff --git a/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ProductRepository.java b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ProductRepository.java new file mode 100644 index 00000000..a2909a62 --- /dev/null +++ b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ProductRepository.java @@ -0,0 +1,22 @@ + +package com.javabootcamp.finalproject.data.jpa.service; + +import com.javabootcamp.finalproject.data.jpa.domain.Product; +import java.util.List; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; + +public interface ProductRepository extends CrudRepository{ + + Product findByIdproduct(Long productid); + + List findAll(); + + @Query("Select idproduct from Product p where p.name = ?1") + Long getIdproductByName(String name); + + @Query("Select price from Product p where p.idproduct = ?1") + public double getPriceForId(Long idprod); + + List findAllByCategory(String category); +} diff --git a/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ProductService.java b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ProductService.java new file mode 100644 index 00000000..53451556 --- /dev/null +++ b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ProductService.java @@ -0,0 +1,27 @@ + +package com.javabootcamp.finalproject.data.jpa.service; + +import com.javabootcamp.finalproject.data.jpa.domain.Product; +import java.util.List; + +public interface ProductService { + /** + * Returns product with the given id + * @param productid + * @return + */ + Product findByIdproduct(Long productid); + + /** + * Returns list with all products + * @return + */ + List findAll(); + + /** + * Returns list with the products of the given category + * @param category + * @return + */ + List findAllByCategory(String category); +} diff --git a/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ProductServiceImp.java b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ProductServiceImp.java new file mode 100644 index 00000000..a7e7488f --- /dev/null +++ b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ProductServiceImp.java @@ -0,0 +1,35 @@ + +package com.javabootcamp.finalproject.data.jpa.service; + +import com.javabootcamp.finalproject.data.jpa.domain.Product; +import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; +import org.springframework.util.Assert; + +@Service("productService") +public class ProductServiceImp implements ProductService { + + private final ProductRepository productrepository; + + @Autowired + public ProductServiceImp(ProductRepository productrepository){ + this.productrepository = productrepository; + } + + @Override + public Product findByIdproduct(Long productid) { + Assert.notNull(productid, "Product if must not be null"); + return this.productrepository.findByIdproduct(productid); + } + + @Override + public List findAll(){ + return this.productrepository.findAll(); + } + + @Override + public List findAllByCategory(String category){ + return this.productrepository.findAllByCategory(category); + } +} diff --git a/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ShoppingCartRepository.java b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ShoppingCartRepository.java new file mode 100644 index 00000000..15791e7a --- /dev/null +++ b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ShoppingCartRepository.java @@ -0,0 +1,36 @@ + +package com.javabootcamp.finalproject.data.jpa.service; + +import com.javabootcamp.finalproject.data.jpa.domain.Shoppingcart; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.CrudRepository; + +public interface ShoppingCartRepository extends CrudRepository { + + @Query("Select sp.idshoppingcart from Shoppingcart sp where sp.idshoppingcart = ?1") + public Long getShoppingCartId(Long id); + + @Modifying + @Query(value = "Insert into Itemtobuy(idcart, idprod, quantity) values (?1, ?2, ?3)", nativeQuery = true) + public void addItemToBuy(Long id,Long idprod,int i); + + @Modifying + @Query("Update Itemtobuy i set quantity = (quantity - ?1) where i.idcart = ?2 AND i.idprod = ?3") + public void removeItemToBuy(int quantity, Long id, Long idprod); + + @Modifying + @Query("Update Shoppingcart s set total = (total + (?2 * ?3)) where s.idshoppingcart = ?1") + public void addTotal(Long id, double price, int quantity); + + @Query("Select s.total from Shoppingcart s where s.idshoppingcart = ?1") + public double findTotalByIdshoppingcart(Long id); + + @Modifying + @Query("Update Shoppingcart s set total = (total - (?2 * ?3)) where s.idshoppingcart = ?1") + public void removeFromTotal(Long id, int quantity, double price); + + @Modifying + @Query("Update Shoppingcart s set total = 0 where s.idshoppingcart = ?1") + public void clearTotal(Long id); +} diff --git a/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ShoppingService.java b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ShoppingService.java new file mode 100644 index 00000000..71b98107 --- /dev/null +++ b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ShoppingService.java @@ -0,0 +1,59 @@ + +package com.javabootcamp.finalproject.data.jpa.service; + +import com.javabootcamp.finalproject.data.jpa.domain.Product; +import com.javabootcamp.finalproject.data.jpa.domain.Shoppingcart; +import org.springframework.transaction.annotation.Transactional; + +public interface ShoppingService { + + /** + * Adds single item to cart given an product name + * @param name + * @param username + * @param password + * @return + */ + @Transactional + Product addToCart(String name, String username, String password); + /** + * Adds items to cart given by product name and quantity + * @param quantity + * @param name + * @param username + * @param password + * @return + */ + @Transactional + Product addToCartItems(int quantity, String name, String username, String password); + /** + * Removes an item from cart given by product name and quantity + * @param quantity + * @param name + * @param username + * @param password + * @return + */ + @Transactional + String removeFromCart(int quantity, String name, String username, String password); + /** + * Saves cart + * @param s + */ + void save(Shoppingcart s); + /** + * Returns cart of given user + * @param username + * @param password + * @return + */ + String getCart(String username, String password); + /** + * Empty cart of the given user. Sets total to zero and deletes items in cart. + * @param username + * @param password + * @return + */ + @Transactional + public String clearCart(String username, String password); +} diff --git a/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ShoppingServiceImpl.java b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ShoppingServiceImpl.java new file mode 100644 index 00000000..9dda239c --- /dev/null +++ b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/ShoppingServiceImpl.java @@ -0,0 +1,127 @@ + +package com.javabootcamp.finalproject.data.jpa.service; + +import com.javabootcamp.finalproject.data.jpa.domain.Itemtobuy; +import com.javabootcamp.finalproject.data.jpa.domain.Product; +import com.javabootcamp.finalproject.data.jpa.domain.Shoppingcart; +import com.javabootcamp.finalproject.data.jpa.domain.User; +import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service("shoppingCartService") +public class ShoppingServiceImpl implements ShoppingService { + + private final ShoppingCartRepository shoppingRepository; + private final UserRepository userRepository; + private final ProductRepository productRepository; + private final ItemToBuyRepository itemToBuyRepository; + + @Autowired + public ShoppingServiceImpl(ShoppingCartRepository shoppingRepository,UserRepository userRepository, ProductRepository productRepository, ItemToBuyRepository itemToBuyRepository){ + this.shoppingRepository = shoppingRepository; + this.userRepository = userRepository; + this.productRepository = productRepository; + this.itemToBuyRepository = itemToBuyRepository; + } + + @Override + public Product addToCart(String name, String username, String password) { + User u = this.userRepository.getUserByUsername(username); + if (u.getPassword().equals(password)){ + Long id = this.shoppingRepository.getShoppingCartId(u.getId()); + Long idprod = this.productRepository.getIdproductByName(name); + double price = this.productRepository.getPriceForId(idprod); + Itemtobuy i = new Itemtobuy(id, idprod, 1); + this.itemToBuyRepository.save(i); + + this.shoppingRepository.addTotal(id, price, 1); + + return this.productRepository.findByIdproduct(idprod); + } else { + throw new IllegalArgumentException ("Username and password does not match"); + } + } + + @Override + public Product addToCartItems(int quantity, String name, String username, String password) { + if (quantity < 1){ + throw new IllegalArgumentException ("Please insert a valid quantity value"); + } else { + User u = this.userRepository.getUserByUsername(username); + if (u.getPassword().equals(password)){ + Long id = this.shoppingRepository.getShoppingCartId(u.getId()); + Long idprod = this.productRepository.getIdproductByName(name); + double price = this.productRepository.getPriceForId(idprod); + + Itemtobuy i = new Itemtobuy(id, idprod, quantity); + this.itemToBuyRepository.save(i); + + this.shoppingRepository.addTotal(id, price, quantity); + return this.productRepository.findByIdproduct(idprod); + } else { + throw new IllegalArgumentException ("Username and password does not match"); + } + } + } + + @Override + public String removeFromCart(int quantity, String name, String username, String password) { + User u = this.userRepository.getUserByUsername(username); + if (u.getPassword().equals(password)){ + Long id = this.shoppingRepository.getShoppingCartId(u.getId()); + Long idprod = this.productRepository.getIdproductByName(name); + Itemtobuy i = this.itemToBuyRepository.findByShopId(id, idprod); + if (quantity > i.getQuantity()) { + throw new IllegalArgumentException ("Given quantity is greater than quantity in cart"); + } else { + double price = this.productRepository.getPriceForId(idprod); + this.shoppingRepository.removeFromTotal(id, quantity, price); + this.shoppingRepository.removeItemToBuy(quantity, id,idprod); + return "Removed item from cart"; + } + } else { + throw new IllegalArgumentException ("Username and password does not match"); + } + } + + @Override + public void save(Shoppingcart s){ + this.shoppingRepository.save(s); + } + + @Override + public String getCart(String username, String password) { + User u = this.userRepository.getUserByUsername(username); + if (u.getPassword().equals(password)){ + Long id = this.shoppingRepository.getShoppingCartId(u.getId()); + StringBuilder sb = new StringBuilder(); + + List idlist = this.itemToBuyRepository.getIdItemsInCart(id); + + for (int i = 0; i < idlist.size(); i++){ + sb.append(this.itemToBuyRepository.findAllProductsById(idlist.get(i))); + sb.append(", "); + } + sb.append(" Total: $"); + sb.append(this.shoppingRepository.findTotalByIdshoppingcart(id)); + return sb.toString(); + } else{ + throw new IllegalArgumentException ("Username and password does not match"); + } + } + + @Override + public String clearCart(String username, String password) { + User u = this.userRepository.getUserByUsername(username); + if (u.getPassword().equals(password)){ + Long id = this.shoppingRepository.getShoppingCartId(u.getId()); + this.itemToBuyRepository.deleteAllById(id); + this.shoppingRepository.clearTotal(id); + + return "Shopping cart cleared"; + }else{ + throw new IllegalArgumentException ("Username and password does not match"); + } + } +} diff --git a/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/UserRepository.java b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/UserRepository.java new file mode 100644 index 00000000..4a56a24b --- /dev/null +++ b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/UserRepository.java @@ -0,0 +1,11 @@ + +package com.javabootcamp.finalproject.data.jpa.service; + +import com.javabootcamp.finalproject.data.jpa.domain.User; +import org.springframework.data.repository.CrudRepository; + +public interface UserRepository extends CrudRepository { + + User getUserByUsername(String username); + +} diff --git a/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/UserService.java b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/UserService.java new file mode 100644 index 00000000..8657a402 --- /dev/null +++ b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/UserService.java @@ -0,0 +1,35 @@ + +package com.javabootcamp.finalproject.data.jpa.service; + +import com.javabootcamp.finalproject.data.jpa.domain.User; +import org.springframework.transaction.annotation.Transactional; + +public interface UserService { + /** + * Creates a new user and assigns a shopping cart + * @param username Username. Must be at least 6 characters + * @param password Password. Must be at least 6 characters + * @return Returns User object + */ + @Transactional + User createUser(String username, String password); + /** + * Deletes user with the given username + * @param username + * @param password + * @return + */ + String deleteUser(String username, String password); + /** + * Creates a Shopping cart. This method is called when creating a new user + * @param u + */ + @Transactional + void createCart(User u); + /** + * Returns Id given an username + * @param username + * @return + */ + Long getIdByUsername(String username); +} diff --git a/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/UserServiceImpl.java b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/UserServiceImpl.java new file mode 100644 index 00000000..4b65353f --- /dev/null +++ b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/service/UserServiceImpl.java @@ -0,0 +1,55 @@ + +package com.javabootcamp.finalproject.data.jpa.service; + +import com.javabootcamp.finalproject.data.jpa.domain.Shoppingcart; +import com.javabootcamp.finalproject.data.jpa.domain.User; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service("userService") +public class UserServiceImpl implements UserService { + + private final UserRepository userrepository; + private final ShoppingCartRepository shoppingRepository; + + @Autowired + public UserServiceImpl(UserRepository userrepository, ShoppingCartRepository shoppingRepository) { + this.userrepository = userrepository; + this.shoppingRepository = shoppingRepository; + } + + @Override + public User createUser(String username, String password) { + if( username.length() < 6 || password.length() < 6){ + throw new IllegalArgumentException ("Username and password must be at least 6 chars"); + } else { + User user = new User(username, password); + this.userrepository.save(user); + createCart(user); + return user; + } + } + + @Override + public void createCart(User u){ + Shoppingcart sp = new Shoppingcart(u.getId(), 0); + this.shoppingRepository.save(sp); + } + + @Override + public String deleteUser(String username, String password) { + User user = new User(username, password); + this.userrepository.delete(user); + StringBuilder sb = new StringBuilder(); + sb.append("Username: "); + sb.append(username); + sb.append(" deleted."); + return sb.toString(); + } + + @Override + public Long getIdByUsername(String username) { + User u = this.userrepository.getUserByUsername(username); + return u.getId(); + } +} diff --git a/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/web/Controller.java b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/web/Controller.java new file mode 100644 index 00000000..dd896357 --- /dev/null +++ b/FinalProject/src/main/java/com/javabootcamp/finalproject/data/jpa/web/Controller.java @@ -0,0 +1,86 @@ + +package com.javabootcamp.finalproject.data.jpa.web; + +import com.javabootcamp.finalproject.data.jpa.domain.Product; +import com.javabootcamp.finalproject.data.jpa.domain.User; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.transaction.annotation.Transactional; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; +import com.javabootcamp.finalproject.data.jpa.service.ProductService; +import com.javabootcamp.finalproject.data.jpa.service.ShoppingService; +import com.javabootcamp.finalproject.data.jpa.service.UserService; +import java.util.List; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class Controller { + + @Autowired + private ProductService productService; + + @Autowired + private UserService userService; + + @Autowired + private ShoppingService shoppingService; + + @RequestMapping(value = "/shop/product", method = RequestMethod.GET) + @ResponseBody + @Transactional(readOnly = true) + public List findAll() { + return this.productService.findAll(); + } + + @RequestMapping(value = "/shop/product/id/{productid}", method = RequestMethod.GET) + @ResponseBody + @Transactional(readOnly = true) + public Product findById(@PathVariable int productid){ + Long x = (long) productid; + return this.productService.findByIdproduct(x); + + } + + @RequestMapping(value = "/shop/product/category/{category}", method = RequestMethod.GET) + @ResponseBody + @Transactional(readOnly = true) + public List findAllByCategory(@PathVariable String category){ + return this.productService.findAllByCategory(category); + } + + @RequestMapping(value = "/shop/user", method = RequestMethod.POST) + public User createUser(@RequestParam String username, @RequestParam String password){ + return this.userService.createUser(username, password); + } + + @RequestMapping(value = "/shop/{name}", method = RequestMethod.PUT) + public Product addToCart(@PathVariable String name, @RequestParam String username, @RequestParam String password){ + return this.shoppingService.addToCart(name, username, password); + } + + @RequestMapping(value = "/shop/item/{name}", method = RequestMethod.PUT) + public Product addToCartItems(@PathVariable String name,@RequestParam int quantity, @RequestParam String username, @RequestParam String password){ + return this.shoppingService.addToCartItems(quantity,name, username, password); + } + + @RequestMapping(value = "/shop/{name}", method = RequestMethod.DELETE) + public String removeFromCart(@PathVariable String name, @RequestParam int quantity, @RequestParam String username, @RequestParam String password){ + return this.shoppingService.removeFromCart(quantity, name, username, password); + } + + + @RequestMapping(value = "/shop/user", method = RequestMethod.GET) + public String getCart(@RequestParam String username, @RequestParam String password){ + return this.shoppingService.getCart(username, password); + } + + @RequestMapping(value = "/shop/user/clear", method = RequestMethod.DELETE) + public String clearCart(@RequestParam String username, @RequestParam String password){ + return this.shoppingService.clearCart(username, password); + } + + +} diff --git a/FinalProject/src/main/resources/application.properties b/FinalProject/src/main/resources/application.properties new file mode 100644 index 00000000..0ce8e64d --- /dev/null +++ b/FinalProject/src/main/resources/application.properties @@ -0,0 +1,5 @@ +spring.datasource.driverClassName=com.mysql.jdbc.Driver +spring.datasource.url=jdbc:mysql://localhost:3306/shoppingcart +spring.datasource.username=root +spring.datasource.password=mysql +hibernate.hbm2dll.auto=update