Commit 32d85361 authored by Dominik Kozubík's avatar Dominik Kozubík
Browse files

added Match MVC base

parent d02655d3
Pipeline #141621 passed with stage
in 1 minute and 5 seconds
......@@ -3,6 +3,8 @@ package cz.muni.fi.pa165.dto;
import cz.muni.fi.pa165.entity.PlayerEntity;
import cz.muni.fi.pa165.entity.TeamEntity;
import cz.muni.fi.pa165.entity.TournamentEntity;
import lombok.Getter;
import lombok.Setter;
import java.util.HashSet;
import java.util.Set;
......@@ -10,6 +12,8 @@ import java.util.Set;
/**
* @author Dominik Kozubík
*/
@Getter
@Setter
public class MatchDTO extends BaseDTO {
private Set<PlayerEntity> playerList = new HashSet<>();
......
package cz.muni.fi.pa165.facade;
import cz.muni.fi.pa165.dto.MatchDTO;
import cz.muni.fi.pa165.dto.PlayerDTO;
import java.util.List;
/**
* @author Dominik Kozubík
*/
public interface MatchFacade {
List<MatchDTO> findAllMatches();
MatchDTO getMatchById(String uuid);
String createMatch(MatchDTO matchDTO);
void delete(String uuid);
}
package cz.muni.fi.pa165.facade;
import cz.muni.fi.pa165.dto.MatchDTO;
import cz.muni.fi.pa165.entity.MatchEntity;
import cz.muni.fi.pa165.entity.TournamentEntity;
import cz.muni.fi.pa165.services.BeanMappingService;
import cz.muni.fi.pa165.services.match.MatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.List;
import java.util.Optional;
/**
* @author Dominik Kozubík
*/
@Service
@Transactional
public class MatchFacadeImpl implements MatchFacade {
@Autowired
private MatchService matchService;
@Autowired
private BeanMappingService beanMappingService;
@Override
public List<MatchDTO> findAllMatches() {
return beanMappingService.mapTo(matchService.findAll(), MatchDTO.class);
}
@Override
public MatchDTO getMatchById(String uuid) {
return beanMappingService.mapTo(matchService.findById(uuid), MatchDTO.class);
}
@Override
public String createMatch(MatchDTO matchDTO) {
MatchEntity match = new MatchEntity();
match.setId(matchDTO.getId());
match.setPlayerList(matchDTO.getPlayerList());
match.setPlayers(matchDTO.getPlayers());
match.setTeams(matchDTO.getTeams());
match.setTournament(matchDTO.getTournament());
matchService.create(match);
return match.getId();
}
@Override
public void delete(String uuid) {
var match = new MatchEntity();
match.setId(uuid);
matchService.remove(match);
}
}
package cz.muni.fi.pa165.mvc.controllers;
import cz.muni.fi.pa165.dto.MatchDTO;
import cz.muni.fi.pa165.dto.TeamDTO;
import cz.muni.fi.pa165.dto.TournamentDTO;
import cz.muni.fi.pa165.facade.MatchFacade;
import cz.muni.fi.pa165.facade.TeamFacade;
import cz.muni.fi.pa165.facade.TournamentFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springframework.web.util.UriComponentsBuilder;
import javax.validation.Valid;
import java.util.List;
/**
* @author Dominik Kozubík
*/
@Controller
@RequestMapping(value = "/match")
public class MatchController {
@Autowired
private MatchFacade matchFacade;
@Autowired
private TournamentFacade tournamentFacade;
@Autowired
private TeamFacade teamFacade;
@ModelAttribute("tournaments")
public List<TournamentDTO> countries() {
return tournamentFacade.findAllTournament();
}
@ModelAttribute("teams")
public List<TeamDTO> teams() {
return teamFacade.findAllTeams();
}
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String list(Model model) {
List<MatchDTO> matchDTOList = matchFacade.findAllMatches();
model.addAttribute("matches", matchDTOList);
return "/match/list";
}
@RequestMapping(value = "/new", method = RequestMethod.GET)
public String newMatch(Model model) {
model.addAttribute("createMatch", new MatchDTO());
return "match/new";
}
@RequestMapping(value = "/view/{uuid}", method = RequestMethod.GET)
public String view(@PathVariable String uuid, Model model) {
model.addAttribute("match", matchFacade.getMatchById(uuid));
return "match/view";
}
@RequestMapping(value = "/delete/{uuid}", method = RequestMethod.POST)
public String delete(@PathVariable String uuid, UriComponentsBuilder uriBuilder, RedirectAttributes redirectAttributes) {
MatchDTO match = matchFacade.getMatchById(uuid);
try {
matchFacade.delete(uuid);
redirectAttributes.addFlashAttribute("alert_success", "Match \"" + match.getId() + "\" was deleted.");
} catch (Exception ex) {
redirectAttributes.addFlashAttribute("alert_danger", "Match \"" + match.getId() + "\" cannot be deleted.");
}
return "redirect:" + uriBuilder.path("/match/list").toUriString();
}
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String create(@Valid @ModelAttribute("matchCreate") MatchDTO formBean, BindingResult bindingResult,
Model model, RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder) {
//in case of validation error forward back to the the form
if (bindingResult.hasErrors()) {
return "match/new";
}
//create tournament
String uuid = matchFacade.createMatch(formBean);
//report success
redirectAttributes.addFlashAttribute("alert_success", "Match " + uuid + " was created");
return "redirect:" + uriBuilder.path("/match/view/{id}").buildAndExpand(uuid).encode().toUriString();
}
}
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8" trimDirectiveWhitespaces="false" session="false" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="my" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<my:pagetemplate title="Matches">
<jsp:attribute name="body">
<my:a href="/match/new" class="btn btn-primary">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
New match
</my:a>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Tournament</th>
<th>Teams</th>
<th>Players</th>
</tr>
</thead>
<tbody>
<c:forEach items="${matches}" var="match">
<tr>
<td>${match.id}</td>
<td><c:out value="${match.tournament.city}"/></td>
<td><c:out value="${match.teams.toArray()}"/></td>
<td><c:out value="${match.playerList.toArray()}"/></td>
<td>
<my:a href="/match/view/${match.id}" class="btn btn-primary">View</my:a>
</td>
<td>
<form method="post" action="${pageContext.request.contextPath}/match/delete/${match.id}">
<button type="submit" class="btn btn-primary">Delete</button>
</form>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</jsp:attribute>
</my:pagetemplate>
\ No newline at end of file
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8" trimDirectiveWhitespaces="false" session="false" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="my" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<my:pagetemplate title="New match">
<jsp:attribute name="body">
<form:form method="post" action="${pageContext.request.contextPath}/match/create"
modelAttribute="createMatch" cssClass="form-horizontal">
<div class="form-group">
<form:label path="tournament" cssClass="col-sm-2 control-label">Tournament</form:label>
<div class="col-sm-10">
<form:select path="tournament" cssClass="form-control">
<c:forEach items="${tournaments}" var="tournament">
<form:option value="${tournament.id}">${tournament.id}</form:option>
</c:forEach>
</form:select>
<form:errors path="tournament" cssClass="error"/>
</div>
</div>
<div class="form-group">
<form:label path="teams" cssClass="col-sm-2 control-label">Team</form:label>
<div class="col-sm-10">
<form:select path="teams" cssClass="form-control">
<c:forEach items="${teams}" var="team">
<form:option value="${team.name}">${team.name}</form:option>
</c:forEach>
</form:select>
<form:errors path="teams" cssClass="error"/>
</div>
<button class="btn btn-primary" type="submit">Create match</button>
</form:form>
</jsp:attribute>
</my:pagetemplate>
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8" trimDirectiveWhitespaces="false" session="false" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="my" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<my:pagetemplate title="Match Administration">
<jsp:attribute name="body">
<form method="post" action="${pageContext.request.contextPath}/match/delete/${match.id}">
<button type="submit" class="btn btn-primary">Delete</button>
</form>
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Tournament</th>
<th>Teams</th>
<th>Players</th>
</tr>
</thead>
<tbody>
<tr>
<td><c:out value="${match.id}"/></td>
<td><c:out value="${match.tournament}"/></td>
<td><c:out value="${match.teams}"/></td>
<td><c:out value="${match.players}"/></td>
</tr>
</tbody>
</table>
</jsp:attribute>
</my:pagetemplate>
\ No newline at end of file
......@@ -48,7 +48,7 @@
</ul>
</li>
<li><my:a href="/tournament/list"><f:message key="navigation.tournaments"/></my:a></li>
<li><my:a href="/tournament/list"><f:message key="navigation.matches"/></my:a></li>
<li><my:a href="/match/list"><f:message key="navigation.matches"/></my:a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment