diff --git a/src/main/java/org/springframework/cheapy/model/FoodOffer.java b/src/main/java/org/springframework/cheapy/model/FoodOffer.java index b82fed73b..ac3838a8a 100644 --- a/src/main/java/org/springframework/cheapy/model/FoodOffer.java +++ b/src/main/java/org/springframework/cheapy/model/FoodOffer.java @@ -18,6 +18,7 @@ package org.springframework.cheapy.model; import javax.persistence.Entity; import javax.persistence.Table; import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; @Entity @Table(name = "food_offers") @@ -29,7 +30,7 @@ public class FoodOffer extends Offer { @NotBlank private String discount; - @NotBlank + @NotNull private Integer units; // revisar public String getFood() { diff --git a/src/main/java/org/springframework/cheapy/model/Offer.java b/src/main/java/org/springframework/cheapy/model/Offer.java index 7ee58ff40..70a765526 100644 --- a/src/main/java/org/springframework/cheapy/model/Offer.java +++ b/src/main/java/org/springframework/cheapy/model/Offer.java @@ -17,15 +17,13 @@ package org.springframework.cheapy.model; import java.time.LocalDateTime; -import javax.persistence.Entity; import javax.persistence.EnumType; import javax.persistence.Enumerated; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; import javax.persistence.MappedSuperclass; -import javax.persistence.Table; import javax.validation.constraints.Future; -import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; import org.springframework.format.annotation.DateTimeFormat; @@ -33,12 +31,12 @@ import org.springframework.format.annotation.DateTimeFormat; public class Offer extends BaseEntity { @DateTimeFormat(pattern = "dd/MM/yyyy HH:mm") - @NotBlank + @NotNull @Future private LocalDateTime start; @DateTimeFormat(pattern = "dd/MM/yyyy HH:mm") - @NotBlank + @NotNull @Future private LocalDateTime end; @@ -47,6 +45,7 @@ public class Offer extends BaseEntity { @Enumerated(value = EnumType.STRING) private StatusOffer type; + @ManyToOne @JoinColumn(name="client_id") private Client client; diff --git a/src/main/java/org/springframework/cheapy/model/SpeedOffer.java b/src/main/java/org/springframework/cheapy/model/SpeedOffer.java index 0399d4baf..9d8ee0373 100644 --- a/src/main/java/org/springframework/cheapy/model/SpeedOffer.java +++ b/src/main/java/org/springframework/cheapy/model/SpeedOffer.java @@ -19,26 +19,27 @@ import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Table; import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; @Entity @Table(name = "speed_offers") public class SpeedOffer extends Offer { - @NotBlank + @NotNull private Integer gold; // x minutos @Column(name = "discount_gold") @NotBlank private String discountGold; - @NotBlank + @NotNull private Integer silver; @Column(name = "discount_silver") @NotBlank private String discountSilver; - @NotBlank + @NotNull private Integer bronze; @Column(name = "discount_bronze") diff --git a/src/main/java/org/springframework/cheapy/service/ClientService.java b/src/main/java/org/springframework/cheapy/service/ClientService.java index edc9773a9..7994110ad 100644 --- a/src/main/java/org/springframework/cheapy/service/ClientService.java +++ b/src/main/java/org/springframework/cheapy/service/ClientService.java @@ -16,8 +16,10 @@ package org.springframework.cheapy.service; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cheapy.model.Client; import org.springframework.cheapy.repository.ClientRepository; +import org.springframework.cheapy.repository.SpeedOfferRepository; import org.springframework.dao.DataAccessException; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; @@ -36,6 +38,11 @@ import org.springframework.transaction.annotation.Transactional; public class ClientService { private ClientRepository clientRepository; + + @Autowired + public ClientService(final ClientRepository clientRepository) { + this.clientRepository = clientRepository; + } @Transactional public Client getCurrentclient() throws DataAccessException { diff --git a/src/main/java/org/springframework/cheapy/web/FoodOfferController.java b/src/main/java/org/springframework/cheapy/web/FoodOfferController.java index 28a050574..f221a3600 100644 --- a/src/main/java/org/springframework/cheapy/web/FoodOfferController.java +++ b/src/main/java/org/springframework/cheapy/web/FoodOfferController.java @@ -73,17 +73,17 @@ public class FoodOfferController { } } - @GetMapping(value = "/foodOffers/{foodOfferid}/activate") - public String activateFoodOffer(@PathVariable("foodOffer") final int foodOfferId, final ModelMap modelMap) { + @GetMapping(value = "/foodOffers/{foodOfferId}/activate") + public String activateFoodOffer(@PathVariable("foodOfferId") final int foodOfferId, ModelMap modelMap) { FoodOffer foodOffer = this.foodOfferService.findFoodOfferById(foodOfferId); Client client = this.clientService.getCurrentclient(); if(foodOffer.getClient().equals(client)) { foodOffer.setType(StatusOffer.active); - foodOffer.setCode("SE-"+foodOfferId); + foodOffer.setCode("FE-"+foodOfferId); this.foodOfferService.saveFoodOffer(foodOffer); } else { modelMap.addAttribute("message", "You don't have access to this food offer"); } - return "redirect:/foodOffers/" + foodOffer.getId(); + return "redirect:/foodOffers/"; } } diff --git a/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java b/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java index f633dfaba..0456f9742 100644 --- a/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java +++ b/src/main/java/org/springframework/cheapy/web/SpeedOfferController.java @@ -73,8 +73,8 @@ public class SpeedOfferController { } } - @GetMapping(value = "/speedOffers/{speedOfferid}/activate") - public String activateSpeedOffer(@PathVariable("speedOffer") final int speedOfferId, final ModelMap modelMap) { + @GetMapping(value = "/speedOffers/{speedOfferId}/activate") + public String activateSpeedOffer(@PathVariable("speedOfferId") final int speedOfferId, ModelMap modelMap) { SpeedOffer speedOffer = this.speedOfferService.findSpeedOfferById(speedOfferId); Client client = this.clientService.getCurrentclient(); if(speedOffer.getClient().equals(client)) { @@ -84,6 +84,6 @@ public class SpeedOfferController { } else { modelMap.addAttribute("message", "You don't have access to this speed offer"); } - return "redirect:/speedOffers/" + speedOffer.getId(); + return "redirect:/speedOffers/"; } } diff --git a/src/main/resources/db/mysql/data.sql b/src/main/resources/db/mysql/data.sql index 3dd3b74c7..5db5228c9 100644 --- a/src/main/resources/db/mysql/data.sql +++ b/src/main/resources/db/mysql/data.sql @@ -10,13 +10,12 @@ INSERT INTO owners VALUES (9, 'David', 'Schroeder', '2749 Blackhawk Trail', 'Mad INSERT INTO owners VALUES (10, 'Carlos', 'Estaban', '2335 Independence La.', 'Waunakee', '6085555487'); -INSERT INTO food_offers(start, end, code, type, client_id, food, discount, units) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'jkhlljk', 'active', null, 'macarrones', '15%', 10); - +INSERT INTO food_offers(start, end, code, type, client_id, food, discount, units) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'FE-1', 'active', null, 'macarrones', '15%', 10); +INSERT INTO speed_offers(start, end, code, type, client_id, gold, discount_gold, silver, discount_silver, bronze, discount_bronze) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'SE-1', 'active', null, 5, '15%', 10, '10%', 15, '5%'); INSERT INTO time_offers(start, end, code, type, client_id, init, finish, discount) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'jkhlljk', 'active', null, '12:00:00', '13:00:00', '10%'); --insert into usuarios(username, password, enabled) values ('admin3', 'admin', true); --insert into authorities(id ,usuario, authority) values (42,'admin3', 'admin'); INSERT INTO users(username,password,enabled) VALUES ('admin1','4dm1n',TRUE); -INSERT INTO authorities(id,username,authority) VALUES (1,'admin1','admin'); - +INSERT INTO authorities(id,username,authority) VALUES (1,'admin1','admin'); \ No newline at end of file