Añadidas ofertas de numero de clientes y de franja horaria

This commit is contained in:
abemorcardc 2021-03-24 21:58:46 +01:00
parent a10a0c2476
commit 83897d84d5
15 changed files with 428 additions and 15 deletions

View file

@ -37,10 +37,11 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
http.authorizeRequests().antMatchers("/resources/**", "/webjars/**", "/h2-console/**").permitAll()
.antMatchers(HttpMethod.GET, "/", "/oups").permitAll()
.antMatchers("/users/new").permitAll()
.antMatchers("/nuOffers/new").hasAnyAuthority("admin","client")
.antMatchers("/timeOffers/new").hasAnyAuthority("admin","client")
.antMatchers("/usuarios/new").permitAll()
.antMatchers("/admin/**").hasAnyAuthority("admin")
.antMatchers("/owners/**").hasAnyAuthority("owner", "admin")
.antMatchers("/vets/**").authenticated().anyRequest().denyAll()
.and().formLogin()
/* .loginPage("/login") */
.failureUrl("/login-error").and().logout().logoutSuccessUrl("/");

View file

@ -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 = "nu_offers")
public class NuOffer extends Offer {
@NotBlank
@NotNull
private Integer gold;
@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")

View file

@ -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,16 +31,16 @@ 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;
@NotBlank
private String code;
@Enumerated(value = EnumType.STRING)
@ -84,4 +82,12 @@ public class Offer extends BaseEntity {
this.type = type;
}
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}
}

View file

@ -15,13 +15,12 @@
*/
package org.springframework.cheapy.model;
import java.time.LocalDateTime;
import java.time.LocalTime;
import javax.persistence.Entity;
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;
@ -30,11 +29,11 @@ import org.springframework.format.annotation.DateTimeFormat;
public class TimeOffer extends Offer {
@DateTimeFormat(pattern = "HH:mm")
@NotBlank
@NotNull
private LocalTime init;
@DateTimeFormat(pattern = "HH:mm")
@NotBlank
@NotNull
private LocalTime finish;
@NotBlank
@ -42,6 +41,22 @@ public class TimeOffer extends Offer {
public LocalTime getInit() {
return init;
}
public void setInit(LocalTime init) {
this.init = init;
}
public LocalTime getFinish() {
return finish;
}
public void setFinish(LocalTime finish) {
this.finish = finish;
}
public String getDiscount() {
return discount;
}

View file

@ -0,0 +1,11 @@
package org.springframework.cheapy.repository;
import org.springframework.cheapy.model.Client;
import org.springframework.data.repository.CrudRepository;
public interface ClientRepository extends CrudRepository<Client, String> {
Client findByUsername(String currentPrincipalName);
}

View file

@ -0,0 +1,18 @@
package org.springframework.cheapy.repository;
import org.springframework.cheapy.model.NuOffer;
import org.springframework.data.repository.Repository;
public interface NuOfferRepository extends Repository<NuOffer, Integer> {
NuOffer findNuOfferById(int nuOfferId);
void save(NuOffer nuOffer);
}

View file

@ -0,0 +1,19 @@
package org.springframework.cheapy.repository;
import org.springframework.cheapy.model.NuOffer;
import org.springframework.cheapy.model.TimeOffer;
import org.springframework.data.repository.Repository;
public interface TimeOfferRepository extends Repository<NuOffer, Integer> {
TimeOffer findTimeOfferById(int timeOfferId);
void save(TimeOffer timeOffer);
}

View file

@ -0,0 +1,41 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cheapy.service;
import org.springframework.cheapy.model.Client;
import org.springframework.cheapy.repository.ClientRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class ClientService {
private ClientRepository clientRepository;
@Transactional
public Client getCurrentClient() throws DataAccessException {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String currentPrincipalName = authentication.getName();
return this.clientRepository.findByUsername(currentPrincipalName);
}
}

View file

@ -0,0 +1,28 @@
package org.springframework.cheapy.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cheapy.model.NuOffer;
import org.springframework.cheapy.repository.NuOfferRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
@Service
public class NuOfferService {
private NuOfferRepository nuOfferRepository;
@Autowired
public NuOfferService(final NuOfferRepository nuOfferRepository) {
this.nuOfferRepository = nuOfferRepository;
}
public NuOffer findNuOfferById(final int id) {
return this.nuOfferRepository.findNuOfferById(id);
}
public void saveNuOffer(final NuOffer nuOffer) throws DataAccessException { //
this.nuOfferRepository.save(nuOffer);
}
}

View file

@ -0,0 +1,28 @@
package org.springframework.cheapy.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cheapy.model.TimeOffer;
import org.springframework.cheapy.repository.TimeOfferRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
@Service
public class TimeOfferService {
private TimeOfferRepository TimeOfferRepository;
@Autowired
public TimeOfferService(final TimeOfferRepository TimeOfferRepository) {
this.TimeOfferRepository = TimeOfferRepository;
}
public TimeOffer findTimeOfferById(final int id) {
return this.TimeOfferRepository.findTimeOfferById(id);
}
public void saveTimeOffer(final TimeOffer TimeOffer) throws DataAccessException { //
this.TimeOfferRepository.save(TimeOffer);
}
}

View file

@ -0,0 +1,84 @@
package org.springframework.cheapy.web;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.cheapy.model.Client;
import org.springframework.cheapy.model.NuOffer;
import org.springframework.cheapy.model.StatusOffer;
import org.springframework.cheapy.service.ClientService;
import org.springframework.cheapy.service.NuOfferService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class NuOfferController {
private static final String VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM = "nuOffers/createOrUpdateNuOfferForm";
private final NuOfferService nuOfferService;
private final ClientService clientService;
public NuOfferController(final NuOfferService nuOfferService,ClientService clientService) {
this.nuOfferService = nuOfferService;
this.clientService = clientService;
}
@InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id");
}
@GetMapping("/nuOffers/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("/nuOffers/new")
public String processCreationForm(@Valid NuOffer nuOffer, BindingResult result) {
if (result.hasErrors()) {
return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM;
}
else {
nuOffer.setType(StatusOffer.hidden);
Client client = this.clientService.getCurrentClient();
nuOffer.setClient(client);
this.nuOfferService.saveNuOffer(nuOffer);
return "redirect:/nuOffers/" + nuOffer.getId();
}
}
@GetMapping(value ="/nuOffers/{nuOfferId}/activate")
public String activateNuOffer(@PathVariable("nuOfferId") final int nuOfferId, final ModelMap modelMap) {
Client client = this.clientService.getCurrentClient();
NuOffer nuOffer=this.nuOfferService.findNuOfferById(nuOfferId);
if(nuOffer.getClient().equals(client)) {
nuOffer.setType(StatusOffer.active);
nuOffer.setCode("NU-"+nuOfferId);
return "redirect:/nuOffers/" + nuOffer.getId();
} else {
modelMap.addAttribute("message", "You don't have access to this number offer");
}
return "redirect:/nuOffers/" + nuOffer.getId();
}
}

View file

@ -0,0 +1,84 @@
package org.springframework.cheapy.web;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.cheapy.model.Client;
import org.springframework.cheapy.model.TimeOffer;
import org.springframework.cheapy.model.StatusOffer;
import org.springframework.cheapy.service.ClientService;
import org.springframework.cheapy.service.TimeOfferService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class TimeOfferController {
private static final String VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM = "timeOffers/createOrUpdateTimeOfferForm";
private final TimeOfferService timeOfferService;
private final ClientService clientService;
public TimeOfferController(final TimeOfferService timeOfferService,ClientService clientService) {
this.timeOfferService = timeOfferService;
this.clientService = clientService;
}
@InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id");
}
@GetMapping("/timeOffers/new")
public String initCreationForm(Map<String, Object> model) {
TimeOffer timeOffer = new TimeOffer();
model.put("timeOffer", timeOffer);
return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM;
}
@PostMapping("/timeOffers/new")
public String processCreationForm(@Valid TimeOffer timeOffer, BindingResult result) {
if (result.hasErrors()) {
return VIEWS_NU_OFFER_CREATE_OR_UPDATE_FORM;
}
else {
timeOffer.setType(StatusOffer.hidden);
Client client = this.clientService.getCurrentClient();
timeOffer.setClient(client);
this.timeOfferService.saveTimeOffer(timeOffer);
return "redirect:/TimeOffers/" + timeOffer.getId();
}
}
@GetMapping(value ="/timeOffers/{timeOfferId}/activate")
public String activateTimeOffer(@PathVariable("timeOfferId") final int timeOfferId, final ModelMap modelMap) {
Client client = this.clientService.getCurrentClient();
TimeOffer timeOffer=this.timeOfferService.findTimeOfferById(timeOfferId);
if(timeOffer.getClient().equals(client)) {
timeOffer.setType(StatusOffer.active);
timeOffer.setCode("TI-"+timeOfferId);
return "redirect:/timeOffers/" + timeOffer.getId();
} else {
modelMap.addAttribute("message", "You don't have access to this number offer");
}
return "redirect:/timeOffers/" + timeOffer.getId();
}
}

View file

@ -0,0 +1,39 @@
<%@ 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="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="petclinic" tagdir="/WEB-INF/tags" %>
<petclinic:layout pageName="NumOffers">
<h2>
<c:if test="${nuOffer['new']}">New </c:if> NuOffer
</h2>
<form:form modelAttribute="nuOffer" class="form-horizontal" id="add-nuOffer-form">
<div class="form-group has-feedback">
<petclinic:inputField label="Fecha de inicio" name="start"/>
<petclinic:inputField label="Fecha de fin" name="end"/>
<petclinic:inputField label="Oro" name="gold"/>
<petclinic:inputField label="descuento de oro" name="discountGold"/>
<petclinic:inputField label="Plata" name="silver"/>
<petclinic:inputField label="Descuento de plata" name="discountSilver"/>
<petclinic:inputField label="Bronce" name="bronze"/>
<petclinic:inputField label="Descuento de bronce" name="discountBronze"/>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<c:choose>
<c:when test="${nuOffer['new']}">
<button class="btn btn-default" type="submit">Add Offer</button>
</c:when>
<c:otherwise>
<button class="btn btn-default" type="submit">Update Offer</button>
</c:otherwise>
</c:choose>
</div>
</div>
</form:form>
</petclinic:layout>

View file

@ -0,0 +1,36 @@
<%@ 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="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="petclinic" tagdir="/WEB-INF/tags" %>
<petclinic:layout pageName="TimeOffers">
<h2>
<c:if test="${timeOffer['new']}">New </c:if> TimeOffer
</h2>
<form:form modelAttribute="timeOffer" class="form-horizontal" id="add-timeOffer-form">
<div class="form-group has-feedback">
<petclinic:inputField label="Fecha de inicio" name="start"/>
<petclinic:inputField label="Fecha de fin" name="end"/>
<petclinic:inputField label="Hora de inicio" name="init"/>
<petclinic:inputField label="Hora de final" name="finish"/>
<petclinic:inputField label="Decuento" name="discount"/>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<c:choose>
<c:when test="${timeOffer['new']}">
<button class="btn btn-default" type="submit">Add Offer</button>
</c:when>
<c:otherwise>
<button class="btn btn-default" type="submit">Update Offer</button>
</c:otherwise>
</c:choose>
</div>
</div>
</form:form>
</petclinic:layout>

View file

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
package org.springframework.cheapy.web;
import static org.hamcrest.Matchers.hasProperty;
@ -40,6 +40,7 @@ import org.springframework.test.web.servlet.MockMvc;
*
* @author Colin But
*/
/*
@WebMvcTest(OwnerController.class)
class OwnerControllerTests {
@ -161,3 +162,4 @@ class OwnerControllerTests {
}
}
*/