Merge pull request #26 from cheapy-ispp/007-publicarOfertas-Tibo

007 publicar ofertas tibo
This commit is contained in:
abemorcardc 2021-03-25 21:33:06 +01:00 committed by GitHub
commit 20792da435
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 383 additions and 25 deletions

View file

@ -41,6 +41,8 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
.antMatchers("/timeOffers/**").hasAnyAuthority("admin","client")
.antMatchers("/usuarios/new").permitAll()
.antMatchers("/admin/**").hasAnyAuthority("admin")
.antMatchers("/speedOffers/**").hasAnyAuthority("admin", "client")
.antMatchers("/foodOffers/**").hasAnyAuthority("admin", "client")
.antMatchers("/owners/**").hasAnyAuthority("owner", "admin")
.and().formLogin()
/* .loginPage("/login") */

View file

@ -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() {

View file

@ -40,12 +40,13 @@ public class Offer extends BaseEntity {
@Future
private LocalDateTime end;
private String code;
@Enumerated(value = EnumType.STRING)
private StatusOffer type;
@ManyToOne
@JoinColumn(name="client_id")
private Client client;
@ -85,7 +86,7 @@ public class Offer extends BaseEntity {
public Client getClient() {
return client;
}
public void setClient(Client client) {
this.client = client;
}

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 = "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")

View file

@ -1,6 +1,7 @@
package org.springframework.cheapy.repository;
import org.springframework.cheapy.model.Client;
import org.springframework.data.repository.CrudRepository;

View file

@ -0,0 +1,33 @@
/*
* Copyright 2012-2019 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
*
* https://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.repository;
import org.springframework.cheapy.model.FoodOffer;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
public interface FoodOfferRepository extends Repository<FoodOffer, Integer> {
@Query("SELECT foodOffer FROM FoodOffer foodOffer WHERE id =:id")
@Transactional(readOnly = true)
FoodOffer findById(@Param("id") Integer id);
void save(FoodOffer foodOffer);
}

View file

@ -0,0 +1,33 @@
/*
* Copyright 2012-2019 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
*
* https://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.repository;
import org.springframework.cheapy.model.SpeedOffer;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;
public interface SpeedOfferRepository extends Repository<SpeedOffer, Integer> {
@Query("SELECT speedOffer FROM SpeedOffer speedOffer WHERE id =:id")
@Transactional(readOnly = true)
SpeedOffer findById(@Param("id") Integer id);
void save(SpeedOffer speedOffer);
}

View file

@ -36,12 +36,14 @@ public class ClientService {
public ClientService(final ClientRepository clientRepository) {
this.clientRepository = clientRepository;
}
@Transactional
public Client getCurrentClient() throws DataAccessException {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String username = authentication.getName();
return this.clientRepository.findByUsername(username);
}
}

View file

@ -0,0 +1,27 @@
package org.springframework.cheapy.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cheapy.model.FoodOffer;
import org.springframework.cheapy.repository.FoodOfferRepository;
import org.springframework.dao.DataAccessException;
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);
}
public void saveFoodOffer(final FoodOffer foodOffer) throws DataAccessException {
this.foodOfferRepository.save(foodOffer);
}
}

View file

@ -0,0 +1,27 @@
package org.springframework.cheapy.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cheapy.model.SpeedOffer;
import org.springframework.cheapy.repository.SpeedOfferRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
@Service
public class SpeedOfferService {
private SpeedOfferRepository speedOfferRepository;
@Autowired
public SpeedOfferService(final SpeedOfferRepository speedOfferRepository) {
this.speedOfferRepository = speedOfferRepository;
}
public SpeedOffer findSpeedOfferById(final int id) {
return this.speedOfferRepository.findById(id);
}
public void saveSpeedOffer(final SpeedOffer speedOffer) throws DataAccessException {
this.speedOfferRepository.save(speedOffer);
}
}

View file

@ -0,0 +1,89 @@
/*
* Copyright 2012-2019 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
*
* https://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.web;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.cheapy.model.Client;
import org.springframework.cheapy.model.FoodOffer;
import org.springframework.cheapy.model.StatusOffer;
import org.springframework.cheapy.service.ClientService;
import org.springframework.cheapy.service.FoodOfferService;
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 FoodOfferController {
private static final String VIEWS_FOOD_OFFER_CREATE_OR_UPDATE_FORM = "foodOffers/createOrUpdateFoodOfferForm";
private final FoodOfferService foodOfferService;
private final ClientService clientService;
public FoodOfferController(final FoodOfferService foodOfferService, final ClientService clientService) {
this.foodOfferService = foodOfferService;
this.clientService = clientService;
}
@InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id");
}
@GetMapping("/foodOffers/new")
public String initCreationForm(Map<String, Object> model) {
FoodOffer foodOffer = new FoodOffer();
model.put("foodOffer", foodOffer);
return VIEWS_FOOD_OFFER_CREATE_OR_UPDATE_FORM;
}
@PostMapping("/foodOffers/new")
public String processCreationForm(@Valid FoodOffer foodOffer, BindingResult result) {
if (result.hasErrors()) {
return VIEWS_FOOD_OFFER_CREATE_OR_UPDATE_FORM;
}
else {
Client client = this.clientService.getCurrentclient();
foodOffer.setClient(client);
foodOffer.setType(StatusOffer.hidden);
this.foodOfferService.saveFoodOffer(foodOffer);
return "redirect:/foodOffers/" + foodOffer.getId();
}
}
@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("FO-"+foodOfferId);
this.foodOfferService.saveFoodOffer(foodOffer);
} else {
modelMap.addAttribute("message", "You don't have access to this food offer");
}
return "redirect:/foodOffers/";
}
}

View file

@ -0,0 +1,89 @@
/*
* Copyright 2012-2019 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
*
* https://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.web;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.cheapy.model.Client;
import org.springframework.cheapy.model.SpeedOffer;
import org.springframework.cheapy.model.StatusOffer;
import org.springframework.cheapy.service.ClientService;
import org.springframework.cheapy.service.SpeedOfferService;
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 SpeedOfferController {
private static final String VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM = "speedOffers/createOrUpdateSpeedOfferForm";
private final SpeedOfferService speedOfferService;
private final ClientService clientService;
public SpeedOfferController(final SpeedOfferService speedOfferService, final ClientService clientService) {
this.speedOfferService = speedOfferService;
this.clientService = clientService;
}
@InitBinder
public void setAllowedFields(WebDataBinder dataBinder) {
dataBinder.setDisallowedFields("id");
}
@GetMapping("/speedOffers/new")
public String initCreationForm(Map<String, Object> model) {
SpeedOffer speedOffer = new SpeedOffer();
model.put("speedOffer", speedOffer);
return VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM;
}
@PostMapping("/speedOffers/new")
public String processCreationForm(@Valid SpeedOffer speedOffer, BindingResult result) {
if (result.hasErrors()) {
return VIEWS_SPEED_OFFER_CREATE_OR_UPDATE_FORM;
}
else {
Client client = this.clientService.getCurrentclient();
speedOffer.setClient(client);
speedOffer.setType(StatusOffer.hidden);
this.speedOfferService.saveSpeedOffer(speedOffer);
return "redirect:/speedOffers/" + speedOffer.getId();
}
}
@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)) {
speedOffer.setType(StatusOffer.active);
speedOffer.setCode("SP-"+speedOfferId);
this.speedOfferService.saveSpeedOffer(speedOffer);
} else {
modelMap.addAttribute("message", "You don't have access to this speed offer");
}
return "redirect:/speedOffers/";
}
}

View file

@ -10,24 +10,11 @@ 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', 'FO-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', 'SP-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 clients(username,password,enabled, email, address, timetable,telephone,description,code,food) VALUES ('cliente','cliente',TRUE,'cliente@hotmail.com','Calle Tahona nº5','12:00-23:00','954876351','Descripcion','codigo','variado');
INSERT INTO users(username,password,enabled) VALUES ('cliente','cliente',TRUE);
INSERT INTO authorities(id,username,authority) VALUES (2,'cliente','client');
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', 'cliente', '12:00:00', '13:00:00', '10%');
INSERT INTO clients(username,password,enabled, email, address, timetable,telephone,description,code,food) VALUES ('cliente2','cliente2',TRUE,'cliente@hotmail.com','Calle Tahona nº5','12:00-23:00','954876351','Descripcion','codigo','variado');
INSERT INTO users(username,password,enabled) VALUES ('cliente2','cliente2',TRUE);
INSERT INTO authorities(id,username,authority) VALUES (3,'cliente2','client');

View file

@ -0,0 +1,31 @@
<%@ 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="foodOffers">
<h2>
<c:if test="${foodOffer['new']}">New </c:if> FoodOffer
</h2>
<form:form modelAttribute="foodOffer" class="form-horizontal" id="add-foodOffer-form">
<div class="form-group has-feedback">
<petclinic:inputField label="Start Date" name="start"/>
<petclinic:inputField label="End Date" name="end"/>
<petclinic:inputField label="Food" name="food"/>
<petclinic:inputField label="Discount" name="discount"/>
<petclinic:inputField label="Units" name="units"/>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<c:choose>
<c:when test="${foodOffer['new']}">
<button class="btn btn-default" type="submit">Add Food Offer</button>
</c:when>
</c:choose>
</div>
</div>
</form:form>
</petclinic:layout>

View file

@ -0,0 +1,34 @@
<%@ 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="speedOffers">
<h2>
<c:if test="${speedOffer['new']}">New </c:if> SpeedOffer
</h2>
<form:form modelAttribute="speedOffer" class="form-horizontal" id="add-speedOffer-form">
<div class="form-group has-feedback">
<petclinic:inputField label="Start Date" name="start"/>
<petclinic:inputField label="End Date" name="end"/>
<petclinic:inputField label="Gold" name="gold"/>
<petclinic:inputField label="Gold Discount" name="discountGold"/>
<petclinic:inputField label="Silver" name="silver"/>
<petclinic:inputField label="Silver Discount" name="discountSilver"/>
<petclinic:inputField label="Bronze" name="bronze"/>
<petclinic:inputField label="Bronze Discount" name="discountBronze"/>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<c:choose>
<c:when test="${speedOffer['new']}">
<button class="btn btn-default" type="submit">Add Speed Offer</button>
</c:when>
</c:choose>
</div>
</div>
</form:form>
</petclinic:layout>

View file

@ -44,7 +44,7 @@ import org.springframework.test.web.servlet.MockMvc;
@WebMvcTest(OwnerController.class)
class OwnerControllerTests {
private static final int TEST_OWNER_ID = 1;
/*private static final int TEST_OWNER_ID = 1;
@Autowired
private MockMvc mockMvc;
@ -159,7 +159,7 @@ class OwnerControllerTests {
.andExpect(model().attribute("owner", hasProperty("city", is("Madison"))))
.andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023"))))
.andExpect(view().name("owners/ownerDetails"));
}
}*/
}
*/