Commit 296253f4 authored by Adrián Piaček's avatar Adrián Piaček
Browse files

Merge branch 'frontend_match' into 'main'

Frontend match

See merge request !26
parents d02655d3 5a9a782e
Pipeline #141783 passed with stage
in 1 minute and 1 second
......@@ -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<>();
......@@ -19,4 +23,6 @@ public class MatchDTO extends BaseDTO {
private Set<TeamEntity> teams = new HashSet<>();
private Set<PlayerEntity> players = new HashSet<>();
private String tournamentId;
}
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);
}
......@@ -16,4 +16,6 @@ public interface TeamFacade {
TeamDTO getTeamById(String uuid);
String createTeam(TeamDTO TeamDTO);
void delete(String uuid);
}
......@@ -4,6 +4,7 @@ import cz.muni.fi.pa165.entity.PlayerEntity;
import cz.muni.fi.pa165.entity.TeamEntity;
import cz.muni.fi.pa165.enums.CountryEnum;
import cz.muni.fi.pa165.services.player.PlayerService;
import cz.muni.fi.pa165.services.team.TeamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -24,25 +25,31 @@ public class SampleDataLoadingFacadeImpl implements SampleDataLoadingFacade {
@Autowired
private PlayerService playerService;
@Autowired
private TeamService teamService;
@Override
@SuppressWarnings("unused")
public void loadData() throws IOException {
TeamEntity team = new TeamEntity();
team.setName("Autisti");
TeamEntity g2_esports = team("g2_esports", CountryEnum.GERMANY);
TeamEntity skt_t1 = team("skt_t1", CountryEnum.SOUTH_KOREA);
PlayerEntity crimsix = player("9384b466-badf-40ca-bbeb-4d8f6eb98042", "Ian Porter",
CountryEnum.AFGHANISTAN,5,6,7);
CountryEnum.AFGHANISTAN,g2_esports,5,6,7);
PlayerEntity JJoNak = player("6e374a4e-5213-4581-847c-45f9e73ffe90", "Bang Seong-hyun ",
CountryEnum.ALBANIA,5,6,7);
CountryEnum.ALBANIA,skt_t1,5,6,7);
log.info("Loaded Sample Players.");
}
private PlayerEntity player(String uuid, String name, CountryEnum country, int killStat, int deathStat, int assistStat) {
private PlayerEntity player(String uuid, String name, CountryEnum country, TeamEntity team, int killStat, int deathStat, int assistStat) {
PlayerEntity player = new PlayerEntity();
player.setId(uuid);
player.setName(name);
player.setCountry(country);
player.setTeam(team);
player.setKillStat(killStat);
player.setDeathStat(deathStat);
player.setAssistStat(assistStat);
......@@ -50,4 +57,12 @@ public class SampleDataLoadingFacadeImpl implements SampleDataLoadingFacade {
return player;
}
private TeamEntity team(String name, CountryEnum country) {
TeamEntity team = new TeamEntity();
team.setName(name);
team.setCountry(country);
teamService.create(team);
return team;
}
}
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 cz.muni.fi.pa165.services.team.TeamService;
import cz.muni.fi.pa165.services.tournament.TournamentService;
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;
@Autowired
private TeamService teamService;
@Autowired
private TournamentService tournamentService;
@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(tournamentService.findById(matchDTO.getTournament().getId()));
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.facade;
import cz.muni.fi.pa165.dto.TeamDTO;
import cz.muni.fi.pa165.entity.PlayerEntity;
import cz.muni.fi.pa165.entity.TeamEntity;
import cz.muni.fi.pa165.services.BeanMappingService;
import cz.muni.fi.pa165.services.player.PlayerService;
import cz.muni.fi.pa165.services.team.TeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
......@@ -13,18 +17,37 @@ import java.util.UUID;
@Service
@Transactional
public class TeamFacadeImpl implements TeamFacade {
@Autowired
private TeamService teamService;
@Autowired
private BeanMappingService beanMappingService;
@Override
public List<TeamDTO> findAllTeams() {
return null;
return beanMappingService.mapTo(teamService.findAll(), TeamDTO.class);
}
@Override
public TeamDTO getTeamById(String uuid) {
return null;
TeamEntity team = teamService.findById(uuid);
return (team == null) ? null : beanMappingService.mapTo(team, TeamDTO.class);
}
@Override
public String createTeam(TeamDTO TeamDTO) {
return null;
TeamEntity team = new TeamEntity();
team.setName(TeamDTO.getName());
team.setCountry(TeamDTO.getCountry());
teamService.create(team);
return team.getId();
}
@Override
public void delete(String uuid) {
var team = new TeamEntity();
team.setId(uuid);
teamService.remove(team);
}
}
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> tournaments() {
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("matchCreate", 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 form
if (bindingResult.hasErrors()) {
return "match/new";
}
//create match
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();
}
}
package cz.muni.fi.pa165.mvc.controllers;
import cz.muni.fi.pa165.dto.PlayerDTO;
import cz.muni.fi.pa165.dto.TournamentDTO;
import cz.muni.fi.pa165.dto.TeamDTO;
import cz.muni.fi.pa165.enums.CountryEnum;
import cz.muni.fi.pa165.facade.PlayerFacade;
import cz.muni.fi.pa165.facade.TournamentFacade;
import cz.muni.fi.pa165.facade.TeamFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
......@@ -18,7 +18,6 @@ import org.springframework.web.util.UriComponentsBuilder;
import javax.validation.Valid;
import java.util.List;
import java.util.UUID;
/**
* @author Adrian Piacek
......@@ -29,6 +28,8 @@ public class PlayerController {
@Autowired
private PlayerFacade playerFacade;
@Autowired
private TeamFacade teamFacade;
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String list(Model model) {
......@@ -47,6 +48,11 @@ public class PlayerController {
return CountryEnum.values();
}
@ModelAttribute("teams")
public List<TeamDTO> teams() {
List<TeamDTO> teams = teamFacade.findAllTeams();
return teams;
}
@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
public String view(@PathVariable String id, Model model) {
model.addAttribute("player", playerFacade.getPlayerById(id));
......
package cz.muni.fi.pa165.mvc.controllers;
import cz.muni.fi.pa165.dto.PlayerDTO;
import cz.muni.fi.pa165.dto.TeamDTO;
import cz.muni.fi.pa165.enums.CountryEnum;
import cz.muni.fi.pa165.facade.PlayerFacade;
import cz.muni.fi.pa165.facade.TeamFacade;
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 Matus Valko
*/
@Controller
@RequestMapping("/team")
public class TeamController {
@Autowired
private TeamFacade teamFacade;
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String list(Model model) {
List<TeamDTO> teams = teamFacade.findAllTeams();
model.addAttribute("teams", teams);
return "team/list";
}
@RequestMapping(value = "/new", method = RequestMethod.GET)
public String newTeam(Model model) {
model.addAttribute("teamCreate", new TeamDTO());
return "team/new";
}
@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
public String view(@PathVariable String id, Model model) {
model.addAttribute("team", teamFacade.getTeamById(id));
model.addAttribute("teamPlayers", teamFacade.getTeamById(id).getPlayerList());
return "team/view";
}
@ModelAttribute("countries")
public CountryEnum[] countries() {
return CountryEnum.values();
}
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String create(@Valid @ModelAttribute("playerCreate") TeamDTO formBean, BindingResult bindingResult,
Model model, RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder) {
//in case of validation error forward back to the the form
if (bindingResult.hasErrors()) {
return "team/new";
}
//create product
String id = teamFacade.createTeam(formBean);
//report success
redirectAttributes.addFlashAttribute("alert_success", "Team " + id + " was created");
return "redirect:" + uriBuilder.path("/team/view/{id}").buildAndExpand(id).encode().toUriString();
}
@RequestMapping(value = "/delete/{uuid}", method = RequestMethod.POST)
public String delete(@PathVariable String uuid, UriComponentsBuilder uriBuilder, RedirectAttributes redirectAttributes) {
TeamDTO player = teamFacade.getTeamById(uuid);
try {
teamFacade.delete(uuid);
redirectAttributes.addFlashAttribute("alert_success", "Team \"" + player.getId() + "\" was deleted.");
} catch (Exception ex) {
redirectAttributes.addFlashAttribute("alert_danger", "Team \"" + player.getId() + "\" cannot be deleted.");
}
return "redirect:" + uriBuilder.path("/team/list").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="matchCreate" 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="tournamentId" required="required" cssClass="form-control">
<c:forEach items="${tournaments}" var="t">
<form:option value="${t.id}">${t.id}</form:option>
</c:forEach>
</form:select>
<form:errors path="tournamentId" 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" required="required" cssClass="form-control">
<c:forEach items="${teams}" var="team">
<form:option value="${team}">${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}"/></