diff --git a/src/main/java/io/khasang/bazaar/config/HibernateConfig.java b/src/main/java/io/khasang/bazaar/config/HibernateConfig.java index 6a39a11..ebf20f8 100644 --- a/src/main/java/io/khasang/bazaar/config/HibernateConfig.java +++ b/src/main/java/io/khasang/bazaar/config/HibernateConfig.java @@ -45,6 +45,7 @@ public LocalSessionFactoryBean sessionFactory(){ return sessionFactory; } + private Properties hibernateProperties(){ Properties properties = new Properties(); properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect")); diff --git a/src/main/java/io/khasang/bazaar/config/application/AppConfig.java b/src/main/java/io/khasang/bazaar/config/application/AppConfig.java index 330a3e7..44f3566 100644 --- a/src/main/java/io/khasang/bazaar/config/application/AppConfig.java +++ b/src/main/java/io/khasang/bazaar/config/application/AppConfig.java @@ -1,8 +1,11 @@ package io.khasang.bazaar.config.application; import io.khasang.bazaar.dao.CatDao; +import io.khasang.bazaar.dao.FeedbackDao; import io.khasang.bazaar.dao.impl.CatDaoImpl; +import io.khasang.bazaar.dao.impl.FeedbackDaoImpl; import io.khasang.bazaar.entity.Cat; +import io.khasang.bazaar.entity.Feedback; import io.khasang.bazaar.model.CreateTable; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; @@ -56,4 +59,9 @@ public CreateTable createTable(){ public CatDao catDao(){ return new CatDaoImpl(Cat.class); } + + @Bean + public FeedbackDao feedbackDao() { + return new FeedbackDaoImpl(Feedback.class); + } } diff --git a/src/main/java/io/khasang/bazaar/controller/AppController.java b/src/main/java/io/khasang/bazaar/controller/AppController.java index 6c94759..8f4b28a 100644 --- a/src/main/java/io/khasang/bazaar/controller/AppController.java +++ b/src/main/java/io/khasang/bazaar/controller/AppController.java @@ -1,7 +1,6 @@ package io.khasang.bazaar.controller; -import io.khasang.bazaar.model.CreateTable; -import io.khasang.bazaar.model.Message; +import io.khasang.bazaar.model.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; @@ -22,6 +21,27 @@ public class AppController { @Autowired private CreateTable createTable; + @Autowired + private PopulateTable populateTable; + + @Autowired + private SelectFromTable selectFromTable; + + @Autowired + private UpdateTable updateTable; + + @Autowired + private DeleteFromTable deleteFromTable; + + @Autowired + private JoinTable joinTable; + + @Autowired + private SubqueryTable subqueryTable; + + @Autowired + private CaseWhenTable caseWhenTable; + // http://localhost:8080/ @RequestMapping("/") public String javaPageHello() { @@ -31,7 +51,56 @@ public String javaPageHello() { @RequestMapping("/create") public String createTable(Model model) { model.addAttribute("status", createTable.createStatus()); - // hello.jsp + // table.jsp + return "table"; + } + + @RequestMapping("/populate") + public String populateTable(Model model) { + model.addAttribute("status", populateTable.populateStatus()); + // table.jsp + return "table"; + } + + @RequestMapping("/select") + public String selectFromTable(Model model) { + model.addAttribute("status", selectFromTable.selectStatus()); + // table.jsp + return "table"; + } + + @RequestMapping("/update") + public String updateTable(Model model) { + model.addAttribute("status", updateTable.updateStatus()); + // table.jsp + return "table"; + } + + @RequestMapping("/delete") + public String deleteFromTable(Model model) { + model.addAttribute("status", deleteFromTable.deleteStatus()); + // table.jsp + return "table"; + } + + @RequestMapping("/join") + public String joinTable(Model model) { + model.addAttribute("status", joinTable.joinStatus()); + // table.jsp + return "table"; + } + + @RequestMapping("/subquery") + public String subqueryTable(Model model) { + model.addAttribute("status", subqueryTable.subqueryStatus()); + // table.jsp + return "table"; + } + + @RequestMapping("/casewhen") + public String caseWhenTable(Model model) { + model.addAttribute("status", caseWhenTable.caseWhenStatus()); + // table.jsp return "table"; } @@ -41,6 +110,13 @@ public String getAdminInfo(Model model){ return "admin"; } + @RequestMapping("/user") + public String getUserInfo(Model model) { + model.addAttribute("secure", "It's a very secure page!"); + // user.jsp + return "user"; + } + @RequestMapping(value = {"/password/{password}"}, method = RequestMethod.GET) public ModelAndView passwordEncode(@PathVariable("password") String password) { ModelAndView modelAndView = new ModelAndView(); diff --git a/src/main/java/io/khasang/bazaar/controller/FeedbackController.java b/src/main/java/io/khasang/bazaar/controller/FeedbackController.java new file mode 100644 index 0000000..524fb38 --- /dev/null +++ b/src/main/java/io/khasang/bazaar/controller/FeedbackController.java @@ -0,0 +1,62 @@ +package io.khasang.bazaar.controller; + +import io.khasang.bazaar.entity.Feedback; +import io.khasang.bazaar.service.FeedbackService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@Controller +@RequestMapping(value = "/feedback") +public class FeedbackController { + private final FeedbackService feedbackService; + + @Autowired + public FeedbackController(FeedbackService feedbackService) { + this.feedbackService = feedbackService; + } + + @RequestMapping(value = "/get/id/{id}", method = RequestMethod.GET) + @ResponseBody + public Feedback getFeedbackById(@PathVariable(value = "id") String id) { + return feedbackService.getById(Long.parseLong(id)); + } + + @RequestMapping(value = "/add", method = RequestMethod.PUT, produces = "application/json;charset=utf-8") + @ResponseBody + public Feedback addFeedback(@RequestBody Feedback feedback) { + return feedbackService.addFeedback(feedback); + } + + @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json;charset=utf-8") + @ResponseBody + public Feedback updateFeedback(@RequestBody Feedback feedback) { + return feedbackService.updateFeedback(feedback); + } + + @RequestMapping(value = "/delete", method = RequestMethod.DELETE) + @ResponseBody + public Feedback deleteFeedback(@RequestParam(value = "id") String id){ + return feedbackService.deleteFeedback(Long.parseLong(id)); + } + + @RequestMapping(value = "/get/byuser/{user_id}", method = RequestMethod.GET) + @ResponseBody + public List getFeedbacksByUser(@PathVariable(value = "user_id") String user_id){ + return feedbackService.getFeedbacksByUser(Long.parseLong(user_id)); + } + + @RequestMapping(value = "/get/bygood/{good_id}", method = RequestMethod.GET) + @ResponseBody + public List getFeedbacksByGood(@PathVariable(value = "good_id") String good_id){ + return feedbackService.getFeedbacksByGood(Long.parseLong(good_id)); + } + + @RequestMapping(value = "/get/avgratingbygood/{good_id}", method = RequestMethod.GET) + @ResponseBody + public Double getGoodAverageFeedbackRating(@PathVariable(value = "good_id") String good_id){ + return feedbackService.getGoodAverageFeedbackRating(Long.parseLong(good_id)); + } +} diff --git a/src/main/java/io/khasang/bazaar/controller/GoodsCategoryController.java b/src/main/java/io/khasang/bazaar/controller/GoodsCategoryController.java new file mode 100644 index 0000000..40eeac7 --- /dev/null +++ b/src/main/java/io/khasang/bazaar/controller/GoodsCategoryController.java @@ -0,0 +1,62 @@ +package io.khasang.bazaar.controller; + +import io.khasang.bazaar.entity.GoodsCategory; +import io.khasang.bazaar.service.GoodsCategoryService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * Controller for Goods Category entity. + * + * @author Zulfia Garifullina + * @date 26.09.2017. + */ +@Controller +@RequestMapping(value = "/goodscategories") +public class GoodsCategoryController { + private final GoodsCategoryService goodsCategoryService; + + @Autowired + public GoodsCategoryController(GoodsCategoryService goodsCategoryService) { + this.goodsCategoryService = goodsCategoryService; + } + + @RequestMapping(value = "/get/id/{id}", method = RequestMethod.GET) + @ResponseBody + public GoodsCategory getGoodsCategoryById(@PathVariable(value = "id") String id) { + return goodsCategoryService.getById(Long.parseLong(id)); + } + + @RequestMapping(value = "/get/name/{name}", method = RequestMethod.GET) + @ResponseBody + public List getGoodsCategoriesByName(@PathVariable(value = "name") String name) { + return goodsCategoryService.getGoodsCategoriesByName(name); + } + + @RequestMapping(value = "/add", method = RequestMethod.PUT, produces = "application/json;charset=utf-8") + @ResponseBody + public GoodsCategory addGoodsCategory(@RequestBody GoodsCategory goodsCategory) { + return goodsCategoryService.addGoodsCategory(goodsCategory); + } + + @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json;charset=utf-8") + @ResponseBody + public GoodsCategory updateGoodsCategory(@RequestBody GoodsCategory goodsCategory) { + return goodsCategoryService.updateGoodsCategory(goodsCategory); + } + + @RequestMapping(value = "/delete", method = RequestMethod.DELETE) + @ResponseBody + public GoodsCategory deleteGoodsCategory(@RequestParam(value = "id") String id) { + return goodsCategoryService.deleteGoodsCategory(Long.parseLong(id)); + } + + @RequestMapping(value = "/all", method = RequestMethod.GET) + @ResponseBody + public List getGoodsCategories() { + return goodsCategoryService.getList(); + } +} \ No newline at end of file diff --git a/src/main/java/io/khasang/bazaar/controller/GoodsController.java b/src/main/java/io/khasang/bazaar/controller/GoodsController.java new file mode 100644 index 0000000..43b8faa --- /dev/null +++ b/src/main/java/io/khasang/bazaar/controller/GoodsController.java @@ -0,0 +1,80 @@ +package io.khasang.bazaar.controller; + +import io.khasang.bazaar.entity.Goods; +import io.khasang.bazaar.service.GoodsService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * Controller for Goods entity. + * + * @author Zulfia Garifullina + * @date 04.10.2017. + */ +@Controller +@RequestMapping(value = "/goods") +public class GoodsController { + private final GoodsService goodsService; + + @Autowired + public GoodsController(GoodsService goodsService) { + this.goodsService = goodsService; + } + + @RequestMapping(value = "/get/id/{id}", method = RequestMethod.GET) + @ResponseBody + public Goods getGoodsById(@PathVariable(value = "id") String id) { + return goodsService.getById(Long.parseLong(id)); + } + + @RequestMapping(value = "/get/name/{name}", method = RequestMethod.GET) + @ResponseBody + public List getGoodsByName(@PathVariable(value = "name") String name) { + return goodsService.getGoodsByName(name); + } + + @RequestMapping(value = "/add", method = RequestMethod.PUT, produces = "application/json;charset=utf-8") + @ResponseBody + public Goods addGoods(@RequestBody Goods goods) { + return goodsService.addGoods(goods); + } + + @RequestMapping(value = "/update", method = RequestMethod.POST, produces = "application/json;charset=utf-8") + @ResponseBody + public Goods updateGoods(@RequestBody Goods goods) { + return goodsService.updateGoods(goods); + } + + @RequestMapping(value = "/delete", method = RequestMethod.DELETE) + @ResponseBody + public Goods deleteGoods(@RequestParam(value = "id") String id) { + return goodsService.deleteGoods(Long.parseLong(id)); + } + + @RequestMapping(value = "/all", method = RequestMethod.GET) + @ResponseBody + public List getGoods() { + return goodsService.getList(); + } + + @RequestMapping(value = "/reserve", method = RequestMethod.POST) + @ResponseBody + public Goods reserveGoods(@RequestParam(value = "id") String id, @RequestParam(value = "quantity") String quantity) { + return goodsService.reserveGoods(Long.parseLong(id), Integer.parseInt(quantity)); + } + + @RequestMapping(value = "/unreserve", method = RequestMethod.POST) + @ResponseBody + public Goods unreserveGoods(@RequestParam(value = "id") String id, @RequestParam(value = "quantity") String quantity) { + return goodsService.unreserveGoods(Long.parseLong(id), Integer.parseInt(quantity)); + } + + @RequestMapping(value = "/buy", method = RequestMethod.POST) + @ResponseBody + public Goods buyGoods(@RequestParam(value = "id") String id, @RequestParam(value = "quantity") String quantity) { + return goodsService.buyGoods(Long.parseLong(id), Integer.parseInt(quantity)); + } +} diff --git a/src/main/java/io/khasang/bazaar/dao/FeedbackDao.java b/src/main/java/io/khasang/bazaar/dao/FeedbackDao.java new file mode 100644 index 0000000..43355ec --- /dev/null +++ b/src/main/java/io/khasang/bazaar/dao/FeedbackDao.java @@ -0,0 +1,31 @@ +package io.khasang.bazaar.dao; + +import io.khasang.bazaar.entity.Feedback; + +import java.util.List; + +public interface FeedbackDao extends BasicDao { + /** + * Receive all feedback records from database made by user specified by his id + * + * @param user_id - user's id + * @return list of feedback records + */ + List getFeedbacksByUser(Long user_id); + + /** + * Receive all feedback records from database about some good item specified by it's id + * + * @param good_id - good's id + * @return list of feedback records + */ + List getFeedbacksByGood(Long good_id); + + /** + * Receive average feedback rating for good item specified by it's id + * + * @param good_id - good's id + * @return list of feedback records + */ + Double getGoodAverageFeedbackRating(Long good_id); +} diff --git a/src/main/java/io/khasang/bazaar/dao/GoodsCategoryDao.java b/src/main/java/io/khasang/bazaar/dao/GoodsCategoryDao.java new file mode 100644 index 0000000..86d318b --- /dev/null +++ b/src/main/java/io/khasang/bazaar/dao/GoodsCategoryDao.java @@ -0,0 +1,21 @@ +package io.khasang.bazaar.dao; + +import io.khasang.bazaar.entity.GoodsCategory; + +import java.util.List; + +/** + * Interface for representing DAO for GoodsCategory entity. + * + * @author Zulfia Garifullina + * @date 26.09.2017. + */ +public interface GoodsCategoryDao extends BasicDao { + /** + * Method for retrieving goods categories from the database by their name. + * + * @param name - name of the category + * @return - returns all the goods categories with the given name + */ + List getGoodsCategoriesByName(String name); +} diff --git a/src/main/java/io/khasang/bazaar/dao/GoodsDao.java b/src/main/java/io/khasang/bazaar/dao/GoodsDao.java new file mode 100644 index 0000000..126b440 --- /dev/null +++ b/src/main/java/io/khasang/bazaar/dao/GoodsDao.java @@ -0,0 +1,21 @@ +package io.khasang.bazaar.dao; + +import io.khasang.bazaar.entity.Goods; + +import java.util.List; + +/** + * Interface for representing DAO for Goods entity. + * + * @author Zulfia Garifullina + * @date 04.10.2017. + */ +public interface GoodsDao extends BasicDao { + /** + * Method for retrieving goods from the database by their name. + * + * @param name - name of the goods + * @return goods with the given name + */ + List getGoodsByName(String name); +} diff --git a/src/main/java/io/khasang/bazaar/dao/impl/FeedbackDaoImpl.java b/src/main/java/io/khasang/bazaar/dao/impl/FeedbackDaoImpl.java new file mode 100644 index 0000000..c5192c0 --- /dev/null +++ b/src/main/java/io/khasang/bazaar/dao/impl/FeedbackDaoImpl.java @@ -0,0 +1,52 @@ +package io.khasang.bazaar.dao.impl; + +import io.khasang.bazaar.dao.FeedbackDao; +import io.khasang.bazaar.entity.Feedback; + +import javax.persistence.Query; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.criteria.CriteriaQuery; +import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Root; +import java.util.List; + +public class FeedbackDaoImpl extends BasicDaoImpl implements FeedbackDao{ + public FeedbackDaoImpl(Class entityClass) { + super(entityClass); + } + + @Override + public List getFeedbacksByUser(Long user_id) { + List results = getFeedbackSelectQuery("user_id", user_id).getResultList(); + return results; + } + + @Override + public List getFeedbacksByGood(Long good_id) { + List results = getFeedbackSelectQuery("good_id", good_id).getResultList(); + return results; + } + + @Override + public Double getGoodAverageFeedbackRating(Long good_id) { + CriteriaBuilder builder = sessionFactory.getCriteriaBuilder(); + CriteriaQuery criteriaQuery = builder.createQuery(Feedback.class); + Root feedback= criteriaQuery.from(Feedback.class); + criteriaQuery.select(builder.avg(feedback.get("rating"))); + Predicate where = builder.equal(feedback.get("good_id"),good_id); + criteriaQuery.where(where); + Query query = sessionFactory.getCurrentSession().createQuery(criteriaQuery); + Double result = (Double)query.getSingleResult(); + return result; + } + + private Query getFeedbackSelectQuery(String idName, Long idVal) { + CriteriaBuilder builder = sessionFactory.getCriteriaBuilder(); + CriteriaQuery criteriaQuery = builder.createQuery(Feedback.class); + Root root = criteriaQuery.from(Feedback.class); + criteriaQuery.select(root); + Predicate where = builder.equal(root.get(idName),idVal); + criteriaQuery.where(where); + return sessionFactory.getCurrentSession().createQuery(criteriaQuery); + } +} \ No newline at end of file diff --git a/src/main/java/io/khasang/bazaar/dao/impl/GoodsCategoryDaoImpl.java b/src/main/java/io/khasang/bazaar/dao/impl/GoodsCategoryDaoImpl.java new file mode 100644 index 0000000..7dc3eb8 --- /dev/null +++ b/src/main/java/io/khasang/bazaar/dao/impl/GoodsCategoryDaoImpl.java @@ -0,0 +1,24 @@ +package io.khasang.bazaar.dao.impl; + +import io.khasang.bazaar.dao.GoodsCategoryDao; +import io.khasang.bazaar.entity.GoodsCategory; + +import java.util.List; + +/** + * Implementation of GoodsCategoryDao interface for representing DAO for Goods Category entity. + * + * @author Zulfia Garifullina + * @date 26.09.2017. + */ +public class GoodsCategoryDaoImpl extends BasicDaoImpl implements GoodsCategoryDao { + public GoodsCategoryDaoImpl(Class entityClass) { + super(entityClass); + } + + @Override + public List getGoodsCategoriesByName(String name) { + return (List) sessionFactory.getCurrentSession(). + createQuery("from GoodsCategory as gc where gc.name = ?").setParameter(0, name).list(); + } +} diff --git a/src/main/java/io/khasang/bazaar/dao/impl/GoodsDaoImpl.java b/src/main/java/io/khasang/bazaar/dao/impl/GoodsDaoImpl.java new file mode 100644 index 0000000..a8d5c11 --- /dev/null +++ b/src/main/java/io/khasang/bazaar/dao/impl/GoodsDaoImpl.java @@ -0,0 +1,24 @@ +package io.khasang.bazaar.dao.impl; + +import io.khasang.bazaar.dao.GoodsDao; +import io.khasang.bazaar.entity.Goods; + +import java.util.List; + +/** + * Implementation of GoodsDao interface for representing DAO for Goods entity. + * + * @author Zulfia Garifullina + * @date 04.10.2017. + */ +public class GoodsDaoImpl extends BasicDaoImpl implements GoodsDao { + public GoodsDaoImpl(Class entityClass) { + super(entityClass); + } + + @Override + public List getGoodsByName(String name) { + return (List) sessionFactory.getCurrentSession(). + createQuery("from Goods as g where g.name = ?").setParameter(0, name).list(); + } +} diff --git a/src/main/java/io/khasang/bazaar/entity/Feedback.java b/src/main/java/io/khasang/bazaar/entity/Feedback.java new file mode 100644 index 0000000..035f073 --- /dev/null +++ b/src/main/java/io/khasang/bazaar/entity/Feedback.java @@ -0,0 +1,95 @@ +package io.khasang.bazaar.entity; + +import javax.persistence.*; + +/** + * This class describes a database entity that represents feedback made by users about goods they buy. + * + * @author Artem Kovalev + */ +@Entity +@Table(name = "feedback") +public class Feedback { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String message; + + @Column(name="user_id") + private Long user_id; + + @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) + @JoinColumn(name = "goods_id") + private Goods good; + + @Column(name="rating") + private Short feedbackRating; + + public Feedback() { + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Feedback feedback = (Feedback) o; + + if (!id.equals(feedback.id)) return false; + if (message != null ? !message.equals(feedback.message) : feedback.message != null) return false; + if (user_id != null ? !user_id.equals(feedback.user_id) : feedback.user_id != null) return false; + if (good != null ? !good.equals(feedback.good) : feedback.good != null) return false; + return feedbackRating != null ? feedbackRating.equals(feedback.feedbackRating) : feedback.feedbackRating == null; + } + + @Override + public int hashCode() { + int result = id.hashCode(); + result = 31 * result + (message != null ? message.hashCode() : 0); + result = 31 * result + (user_id != null ? user_id.hashCode() : 0); + result = 31 * result + (good != null ? good.hashCode() : 0); + result = 31 * result + (feedbackRating != null ? feedbackRating.hashCode() : 0); + return result; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public Long getUser_id() { + return user_id; + } + + public void setUser_id(Long user_id) { + this.user_id = user_id; + } + + public Goods getGood() { + return good; + } + + public void setGoods(Goods good) { + this.good = good; + } + + public Short getFeedbackRating() { + return feedbackRating; + } + + public void setFeedbackRating(Short feedbackRating) { + this.feedbackRating = feedbackRating; + } +} diff --git a/src/main/java/io/khasang/bazaar/entity/Goods.java b/src/main/java/io/khasang/bazaar/entity/Goods.java new file mode 100644 index 0000000..f8a73ef --- /dev/null +++ b/src/main/java/io/khasang/bazaar/entity/Goods.java @@ -0,0 +1,120 @@ +package io.khasang.bazaar.entity; + +import javax.persistence.*; + +/** + * This class describes a database entity that represents goods for sale. + * + * @author Zulfia Garifullina + * @date 03.10.2017. + */ +@Entity +@Table(name = "goods") +public class Goods { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "goods_id") + private Long id; + + @Column(nullable = false) + private String name; + + @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL) + @JoinColumn(name = "category_id") + private GoodsCategory category; + + private String description; + private Integer price; + + @Column(name = "quantity_in_stock") + private Integer quantityInStock; + + @Column(name = "quantity_reserved") + private Integer quantityReserved; + + public Goods() { + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Goods goods = (Goods) o; + + if (id != null ? !id.equals(goods.id) : goods.id != null) return false; + if (name != null ? !name.equals(goods.name) : goods.name != null) return false; + if (category != null ? !category.equals(goods.category) : goods.category != null) return false; + if (price != null ? !price.equals(goods.price) : goods.price != null) return false; + if (quantityInStock != null ? !quantityInStock.equals(goods.quantityInStock) : goods.quantityInStock != null) + return false; + return quantityReserved != null ? quantityReserved.equals(goods.quantityReserved) : goods.quantityReserved == null; + } + + @Override + public int hashCode() { + int result = id != null ? id.hashCode() : 0; + result = 31 * result + (name != null ? name.hashCode() : 0); + result = 31 * result + (category != null ? category.hashCode() : 0); + result = 31 * result + (price != null ? price.hashCode() : 0); + result = 31 * result + (quantityInStock != null ? quantityInStock.hashCode() : 0); + result = 31 * result + (quantityReserved != null ? quantityReserved.hashCode() : 0); + return result; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public GoodsCategory getCategory() { + return category; + } + + public void setCategory(GoodsCategory category) { + this.category = category; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Integer getPrice() { + return price; + } + + public void setPrice(Integer price) { + this.price = price; + } + + public Integer getQuantityInStock() { + return quantityInStock; + } + + public void setQuantityInStock(Integer quantityInStock) { + this.quantityInStock = quantityInStock; + } + + public Integer getQuantityReserved() { + return quantityReserved; + } + + public void setQuantityReserved(Integer quantityReserved) { + this.quantityReserved = quantityReserved; + } +} diff --git a/src/main/java/io/khasang/bazaar/entity/GoodsCategory.java b/src/main/java/io/khasang/bazaar/entity/GoodsCategory.java new file mode 100644 index 0000000..093d967 --- /dev/null +++ b/src/main/java/io/khasang/bazaar/entity/GoodsCategory.java @@ -0,0 +1,70 @@ +package io.khasang.bazaar.entity; + +import javax.persistence.*; + +/** + * This class describes a database entity that represents categories of goods + * + * @author Zulfia Garifullina + * @date 26.09.2017. + */ +@Entity +@Table(name = "goods_categories") +public class GoodsCategory { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "category_id") + private Long id; + + @Column(nullable = false) + private String name; + + private String description; + + public GoodsCategory() { + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + GoodsCategory that = (GoodsCategory) o; + + if (id != null ? !id.equals(that.id) : that.id != null) return false; + if (name != null ? !name.equals(that.name) : that.name != null) return false; + return description != null ? description.equals(that.description) : that.description == null; + } + + @Override + public int hashCode() { + int result = id != null ? id.hashCode() : 0; + result = 31 * result + (name != null ? name.hashCode() : 0); + result = 31 * result + (description != null ? description.hashCode() : 0); + return result; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +} diff --git a/src/main/java/io/khasang/bazaar/model/CaseWhenTable.java b/src/main/java/io/khasang/bazaar/model/CaseWhenTable.java new file mode 100644 index 0000000..137def7 --- /dev/null +++ b/src/main/java/io/khasang/bazaar/model/CaseWhenTable.java @@ -0,0 +1,36 @@ +package io.khasang.bazaar.model; + +import org.springframework.jdbc.core.JdbcTemplate; + +/** + * Created by Zulfia Garifullina on 20.09.2017. + */ +public class CaseWhenTable { + private JdbcTemplate jdbcTemplate; + + public CaseWhenTable() { + } + + public CaseWhenTable(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public String caseWhenStatus() { + try { + jdbcTemplate.execute("SELECT title, release_year,\n " + + " CASE WHEN release_year > 2000 THEN 'Meh' ELSE 'Brilliant' END AS review\n" + + " FROM films;"); + return "Case when successful"; + } catch (Exception e) { + return "Case when failed " + e; + } + } + + public JdbcTemplate getJdbcTemplate() { + return jdbcTemplate; + } + + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } +} diff --git a/src/main/java/io/khasang/bazaar/model/CreateTable.java b/src/main/java/io/khasang/bazaar/model/CreateTable.java index abe98c7..e49fb55 100644 --- a/src/main/java/io/khasang/bazaar/model/CreateTable.java +++ b/src/main/java/io/khasang/bazaar/model/CreateTable.java @@ -15,20 +15,26 @@ public CreateTable(JdbcTemplate jdbcTemplate) { public String createStatus() { try { jdbcTemplate.execute("DROP TABLE IF EXISTS films"); -// jdbcTemplate.execute("CREATE TABLE films (\n" + -// "code char(5) CONSTRAINT firstkey PRIMARY KEY, \n" + -// "title varchar(255) NOT NULL , \n" + -// "id integer NOT NULL);"); - jdbcTemplate.execute("CREATE TABLE public.films\n" + + jdbcTemplate.execute("CREATE TABLE films (\n" + + "code CHAR (5) CONSTRAINT films_pkey PRIMARY KEY, \n" + + "title VARCHAR (255) NOT NULL , \n" + + "director_l_name VARCHAR (255) NOT NULL , \n" + + "release_year INTEGER NOT NULL);"); + jdbcTemplate.execute("DROP TABLE IF EXISTS directors"); + jdbcTemplate.execute("CREATE TABLE directors (\n" + + "id INTEGER CONSTRAINT directors_pkey PRIMARY KEY, \n" + + "f_name VARCHAR(255) NOT NULL , \n" + + "l_name VARCHAR(255) NOT NULL);"); +/* jdbcTemplate.execute("CREATE TABLE public.films\n" + "(\n" + " code character(5) NOT NULL,\n" + " title character varying(255) NOT NULL,\n" + - " id integer NOT NULL,\n" + + " year integer NOT NULL,\n" + " CONSTRAINT firstkey PRIMARY KEY (code)\n" + - ");"); + ");");*/ return "Table created"; } catch (Exception e) { - return "Table creation failed" + e; + return "Table creation failed " + e; } } diff --git a/src/main/java/io/khasang/bazaar/model/DeleteFromTable.java b/src/main/java/io/khasang/bazaar/model/DeleteFromTable.java new file mode 100644 index 0000000..e777f17 --- /dev/null +++ b/src/main/java/io/khasang/bazaar/model/DeleteFromTable.java @@ -0,0 +1,35 @@ +package io.khasang.bazaar.model; + +import org.springframework.jdbc.core.JdbcTemplate; + +/** + * Created by Zulfia Garifullina on 20.09.2017. + */ +public class DeleteFromTable { + private JdbcTemplate jdbcTemplate; + + public DeleteFromTable() { + } + + public DeleteFromTable(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public String deleteStatus() { + try { + jdbcTemplate.execute("DELETE FROM films\n" + + " WHERE title = 'Wonder Woman' AND release_year = 2017;"); + return "Table record deleted"; + } catch (Exception e) { + return "Table record deletion failed " + e; + } + } + + public JdbcTemplate getJdbcTemplate() { + return jdbcTemplate; + } + + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } +} diff --git a/src/main/java/io/khasang/bazaar/model/JoinTable.java b/src/main/java/io/khasang/bazaar/model/JoinTable.java new file mode 100644 index 0000000..650ca7a --- /dev/null +++ b/src/main/java/io/khasang/bazaar/model/JoinTable.java @@ -0,0 +1,35 @@ +package io.khasang.bazaar.model; + +import org.springframework.jdbc.core.JdbcTemplate; + +/** + * Created by Zulfia Garifullina on 20.09.2017. + */ +public class JoinTable { + private JdbcTemplate jdbcTemplate; + + public JoinTable() { + } + + public JoinTable(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public String joinStatus() { + try { + jdbcTemplate.execute("SELECT title, f_name, l_name FROM films f JOIN directors d\n" + + " ON f.director_l_name = d.l_name WHERE release_year < 2000;"); + return "Join successful"; + } catch (Exception e) { + return "Join failed " + e; + } + } + + public JdbcTemplate getJdbcTemplate() { + return jdbcTemplate; + } + + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } +} diff --git a/src/main/java/io/khasang/bazaar/model/PopulateTable.java b/src/main/java/io/khasang/bazaar/model/PopulateTable.java new file mode 100644 index 0000000..5e06214 --- /dev/null +++ b/src/main/java/io/khasang/bazaar/model/PopulateTable.java @@ -0,0 +1,56 @@ +package io.khasang.bazaar.model; + +import org.springframework.jdbc.core.JdbcTemplate; + +public class PopulateTable { + private JdbcTemplate jdbcTemplate; + + public PopulateTable() { + } + + public PopulateTable(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public String populateStatus() { + try { + jdbcTemplate.execute("DELETE FROM films;"); + jdbcTemplate.execute("INSERT INTO films\n" + + "(code, title, director_l_name, release_year)\n" + + " VALUES ('HR814', 'Pulp Fiction', 'Tarantino', 1994);"); + jdbcTemplate.execute("INSERT INTO films\n" + + "(code, title, director_l_name, release_year)\n" + + " VALUES ('KW872', 'The Fifth Element', 'Besson', 1997);"); + jdbcTemplate.execute("INSERT INTO films\n" + + "(code, title, director_l_name, release_year)\n" + + " VALUES ('TE271', 'Titanic', 'Cameron', 1999);"); + jdbcTemplate.execute("INSERT INTO films\n" + + "(code, title, director_l_name, release_year)\n" + + " VALUES ('WT732', 'Wonder Woman', 'Jenkins', 2017);"); + jdbcTemplate.execute("DELETE FROM directors;"); + jdbcTemplate.execute("INSERT INTO directors\n" + + "(id, f_name, l_name)\n" + + " VALUES ('201', 'Quentin', 'Tarantino');"); + jdbcTemplate.execute("INSERT INTO directors\n" + + "(id, f_name, l_name)\n" + + " VALUES ('202', 'Luc', 'Besson');"); + jdbcTemplate.execute("INSERT INTO directors\n" + + "(id, f_name, l_name)\n" + + " VALUES ('203', 'James', 'Cameron');"); + jdbcTemplate.execute("INSERT INTO directors\n" + + "(id, f_name, l_name)\n" + + " VALUES ('204', 'Patty', 'Jenkins');"); + return "Table populated"; + } catch (Exception e) { + return "Table population failed " + e; + } + } + + public JdbcTemplate getJdbcTemplate() { + return jdbcTemplate; + } + + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } +} diff --git a/src/main/java/io/khasang/bazaar/model/SelectFromTable.java b/src/main/java/io/khasang/bazaar/model/SelectFromTable.java new file mode 100644 index 0000000..1f7f608 --- /dev/null +++ b/src/main/java/io/khasang/bazaar/model/SelectFromTable.java @@ -0,0 +1,35 @@ +package io.khasang.bazaar.model; + +import org.springframework.jdbc.core.JdbcTemplate; + +/** + * Created by Zulfia Garifullina on 20.09.2017. + */ +public class SelectFromTable { + private JdbcTemplate jdbcTemplate; + + public SelectFromTable() { + } + + public SelectFromTable(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public String selectStatus() { + try { + jdbcTemplate.execute("SELECT title FROM films\n" + + " WHERE release_year = 1994;"); + return "Select successful"; + } catch (Exception e) { + return "Select failed " + e; + } + } + + public JdbcTemplate getJdbcTemplate() { + return jdbcTemplate; + } + + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } +} diff --git a/src/main/java/io/khasang/bazaar/model/SubqueryTable.java b/src/main/java/io/khasang/bazaar/model/SubqueryTable.java new file mode 100644 index 0000000..7343a59 --- /dev/null +++ b/src/main/java/io/khasang/bazaar/model/SubqueryTable.java @@ -0,0 +1,35 @@ +package io.khasang.bazaar.model; + +import org.springframework.jdbc.core.JdbcTemplate; + +/** + * Created by Zulfia Garifullina on 20.09.2017. + */ +public class SubqueryTable { + private JdbcTemplate jdbcTemplate; + + public SubqueryTable() { + } + + public SubqueryTable(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public String subqueryStatus() { + try { + jdbcTemplate.execute("SELECT title FROM films\n" + + " WHERE director_l_name IN (SELECT l_name FROM directors WHERE id = 203);"); + return "Subquery successful"; + } catch (Exception e) { + return "Subquery failed " + e; + } + } + + public JdbcTemplate getJdbcTemplate() { + return jdbcTemplate; + } + + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } +} diff --git a/src/main/java/io/khasang/bazaar/model/UpdateTable.java b/src/main/java/io/khasang/bazaar/model/UpdateTable.java new file mode 100644 index 0000000..8eae0cc --- /dev/null +++ b/src/main/java/io/khasang/bazaar/model/UpdateTable.java @@ -0,0 +1,36 @@ +package io.khasang.bazaar.model; + +import org.springframework.jdbc.core.JdbcTemplate; + +/** + * Created by Zulfia Garifullina on 20.09.2017. + */ +public class UpdateTable { + private JdbcTemplate jdbcTemplate; + + public UpdateTable() { + } + + public UpdateTable(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } + + public String updateStatus() { + try { + jdbcTemplate.execute("UPDATE films\n" + + " SET release_year = release_year - 2\n" + + " WHERE title = 'Titanic';"); + return "Table updated"; + } catch (Exception e) { + return "Table update failed " + e; + } + } + + public JdbcTemplate getJdbcTemplate() { + return jdbcTemplate; + } + + public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { + this.jdbcTemplate = jdbcTemplate; + } +} diff --git a/src/main/java/io/khasang/bazaar/service/FeedbackService.java b/src/main/java/io/khasang/bazaar/service/FeedbackService.java new file mode 100644 index 0000000..96b0d52 --- /dev/null +++ b/src/main/java/io/khasang/bazaar/service/FeedbackService.java @@ -0,0 +1,63 @@ +package io.khasang.bazaar.service; + +import io.khasang.bazaar.entity.Feedback; + +import java.util.List; + +public interface FeedbackService { + /** + * Receive Feedback by id + * + * @param id - feedback's id what we want to receive + * @return feedback + */ + Feedback getById(Long id); + + /** + * Create feedback at database + * + * @param feedback - feedback for creation + * @return feedback + */ + Feedback addFeedback(Feedback feedback); + + /** + * Update feedback at database + * + * @param feedback - feedback to update + * @return feedback + */ + Feedback updateFeedback(Feedback feedback); + + /** + * Receive all feedback records from database made by user specified by his id + * + * @param user_id - user's id + * @return list of feedback records + */ + List getFeedbacksByUser(Long user_id); + + /** + * Receive all feedback records from database about some good item specified by it's id + * + * @param good_id - good's id + * @return list of feedback records + */ + List getFeedbacksByGood(Long good_id); + + /** + * Receive average feedback rating for good item specified by it's id + * + * @param good_id - good's id + * @return list of feedback records + */ + Double getGoodAverageFeedbackRating(Long good_id); + + /** + * Delete feedbacks from database + * + * @param id - feedbacks's id for delete + * @return feedback + */ + Feedback deleteFeedback (Long id); +} diff --git a/src/main/java/io/khasang/bazaar/service/GoodsCategoryService.java b/src/main/java/io/khasang/bazaar/service/GoodsCategoryService.java new file mode 100644 index 0000000..f69b918 --- /dev/null +++ b/src/main/java/io/khasang/bazaar/service/GoodsCategoryService.java @@ -0,0 +1,60 @@ +package io.khasang.bazaar.service; + +import io.khasang.bazaar.entity.GoodsCategory; + +import java.util.List; + +/** + * Service interface for Goods Category entity. + * + * @author Zulfia Garifullina + * @date 26.09.2017. + */ +public interface GoodsCategoryService { + /** + * Method for retrieving goods categories by their database id. + * + * @param id - id of the category in the database + * @return a category of goods with given id + */ + GoodsCategory getById(Long id); + + /** + * Method for retrieving goods categories from the database by their name. + * + * @param name - name of the category + * @return all the goods categories with the given name + */ + List getGoodsCategoriesByName(String name); + + /** + * Method for adding a new goods category to the database table + * + * @param goodsCategory - the object being added to the database table as a row + * @return the object that was added + */ + GoodsCategory addGoodsCategory(GoodsCategory goodsCategory); + + /** + * Method for updating a goods category in the database + * + * @param goodsCategory - the object being updated in the database + * @return the goods category that was updated + */ + GoodsCategory updateGoodsCategory(GoodsCategory goodsCategory); + + /** + * Deletes the goods category with the specified id from the database + * + * @param id - database id of the goods category that needs to be deleted + * @return the deleted goods category + */ + GoodsCategory deleteGoodsCategory(Long id); + + /** + * Method returns all the goods categories from the corresponding table in the database. + * + * @return a List of goods categories objects from the database table. + */ + List getList(); +} diff --git a/src/main/java/io/khasang/bazaar/service/GoodsService.java b/src/main/java/io/khasang/bazaar/service/GoodsService.java new file mode 100644 index 0000000..e633d79 --- /dev/null +++ b/src/main/java/io/khasang/bazaar/service/GoodsService.java @@ -0,0 +1,90 @@ +package io.khasang.bazaar.service; + +import io.khasang.bazaar.entity.Goods; + +import java.util.List; + +/** + * Service interface for Goods entity. + * + * @author Zulfia Garifullina + * @date 04.10.2017. + */ +public interface GoodsService { + /** + * Method for retrieving goods by their database id. + * + * @param id - id of the goods in the database + * @return goods with given id + */ + Goods getById(Long id); + + /** + * Method for retrieving goods from the database by their name. + * + * @param name - name of the goods + * @return all the goods with the given name + */ + List getGoodsByName(String name); + + /** + * Method for adding new goods to the database table + * + * @param goods- the object being added to the database table as a row + * @return the goods that was added + */ + Goods addGoods(Goods goods); + + /** + * Method for updating goods in the database + * + * @param goods - the object being updated in the database + * @return the goods that were updated + */ + Goods updateGoods(Goods goods); + + /** + * Deletes the goods with the specified id from the database + * + * @param id database id of the goods that needs to be deleted + * @return the deleted goods + */ + Goods deleteGoods(Long id); + + /** + * Method returns all the goods from the corresponding table in the database. + * + * @return a List of goods objects from the database table. + */ + List getList(); + + /** + * Reserves a specified amount of goods and reduces the amount in stock by that number. + * This method should be called before processing order payment. + * + * @param id database id for chosen goods + * @param quantity quantity to be reserved + * @return goods that were reserved + */ + Goods reserveGoods(Long id, Integer quantity); + + /** + * Cancels reservation on a specified amount of goods and increases the amount in stock by that number. + * This method should be called if processing order payment failed. + * + * @param id database id for chosen goods + * @param quantity quantity to be removed from reserved and added back to quantity in stock + * @return goods that were removed from reserved and added back to quantity in stock + */ + Goods unreserveGoods(Long id, Integer quantity); + + /** + * Reduces the amount of goods which were reserved before payment processing, by the specified amount. + * This method should be called after successful processing of order payment. + * + * @param id database id for chosen goods + * @param quantity amount by which reserved quantity is reduced + * @return goods for which reserved quantity was reduced + */ + Goods buyGoods(Long id, Integer quantity); +} diff --git a/src/main/java/io/khasang/bazaar/service/impl/CatServiceImpl.java b/src/main/java/io/khasang/bazaar/service/impl/CatServiceImpl.java index 49d716f..306fd7b 100644 --- a/src/main/java/io/khasang/bazaar/service/impl/CatServiceImpl.java +++ b/src/main/java/io/khasang/bazaar/service/impl/CatServiceImpl.java @@ -31,12 +31,12 @@ public Cat deleteCat(Long id) { @Override public Cat addCat(Cat cat) { - return catDao.addCat(cat); + return catDao.add(cat); } @Override public Cat updateCat(Cat cat) { - return catDao.updateCat(cat); + return catDao.update(cat); } @Override diff --git a/src/main/java/io/khasang/bazaar/service/impl/FeedbackServiceImpl.java b/src/main/java/io/khasang/bazaar/service/impl/FeedbackServiceImpl.java new file mode 100644 index 0000000..5eba276 --- /dev/null +++ b/src/main/java/io/khasang/bazaar/service/impl/FeedbackServiceImpl.java @@ -0,0 +1,51 @@ +package io.khasang.bazaar.service.impl; + +import io.khasang.bazaar.dao.FeedbackDao; +import io.khasang.bazaar.entity.Feedback; +import io.khasang.bazaar.service.FeedbackService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +@Service("feedbackService") +public class FeedbackServiceImpl implements FeedbackService { + @Autowired + public FeedbackDao feedbackDao; + + @Override + public Feedback getById(Long id) { + return feedbackDao.getById(id); + } + + @Override + public Feedback addFeedback(Feedback feedback) { + return feedbackDao.add(feedback); + } + + @Override + public Feedback updateFeedback(Feedback feedback) { + return feedbackDao.update(feedback); + } + + @Override + public List getFeedbacksByUser(Long user_id) { + return feedbackDao.getFeedbacksByUser(user_id); + } + + @Override + public List getFeedbacksByGood(Long good_id) { + return feedbackDao.getFeedbacksByGood(good_id); + } + + @Override + public Double getGoodAverageFeedbackRating(Long good_id) { + return feedbackDao.getGoodAverageFeedbackRating(good_id); + } + + @Override + public Feedback deleteFeedback(Long id) { + Feedback feedback = getById(id); + return feedbackDao.delete(feedback); + } +} diff --git a/src/main/java/io/khasang/bazaar/service/impl/GoodsCategoryServiceImpl.java b/src/main/java/io/khasang/bazaar/service/impl/GoodsCategoryServiceImpl.java new file mode 100644 index 0000000..01876be --- /dev/null +++ b/src/main/java/io/khasang/bazaar/service/impl/GoodsCategoryServiceImpl.java @@ -0,0 +1,52 @@ +package io.khasang.bazaar.service.impl; + +import io.khasang.bazaar.dao.GoodsCategoryDao; +import io.khasang.bazaar.entity.GoodsCategory; +import io.khasang.bazaar.service.GoodsCategoryService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * Implementation of the service interface for Goods Category entity. + * + * @author Zulfia Garifullina + * @date 26.09.2017. + */ +@Service("goodsCategoryService") +public class GoodsCategoryServiceImpl implements GoodsCategoryService { + @Autowired + private GoodsCategoryDao goodsCategoryDao; + + @Override + public GoodsCategory getById(Long id) { + return goodsCategoryDao.getById(id); + } + + @Override + public List getGoodsCategoriesByName(String name) { + return goodsCategoryDao.getGoodsCategoriesByName(name); + } + + @Override + public GoodsCategory addGoodsCategory(GoodsCategory goodsCategory) { + return goodsCategoryDao.add(goodsCategory); + } + + @Override + public GoodsCategory updateGoodsCategory(GoodsCategory goodsCategory) { + return goodsCategoryDao.update(goodsCategory); + } + + @Override + public GoodsCategory deleteGoodsCategory(Long id) { + GoodsCategory goodsCategory = goodsCategoryDao.getById(id); + return goodsCategoryDao.delete(goodsCategory); + } + + @Override + public List getList() { + return goodsCategoryDao.getList(); + } +} diff --git a/src/main/java/io/khasang/bazaar/service/impl/GoodsServiceImpl.java b/src/main/java/io/khasang/bazaar/service/impl/GoodsServiceImpl.java new file mode 100644 index 0000000..aca1140 --- /dev/null +++ b/src/main/java/io/khasang/bazaar/service/impl/GoodsServiceImpl.java @@ -0,0 +1,75 @@ +package io.khasang.bazaar.service.impl; + +import io.khasang.bazaar.dao.GoodsDao; +import io.khasang.bazaar.entity.Goods; +import io.khasang.bazaar.service.GoodsService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * Implementation of the service interface for Goods entity. + * + * @author Zulfia Garifullina + * @date 04.10.2017. + */ +@Service("goodsService") +public class GoodsServiceImpl implements GoodsService { + @Autowired + private GoodsDao goodsDao; + + @Override + public Goods getById(Long id) { + return goodsDao.getById(id); + } + + @Override + public List getGoodsByName(String name) { + return goodsDao.getGoodsByName(name); + } + + @Override + public Goods addGoods(Goods goods) { + return goodsDao.add(goods); + } + + @Override + public Goods updateGoods(Goods goods) { + return goodsDao.update(goods); + } + + @Override + public Goods deleteGoods(Long id) { + Goods goods = goodsDao.getById(id); + return goodsDao.delete(goods); + } + + @Override + public List getList() { + return goodsDao.getList(); + } + + @Override + public Goods reserveGoods(Long id, Integer quantity) { + Goods goods = goodsDao.getById(id); + goods.setQuantityInStock(goods.getQuantityInStock() - quantity); + goods.setQuantityReserved(goods.getQuantityReserved() + quantity); + return goodsDao.update(goods); + } + + @Override + public Goods unreserveGoods(Long id, Integer quantity) { + Goods goods = goodsDao.getById(id); + goods.setQuantityInStock(goods.getQuantityInStock() + quantity); + goods.setQuantityReserved(goods.getQuantityReserved() - quantity); + return goodsDao.update(goods); + } + + @Override + public Goods buyGoods(Long id, Integer quantity) { + Goods goods = goodsDao.getById(id); + goods.setQuantityReserved(goods.getQuantityReserved() - quantity); + return goodsDao.update(goods); + } +} diff --git a/src/main/resources/hibernate.properties b/src/main/resources/hibernate.properties index 2b0a6e8..c7c6766 100644 --- a/src/main/resources/hibernate.properties +++ b/src/main/resources/hibernate.properties @@ -5,5 +5,5 @@ hibernate.password=root hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect hibernate.show_sql=true hibernate.format_sql=true -hibernate.hbm2ddl.auto=update +hibernate.hbm2ddl.auto=create diff --git a/src/test/java/io/khasang/bazaar/CatControllerIntegrationTest.java b/src/test/java/io/khasang/bazaar/CatControllerIntegrationTest.java index 1452ec8..5281e58 100644 --- a/src/test/java/io/khasang/bazaar/CatControllerIntegrationTest.java +++ b/src/test/java/io/khasang/bazaar/CatControllerIntegrationTest.java @@ -34,8 +34,8 @@ public void addCat() { Cat receivedCat = responseEntity.getBody(); assertEquals("OK", responseEntity.getStatusCode().getReasonPhrase()); assertNotNull(receivedCat); - assertNotNull(receivedCat.getCatWomanList()); - assertEquals("Murka", receivedCat.getCatWomanList().get(1).getName()); + //assertNotNull(receivedCat.getCatWomanList()); + //assertEquals("Murka", receivedCat.getCatWomanList().get(1).getName()); assertEquals(cat.getName(), receivedCat.getName()); } @@ -93,7 +93,7 @@ private Cat prefillCat() { List list = new ArrayList<>(); list.add(catWoman1); list.add(catWoman2); - cat.setCatWomanList(list); + //cat.setCatWomanList(list); return cat; } diff --git a/src/test/java/io/khasang/bazaar/FeedbackControllerIntegrationTest.java b/src/test/java/io/khasang/bazaar/FeedbackControllerIntegrationTest.java new file mode 100644 index 0000000..2461ec7 --- /dev/null +++ b/src/test/java/io/khasang/bazaar/FeedbackControllerIntegrationTest.java @@ -0,0 +1,71 @@ +package io.khasang.bazaar; + +import io.khasang.bazaar.entity.Feedback; +import io.khasang.bazaar.entity.Goods; +import org.junit.Test; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.*; +import org.springframework.web.client.RestTemplate; + +import java.util.List; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +public class FeedbackControllerIntegrationTest { + private final String ROOT = "http://localhost:8080/feedback"; + private final String ADD = "/add"; + private final String GET_BY_ID = "/get/id"; + private final String GET_ALL_BY_USER_ID = "/get/byuser"; + private final String GET_ALL_BY_GOOD_ID = "/get/bygood"; + + @Test + public void getFeedbackByUserId() { + RestTemplate restTemplate = new RestTemplate(); + createfeedback(); + Feedback createdFeedback = createfeedback(); + + ResponseEntity> result = restTemplate.exchange( + ROOT + GET_ALL_BY_USER_ID+"/{user_id}", + HttpMethod.GET, + null, + new ParameterizedTypeReference>(){}, + createdFeedback.getUser_id() + ); + + assertEquals(HttpStatus.OK, result.getStatusCode()); + assertNotNull(result.getBody()); + } + + private Feedback createfeedback() { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); + Feedback feedback = prefillFeedback(); + HttpEntity httpEntity = new HttpEntity<>(feedback, httpHeaders); + RestTemplate restTemplate = new RestTemplate(); + Feedback createdFeedback = restTemplate.exchange( + ROOT + ADD, + HttpMethod.PUT, + httpEntity, + Feedback.class).getBody(); + assertNotNull(createdFeedback); + assertEquals((Long)1L,createdFeedback.getUser_id()); + assertEquals((Long)1L,createdFeedback.getGood_id()); + //assertEquals(java.util.Optional.of(5),createdFeedback.getFeedbackRating()); + assertNotNull(createdFeedback.getId()); + return createdFeedback; + } + + private Goods prefillGoods() { + + } + + private Feedback prefillFeedback() { + Feedback feedback = new Feedback(); + feedback.setMessage("It's a test message by user 1 about good 1"); + feedback.setUser_id(1L); + feedback.setGood_id(1L); + feedback.setFeedbackRating((short)5); + return feedback; + } +} diff --git a/src/test/java/io/khasang/bazaar/GoodsCategoryControllerIntegrationTest.java b/src/test/java/io/khasang/bazaar/GoodsCategoryControllerIntegrationTest.java new file mode 100644 index 0000000..b450d70 --- /dev/null +++ b/src/test/java/io/khasang/bazaar/GoodsCategoryControllerIntegrationTest.java @@ -0,0 +1,177 @@ +package io.khasang.bazaar; + +import io.khasang.bazaar.entity.GoodsCategory; +import org.junit.Test; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.*; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; + +import java.util.List; + +import static org.junit.Assert.*; + +/** + * Integration tests for GoodsCategoryController. + * + * @author Zulfia Garifullina + * @date 04.10.2017. + */ +public class GoodsCategoryControllerIntegrationTest { + private final String ROOT = "http://localhost:8080/goodscategories"; + private final String ADD = "/add"; + private final String UPDATE = "/update"; + private final String DELETE = "/delete"; + private final String GET_BY_ID = "/get/id"; + private final String GET_BY_NAME = "/get/name"; + private final String GET_ALL = "/all"; + + @Test + public void addGoodsCategory() { + GoodsCategory goodsCategory = createGoodsCategory(); + + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity responseEntity = restTemplate.exchange( + ROOT + GET_BY_ID + "/{id}", + HttpMethod.GET, + null, + GoodsCategory.class, + goodsCategory.getId() + ); + + GoodsCategory receivedGoodsCategory = responseEntity.getBody(); + assertEquals("OK", responseEntity.getStatusCode().getReasonPhrase()); + assertNotNull(receivedGoodsCategory); + assertEquals(goodsCategory.getName(), receivedGoodsCategory.getName()); + assertEquals(goodsCategory.getDescription(), receivedGoodsCategory.getDescription()); + } + + @Test + public void updateGoodsCategory() { + GoodsCategory goodsCategory = createGoodsCategory(); + GoodsCategory changedGoodsCategory = changeGoodsCategory(goodsCategory); + + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity responseEntity = restTemplate.exchange( + ROOT + GET_BY_ID + "/{id}", + HttpMethod.GET, + null, + GoodsCategory.class, + changedGoodsCategory.getId() + ); + + GoodsCategory receivedGoodsCategory = responseEntity.getBody(); + assertEquals("OK", responseEntity.getStatusCode().getReasonPhrase()); + assertNotNull(receivedGoodsCategory); + assertEquals(changedGoodsCategory.getName(), receivedGoodsCategory.getName()); + assertEquals(changedGoodsCategory.getDescription(), receivedGoodsCategory.getDescription()); + } + + @Test + public void deleteGoodsCategory() { + GoodsCategory goodsCategory = createGoodsCategory(); + RestTemplate restTemplate = new RestTemplate(); + + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(ROOT + DELETE) + .queryParam("id", goodsCategory.getId()); + + restTemplate.delete(builder.build().encode().toUri()); + + ResponseEntity responseEntity = restTemplate.exchange( + ROOT + GET_BY_ID + "/{id}", + HttpMethod.GET, + null, + GoodsCategory.class, + goodsCategory.getId() + ); + + GoodsCategory receivedGoodsCategory = responseEntity.getBody(); + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + assertNull(receivedGoodsCategory); + } + + @Test + public void getGoodsCategoryByName() { + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity> result = restTemplate.exchange( + ROOT + GET_BY_NAME + "/{name}", + HttpMethod.GET, + null, + new ParameterizedTypeReference>() { + }, + "Music" + ); + + assertEquals(HttpStatus.OK, result.getStatusCode()); + assertNotNull(result.getBody()); + } + + @Test + public void getAllGoodsCategories() { + RestTemplate restTemplate = new RestTemplate(); + createGoodsCategory(); + createGoodsCategory(); + + ResponseEntity> result = restTemplate.exchange( + ROOT + GET_ALL, + HttpMethod.GET, + null, + new ParameterizedTypeReference>() { + } + ); + + assertEquals(HttpStatus.OK, result.getStatusCode()); + assertNotNull(result.getBody()); + } + + private GoodsCategory createGoodsCategory() { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); + + GoodsCategory goodsCategory = prefillGoodsCategory(); + HttpEntity httpEntity = new HttpEntity<>(goodsCategory, httpHeaders); + + RestTemplate restTemplate = new RestTemplate(); + GoodsCategory createdGoodsCategory = restTemplate.exchange( + ROOT + ADD, + HttpMethod.PUT, + httpEntity, + GoodsCategory.class).getBody(); + assertNotNull(createdGoodsCategory); + assertEquals("Electronics", createdGoodsCategory.getName()); + assertEquals("Gadgets: laptops, smartphones, cameras", createdGoodsCategory.getDescription()); + assertNotNull(createdGoodsCategory.getId()); + + return createdGoodsCategory; + } + + private GoodsCategory prefillGoodsCategory() { + GoodsCategory goodsCategory = new GoodsCategory(); + goodsCategory.setName("Electronics"); + goodsCategory.setDescription("Gadgets: laptops, smartphones, cameras"); + return goodsCategory; + } + + private GoodsCategory changeGoodsCategory(GoodsCategory goodsCategory) { + goodsCategory.setName("Fashion"); + goodsCategory.setDescription("Womenswear and menswear"); + + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); + + HttpEntity httpEntity = new HttpEntity<>(goodsCategory, httpHeaders); + + RestTemplate restTemplate = new RestTemplate(); + GoodsCategory changedGoodsCategory = restTemplate.exchange( + ROOT + UPDATE, + HttpMethod.POST, + httpEntity, + GoodsCategory.class).getBody(); + assertNotNull(changedGoodsCategory); + assertEquals("Fashion", changedGoodsCategory.getName()); + assertEquals("Womenswear and menswear", changedGoodsCategory.getDescription()); + assertNotNull(changedGoodsCategory.getId()); + + return changedGoodsCategory; + } +} diff --git a/src/test/java/io/khasang/bazaar/GoodsControllerIntegrationTest.java b/src/test/java/io/khasang/bazaar/GoodsControllerIntegrationTest.java new file mode 100644 index 0000000..00d1051 --- /dev/null +++ b/src/test/java/io/khasang/bazaar/GoodsControllerIntegrationTest.java @@ -0,0 +1,373 @@ +package io.khasang.bazaar; + +import io.khasang.bazaar.entity.Goods; +import io.khasang.bazaar.entity.GoodsCategory; +import org.junit.Test; +import org.springframework.core.ParameterizedTypeReference; +import org.springframework.http.*; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.util.UriComponentsBuilder; + +import java.util.List; + +import static org.junit.Assert.*; + +/** + * Integration tests for GoodsController. + * + * @author Zulfia Garifullina + * @date 04.10.2017. + */ +public class GoodsControllerIntegrationTest { + private final String ROOT = "http://localhost:8080/goods"; + private final String ADD = "/add"; + private final String UPDATE = "/update"; + private final String DELETE = "/delete"; + private final String GET_BY_ID = "/get/id"; + private final String GET_BY_NAME = "/get/name"; + private final String GET_ALL = "/all"; + private final String RESERVE = "/reserve"; + private final String UNRESERVE = "/unreserve"; + private final String BUY = "/buy"; + + @Test + public void addGoods() { + Goods goods = createGoods(); + + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity responseEntity = restTemplate.exchange( + ROOT + GET_BY_ID + "/{id}", + HttpMethod.GET, + null, + Goods.class, + goods.getId() + ); + + Goods receivedGoods = responseEntity.getBody(); + assertEquals("OK", responseEntity.getStatusCode().getReasonPhrase()); + assertNotNull(receivedGoods); + assertEquals(goods.getName(), receivedGoods.getName()); + assertEquals(goods.getDescription(), receivedGoods.getDescription()); + } + + @Test + public void updateGoods() { + Goods goods = createGoods(); + Goods changedGoods = changeGoods(goods); + + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity responseEntity = restTemplate.exchange( + ROOT + GET_BY_ID + "/{id}", + HttpMethod.GET, + null, + Goods.class, + changedGoods.getId() + ); + + Goods receivedGoods = responseEntity.getBody(); + assertEquals("OK", responseEntity.getStatusCode().getReasonPhrase()); + assertNotNull(receivedGoods); + assertNotNull(receivedGoods.getCategory()); + assertEquals(changedGoods.getName(), receivedGoods.getName()); + assertEquals(changedGoods.getDescription(), receivedGoods.getDescription()); + assertEquals(changedGoods.getCategory(), receivedGoods.getCategory()); + assertEquals(changedGoods.getPrice(), receivedGoods.getPrice()); + assertEquals(changedGoods.getQuantityInStock(), receivedGoods.getQuantityInStock()); + assertEquals(changedGoods.getQuantityReserved(), receivedGoods.getQuantityReserved()); + } + + @Test + public void deleteGoods() { + Goods goods = createGoods(); + RestTemplate restTemplate = new RestTemplate(); + + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(ROOT + DELETE) + .queryParam("id", goods.getId()); + + restTemplate.delete(builder.build().encode().toUri()); + + ResponseEntity responseEntity = restTemplate.exchange( + ROOT + GET_BY_ID + "/{id}", + HttpMethod.GET, + null, + Goods.class, + goods.getId() + ); + + Goods receivedGoods = responseEntity.getBody(); + assertEquals(HttpStatus.OK, responseEntity.getStatusCode()); + assertNull(receivedGoods); + } + + @Test + public void getGoodsByName() { + Goods goods = createGoods(); + + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity> result = restTemplate.exchange( + ROOT + GET_BY_NAME + "/{name}", + HttpMethod.GET, + null, + new ParameterizedTypeReference>() { + }, + goods.getName() + ); + + assertEquals(HttpStatus.OK, result.getStatusCode()); + assertNotNull(result.getBody()); + } + + @Test + public void getAllGoodsCategories() { + RestTemplate restTemplate = new RestTemplate(); + createGoods(); + createGoods(); + + ResponseEntity> result = restTemplate.exchange( + ROOT + GET_ALL, + HttpMethod.GET, + null, + new ParameterizedTypeReference>() { + } + ); + + assertEquals(HttpStatus.OK, result.getStatusCode()); + assertNotNull(result.getBody()); + } + + @Test + public void reserveGoods() { + Goods goods = createGoods(); + Goods reservedGoods = reserve(goods); + + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity responseEntity = restTemplate.exchange( + ROOT + GET_BY_ID + "/{id}", + HttpMethod.GET, + null, + Goods.class, + reservedGoods.getId() + ); + + Goods receivedGoods = responseEntity.getBody(); + assertEquals("OK", responseEntity.getStatusCode().getReasonPhrase()); + assertNotNull(receivedGoods); + assertEquals(reservedGoods.getName(), receivedGoods.getName()); + assertEquals(reservedGoods.getDescription(), receivedGoods.getDescription()); + assertEquals(reservedGoods.getCategory(), receivedGoods.getCategory()); + assertEquals(reservedGoods.getPrice(), receivedGoods.getPrice()); + assertEquals(reservedGoods.getQuantityInStock(), receivedGoods.getQuantityInStock()); + assertEquals(reservedGoods.getQuantityReserved(), receivedGoods.getQuantityReserved()); + } + + @Test + public void unreserveGoods() { + Goods goods = createGoods(); + Goods reservedGoods = reserve(goods); + Goods unreservedGoods = unreserve(reservedGoods); + + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity responseEntity = restTemplate.exchange( + ROOT + GET_BY_ID + "/{id}", + HttpMethod.GET, + null, + Goods.class, + unreservedGoods.getId() + ); + + Goods receivedGoods = responseEntity.getBody(); + assertEquals("OK", responseEntity.getStatusCode().getReasonPhrase()); + assertNotNull(receivedGoods); + assertEquals(unreservedGoods.getName(), receivedGoods.getName()); + assertEquals(unreservedGoods.getDescription(), receivedGoods.getDescription()); + assertEquals(unreservedGoods.getCategory(), receivedGoods.getCategory()); + assertEquals(unreservedGoods.getPrice(), receivedGoods.getPrice()); + assertEquals(unreservedGoods.getQuantityInStock(), receivedGoods.getQuantityInStock()); + assertEquals(unreservedGoods.getQuantityReserved(), receivedGoods.getQuantityReserved()); + } + + @Test + public void buyGoods() { + Goods goods = createGoods(); + Goods reservedGoods = reserve(goods); + Goods boughtGoods = buy(reservedGoods); + + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity responseEntity = restTemplate.exchange( + ROOT + GET_BY_ID + "/{id}", + HttpMethod.GET, + null, + Goods.class, + boughtGoods.getId() + ); + + Goods receivedGoods = responseEntity.getBody(); + assertEquals("OK", responseEntity.getStatusCode().getReasonPhrase()); + assertNotNull(receivedGoods); + assertEquals(boughtGoods.getName(), receivedGoods.getName()); + assertEquals(boughtGoods.getDescription(), receivedGoods.getDescription()); + assertEquals(boughtGoods.getCategory(), receivedGoods.getCategory()); + assertEquals(boughtGoods.getPrice(), receivedGoods.getPrice()); + assertEquals(boughtGoods.getQuantityInStock(), receivedGoods.getQuantityInStock()); + assertEquals(boughtGoods.getQuantityReserved(), receivedGoods.getQuantityReserved()); + } + + private Goods createGoods() { + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); + + Goods goods = prefillGoods(); + HttpEntity httpEntity = new HttpEntity<>(goods, httpHeaders); + + RestTemplate restTemplate = new RestTemplate(); + Goods createdGoods = restTemplate.exchange( + ROOT + ADD, + HttpMethod.PUT, + httpEntity, + Goods.class).getBody(); + assertNotNull(createdGoods); + assertEquals("Skis", createdGoods.getName()); + assertEquals("Mountain skis", createdGoods.getDescription()); + assertEquals("Sports goods", createdGoods.getCategory().getName()); + assertEquals("All you need to keep active", createdGoods.getCategory().getDescription()); + assertEquals(new Integer(5000), createdGoods.getPrice()); + assertEquals(new Integer(600), createdGoods.getQuantityInStock()); + assertEquals(new Integer(200), createdGoods.getQuantityReserved()); + assertNotNull(createdGoods.getId()); + + return createdGoods; + } + + private Goods prefillGoods() { + Goods goods = new Goods(); + goods.setName("Skis"); + goods.setDescription("Mountain skis"); + GoodsCategory category = new GoodsCategory(); + category.setName("Sports goods"); + category.setDescription("All you need to keep active"); + goods.setCategory(category); + goods.setPrice(5000); + goods.setQuantityInStock(600); + goods.setQuantityReserved(200); + return goods; + } + + private Goods changeGoods(Goods goods) { + goods.setName("Snowboard"); + goods.setDescription("All-mountain snowboard"); + goods.setPrice(8000); + goods.setQuantityInStock(400); + goods.setQuantityReserved(150); + + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); + + HttpEntity httpEntity = new HttpEntity<>(goods, httpHeaders); + + RestTemplate restTemplate = new RestTemplate(); + Goods changedGoods = restTemplate.exchange( + ROOT + UPDATE, + HttpMethod.POST, + httpEntity, + Goods.class).getBody(); + assertNotNull(changedGoods); + assertEquals("Snowboard", changedGoods.getName()); + assertEquals("All-mountain snowboard", changedGoods.getDescription()); + assertEquals(new Integer(8000), changedGoods.getPrice()); + assertEquals(new Integer(400), changedGoods.getQuantityInStock()); + assertEquals(new Integer(150), changedGoods.getQuantityReserved()); + assertNotNull(changedGoods.getId()); + + return changedGoods; + } + + private Goods reserve(Goods goods) { + goods.setQuantityInStock(goods.getQuantityInStock() - 10); + goods.setQuantityReserved(goods.getQuantityReserved() + 10); + + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); + + HttpEntity httpEntity = new HttpEntity<>(goods, httpHeaders); + + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(ROOT + RESERVE) + .queryParam("id", goods.getId()) + .queryParam("quantity", 10); + + RestTemplate restTemplate = new RestTemplate(); + Goods reservedGoods = restTemplate.exchange( + builder.build().encode().toUri(), + HttpMethod.POST, + httpEntity, + Goods.class).getBody(); + assertNotNull(reservedGoods); + assertEquals("Skis", reservedGoods.getName()); + assertEquals("Mountain skis", reservedGoods.getDescription()); + assertEquals(new Integer(5000), reservedGoods.getPrice()); + assertEquals(new Integer(590), reservedGoods.getQuantityInStock()); + assertEquals(new Integer(210), reservedGoods.getQuantityReserved()); + assertNotNull(reservedGoods.getId()); + + return reservedGoods; + } + + private Goods unreserve(Goods reservedGoods) { + reservedGoods.setQuantityInStock(reservedGoods.getQuantityInStock() + 10); + reservedGoods.setQuantityReserved(reservedGoods.getQuantityReserved() - 10); + + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); + + HttpEntity httpEntity = new HttpEntity<>(reservedGoods, httpHeaders); + + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(ROOT + UNRESERVE) + .queryParam("id", reservedGoods.getId()) + .queryParam("quantity", 10); + + RestTemplate restTemplate = new RestTemplate(); + Goods unreservedGoods = restTemplate.exchange( + builder.build().encode().toUri(), + HttpMethod.POST, + httpEntity, + Goods.class).getBody(); + assertNotNull(unreservedGoods); + assertEquals("Skis", unreservedGoods.getName()); + assertEquals("Mountain skis", unreservedGoods.getDescription()); + assertEquals(new Integer(5000), unreservedGoods.getPrice()); + assertEquals(new Integer(600), unreservedGoods.getQuantityInStock()); + assertEquals(new Integer(200), unreservedGoods.getQuantityReserved()); + assertNotNull(unreservedGoods.getId()); + + return unreservedGoods; + } + + private Goods buy(Goods reservedGoods) { + reservedGoods.setQuantityInStock(reservedGoods.getQuantityReserved() - 10); + + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.setContentType(MediaType.APPLICATION_JSON_UTF8); + + HttpEntity httpEntity = new HttpEntity<>(reservedGoods, httpHeaders); + + UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(ROOT + BUY) + .queryParam("id", reservedGoods.getId()) + .queryParam("quantity", 10); + + + RestTemplate restTemplate = new RestTemplate(); + Goods boughtGoods = restTemplate.exchange( + builder.build().encode().toUri(), + HttpMethod.POST, + httpEntity, + Goods.class).getBody(); + assertNotNull(boughtGoods); + assertEquals("Skis", boughtGoods.getName()); + assertEquals("Mountain skis", boughtGoods.getDescription()); + assertEquals(new Integer(5000), boughtGoods.getPrice()); + assertEquals(new Integer(590), boughtGoods.getQuantityInStock()); + assertEquals(new Integer(200), boughtGoods.getQuantityReserved()); + assertNotNull(boughtGoods.getId()); + + return boughtGoods; + } +} diff --git a/web/WEB-INF/views/user.jsp b/web/WEB-INF/views/user.jsp new file mode 100644 index 0000000..eccc843 --- /dev/null +++ b/web/WEB-INF/views/user.jsp @@ -0,0 +1,16 @@ +<%-- + Created by IntelliJ IDEA. + User: Zulfia Garifullina + Date: 21.09.2017 + Time: 21:21 + To change this template use File | Settings | File Templates. +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + Title + + +${secure} + +