mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-23 15:55:49 +00:00
Validadores de fechas de ofertas, horarios, condiciones de descuentos y
descuentos.
This commit is contained in:
parent
db9cbbd8fa
commit
78bba05da2
4 changed files with 158 additions and 10 deletions
|
@ -52,6 +52,14 @@ public class FoodOfferController {
|
|||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private boolean checkDates(final FoodOffer foodOffer) {
|
||||
boolean res = false;
|
||||
if(foodOffer.getEnd().isAfter(foodOffer.getStart())) {
|
||||
res = true;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@GetMapping("/offers/food/new")
|
||||
public String initCreationForm(Map<String, Object> model) {
|
||||
|
@ -65,6 +73,10 @@ public class FoodOfferController {
|
|||
if (result.hasErrors()) {
|
||||
return VIEWS_FOOD_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
} else {
|
||||
if(!this.checkDates(foodOffer)) {
|
||||
//Poner aqui mensaje de error
|
||||
return VIEWS_FOOD_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
Client client = this.clientService.getCurrentClient();
|
||||
foodOffer.setClient(client);
|
||||
foodOffer.setStatus(StatusOffer.hidden);
|
||||
|
@ -122,13 +134,11 @@ public class FoodOfferController {
|
|||
final ModelMap model, HttpServletRequest request) {
|
||||
|
||||
if (!this.checkIdentity(foodOfferEdit.getId())) {
|
||||
System.out.println("Fallo Indentity");
|
||||
return "error";
|
||||
}
|
||||
Integer id = (Integer) request.getSession().getAttribute("idFood");
|
||||
FoodOffer foodOffer = this.foodOfferService.findFoodOfferById(id);
|
||||
if (!this.checkOffer(foodOffer, foodOfferEdit)) {
|
||||
System.out.println("Fallo offer");
|
||||
return "error";
|
||||
}
|
||||
|
||||
|
@ -137,6 +147,10 @@ public class FoodOfferController {
|
|||
return FoodOfferController.VIEWS_FOOD_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
|
||||
} else {
|
||||
if(!this.checkDates(foodOfferEdit)) {
|
||||
//Poner aqui mensaje de error
|
||||
return FoodOfferController.VIEWS_FOOD_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
BeanUtils.copyProperties(this.foodOfferService.findFoodOfferById(foodOfferEdit.getId()), foodOfferEdit,
|
||||
"start", "end", "food", "discount");
|
||||
this.foodOfferService.saveFoodOffer(foodOfferEdit);
|
||||
|
|
|
@ -8,9 +8,11 @@ import javax.servlet.http.HttpServletRequest;
|
|||
import javax.validation.Valid;
|
||||
|
||||
import org.springframework.cheapy.model.NuOffer;
|
||||
import org.springframework.cheapy.model.SpeedOffer;
|
||||
import org.springframework.cheapy.model.StatusOffer;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.cheapy.model.Client;
|
||||
import org.springframework.cheapy.model.FoodOffer;
|
||||
import org.springframework.cheapy.service.ClientService;
|
||||
import org.springframework.cheapy.service.NuOfferService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
@ -32,14 +34,7 @@ public class NuOfferController {
|
|||
this.nuOfferService = nuOfferService;
|
||||
this.clientService = clientService;
|
||||
}
|
||||
|
||||
@GetMapping("/offers/nu/new")
|
||||
public String initCreationForm(Map<String, Object> model) {
|
||||
NuOffer nuOffer = new NuOffer();
|
||||
model.put("nuOffer", nuOffer);
|
||||
return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
|
||||
|
||||
private boolean checkIdentity(final int nuOfferId) {
|
||||
boolean res = false;
|
||||
Client client = this.clientService.getCurrentClient();
|
||||
|
@ -59,12 +54,55 @@ public class NuOfferController {
|
|||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private boolean checkDates(final NuOffer nuOffer) {
|
||||
boolean res = false;
|
||||
if(nuOffer.getEnd().isAfter(nuOffer.getStart())) {
|
||||
res = true;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private boolean checkConditions(final NuOffer NuOffer) {
|
||||
boolean res = false;
|
||||
if(NuOffer.getGold() > NuOffer.getSilver() && NuOffer.getSilver() > NuOffer.getBronze()) {
|
||||
res = true;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private boolean checkDiscounts(final NuOffer NuOffer) {
|
||||
boolean res = false;
|
||||
if(NuOffer.getDiscountGold() > NuOffer.getDiscountSilver() && NuOffer.getDiscountSilver() > NuOffer.getDiscountBronze()) {
|
||||
res = true;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@GetMapping("/offers/nu/new")
|
||||
public String initCreationForm(Map<String, Object> model) {
|
||||
NuOffer nuOffer = new NuOffer();
|
||||
model.put("nuOffer", nuOffer);
|
||||
return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
|
||||
@PostMapping("/offers/nu/new")
|
||||
public String processCreationForm(@Valid NuOffer nuOffer, BindingResult result) {
|
||||
if (result.hasErrors()) {
|
||||
return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
} else {
|
||||
if(!this.checkDates(nuOffer)) {
|
||||
//Poner aqui mensaje de error
|
||||
return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
if(!this.checkConditions(nuOffer)) {
|
||||
//Poner aqui mensaje de error
|
||||
return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
if(!this.checkDiscounts(nuOffer)) {
|
||||
//Poner aqui mensaje de error
|
||||
return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
nuOffer.setStatus(StatusOffer.hidden);
|
||||
|
||||
Client client = this.clientService.getCurrentClient();
|
||||
|
@ -136,6 +174,18 @@ public class NuOfferController {
|
|||
return NuOfferController.VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
|
||||
} else {
|
||||
if(!this.checkDates(nuOffer)) {
|
||||
//Poner aqui mensaje de error
|
||||
return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
if(!this.checkConditions(nuOffer)) {
|
||||
//Poner aqui mensaje de error
|
||||
return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
if(!this.checkDiscounts(nuOffer)) {
|
||||
//Poner aqui mensaje de error
|
||||
return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
BeanUtils.copyProperties(this.nuOfferService.findNuOfferById(nuOfferEdit.getId()), nuOfferEdit, "start",
|
||||
"end", "gold", "discount_gold", "silver", "discount_silver", "bronze", "discount_bronze");
|
||||
this.nuOfferService.saveNuOffer(nuOfferEdit);
|
||||
|
|
|
@ -10,6 +10,7 @@ import org.springframework.cheapy.model.SpeedOffer;
|
|||
import org.springframework.cheapy.model.StatusOffer;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.cheapy.model.Client;
|
||||
import org.springframework.cheapy.model.FoodOffer;
|
||||
import org.springframework.cheapy.service.ClientService;
|
||||
import org.springframework.cheapy.service.SpeedOfferService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
|
@ -51,6 +52,30 @@ public class SpeedOfferController {
|
|||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private boolean checkDates(final SpeedOffer speedOffer) {
|
||||
boolean res = false;
|
||||
if(speedOffer.getEnd().isAfter(speedOffer.getStart())) {
|
||||
res = true;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private boolean checkConditions(final SpeedOffer speedOffer) {
|
||||
boolean res = false;
|
||||
if(speedOffer.getGold() < speedOffer.getSilver() && speedOffer.getSilver() < speedOffer.getBronze()) {
|
||||
res = true;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private boolean checkDiscounts(final SpeedOffer speedOffer) {
|
||||
boolean res = false;
|
||||
if(speedOffer.getDiscountGold() > speedOffer.getDiscountSilver() && speedOffer.getDiscountSilver() > speedOffer.getDiscountBronze()) {
|
||||
res = true;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@GetMapping("/offers/speed/new")
|
||||
public String initCreationForm(Map<String, Object> model) {
|
||||
|
@ -64,6 +89,18 @@ public class SpeedOfferController {
|
|||
if (result.hasErrors()) {
|
||||
return VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
} else {
|
||||
if(!this.checkDates(speedOffer)) {
|
||||
//Poner aqui mensaje de error
|
||||
return VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
if(!this.checkConditions(speedOffer)) {
|
||||
//Poner aqui mensaje de error
|
||||
return VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
if(!this.checkDiscounts(speedOffer)) {
|
||||
//Poner aqui mensaje de error
|
||||
return VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
Client client = this.clientService.getCurrentClient();
|
||||
speedOffer.setClient(client);
|
||||
speedOffer.setStatus(StatusOffer.hidden);
|
||||
|
@ -130,6 +167,18 @@ public class SpeedOfferController {
|
|||
return SpeedOfferController.VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
|
||||
} else {
|
||||
if(!this.checkDates(speedOffer)) {
|
||||
//Poner aqui mensaje de error
|
||||
return VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
if(!this.checkConditions(speedOffer)) {
|
||||
//Poner aqui mensaje de error
|
||||
return VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
if(!this.checkDiscounts(speedOffer)) {
|
||||
//Poner aqui mensaje de error
|
||||
return VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
BeanUtils.copyProperties(this.speedOfferService.findSpeedOfferById(speedOfferEdit.getId()), speedOfferEdit,
|
||||
"start", "end", "gold", "discount_gold", "silver", "discount_silver", "bronze", "discount_bronze");
|
||||
this.speedOfferService.saveSpeedOffer(speedOfferEdit);
|
||||
|
|
|
@ -8,6 +8,7 @@ import javax.validation.Valid;
|
|||
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.cheapy.model.Client;
|
||||
import org.springframework.cheapy.model.FoodOffer;
|
||||
import org.springframework.cheapy.model.StatusOffer;
|
||||
import org.springframework.cheapy.model.TimeOffer;
|
||||
import org.springframework.cheapy.service.ClientService;
|
||||
|
@ -50,6 +51,22 @@ public class TimeOfferController {
|
|||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private boolean checkDates(final TimeOffer timeOffer) {
|
||||
boolean res = false;
|
||||
if(timeOffer.getEnd().isAfter(timeOffer.getStart())) {
|
||||
res = true;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
private boolean checkTimes(final TimeOffer timeOffer) {
|
||||
boolean res = false;
|
||||
if(timeOffer.getFinish().isAfter(timeOffer.getInit())) {
|
||||
res = true;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@GetMapping("/offers/time/new")
|
||||
public String initCreationForm(Map<String, Object> model) {
|
||||
|
@ -63,6 +80,16 @@ public class TimeOfferController {
|
|||
if (result.hasErrors()) {
|
||||
return VIEWS_TIME_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
} else {
|
||||
if(!this.checkDates(timeOffer)) {
|
||||
//Poner aqui mensaje de error
|
||||
return VIEWS_TIME_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
|
||||
if(!this.checkTimes(timeOffer)) {
|
||||
//Poner aqui mensaje de error
|
||||
return VIEWS_TIME_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
|
||||
timeOffer.setStatus(StatusOffer.hidden);
|
||||
|
||||
Client client = this.clientService.getCurrentClient();
|
||||
|
@ -138,6 +165,14 @@ public class TimeOfferController {
|
|||
return TimeOfferController.VIEWS_TIME_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
|
||||
} else {
|
||||
if(!this.checkDates(timeOffer)) {
|
||||
//Poner aqui mensaje de error
|
||||
return VIEWS_TIME_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
if(!this.checkTimes(timeOffer)) {
|
||||
//Poner aqui mensaje de error
|
||||
return VIEWS_TIME_OFFER_CREATE_OR_UPDATE_FORM;
|
||||
}
|
||||
BeanUtils.copyProperties(this.timeOfferService.findTimeOfferById(timeOfferEdit.getId()), timeOfferEdit,
|
||||
"start", "end", "init", "finish", "discount");
|
||||
this.timeOfferService.saveTimeOffer(timeOfferEdit);
|
||||
|
|
Loading…
Reference in a new issue