Skip to content
Snippets Groups Projects
Commit e78e75e0 authored by Robert Goldmann's avatar Robert Goldmann
Browse files

#338 - edit/update page for normal templates

parent 061617ef
Branches
Tags
No related merge requests found
......@@ -155,8 +155,18 @@ public class TemplateController extends BaseController
@RequestParam(value = "isPayment", required = false) boolean isPayment,
@RequestParam(value = "includeAccount", required = false) boolean includeAccount)
{
String previousTemplateName = null;
boolean isEdit = template.getID() != null;
if(isEdit)
{
final Optional<Template> existingTemplateOptional = templateService.getRepository().findById(template.getID());
if(existingTemplateOptional.isPresent())
{
previousTemplateName = existingTemplateOptional.get().getTemplateName();
}
}
TemplateValidator templateValidator = new TemplateValidator(templateService.getExistingTemplateNames());
TemplateValidator templateValidator = new TemplateValidator(previousTemplateName, templateService.getExistingTemplateNames());
templateValidator.validate(template, bindingResult);
transactionService.handleAmount(template, isPayment);
......@@ -177,4 +187,24 @@ public class TemplateController extends BaseController
templateService.getRepository().save(template);
return "redirect:/templates";
}
@GetMapping("/templates/{ID}/edit")
public String editTemplate(Model model, @PathVariable("ID") Integer ID)
{
Optional<Template> templateOptional = templateService.getRepository().findById(ID);
if(!templateOptional.isPresent())
{
throw new ResourceNotFoundException();
}
Template template = templateOptional.get();
templateService.prepareModelNewOrEdit(model, true, template, template.getAmount() <= 0, accountService.getAllAccountsAsc());
if(template.isTransfer())
{
return "templates/newTransferTemplate";
}
return "templates/newTemplate";
}
}
\ No newline at end of file
......@@ -11,10 +11,12 @@ import java.util.List;
public class TemplateValidator implements Validator
{
private String previousTemplateName;
private List<String> existingTemplateNames;
public TemplateValidator(List<String> existingTemplateNames)
public TemplateValidator(String previousTemplateName, List<String> existingTemplateNames)
{
this.previousTemplateName = previousTemplateName;
this.existingTemplateNames = existingTemplateNames;
}
......@@ -27,9 +29,14 @@ public class TemplateValidator implements Validator
{
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "templateName", Strings.WARNING_EMPTY_TRANSACTION_NAME);
Template template = (Template)obj;
boolean isNameAlreadyUsed = this.existingTemplateNames.stream().anyMatch(template.getTemplateName()::equalsIgnoreCase);
final Template template = (Template) obj;
if(previousTemplateName != null && previousTemplateName.equals(template.getTemplateName()))
{
return;
}
final boolean isNameAlreadyUsed = this.existingTemplateNames.contains(template.getTemplateName());
if(isNameAlreadyUsed)
{
errors.rejectValue("templateName", Strings.WARNING_DUPLICATE_TEMPLATE_NAME);
......
......@@ -245,7 +245,6 @@ public class TransactionController extends BaseController
return "transactions/newTransactionNormal";
}
@GetMapping("/transactions/{ID}/highlight")
public String highlight(Model model, @PathVariable("ID") Integer ID)
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment