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

#546 - added controller with all CRUD methods

parent 1efd9cb2
No related branches found
No related tags found
No related merge requests found
package de.deadlocker8.budgetmaster.templategroup;
import de.deadlocker8.budgetmaster.controller.BaseController;
import de.deadlocker8.budgetmaster.utils.Mappings;
import de.deadlocker8.budgetmaster.utils.ResourceNotFoundException;
import de.deadlocker8.budgetmaster.utils.WebRequestUtils;
import de.deadlocker8.budgetmaster.utils.notification.Notification;
import de.deadlocker8.budgetmaster.utils.notification.NotificationType;
import de.thecodelabs.utils.util.Localization;
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.*;
import org.springframework.web.context.request.WebRequest;
import java.util.Optional;
@Controller
@RequestMapping(Mappings.TEMPLATE_GROUPS)
public class TemplateGroupController extends BaseController
{
private final TemplateGroupService templateGroupService;
@Autowired
public TemplateGroupController(TemplateGroupService templateGroupService)
{
this.templateGroupService = templateGroupService;
}
@GetMapping
public String showTemplateGroups(Model model)
{
model.addAttribute("templateGroups", templateGroupService.getAllEntitiesAsc());
return "templateGroups/templateGroups";
}
@GetMapping("/{ID}/requestDelete")
public String requestDeleteTemplateGroup(Model model, @PathVariable("ID") Integer ID)
{
final Optional<TemplateGroup> templateGroupOptional = templateGroupService.getRepository().findById(ID);
if(templateGroupOptional.isEmpty())
{
throw new ResourceNotFoundException();
}
model.addAttribute("templateGroups", templateGroupService.getAllEntitiesAsc());
model.addAttribute("templateGroupToDelete", templateGroupOptional.get());
return "templateGroups/deleteTemplateGroupModal";
}
@GetMapping("/{ID}/delete")
public String deleteTemplateGroup(WebRequest request, @PathVariable("ID") Integer ID)
{
final TemplateGroup templateGroupToDelete = templateGroupService.getRepository().getById(ID);
templateGroupService.getRepository().deleteById(ID);
WebRequestUtils.putNotification(request, new Notification(Localization.getString("notification.template.group.delete.success", templateGroupToDelete.getName()), NotificationType.SUCCESS));
return "redirect:/templateGroups";
}
@GetMapping("/newTemplateGroup")
public String newTemplate(Model model)
{
final TemplateGroup emptyTemplateGroup = new TemplateGroup();
model.addAttribute("templateGroup", emptyTemplateGroup);
return "templateGroups/newTemplateGroup";
}
@PostMapping(value = "/newTemplateGroup")
public String post(WebRequest request,
Model model,
@ModelAttribute("NewTemplateGroup") TemplateGroup templateGroup, BindingResult bindingResult)
{
templateGroup.setName(templateGroup.getName().trim());
TemplateGroupValidator validator = new TemplateGroupValidator();
validator.validate(templateGroup, bindingResult);
if(bindingResult.hasErrors())
{
model.addAttribute("error", bindingResult);
model.addAttribute("templateGroup", templateGroup);
return "templateGroups/newTemplateGroup";
}
templateGroupService.getRepository().save(templateGroup);
WebRequestUtils.putNotification(request, new Notification(Localization.getString("notification.template.save.success", templateGroup.getName()), NotificationType.SUCCESS));
return "redirect:/templateGroups";
}
@GetMapping("/{ID}/edit")
public String editTemplateGroup(Model model, @PathVariable("ID") Integer ID)
{
Optional<TemplateGroup> templateGroupOptional = templateGroupService.findById(ID);
if(templateGroupOptional.isEmpty())
{
throw new ResourceNotFoundException();
}
TemplateGroup templateGroup = templateGroupOptional.get();
model.addAttribute("templateGroup", templateGroup);
return "templateGroups/newTemplateGroup";
}
}
\ No newline at end of file
package de.deadlocker8.budgetmaster.templategroup;
import de.deadlocker8.budgetmaster.categories.Category;
import de.deadlocker8.budgetmaster.utils.Strings;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
public class TemplateGroupValidator implements Validator
{
public boolean supports(Class clazz)
{
return Category.class.equals(clazz);
}
public void validate(Object obj, Errors errors)
{
ValidationUtils.rejectIfEmptyOrWhitespace(errors, "name", Strings.WARNING_EMPTY_TRANSACTION_NAME);
}
}
\ No newline at end of file
...@@ -21,6 +21,7 @@ public final class Mappings ...@@ -21,6 +21,7 @@ public final class Mappings
public static final String SETTINGS = "/settings"; public static final String SETTINGS = "/settings";
public static final String TEAPOT = "/418"; public static final String TEAPOT = "/418";
public static final String TEMPLATES = "/templates"; public static final String TEMPLATES = "/templates";
public static final String TEMPLATE_GROUPS = "/templateGroups";
public static final String TRANSACTIONS = "/transactions"; public static final String TRANSACTIONS = "/transactions";
public static final String TAGS = "/tags"; public static final String TAGS = "/tags";
public static final String HINTS = "/hints"; public static final String HINTS = "/hints";
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment