diff --git a/src/main/java/de/jottyfan/camporganizer/module/admin/AdminController.java b/src/main/java/de/jottyfan/camporganizer/module/admin/AdminController.java index ac09a20..ee4af05 100644 --- a/src/main/java/de/jottyfan/camporganizer/module/admin/AdminController.java +++ b/src/main/java/de/jottyfan/camporganizer/module/admin/AdminController.java @@ -116,4 +116,38 @@ public class AdminController extends CommonController { model.addAttribute("locations", service.getLocations()); return "/admin/location"; } + + @GetMapping("/admin/location/add") + public String prepareAddLocation(Model model, HttpServletRequest request) { + super.setupSession(model, request); + model.addAttribute("bean", new LocationBean()); + return "/admin/location_edit"; + } + + @GetMapping("/admin/location/edit/{id}") + public String prepareAddLocation(@PathVariable Integer id, Model model, HttpServletRequest request) { + super.setupSession(model, request); + model.addAttribute("bean", service.getLocation(id)); + return "/admin/location_edit"; + } + + @PostMapping("/admin/location/update") + public String updateDocument(@Valid @ModelAttribute("bean") LocationBean bean, + final BindingResult bindingResult, Model model, HttpServletRequest request) { + super.setupSession(model, request); + if (bindingResult.hasErrors()) { + for (ObjectError error : bindingResult.getAllErrors()) + LOGGER.error("error {}: {}", error.getCode(), error.getDefaultMessage()); + return "/admin/location_edit"; + } + service.updateLocation(bean); + return "redirect:/admin/location"; + } + + @GetMapping("/admin/location/delete/{id}") + public String deleteLocation(@PathVariable Integer id, Model model, HttpServletRequest request) { + super.setupSession(model, request); + service.deleteLocation(id); + return "redirect:/admin/location"; + } } diff --git a/src/main/java/de/jottyfan/camporganizer/module/admin/AdminRepository.java b/src/main/java/de/jottyfan/camporganizer/module/admin/AdminRepository.java index 7b8302d..a88708e 100644 --- a/src/main/java/de/jottyfan/camporganizer/module/admin/AdminRepository.java +++ b/src/main/java/de/jottyfan/camporganizer/module/admin/AdminRepository.java @@ -19,6 +19,7 @@ import org.jooq.Field; import org.jooq.InsertResultStep; import org.jooq.InsertReturningStep; import org.jooq.Record5; +import org.jooq.SelectConditionStep; import org.jooq.SelectSeekStep1; import org.jooq.SelectWhereStep; import org.jooq.UpdateConditionStep; @@ -255,7 +256,19 @@ public class AdminRepository { for (TLocationRecord r : sql.fetch()) { list.add(LocationBean.of(r)); } - return null; + return list; + } + + /** + * get the location from the database + * + * @param id the ID + * @return the location bean or null + */ + public LocationBean getLocation(Integer id) { + SelectConditionStep sql = jooq.selectFrom(T_LOCATION).where(T_LOCATION.PK.eq(id)); + LOGGER.debug(sql.toString()); + return LocationBean.of(sql.fetchOne()); } } diff --git a/src/main/java/de/jottyfan/camporganizer/module/admin/AdminService.java b/src/main/java/de/jottyfan/camporganizer/module/admin/AdminService.java index d947f1c..2dcbadd 100644 --- a/src/main/java/de/jottyfan/camporganizer/module/admin/AdminService.java +++ b/src/main/java/de/jottyfan/camporganizer/module/admin/AdminService.java @@ -7,6 +7,7 @@ import javax.validation.Valid; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.jooq.exception.DataAccessException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -84,4 +85,22 @@ public class AdminService { public List getLocations() { return adminRepository.getLocations(); } + + /** + * get the location of id + * + * @param id the id + * @return the location or null + */ + public LocationBean getLocation(Integer id) { + return adminRepository.getLocation(id); + } + + public void updateLocation(@Valid LocationBean bean) { + throw new DataAccessException("not yet implemented"); + } + + public void deleteLocation(Integer id) { + throw new DataAccessException("not yet implemented"); + } } diff --git a/src/main/java/de/jottyfan/camporganizer/module/admin/LocationBean.java b/src/main/java/de/jottyfan/camporganizer/module/admin/LocationBean.java index 35cf45b..840b8eb 100644 --- a/src/main/java/de/jottyfan/camporganizer/module/admin/LocationBean.java +++ b/src/main/java/de/jottyfan/camporganizer/module/admin/LocationBean.java @@ -18,6 +18,9 @@ public class LocationBean implements Serializable { private String url; public static LocationBean of(TLocationRecord r) { + if (r == null) { + return null; + } LocationBean bean = new LocationBean(); bean.setPk(r.getPk()); bean.setName(r.getName()); diff --git a/src/main/resources/templates/admin/location_edit.html b/src/main/resources/templates/admin/location_edit.html new file mode 100644 index 0000000..3f96516 --- /dev/null +++ b/src/main/resources/templates/admin/location_edit.html @@ -0,0 +1,53 @@ + + + + + + + + + + +
+
+
+
+ +
+ +
+ [[${error}]]
+
+
+ +
+
+ +
+
+
+
+
+
+ + \ No newline at end of file