From 0ba60d4ab972f438b35641942330cc890e9af7eb Mon Sep 17 00:00:00 2001 From: Antonio Vidal Date: Tue, 30 Mar 2021 18:47:55 +0200 Subject: [PATCH] Solo Ofertas activas y Mis ofertas --- .../springframework/cheapy/model/Client.java | 25 +-- .../repository/FoodOfferRepository.java | 11 +- .../cheapy/repository/NuOfferRepository.java | 13 +- .../repository/SpeedOfferRepository.java | 12 +- .../repository/TimeOfferRepository.java | 12 +- .../cheapy/service/FoodOfferService.java | 11 +- .../cheapy/service/NuOfferService.java | 9 + .../cheapy/service/SpeedOfferService.java | 10 ++ .../cheapy/service/TimeOfferService.java | 11 +- .../cheapy/web/OfertaController.java | 37 +++- src/main/resources/db/mysql/data.sql | 2 +- .../WEB-INF/jsp/offers/myOffersList.jsp | 158 ++++++++++++++++++ src/main/webapp/WEB-INF/tags/menu.tag | 8 +- 13 files changed, 292 insertions(+), 27 deletions(-) create mode 100644 src/main/webapp/WEB-INF/jsp/offers/myOffersList.jsp diff --git a/src/main/java/org/springframework/cheapy/model/Client.java b/src/main/java/org/springframework/cheapy/model/Client.java index 23fa89b80..e3f939728 100644 --- a/src/main/java/org/springframework/cheapy/model/Client.java +++ b/src/main/java/org/springframework/cheapy/model/Client.java @@ -1,5 +1,6 @@ package org.springframework.cheapy.model; +import java.util.List; import java.util.Set; import javax.persistence.CascadeType; @@ -57,16 +58,16 @@ public class Client extends BaseEntity { private User usuar; @OneToMany - private Set foodOffers; + private List foodOffers; @OneToMany - private Set nuOffers; + private List nuOffers; @OneToMany - private Set speedOffers; + private List speedOffers; @OneToMany - private Set timeOffers; + private List timeOffers; public String getName() { return name; @@ -148,35 +149,35 @@ public class Client extends BaseEntity { this.usuar = usuar; } - public Set getFoodOffers() { + public List getFoodOffers() { return foodOffers; } - public void setFoodOffers(Set foodOffers) { + public void setFoodOffers(List foodOffers) { this.foodOffers = foodOffers; } - public Set getNuOffers() { + public List getNuOffers() { return nuOffers; } - public void setNuOffers(Set nuOffers) { + public void setNuOffers(List nuOffers) { this.nuOffers = nuOffers; } - public Set getSpeedOffers() { + public List getSpeedOffers() { return speedOffers; } - public void setSpeedOffers(Set speedOffers) { + public void setSpeedOffers(List speedOffers) { this.speedOffers = speedOffers; } - public Set getTimeOffers() { + public List getTimeOffers() { return timeOffers; } - public void setTimeOffers(Set timeOffers) { + public void setTimeOffers(List timeOffers) { this.timeOffers = timeOffers; } diff --git a/src/main/java/org/springframework/cheapy/repository/FoodOfferRepository.java b/src/main/java/org/springframework/cheapy/repository/FoodOfferRepository.java index 80fadc686..2e9ca093b 100644 --- a/src/main/java/org/springframework/cheapy/repository/FoodOfferRepository.java +++ b/src/main/java/org/springframework/cheapy/repository/FoodOfferRepository.java @@ -1,6 +1,8 @@ package org.springframework.cheapy.repository; import org.springframework.cheapy.model.FoodOffer; +import org.springframework.cheapy.model.StatusOffer; + import java.util.List; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; @@ -18,5 +20,12 @@ public interface FoodOfferRepository extends Repository { FoodOffer findById(@Param("id") Integer id); void save(FoodOffer foodOffer); - + + @Query("SELECT foodOffer FROM FoodOffer foodOffer WHERE foodOffer.status =:status") + @Transactional(readOnly = true) + List findActiveFoodOffer(StatusOffer status); + + @Query("SELECT foodOffer FROM FoodOffer foodOffer WHERE foodOffer.client.id =:id") + @Transactional(readOnly = true) + List findByUserId(@Param("id") Integer id); } diff --git a/src/main/java/org/springframework/cheapy/repository/NuOfferRepository.java b/src/main/java/org/springframework/cheapy/repository/NuOfferRepository.java index 42d437fdb..8e59df4dc 100644 --- a/src/main/java/org/springframework/cheapy/repository/NuOfferRepository.java +++ b/src/main/java/org/springframework/cheapy/repository/NuOfferRepository.java @@ -1,8 +1,12 @@ package org.springframework.cheapy.repository; import java.util.List; + +import org.springframework.cheapy.model.FoodOffer; import org.springframework.cheapy.model.NuOffer; +import org.springframework.cheapy.model.StatusOffer; import org.springframework.data.repository.Repository; +import org.springframework.data.repository.query.Param; import org.springframework.data.jpa.repository.Query; import org.springframework.transaction.annotation.Transactional; @@ -15,5 +19,12 @@ public interface NuOfferRepository extends Repository { List findAllNuOffer(); void save(NuOffer nuOffer); - + + @Query("SELECT nuOffer FROM NuOffer nuOffer WHERE nuOffer.status =:status") + @Transactional(readOnly = true) + List findActiveNuOffer(StatusOffer status); + + @Query("SELECT nuOffer FROM NuOffer nuOffer WHERE nuOffer.client.id =:id") + @Transactional(readOnly = true) + List findByUserId(@Param("id") Integer id); } diff --git a/src/main/java/org/springframework/cheapy/repository/SpeedOfferRepository.java b/src/main/java/org/springframework/cheapy/repository/SpeedOfferRepository.java index aef04d36d..e4df0426c 100644 --- a/src/main/java/org/springframework/cheapy/repository/SpeedOfferRepository.java +++ b/src/main/java/org/springframework/cheapy/repository/SpeedOfferRepository.java @@ -1,7 +1,10 @@ package org.springframework.cheapy.repository; import java.util.List; + +import org.springframework.cheapy.model.FoodOffer; import org.springframework.cheapy.model.SpeedOffer; +import org.springframework.cheapy.model.StatusOffer; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; import org.springframework.data.repository.query.Param; @@ -18,5 +21,12 @@ public interface SpeedOfferRepository extends Repository { List findAllSpeedOffer(); void save(SpeedOffer speedOffer); - + + @Query("SELECT speedOffer FROM SpeedOffer speedOffer WHERE speedOffer.status =:status") + @Transactional(readOnly = true) + List findActiveSpeedOffer(StatusOffer status); + + @Query("SELECT speedOffer FROM SpeedOffer speedOffer WHERE speedOffer.client.id =:id") + @Transactional(readOnly = true) + List findByUserId(@Param("id") Integer id); } diff --git a/src/main/java/org/springframework/cheapy/repository/TimeOfferRepository.java b/src/main/java/org/springframework/cheapy/repository/TimeOfferRepository.java index b663057a7..1cd4c5e34 100644 --- a/src/main/java/org/springframework/cheapy/repository/TimeOfferRepository.java +++ b/src/main/java/org/springframework/cheapy/repository/TimeOfferRepository.java @@ -1,8 +1,11 @@ package org.springframework.cheapy.repository; import java.util.List; + +import org.springframework.cheapy.model.StatusOffer; import org.springframework.cheapy.model.TimeOffer; import org.springframework.data.repository.Repository; +import org.springframework.data.repository.query.Param; import org.springframework.data.jpa.repository.Query; import org.springframework.transaction.annotation.Transactional; @@ -15,5 +18,12 @@ public interface TimeOfferRepository extends Repository { List findAllTimeOffer(); void save(TimeOffer timeOffer); - + + @Query("SELECT timeOffer FROM TimeOffer timeOffer WHERE timeOffer.status =:status") + @Transactional(readOnly = true) + List findActiveTimeOffer(StatusOffer status); + + @Query("SELECT timeOffer FROM TimeOffer timeOffer WHERE timeOffer.client.id =:id") + @Transactional(readOnly = true) + List findByUserId(@Param("id") Integer id); } diff --git a/src/main/java/org/springframework/cheapy/service/FoodOfferService.java b/src/main/java/org/springframework/cheapy/service/FoodOfferService.java index d23ffc321..c11c9e8a6 100644 --- a/src/main/java/org/springframework/cheapy/service/FoodOfferService.java +++ b/src/main/java/org/springframework/cheapy/service/FoodOfferService.java @@ -2,6 +2,7 @@ package org.springframework.cheapy.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cheapy.model.FoodOffer; +import org.springframework.cheapy.model.StatusOffer; import org.springframework.cheapy.repository.FoodOfferRepository; import java.util.List; import org.springframework.dao.DataAccessException; @@ -20,12 +21,20 @@ public class FoodOfferService { public FoodOffer findFoodOfferById(final int id) { return this.foodOfferRepository.findById(id); } + public List findAllFoodOffer() { // return this.foodOfferRepository.findAllFoodOffer(); } public void saveFoodOffer(final FoodOffer foodOffer) throws DataAccessException { this.foodOfferRepository.save(foodOffer); - + } + + public List findActiveFoodOffer() { + return this.foodOfferRepository.findActiveFoodOffer(StatusOffer.active); + } + + public List findFoodOfferByUserId(final int id) { + return this.foodOfferRepository.findByUserId(id); } } diff --git a/src/main/java/org/springframework/cheapy/service/NuOfferService.java b/src/main/java/org/springframework/cheapy/service/NuOfferService.java index d09255d64..b87093c3d 100644 --- a/src/main/java/org/springframework/cheapy/service/NuOfferService.java +++ b/src/main/java/org/springframework/cheapy/service/NuOfferService.java @@ -3,6 +3,7 @@ package org.springframework.cheapy.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cheapy.model.NuOffer; +import org.springframework.cheapy.model.StatusOffer; import org.springframework.cheapy.repository.NuOfferRepository; import java.util.List; import org.springframework.dao.DataAccessException; @@ -33,4 +34,12 @@ public class NuOfferService { public void saveNuOffer(final NuOffer nuOffer) throws DataAccessException { this.nuOfferRepository.save(nuOffer); } + + public List findActiveNuOffer() { + return this.nuOfferRepository.findActiveNuOffer(StatusOffer.active); + } + + public List findNuOfferByUserId(final int id) { + return this.nuOfferRepository.findByUserId(id); + } } diff --git a/src/main/java/org/springframework/cheapy/service/SpeedOfferService.java b/src/main/java/org/springframework/cheapy/service/SpeedOfferService.java index 121874247..6063b7426 100644 --- a/src/main/java/org/springframework/cheapy/service/SpeedOfferService.java +++ b/src/main/java/org/springframework/cheapy/service/SpeedOfferService.java @@ -3,7 +3,9 @@ package org.springframework.cheapy.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cheapy.model.NuOffer; import org.springframework.cheapy.model.SpeedOffer; +import org.springframework.cheapy.model.StatusOffer; import org.springframework.cheapy.repository.SpeedOfferRepository; import org.springframework.dao.DataAccessException; import org.springframework.stereotype.Service; @@ -34,4 +36,12 @@ public class SpeedOfferService { public void saveSpeedOffer(final SpeedOffer speedOffer) throws DataAccessException { // this.speedOfferRepository.save(speedOffer); } + + public List findActiveSpeedOffer() { + return this.speedOfferRepository.findActiveSpeedOffer(StatusOffer.active); + } + + public List findSpeedOfferByUserId(final int id) { + return this.speedOfferRepository.findByUserId(id); + } } diff --git a/src/main/java/org/springframework/cheapy/service/TimeOfferService.java b/src/main/java/org/springframework/cheapy/service/TimeOfferService.java index b7d5904ef..e23fa5151 100644 --- a/src/main/java/org/springframework/cheapy/service/TimeOfferService.java +++ b/src/main/java/org/springframework/cheapy/service/TimeOfferService.java @@ -3,6 +3,7 @@ package org.springframework.cheapy.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cheapy.model.StatusOffer; import org.springframework.cheapy.model.TimeOffer; import org.springframework.cheapy.repository.TimeOfferRepository; @@ -30,7 +31,13 @@ public class TimeOfferService { public void saveTimeOffer(final TimeOffer TimeOffer) throws DataAccessException { this.timeOfferRepository.save(TimeOffer); - - + } + + public List findActiveTimeOffer() { + return this.timeOfferRepository.findActiveTimeOffer(StatusOffer.active); + } + + public List findTimeOfferByUserId(final int id) { + return this.timeOfferRepository.findByUserId(id); } } diff --git a/src/main/java/org/springframework/cheapy/web/OfertaController.java b/src/main/java/org/springframework/cheapy/web/OfertaController.java index c7cb3de71..73b3553cd 100644 --- a/src/main/java/org/springframework/cheapy/web/OfertaController.java +++ b/src/main/java/org/springframework/cheapy/web/OfertaController.java @@ -8,6 +8,7 @@ import org.springframework.cheapy.model.FoodOffer; import org.springframework.cheapy.model.NuOffer; import org.springframework.cheapy.model.SpeedOffer; import org.springframework.cheapy.model.TimeOffer; +import org.springframework.cheapy.service.ClientService; import org.springframework.cheapy.service.FoodOfferService; import org.springframework.cheapy.service.NuOfferService; import org.springframework.cheapy.service.SpeedOfferService; @@ -17,14 +18,17 @@ import org.springframework.web.bind.annotation.GetMapping; @Controller public class OfertaController { - + + private final ClientService clientService; + private final FoodOfferService foodOfferService; private final NuOfferService nuOfferService; private final SpeedOfferService speedOfferService; private final TimeOfferService timeOfferService; public OfertaController(final FoodOfferService foodOfferService, final NuOfferService nuOfferService, - final SpeedOfferService speedOfferService, final TimeOfferService timeOfferService) { + final SpeedOfferService speedOfferService, final TimeOfferService timeOfferService, ClientService clientService) { + this.clientService = clientService; this.foodOfferService = foodOfferService; this.nuOfferService = nuOfferService; this.speedOfferService = speedOfferService; @@ -34,10 +38,10 @@ public class OfertaController { @GetMapping("/offers") public String processFindForm( Map model) { - List foodOfferLs=this.foodOfferService.findAllFoodOffer(); - List nuOfferLs=this.nuOfferService.findAllNuOffer(); - List speedOfferLs=this.speedOfferService.findAllSpeedOffer(); - List timeOfferLs=this.timeOfferService.findAllTimeOffer(); + List foodOfferLs=this.foodOfferService.findActiveFoodOffer(); + List nuOfferLs=this.nuOfferService.findActiveNuOffer(); + List speedOfferLs=this.speedOfferService.findActiveSpeedOffer(); + List timeOfferLs=this.timeOfferService.findActiveTimeOffer(); model.put("foodOfferLs", foodOfferLs); model.put("nuOfferLs", nuOfferLs); @@ -50,7 +54,28 @@ public class OfertaController { return "offers/offersList"; } + + @GetMapping("/myOffers") + public String processMyOffersForm( Map model) { + + int actual = this.clientService.getCurrentClient().getId(); + + List foodOfferLs = this.foodOfferService.findFoodOfferByUserId(actual); + List nuOfferLs = this.nuOfferService.findNuOfferByUserId(actual); + List speedOfferLs = this.speedOfferService.findSpeedOfferByUserId(actual); + List timeOfferLs = this.timeOfferService.findTimeOfferByUserId(actual); + + model.put("foodOfferLs", foodOfferLs); + model.put("nuOfferLs", nuOfferLs); + model.put("speedOfferLs", speedOfferLs); + model.put("timeOfferLs", timeOfferLs); + + //Se añade formateador de fecha al modelo + model.put("localDateTimeFormat", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")); + + return "offers/myOffersList"; + } // @GetMapping("/owners/{ownerId}/edit") // public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { // Owner owner = this.ownerService.findOwnerById(ownerId); diff --git a/src/main/resources/db/mysql/data.sql b/src/main/resources/db/mysql/data.sql index baa9d8384..39f0ca7b1 100644 --- a/src/main/resources/db/mysql/data.sql +++ b/src/main/resources/db/mysql/data.sql @@ -32,7 +32,7 @@ INSERT INTO usuarios VALUES (4, 'Pepe', 'Lopez', '12456776V', 'C/Macarena', '690 INSERT INTO clients (id, name, email, address, init, finish, telephone, description, code, food, username) VALUES (1,'bar manoli','manoli@gmail.com','C/Betis','10:00','22:00','608726190', 'description 1', 'code1', 'ESPAÑOLA','manoli'); INSERT INTO clients (id, name, email, address, init, finish, telephone, description, code, food, username) VALUES (2,'bar david','david@gmail.com','C/Sevilla','09:30','22:00','608726190', 'description 2', 'code2', 'americana','david'); -INSERT INTO food_offers(start, end, code, status, client_id, food, discount) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'FO-1', 'active', 1, 'macarrones', 15); +INSERT INTO food_offers(start, end, code, status, client_id, food, discount) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'FO-1', 'inactive', 1, 'macarrones', 15); INSERT INTO time_offers(start, end, code, status, client_id, init, finish, discount) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'T-1', 'active', 1, '12:00:00', '13:00:00', 10); INSERT INTO speed_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'SP-1', 'active',1,5,25,10,15,15,10); INSERT INTO nu_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'NU-1', 'active',1,15,25,10,15,5,10); diff --git a/src/main/webapp/WEB-INF/jsp/offers/myOffersList.jsp b/src/main/webapp/WEB-INF/jsp/offers/myOffersList.jsp new file mode 100644 index 000000000..201065542 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/offers/myOffersList.jsp @@ -0,0 +1,158 @@ +<%@ page session="false" trimDirectiveWhitespaces="true" %> +<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> +<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %> +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> +<%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %> + + + +

+ + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + +
+ +
+
+ +

+ + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +
+ +
+
+

+ + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +
+ +
+
+

+ + + + + + + + + + + + + + + + + + + + +
+ + + + + + + +
+ +
+
+
diff --git a/src/main/webapp/WEB-INF/tags/menu.tag b/src/main/webapp/WEB-INF/tags/menu.tag index daa63faa2..10a512467 100644 --- a/src/main/webapp/WEB-INF/tags/menu.tag +++ b/src/main/webapp/WEB-INF/tags/menu.tag @@ -25,7 +25,7 @@ - Home + Inicio Ver ofertas + + + + Mis ofertas + +