Commit 6c403b1b authored by Adrián Piaček's avatar Adrián Piaček
Browse files

Added edit for players

parent 13f8d941
Pipeline #141749 passed with stage
in 1 minute and 14 seconds
......@@ -15,5 +15,7 @@ public interface PlayerFacade {
String createPlayer(PlayerDTO playerDTO);
String editPlayer(PlayerDTO playerDTO);
void delete(String uuid);
}
......@@ -63,6 +63,10 @@ public class BaseDaoImpl<Entity extends BaseEntity> implements BaseDao<Entity> {
@Override
public void remove(Entity entity) {
try {
var attached = findById(entity.getId());
if (attached == null) {
throw new IllegalArgumentException("Cannot delete a record. Given entity does not exist.");
}
em.remove(findById(entity.getId()));
} catch (Exception e) {
throw new DaoDataAccessException(e.getMessage(), e);
......
......@@ -10,7 +10,6 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Optional;
@Service
@Transactional
......@@ -50,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();
......
......@@ -2,6 +2,7 @@ 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.entity.PlayerEntity;
import cz.muni.fi.pa165.enums.CountryEnum;
import cz.muni.fi.pa165.facade.PlayerFacade;
import cz.muni.fi.pa165.facade.TeamFacade;
......@@ -43,6 +44,7 @@ public class PlayerController {
model.addAttribute("playerCreate", new PlayerDTO());
return "player/new";
}
@ModelAttribute("countries")
public CountryEnum[] countries() {
return CountryEnum.values();
......@@ -58,6 +60,22 @@ public class PlayerController {
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) {
......@@ -77,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." + ex.getMessage());
redirectAttributes.addFlashAttribute("alert_danger", "Player \"" + player.getName() + "\" cannot be deleted." + ex.getMessage());
}
return "redirect:" + uriBuilder.path("/player/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="Player Administration">
<jsp:attribute name="body">
<form:form method="post" action="${pageContext.request.contextPath}/player/edit/${player.id}"
modelAttribute="playerEdit" cssClass="form-horizontal">
<div class="form-group ${name_error?'has-error':''}">
<form:label path="name" cssClass="col-sm-2 control-label">Name</form:label>
<div class="col-sm-10">
<form:input path="name" cssClass="form-control"/>
<form:errors path="name" cssClass="help-block"/>
</div>
</div>
<div class="form-group">
<form:label path="country" cssClass="col-sm-2 control-label">Country</form:label>
<div class="col-sm-10">
<form:select path="country" cssClass="form-control">
<c:forEach items="${countries}" var="c">
<form:option value="${c}">${playerEdit.country}</form:option>
</c:forEach>
</form:select>
<form:errors path="country" cssClass="error"/>
</div>
</div>
<div class="form-group">
<form:label path="teamId" cssClass="col-sm-2 control-label">Team</form:label>
<div class="col-sm-10">
<form:select path="teamId" cssClass="form-control">
<c:forEach items="${teams}" var="c">
<form:option value="${c.id}">${c.name}</form:option>
</c:forEach>
</form:select>
<p class="help-block"><form:errors path="teamId" cssClass="error"/></p>
</div>
</div>
<div class="form-group ${killStat_error?'has-error':''}" >
<form:label path="killStat" cssClass="col-sm-2 control-label">Kill stat</form:label>
<div class="col-sm-10">
<form:input placeholder="${playerEdit.killStat}" path="killStat" cssClass="form-control"/>
<form:errors path="killStat" cssClass="help-block"/>
</div>
</div>
<div class="form-group ${deathStat_error?'has-error':''}" >
<form:label path="deathStat" cssClass="col-sm-2 control-label">Death stat</form:label>
<div class="col-sm-10">
<form:input placeholder="${playerEdit.deathStat}" path="deathStat" cssClass="form-control"/>
<form:errors path="deathStat" cssClass="help-block"/>
</div>
</div>
<div class="form-group ${assistStat_error?'has-error':''}" >
<form:label path="assistStat" cssClass="col-sm-2 control-label">Assist stat</form:label>
<div class="col-sm-10">
<form:input placeholder="${playerEdit.assistStat}" path="assistStat" cssClass="form-control"/>
<form:errors path="assistStat" cssClass="help-block"/>
</div>
</div>
<button class="btn btn-primary" type="submit">Update Player</button>
</form:form>
</jsp:attribute>
</my:pagetemplate>
\ No newline at end of file
......@@ -7,11 +7,22 @@
<my:pagetemplate title="Player Administration">
<jsp:attribute name="body">
<form method="post" action="${pageContext.request.contextPath}/player/delete/${player.id}">
<button type="submit" class="btn btn-primary">Delete</button>
</form>
<div class="row">
<div class="col-xs-12 col-sm-6 col-md-2 col-lg-1">
<form method="post" action="${pageContext.request.contextPath}/player/delete/${player.id}">
<button type="submit" class="btn btn-primary ">
<span class="glyphicon glyphicon-minus"></span>
Delete</button>
</form>
</div>
<div class="col-xs-12 col-sm-6 col-md-2 col-lg-1">
<form method="post" action="${pageContext.request.contextPath}/player/edit/${player.id}">
<button type="submit" class="btn btn-primary ">
<span class="glyphicon glyphicon-asterisk"></span>
Edit</button>
</form>
</div>
</div>
<table class="table">
<thead>
......@@ -38,5 +49,10 @@
</tbody>
</table>
<my:a href="/player/list" class="btn btn-primary">
<span class="glyphicon glyphicon-user" aria-hidden="true"></span>
List all players
</my:a>
</jsp:attribute>
</my:pagetemplate>
\ No newline at end of file
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