Solo Ofertas activas y Mis ofertas

This commit is contained in:
Antonio Vidal 2021-03-30 18:47:55 +02:00
parent 8f59d2d2a3
commit 0ba60d4ab9
13 changed files with 292 additions and 27 deletions

View file

@ -1,5 +1,6 @@
package org.springframework.cheapy.model;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
@ -57,16 +58,16 @@ public class Client extends BaseEntity {
private User usuar;
@OneToMany
private Set<FoodOffer> foodOffers;
private List<FoodOffer> foodOffers;
@OneToMany
private Set<NuOffer> nuOffers;
private List<NuOffer> nuOffers;
@OneToMany
private Set<SpeedOffer> speedOffers;
private List<SpeedOffer> speedOffers;
@OneToMany
private Set<TimeOffer> timeOffers;
private List<TimeOffer> timeOffers;
public String getName() {
return name;
@ -148,35 +149,35 @@ public class Client extends BaseEntity {
this.usuar = usuar;
}
public Set<FoodOffer> getFoodOffers() {
public List<FoodOffer> getFoodOffers() {
return foodOffers;
}
public void setFoodOffers(Set<FoodOffer> foodOffers) {
public void setFoodOffers(List<FoodOffer> foodOffers) {
this.foodOffers = foodOffers;
}
public Set<NuOffer> getNuOffers() {
public List<NuOffer> getNuOffers() {
return nuOffers;
}
public void setNuOffers(Set<NuOffer> nuOffers) {
public void setNuOffers(List<NuOffer> nuOffers) {
this.nuOffers = nuOffers;
}
public Set<SpeedOffer> getSpeedOffers() {
public List<SpeedOffer> getSpeedOffers() {
return speedOffers;
}
public void setSpeedOffers(Set<SpeedOffer> speedOffers) {
public void setSpeedOffers(List<SpeedOffer> speedOffers) {
this.speedOffers = speedOffers;
}
public Set<TimeOffer> getTimeOffers() {
public List<TimeOffer> getTimeOffers() {
return timeOffers;
}
public void setTimeOffers(Set<TimeOffer> timeOffers) {
public void setTimeOffers(List<TimeOffer> timeOffers) {
this.timeOffers = timeOffers;
}

View file

@ -1,6 +1,8 @@
package org.springframework.cheapy.repository;
import org.springframework.cheapy.model.FoodOffer;
import org.springframework.cheapy.model.StatusOffer;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
@ -18,5 +20,12 @@ public interface FoodOfferRepository extends Repository<FoodOffer, Integer> {
FoodOffer findById(@Param("id") Integer id);
void save(FoodOffer foodOffer);
@Query("SELECT foodOffer FROM FoodOffer foodOffer WHERE foodOffer.status =:status")
@Transactional(readOnly = true)
List<FoodOffer> findActiveFoodOffer(StatusOffer status);
@Query("SELECT foodOffer FROM FoodOffer foodOffer WHERE foodOffer.client.id =:id")
@Transactional(readOnly = true)
List<FoodOffer> findByUserId(@Param("id") Integer id);
}

View file

@ -1,8 +1,12 @@
package org.springframework.cheapy.repository;
import java.util.List;
import org.springframework.cheapy.model.FoodOffer;
import org.springframework.cheapy.model.NuOffer;
import org.springframework.cheapy.model.StatusOffer;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
@ -15,5 +19,12 @@ public interface NuOfferRepository extends Repository<NuOffer, Integer> {
List<NuOffer> findAllNuOffer();
void save(NuOffer nuOffer);
@Query("SELECT nuOffer FROM NuOffer nuOffer WHERE nuOffer.status =:status")
@Transactional(readOnly = true)
List<NuOffer> findActiveNuOffer(StatusOffer status);
@Query("SELECT nuOffer FROM NuOffer nuOffer WHERE nuOffer.client.id =:id")
@Transactional(readOnly = true)
List<NuOffer> findByUserId(@Param("id") Integer id);
}

View file

@ -1,7 +1,10 @@
package org.springframework.cheapy.repository;
import java.util.List;
import org.springframework.cheapy.model.FoodOffer;
import org.springframework.cheapy.model.SpeedOffer;
import org.springframework.cheapy.model.StatusOffer;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
@ -18,5 +21,12 @@ public interface SpeedOfferRepository extends Repository<SpeedOffer, Integer> {
List<SpeedOffer> findAllSpeedOffer();
void save(SpeedOffer speedOffer);
@Query("SELECT speedOffer FROM SpeedOffer speedOffer WHERE speedOffer.status =:status")
@Transactional(readOnly = true)
List<SpeedOffer> findActiveSpeedOffer(StatusOffer status);
@Query("SELECT speedOffer FROM SpeedOffer speedOffer WHERE speedOffer.client.id =:id")
@Transactional(readOnly = true)
List<SpeedOffer> findByUserId(@Param("id") Integer id);
}

View file

@ -1,8 +1,11 @@
package org.springframework.cheapy.repository;
import java.util.List;
import org.springframework.cheapy.model.StatusOffer;
import org.springframework.cheapy.model.TimeOffer;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;
@ -15,5 +18,12 @@ public interface TimeOfferRepository extends Repository<TimeOffer, Integer> {
List<TimeOffer> findAllTimeOffer();
void save(TimeOffer timeOffer);
@Query("SELECT timeOffer FROM TimeOffer timeOffer WHERE timeOffer.status =:status")
@Transactional(readOnly = true)
List<TimeOffer> findActiveTimeOffer(StatusOffer status);
@Query("SELECT timeOffer FROM TimeOffer timeOffer WHERE timeOffer.client.id =:id")
@Transactional(readOnly = true)
List<TimeOffer> findByUserId(@Param("id") Integer id);
}

View file

@ -2,6 +2,7 @@ package org.springframework.cheapy.service;
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;
@ -20,12 +21,20 @@ public class FoodOfferService {
public FoodOffer findFoodOfferById(final int id) {
return this.foodOfferRepository.findById(id);
}
public List<FoodOffer> findAllFoodOffer() { //
return this.foodOfferRepository.findAllFoodOffer();
}
public void saveFoodOffer(final FoodOffer foodOffer) throws DataAccessException {
this.foodOfferRepository.save(foodOffer);
}
public List<FoodOffer> findActiveFoodOffer() {
return this.foodOfferRepository.findActiveFoodOffer(StatusOffer.active);
}
public List<FoodOffer> findFoodOfferByUserId(final int id) {
return this.foodOfferRepository.findByUserId(id);
}
}

View file

@ -3,6 +3,7 @@ package org.springframework.cheapy.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cheapy.model.NuOffer;
import org.springframework.cheapy.model.StatusOffer;
import org.springframework.cheapy.repository.NuOfferRepository;
import java.util.List;
import org.springframework.dao.DataAccessException;
@ -33,4 +34,12 @@ public class NuOfferService {
public void saveNuOffer(final NuOffer nuOffer) throws DataAccessException {
this.nuOfferRepository.save(nuOffer);
}
public List<NuOffer> findActiveNuOffer() {
return this.nuOfferRepository.findActiveNuOffer(StatusOffer.active);
}
public List<NuOffer> findNuOfferByUserId(final int id) {
return this.nuOfferRepository.findByUserId(id);
}
}

View file

@ -3,7 +3,9 @@ package org.springframework.cheapy.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cheapy.model.NuOffer;
import org.springframework.cheapy.model.SpeedOffer;
import org.springframework.cheapy.model.StatusOffer;
import org.springframework.cheapy.repository.SpeedOfferRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
@ -34,4 +36,12 @@ public class SpeedOfferService {
public void saveSpeedOffer(final SpeedOffer speedOffer) throws DataAccessException { //
this.speedOfferRepository.save(speedOffer);
}
public List<SpeedOffer> findActiveSpeedOffer() {
return this.speedOfferRepository.findActiveSpeedOffer(StatusOffer.active);
}
public List<SpeedOffer> findSpeedOfferByUserId(final int id) {
return this.speedOfferRepository.findByUserId(id);
}
}

View file

@ -3,6 +3,7 @@ package org.springframework.cheapy.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cheapy.model.StatusOffer;
import org.springframework.cheapy.model.TimeOffer;
import org.springframework.cheapy.repository.TimeOfferRepository;
@ -30,7 +31,13 @@ public class TimeOfferService {
public void saveTimeOffer(final TimeOffer TimeOffer) throws DataAccessException {
this.timeOfferRepository.save(TimeOffer);
}
public List<TimeOffer> findActiveTimeOffer() {
return this.timeOfferRepository.findActiveTimeOffer(StatusOffer.active);
}
public List<TimeOffer> findTimeOfferByUserId(final int id) {
return this.timeOfferRepository.findByUserId(id);
}
}

View file

@ -8,6 +8,7 @@ import org.springframework.cheapy.model.FoodOffer;
import org.springframework.cheapy.model.NuOffer;
import org.springframework.cheapy.model.SpeedOffer;
import org.springframework.cheapy.model.TimeOffer;
import org.springframework.cheapy.service.ClientService;
import org.springframework.cheapy.service.FoodOfferService;
import org.springframework.cheapy.service.NuOfferService;
import org.springframework.cheapy.service.SpeedOfferService;
@ -17,14 +18,17 @@ import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class OfertaController {
private final ClientService clientService;
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) {
final SpeedOfferService speedOfferService, final TimeOfferService timeOfferService, ClientService clientService) {
this.clientService = clientService;
this.foodOfferService = foodOfferService;
this.nuOfferService = nuOfferService;
this.speedOfferService = speedOfferService;
@ -34,10 +38,10 @@ public class OfertaController {
@GetMapping("/offers")
public String processFindForm( Map<String, Object> model) {
List<FoodOffer> foodOfferLs=this.foodOfferService.findAllFoodOffer();
List<NuOffer> nuOfferLs=this.nuOfferService.findAllNuOffer();
List<SpeedOffer> speedOfferLs=this.speedOfferService.findAllSpeedOffer();
List<TimeOffer> timeOfferLs=this.timeOfferService.findAllTimeOffer();
List<FoodOffer> foodOfferLs=this.foodOfferService.findActiveFoodOffer();
List<NuOffer> nuOfferLs=this.nuOfferService.findActiveNuOffer();
List<SpeedOffer> speedOfferLs=this.speedOfferService.findActiveSpeedOffer();
List<TimeOffer> timeOfferLs=this.timeOfferService.findActiveTimeOffer();
model.put("foodOfferLs", foodOfferLs);
model.put("nuOfferLs", nuOfferLs);
@ -50,7 +54,28 @@ public class OfertaController {
return "offers/offersList";
}
@GetMapping("/myOffers")
public String processMyOffersForm( Map<String, Object> model) {
int actual = this.clientService.getCurrentClient().getId();
List<FoodOffer> foodOfferLs = this.foodOfferService.findFoodOfferByUserId(actual);
List<NuOffer> nuOfferLs = this.nuOfferService.findNuOfferByUserId(actual);
List<SpeedOffer> speedOfferLs = this.speedOfferService.findSpeedOfferByUserId(actual);
List<TimeOffer> timeOfferLs = this.timeOfferService.findTimeOfferByUserId(actual);
model.put("foodOfferLs", foodOfferLs);
model.put("nuOfferLs", nuOfferLs);
model.put("speedOfferLs", speedOfferLs);
model.put("timeOfferLs", timeOfferLs);
//Se añade formateador de fecha al modelo
model.put("localDateTimeFormat", DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm"));
return "offers/myOffersList";
}
// @GetMapping("/owners/{ownerId}/edit")
// public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) {
// Owner owner = this.ownerService.findOwnerById(ownerId);

View file

@ -32,7 +32,7 @@ INSERT INTO usuarios VALUES (4, 'Pepe', 'Lopez', '12456776V', 'C/Macarena', '690
INSERT INTO clients (id, name, email, address, init, finish, telephone, description, code, food, username) VALUES (1,'bar manoli','manoli@gmail.com','C/Betis','10:00','22:00','608726190', 'description 1', 'code1', 'ESPAÑOLA','manoli');
INSERT INTO clients (id, name, email, address, init, finish, telephone, description, code, food, username) VALUES (2,'bar david','david@gmail.com','C/Sevilla','09:30','22:00','608726190', 'description 2', 'code2', 'americana','david');
INSERT INTO food_offers(start, end, code, status, client_id, food, discount) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'FO-1', 'active', 1, 'macarrones', 15);
INSERT INTO food_offers(start, end, code, status, client_id, food, discount) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'FO-1', 'inactive', 1, 'macarrones', 15);
INSERT INTO time_offers(start, end, code, status, client_id, init, finish, discount) VALUES ('2021-06-15 12:00:00', '2021-06-16 12:00:00', 'T-1', 'active', 1, '12:00:00', '13:00:00', 10);
INSERT INTO speed_offers(start, end, code, status, 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',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-06-15 12:00:00', '2021-06-16 12:00:00', 'NU-1', 'active',1,15,25,10,15,5,10);

View file

@ -0,0 +1,158 @@
<%@ 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="cheapy" tagdir="/WEB-INF/tags" %>
<link href='https://fonts.googleapis.com/css?family=Lobster' rel='stylesheet'>
<cheapy:layout pageName="myOffers">
<h2 style="text-align:center;padding:5px"><fmt:message key="foodOffers"/></h2>
<table id="foodOfferTable" class="table table-striped">
<thead>
<tr>
<!-- <th style="width: 150px;">Restaurante</th> -->
<th><fmt:message key="food"/></th>
<th><fmt:message key="startDate"/></th>
<th><fmt:message key="endDate"/></th>
<th> </th>
</tr>
</thead>
<tbody>
<c:forEach items="${foodOfferLs}" var="foodOffer">
<tr>
<td>
<c:out value="${foodOffer.food}"/>
</td>
<td>
<c:out value="${localDateTimeFormat.format(foodOffer.start)}"/>
</td>
<td>
<c:out value="${localDateTimeFormat.format(foodOffer.end)}"/>
</td>
<td>
<spring:url value="/offers/food/{foodOfferId}" var="foodOfferUrl">
<spring:param name="foodOfferId" value="${foodOffer.id}"/>
</spring:url>
<div class="btn-detalles">
<button type="button" role="link" onclick="window.location='${fn:escapeXml(foodOfferUrl)}'" style="font-family: 'Lobster'; font-size: 20px;">
<span class="glyphicon glyphicon-info-sign" aria-hidden="true" style="padding: 5px"> </span>
<fmt:message key="details"/></button>
</div>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<h2 style="text-align:center;padding:5px"><fmt:message key="nuOffers"/></h2>
<table id="nuOfferTable" class="table table-striped">
<thead>
<tr>
<!-- <th style="width: 150px;">Restaurante</th> -->
<th><fmt:message key="startDate"/></th>
<th><fmt:message key="endDate"/></th>
<th> </th>
</tr>
</thead>
<tbody>
<c:forEach items="${nuOfferLs}" var="nuOffer">
<tr>
<td>
<c:out value="${localDateTimeFormat.format(nuOffer.start)}"/>
</td>
<td>
<c:out value="${localDateTimeFormat.format(nuOffer.end)}"/>
</td>
<td>
<spring:url value="/offers/nu/{nuOfferId}" var="nuOfferUrl">
<spring:param name="nuOfferId" value="${nuOffer.id}"/>
</spring:url>
<div class="btn-detalles">
<button type="button" role="link" onclick="window.location='${fn:escapeXml(nuOfferUrl)}'" class="btn-detalles" style="font-family: 'Lobster'; font-size: 20px;">
<span class="glyphicon glyphicon-info-sign" aria-hidden="true" style="padding: 5px"> </span>
<fmt:message key="details"/> </button>
</div>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<h2 style="text-align:center;padding:5px"><fmt:message key="speedOffers"/></h2>
<table id="speedOfferTable" class="table table-striped">
<thead>
<tr>
<!-- <th style="width: 150px;">Restaurante</th> -->
<th><fmt:message key="startDate"/></th>
<th><fmt:message key="endDate"/></th>
<th> </th>
</tr>
</thead>
<tbody>
<c:forEach items="${speedOfferLs}" var="speedOffer">
<tr>
<td>
<c:out value="${localDateTimeFormat.format(speedOffer.start)}"/>
</td>
<td>
<c:out value="${localDateTimeFormat.format(speedOffer.end)}"/>
</td>
<td>
<spring:url value="/offers/speed/{speedOfferId}" var="speedOfferUrl">
<spring:param name="speedOfferId" value="${speedOffer.id}"/>
</spring:url>
<div class="btn-detalles">
<button type="button" role="link" onclick="window.location='${fn:escapeXml(speedOfferUrl)}'" class="btn-detalles" style="font-family: 'Lobster'; font-size: 20px;">
<span class="glyphicon glyphicon-info-sign" aria-hidden="true" style="padding: 5px"> </span>
<fmt:message key="details"/> </button>
</div>
</td>
</tr>
</c:forEach>
</tbody>
</table>
<h2 style="text-align:center;padding:5px"><fmt:message key="timeOffers"/></h2>
<table id="timeOfferTable" class="table table-striped">
<thead>
<tr>
<!-- <th style="width: 150px;">Restaurante</th> -->
<th><fmt:message key="startDate"/></th>
<th><fmt:message key="endDate"/></th>
<th> </th>
</tr>
</thead>
<tbody>
<c:forEach items="${timeOfferLs}" var="timeOffer">
<tr>
<td>
<c:out value="${localDateTimeFormat.format(timeOffer.start)}"/>
</td>
<td>
<c:out value="${localDateTimeFormat.format(timeOffer.end)}"/>
</td>
<td>
<spring:url value="/offers/time/{timeOfferId}" var="timeOfferUrl">
<spring:param name="timeOfferId" value="${timeOffer.id}"/>
</spring:url>
<div class="btn-detalles">
<button type="button" role="link" onclick="window.location='${fn:escapeXml(timeOfferUrl)}'" class="btn-detalles" style="font-family: 'Lobster'; font-size: 20px;">
<span class="glyphicon glyphicon-info-sign" aria-hidden="true" style="padding: 5px"> </span>
<fmt:message key="details"/> </button>
</div>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</cheapy:layout>

View file

@ -25,7 +25,7 @@
<cheapy:menuItem active="${name eq 'home'}" url="/"
title="home page">
<span class="glyphicon glyphicon-home" aria-hidden="true"></span>
<span>Home</span>
<span>Inicio</span>
</cheapy:menuItem>
<cheapy:menuItem active="${name eq 'ofertas'}" url="/offers"
@ -34,6 +34,12 @@
<span>Ver ofertas</span>
</cheapy:menuItem>
<sec:authorize access="hasAnyAuthority('client')">
<cheapy:menuItem active="${name eq 'ofertas'}" url="/myOffers" title="misOfertas">
<span class="glyphicon glyphicon-cutlery" aria-hidden="true"></span>
<span>Mis ofertas</span>
</cheapy:menuItem>
</sec:authorize>
<!--
<cheapy:menuItem active="${name eq 'contactanos'}" url="/contactanos"
title="contactanos">