Commit a9478aa1 authored by Marek Kadlečík's avatar Marek Kadlečík
Browse files

Merge branch 'main' into 'security'

# Conflicts:
#   gaming-sample-data/src/main/java/cz/muni/fi/pa165/sampledata/SampleDataLoadingFacadeImpl.java
#   gaming-spring-mvc/src/main/webapp/WEB-INF/tags/pagetemplate.tag
parents 2575a085 e682f12b
Pipeline #141826 passed with stage
in 1 minute and 6 seconds
package cz.muni.fi.pa165.dto;
public class CreatePlayerDTO extends PlayerDTO {
}
......@@ -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;
}
......@@ -5,6 +5,7 @@ import cz.muni.fi.pa165.entity.TeamEntity;
import lombok.Getter;
import lombok.Setter;
import javax.validation.constraints.NotNull;
import java.util.HashSet;
import java.util.Set;
......@@ -15,8 +16,6 @@ import java.util.Set;
@Setter
public class PlayerDTO extends PersonDTO {
private TeamEntity team;
private Set<MatchEntity> matches = new HashSet<>();
private int killStat;
......@@ -24,4 +23,10 @@ public class PlayerDTO extends PersonDTO {
private int deathStat;
private int assistStat;
private TeamEntity team;
@NotNull
private String teamId;
}
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);
}
......@@ -15,5 +15,7 @@ public interface PlayerFacade {
String createPlayer(PlayerDTO playerDTO);
String editPlayer(PlayerDTO playerDTO);
void delete(String uuid);
}
......@@ -16,4 +16,6 @@ public interface TeamFacade {
TeamDTO getTeamById(String uuid);
String createTeam(TeamDTO TeamDTO);
void delete(String uuid);
}
......@@ -67,7 +67,7 @@ public class BaseDaoImpl<Entity extends BaseEntity> implements BaseDao<Entity> {
if (attached == null) {
throw new IllegalArgumentException("Cannot delete a record. Given entity does not exist.");
}
em.remove(entity);
em.remove(findById(entity.getId()));
} catch (Exception e) {
throw new DaoDataAccessException(e.getMessage(), e);
}
......
......@@ -37,7 +37,7 @@ public class PlayerEntity extends PersonEntity {
public PlayerEntity() {
}
public PlayerEntity(String name){
super(name);
public PlayerEntity(String id){
super(id);
}
}
package cz.muni.fi.pa165.rest.controllers;
import cz.muni.fi.pa165.dto.CreatePlayerDTO;
import cz.muni.fi.pa165.dto.PlayerDTO;
import cz.muni.fi.pa165.entity.PlayerEntity;
import cz.muni.fi.pa165.facade.PlayerFacade;
import cz.muni.fi.pa165.rest.RootUris;
import cz.muni.fi.pa165.rest.exception.ResourceAlreadyExistsException;
......@@ -14,9 +12,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@RestController
@RequestMapping(RootUris.restPlayer)
......
......@@ -5,6 +5,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;
......@@ -28,22 +29,25 @@ public class SampleDataLoadingFacadeImpl implements SampleDataLoadingFacade {
@Autowired
private PlayerService playerService;
@Autowired
private TeamService teamService;
@Override
@SuppressWarnings("unused")
public void loadData() throws IOException {
TeamEntity g2_esports = team("g2_esports", CountryEnum.GERMANY);
TeamEntity skt_t1 = team("skt_t1", CountryEnum.SOUTH_KOREA);
TeamEntity team = new TeamEntity();
team.setName("Autisti");
PlayerEntity crimsix = player("9384b466-badf-40ca-bbeb-4d8f6eb98042", "Ian Porter", "poter",
"heslo", true, CountryEnum.AFGHANISTAN,5,6,7);
"heslo", true, CountryEnum.AFGHANISTAN, g2_esports, 5, 6, 7);
PlayerEntity JJoNak = player("6e374a4e-5213-4581-847c-45f9e73ffe90", "Bang Seong-hyun ", "bang",
"heslo", true, CountryEnum.ALBANIA,15,25,100);
"heslo", true, CountryEnum.ALBANIA, skt_t1, 15, 25, 100);
log.info("Loaded Sample Players.");
}
private PlayerEntity player(String uuid, String name, String username, String password, boolean admin,
CountryEnum country, int killStat, int deathStat, int assistStat) {
CountryEnum country, TeamEntity team, int killStat, int deathStat, int assistStat) {
PlayerEntity player = new PlayerEntity();
player.setId(uuid);
player.setName(name);
......@@ -52,6 +56,7 @@ public class SampleDataLoadingFacadeImpl implements SampleDataLoadingFacade {
player.setPasswordHash(sha256hex);
player.setAdmin(admin);
player.setCountry(country);
player.setTeam(team);
player.setKillStat(killStat);
player.setDeathStat(deathStat);
player.setAssistStat(assistStat);
......@@ -59,4 +64,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);
}
}
......@@ -4,6 +4,7 @@ import cz.muni.fi.pa165.dto.PlayerDTO;
import cz.muni.fi.pa165.entity.PlayerEntity;
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 org.springframework.transaction.annotation.Transactional;
......@@ -17,6 +18,8 @@ public class PlayerFacadeImpl implements PlayerFacade {
@Autowired
private PlayerService playerService;
@Autowired
private TeamService teamService;
@Autowired
private BeanMappingService beanMappingService;
......@@ -38,6 +41,7 @@ public class PlayerFacadeImpl implements PlayerFacade {
player.setName(playerDTO.getName());
player.setCountry(playerDTO.getCountry());
player.setTeam(teamService.findById(playerDTO.getTeamId()));
player.setKillStat(playerDTO.getKillStat());
player.setDeathStat(playerDTO.getDeathStat());
player.setAssistStat(playerDTO.getAssistStat());
......@@ -45,6 +49,23 @@ public class PlayerFacadeImpl implements PlayerFacade {
playerService.create(player);
return player.getId();
}
@Override
public String editPlayer(PlayerDTO playerDTO) {
PlayerEntity player = playerService.findById(playerDTO.getId());
player.setName(playerDTO.getName());
player.setCountry(playerDTO.getCountry());
player.setTeam(teamService.findById(playerDTO.getTeamId()));
player.setKillStat(playerDTO.getKillStat());
player.setDeathStat(playerDTO.getDeathStat());
player.setAssistStat(playerDTO.getAssistStat());
playerService.update(player);
return player.getId();
}
@Override
public void delete(String uuid) {
var player = new PlayerEntity();
......
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.entity.PlayerEntity;
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 +19,6 @@ import org.springframework.web.util.UriComponentsBuilder;
import javax.validation.Valid;
import java.util.List;
import java.util.UUID;
/**
* @author Adrian Piacek
......@@ -29,6 +29,8 @@ public class PlayerController {
@Autowired
private PlayerFacade playerFacade;
@Autowired
private TeamFacade teamFacade;
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String list(Model model) {
......@@ -42,17 +44,38 @@ public class PlayerController {
model.addAttribute("playerCreate", new PlayerDTO());
return "player/new";
}
@ModelAttribute("countries")
public CountryEnum[] countries() {
return CountryEnum.values();
}
@ModelAttribute("teams")
public List<TeamDTO> teams() {
return teamFacade.findAllTeams();
}
@RequestMapping(value = "/view/{id}", method = RequestMethod.GET)
public String view(@PathVariable String id, Model model) {
model.addAttribute("player", playerFacade.getPlayerById(id));
return "player/view";
}
@RequestMapping(value = "/edit/{id}", method = {RequestMethod.POST, RequestMethod.GET})
public String edit(@PathVariable String id, @Valid @ModelAttribute("playerEdit") PlayerDTO formBean,
BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes,
UriComponentsBuilder uriBuilder) {
model.addAttribute("player", playerFacade.getPlayerById(id));
if (bindingResult.hasErrors()) {
return "player/edit";
}
//create product
String playerID = playerFacade.editPlayer(formBean);
//report success
redirectAttributes.addFlashAttribute("alert_success", "Player " + formBean.getName() + " was edited!");
return "redirect:" + uriBuilder.path("/player/view/{id}").buildAndExpand(playerID).encode().toUriString();
}
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String create(@Valid @ModelAttribute("playerCreate") PlayerDTO formBean, BindingResult bindingResult,
Model model, RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder) {
......@@ -61,10 +84,10 @@ public class PlayerController {
return "player/new";
}
//create product
String id = playerFacade.createPlayer(formBean);
String playerID = playerFacade.createPlayer(formBean);
//report success
redirectAttributes.addFlashAttribute("alert_success", "Player " + id + " was created");
return "redirect:" + uriBuilder.path("/player/view/{id}").buildAndExpand(id).encode().toUriString();
redirectAttributes.addFlashAttribute("alert_success", "Player " + formBean.getName() + " was created!");
return "redirect:" + uriBuilder.path("/player/view/{id}").buildAndExpand(playerID).encode().toUriString();
}
@RequestMapping(value = "/delete/{uuid}", method = RequestMethod.POST)
......@@ -72,9 +95,9 @@ public class PlayerController {
PlayerDTO player = playerFacade.getPlayerById(uuid);
try {
playerFacade.delete(uuid);
redirectAttributes.addFlashAttribute("alert_success", "Player \"" + player.getId() + "\" was deleted.");
redirectAttributes.addFlashAttribute("alert_success", "Player \"" + player.getName() + "\" was deleted.");
} catch (Exception ex) {
redirectAttributes.addFlashAttribute("alert_danger", "Player \"" + player.getId() + "\" cannot be deleted.");
redirectAttributes.addFlashAttribute("alert_danger", "Player \"" + player.getName() + "\" cannot be deleted." + ex.getMessage());
}
return "redirect:" + uriBuilder.path("/player/list").toUriString();
}
......
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;