diff --git a/src/main/java/org/springframework/cheapy/repository/FoodOfferRepository.java b/src/main/java/org/springframework/cheapy/repository/FoodOfferRepository.java index 099f6788c..5ee262df4 100644 --- a/src/main/java/org/springframework/cheapy/repository/FoodOfferRepository.java +++ b/src/main/java/org/springframework/cheapy/repository/FoodOfferRepository.java @@ -1,15 +1,17 @@ + package org.springframework.cheapy.repository; +import java.util.List; + import org.springframework.cheapy.model.FoodOffer; import org.springframework.cheapy.model.StatusOffer; - -import java.util.List; +import org.springframework.data.domain.Pageable; import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.Repository; +import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.query.Param; import org.springframework.transaction.annotation.Transactional; -public interface FoodOfferRepository extends Repository { +public interface FoodOfferRepository extends PagingAndSortingRepository { @Query("SELECT foodOffer FROM FoodOffer foodOffer") @Transactional(readOnly = true) @@ -17,18 +19,18 @@ public interface FoodOfferRepository extends Repository { @Query("SELECT foodOffer FROM FoodOffer foodOffer WHERE id =:id") @Transactional(readOnly = true) - FoodOffer findById(@Param("id") Integer id); + FoodOffer findByIdFO(@Param("id") Integer id); + + //void save(FoodOffer foodOffer); - void save(FoodOffer foodOffer); - @Query("SELECT foodOffer FROM FoodOffer foodOffer WHERE foodOffer.status =:status") @Transactional(readOnly = true) - List findActiveFoodOffer(StatusOffer status); - + List findActiveFoodOffer(StatusOffer status, Pageable p); + @Query("SELECT foodOffer FROM FoodOffer foodOffer WHERE foodOffer.client.id =:id") @Transactional(readOnly = true) List findByUserId(@Param("id") Integer id); - + @Query("SELECT foodOffer FROM FoodOffer foodOffer WHERE foodOffer.client.id =:id AND foodOffer.status!= 'inactive'") @Transactional(readOnly = true) List findFoodOfferActOclByUserId(@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 a555e5c6c..79b98b494 100644 --- a/src/main/java/org/springframework/cheapy/service/FoodOfferService.java +++ b/src/main/java/org/springframework/cheapy/service/FoodOfferService.java @@ -1,43 +1,47 @@ + package org.springframework.cheapy.service; +import java.util.List; + 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; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; @Service public class FoodOfferService { - + private FoodOfferRepository foodOfferRepository; + @Autowired public FoodOfferService(final FoodOfferRepository foodOfferRepository) { this.foodOfferRepository = foodOfferRepository; } public FoodOffer findFoodOfferById(final int id) { - return this.foodOfferRepository.findById(id); + return this.foodOfferRepository.findByIdFO(id); } - - public List findAllFoodOffer() { // + + public List findAllFoodOffer(final Pageable p) { // 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 findActiveFoodOffer(final Pageable p) { + return this.foodOfferRepository.findActiveFoodOffer(StatusOffer.active, p); } - + public List findFoodOfferByUserId(final int id) { return this.foodOfferRepository.findByUserId(id); } - + public List findFoodOfferActOclByUserId(final int id) { return this.foodOfferRepository.findFoodOfferActOclByUserId(id); } diff --git a/src/main/java/org/springframework/cheapy/web/OfertaController.java b/src/main/java/org/springframework/cheapy/web/OfertaController.java index 996773042..2c30db007 100644 --- a/src/main/java/org/springframework/cheapy/web/OfertaController.java +++ b/src/main/java/org/springframework/cheapy/web/OfertaController.java @@ -1,3 +1,4 @@ + package org.springframework.cheapy.web; import java.time.format.DateTimeFormatter; @@ -13,21 +14,23 @@ import org.springframework.cheapy.service.FoodOfferService; import org.springframework.cheapy.service.NuOfferService; import org.springframework.cheapy.service.SpeedOfferService; import org.springframework.cheapy.service.TimeOfferService; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class OfertaController { - private final ClientService clientService; + private final ClientService clientService; - private final FoodOfferService foodOfferService; - private final NuOfferService nuOfferService; - private final SpeedOfferService speedOfferService; - private final TimeOfferService timeOfferService; + 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, ClientService clientService) { + + public OfertaController(final FoodOfferService foodOfferService, final NuOfferService nuOfferService, final SpeedOfferService speedOfferService, final TimeOfferService timeOfferService, final ClientService clientService) { this.clientService = clientService; this.foodOfferService = foodOfferService; this.nuOfferService = nuOfferService; @@ -36,12 +39,13 @@ public class OfertaController { } @GetMapping("/offers") - public String processFindForm( Map model) { + public String processFindForm(final Map model) { + Pageable elements = PageRequest.of(0, 3); - List foodOfferLs=this.foodOfferService.findActiveFoodOffer(); - List nuOfferLs=this.nuOfferService.findActiveNuOffer(); - List speedOfferLs=this.speedOfferService.findActiveSpeedOffer(); - List timeOfferLs=this.timeOfferService.findActiveTimeOffer(); + List foodOfferLs = this.foodOfferService.findActiveFoodOffer(elements); + List nuOfferLs = this.nuOfferService.findActiveNuOffer(); + List speedOfferLs = this.speedOfferService.findActiveSpeedOffer(); + List timeOfferLs = this.timeOfferService.findActiveTimeOffer(); model.put("foodOfferLs", foodOfferLs); model.put("nuOfferLs", nuOfferLs); @@ -55,9 +59,8 @@ public class OfertaController { } - @GetMapping("/myOffers") - public String processMyOffersForm( Map model) { + public String processMyOffersForm(final Map model) { int actual = this.clientService.getCurrentClient().getId(); @@ -75,43 +78,40 @@ public class OfertaController { model.put("localDateTimeFormat", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm")); return "offers/myOffersList"; - } + } - @GetMapping("/offersCreate") - public String createOffers() { + @GetMapping("/offersCreate") + public String createOffers() { - return "offers/offersCreate"; - } - - - -// @GetMapping("/owners/{ownerId}/edit") -// public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { -// Owner owner = this.ownerService.findOwnerById(ownerId); -// model.addAttribute(owner); -// return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; -// } -// -// @PostMapping("/owners/{ownerId}/edit") -// public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, -// @PathVariable("ownerId") int ownerId) { -// if (result.hasErrors()) { -// return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; -// } -// else { -// owner.setId(ownerId); -// this.ownerService.saveOwner(owner); -// return "redirect:/owners/{ownerId}"; -// } -// } -// @GetMapping("/owners/{ownerId}") -// public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { -// ModelAndView mav = new ModelAndView("owners/ownerDetails"); -// Owner owner = this.ownerService.findOwnerById(ownerId); -// -// mav.addObject(owner); -// return mav; -// } + return "offers/offersCreate"; + } + // @GetMapping("/owners/{ownerId}/edit") + // public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { + // Owner owner = this.ownerService.findOwnerById(ownerId); + // model.addAttribute(owner); + // return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; + // } + // + // @PostMapping("/owners/{ownerId}/edit") + // public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, + // @PathVariable("ownerId") int ownerId) { + // if (result.hasErrors()) { + // return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; + // } + // else { + // owner.setId(ownerId); + // this.ownerService.saveOwner(owner); + // return "redirect:/owners/{ownerId}"; + // } + // } + // @GetMapping("/owners/{ownerId}") + // public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { + // ModelAndView mav = new ModelAndView("owners/ownerDetails"); + // Owner owner = this.ownerService.findOwnerById(ownerId); + // + // mav.addObject(owner); + // return mav; + // } } diff --git a/src/main/resources/db/mysql/data.sql b/src/main/resources/db/mysql/data.sql index 3e308ec2d..f32637462 100644 --- a/src/main/resources/db/mysql/data.sql +++ b/src/main/resources/db/mysql/data.sql @@ -24,16 +24,36 @@ INSERT INTO clients (id, name, email, address, init, finish, telephone, descript INSERT INTO food_offers(start, end, code, status, client_id, food, discount) VALUES ('2021-08-14 12:00:00', '2021-08-15 12:00:00', 'FO-1', 'inactive', 1, 'macarrones', 15); INSERT INTO food_offers(start, end, code, status, client_id, food, discount) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'FO-2', 'active', 1, 'macarrones con tomate', 10); +INSERT INTO food_offers(start, end, code, status, client_id, food, discount) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'FO-3', 'active', 1, 'Estofado', 10); +INSERT INTO food_offers(start, end, code, status, client_id, food, discount) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'FO-4', 'active', 1, 'Puchero', 10); +INSERT INTO food_offers(start, end, code, status, client_id, food, discount) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'FO-5', 'active', 2, 'Tumbalobos', 10); +INSERT INTO food_offers(start, end, code, status, client_id, food, discount) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'FO-6', 'active', 2, 'Tortilla', 10); +INSERT INTO food_offers(start, end, code, status, client_id, food, discount) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'FO-7', 'active', 2, 'Arroz con leche', 10); INSERT INTO food_offers(start, end, code, status, client_id, food, discount) VALUES ('2021-08-16 12:00:00', '2021-08-17 12:00:00', null, 'hidden', 1, 'macarrones con queso', 5); INSERT INTO time_offers(start, end, code, status, client_id, init, finish, discount) VALUES ('2021-08-14 12:00:00', '2021-08-15 12:00:00', 'T-1', 'inactive', 1, '12:00:00', '13:00:00', 5); INSERT INTO time_offers(start, end, code, status, client_id, init, finish, discount) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'T-2', 'active', 1, '12:00:00', '13:00:00', 10); +INSERT INTO time_offers(start, end, code, status, client_id, init, finish, discount) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'T-3', 'active', 1, '12:30:00', '14:30:00', 10); +INSERT INTO time_offers(start, end, code, status, client_id, init, finish, discount) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'T-4', 'active', 1, '12:00:00', '13:00:00', 5); +INSERT INTO time_offers(start, end, code, status, client_id, init, finish, discount) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'T-5', 'active', 2, '13:00:00', '16:00:00', 15); +INSERT INTO time_offers(start, end, code, status, client_id, init, finish, discount) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'T-6', 'active', 2, '14:00:00', '17:00:00', 15); +INSERT INTO time_offers(start, end, code, status, client_id, init, finish, discount) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'T-7', 'active', 2, '11:00:00', '20:00:00', 20); INSERT INTO time_offers(start, end, code, status, client_id, init, finish, discount) VALUES ('2021-08-16 12:00:00', '2021-08-17 12:00:00', null, 'hidden', 1, '12:00:00', '13:00:00', 15); INSERT INTO speed_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-14 12:00:00', '2021-08-15 12:00:00', 'SP-1', 'inactive',1,5,25,10,15,15,10); -INSERT INTO speed_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'SP-2', 'active',1,5,25,10,15,15,10); +INSERT INTO speed_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'SP-2', 'active',1,35,25,40,15,55,10); +INSERT INTO speed_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'SP-3', 'active',1,25,25,30,15,35,10); +INSERT INTO speed_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'SP-4', 'active',1,15,25,20,15,35,10); +INSERT INTO speed_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'SP-5', 'active',2,15,30,20,15,50,5); +INSERT INTO speed_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'SP-6', 'active',2,15,30,21,15,50,5); +INSERT INTO speed_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'SP-7', 'active',2,15,30,22,15,50,5); INSERT INTO speed_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-16 12:00:00', '2021-08-17 12:00:00', null, 'hidden',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-08-14 12:00:00', '2021-08-15 12:00:00', 'NU-1', 'inactive',1,15,25,10,15,5,10); INSERT INTO nu_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'NU-2', 'active',1,15,25,10,15,5,10); +INSERT INTO nu_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'NU-3', 'active',1,15,25,12,15,3,5); +INSERT INTO nu_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'NU-4', 'active',1,15,25,13,15,2,5); +INSERT INTO nu_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'NU-5', 'active',2,20,35,15,15,5,5); +INSERT INTO nu_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'NU-6', 'active',2,20,30,15,10,10,5); +INSERT INTO nu_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'NU-7', 'active',2,20,35,15,15,10,5); INSERT INTO nu_offers(start, end, code, status, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-08-16 12:00:00', '2021-08-17 12:00:00', null, 'hidden',1,15,25,10,15,5,10);