From 502e616c44362bb3edbe6390a7be4284cd2757ce Mon Sep 17 00:00:00 2001 From: Jottyfan Date: Sun, 19 Mar 2023 23:36:41 +0100 Subject: [PATCH] not yet finished manipulations of camp registrations --- .../module/admin/AdminController.java | 61 +++++ .../module/admin/AdminRepository.java | 88 ++++++- .../module/admin/AdminService.java | 60 ++++- .../camporganizer/module/admin/CampBean.java | 234 ++++++++++++++++++ .../module/admin/ProfileBean.java | 71 ++++++ src/main/resources/templates/admin/camp.html | 41 +++ .../resources/templates/admin/camp_edit.html | 122 +++++++++ src/main/resources/templates/template.html | 1 + 8 files changed, 673 insertions(+), 5 deletions(-) create mode 100644 src/main/java/de/jottyfan/camporganizer/module/admin/CampBean.java create mode 100644 src/main/java/de/jottyfan/camporganizer/module/admin/ProfileBean.java create mode 100644 src/main/resources/templates/admin/camp.html create mode 100644 src/main/resources/templates/admin/camp_edit.html 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 f3f7f8d..56e2362 100644 --- a/src/main/java/de/jottyfan/camporganizer/module/admin/AdminController.java +++ b/src/main/java/de/jottyfan/camporganizer/module/admin/AdminController.java @@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.servlet.mvc.support.RedirectAttributes; import de.jottyfan.camporganizer.module.camplist.CommonController; import de.jottyfan.camporganizer.module.mail.MailBean; @@ -143,4 +144,64 @@ public class AdminController extends CommonController { service.deleteLocation(id); return "redirect:/admin/location"; } + + @GetMapping("/admin/camp") + public String getCamplist(Model model, HttpServletRequest request) { + super.setupSession(model, request); + model.addAttribute("camps", service.getAllCamps()); + model.addAttribute("locations", service.getLocations()); + return "/admin/camp"; + } + + @GetMapping("/admin/camp/add") + public String prepareAddCamp(Model model, HttpServletRequest request) { + super.setupSession(model, request); + model.addAttribute("bean", new CampBean()); + model.addAttribute("documents", service.getCampDocuments()); + model.addAttribute("locations", service.getLocations()); + model.addAttribute("profiles", service.getProfiles()); + return "/admin/camp_edit"; + } + + @GetMapping("/admin/camp/edit/{id}") + public String prepareEditCamp(@PathVariable Integer id, Model model, HttpServletRequest request) { + super.setupSession(model, request); + model.addAttribute("bean", service.getCamp(id)); + model.addAttribute("documents", service.getCampDocuments()); + model.addAttribute("locations", service.getLocations()); + model.addAttribute("profiles", service.getProfiles()); + String error = (String) request.getAttribute("error"); + if (error != null) { + model.addAttribute("error", error); + } + return "/admin/camp_edit"; + } + + @PostMapping("/admin/camp/update") + public String updateDocument(@Valid @ModelAttribute("bean") CampBean bean, + final BindingResult bindingResult, Model model, HttpServletRequest request, RedirectAttributes redirect) { + super.setupSession(model, request); + if (bindingResult.hasErrors()) { + for (ObjectError error : bindingResult.getAllErrors()) { + LOGGER.error("error {}: {}", error.getCode(), error.getDefaultMessage()); + } + model.addAttribute("documents", service.getCampDocuments()); + model.addAttribute("locations", service.getLocations()); + model.addAttribute("profiles", service.getProfiles()); + return "/admin/camp_edit"; + } + String error = service.upsertCamp(bean); + redirect.addAttribute("error", error); + Integer pk = bean.getPk(); + String errorDest = pk == null ? "redirect:/admin/camp/add" : "redirect:/admin/camp/edit/" + bean.getPk(); + return error != null ? errorDest : "redirect:/admin/camp"; + } + + @GetMapping("/admin/camp/delete/{id}") + public String deleteCamp(@PathVariable Integer id, Model model, HttpServletRequest request, RedirectAttributes redirect) { + super.setupSession(model, request); + String error = service.deleteCamp(id); + redirect.addAttribute("error", error); + return error != null ? "redirect:/admin/camp/edit/" + id : "redirect:/admin/camp"; + } } 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 1190af1..1c81160 100644 --- a/src/main/java/de/jottyfan/camporganizer/module/admin/AdminRepository.java +++ b/src/main/java/de/jottyfan/camporganizer/module/admin/AdminRepository.java @@ -4,6 +4,8 @@ import static de.jottyfan.camporganizer.db.jooq.Tables.T_CAMP; import static de.jottyfan.camporganizer.db.jooq.Tables.T_DOCUMENT; import static de.jottyfan.camporganizer.db.jooq.Tables.T_DOCUMENTROLE; import static de.jottyfan.camporganizer.db.jooq.Tables.T_LOCATION; +import static de.jottyfan.camporganizer.db.jooq.Tables.T_PERSON; +import static de.jottyfan.camporganizer.db.jooq.Tables.T_PROFILE; import java.util.ArrayList; import java.util.Arrays; @@ -42,6 +44,8 @@ import de.jottyfan.camporganizer.db.jooq.tables.records.TCampRecord; import de.jottyfan.camporganizer.db.jooq.tables.records.TDocumentRecord; import de.jottyfan.camporganizer.db.jooq.tables.records.TDocumentroleRecord; import de.jottyfan.camporganizer.db.jooq.tables.records.TLocationRecord; +import de.jottyfan.camporganizer.db.jooq.tables.records.TPersonRecord; +import de.jottyfan.camporganizer.db.jooq.tables.records.TProfileRecord; import de.jottyfan.camporganizer.module.camplist.LambdaResultWrapper; /** @@ -81,7 +85,7 @@ public class AdminRepository { // @formatter:on LOGGER.debug(sql.toString()); Record5 r = sql.fetchOne(); - if (r != null ) { + if (r != null) { DocumentBean bean = new DocumentBean(); bean.setPk(r.get(T_DOCUMENT.PK)); bean.setName(r.get(T_DOCUMENT.NAME)); @@ -234,8 +238,7 @@ public class AdminRepository { /** * delete entry from t_document where pk = ? * - * @param pk - * to be used as reference + * @param pk to be used as reference * @return number of affected database lines * @throws DataAccessException */ @@ -353,4 +356,83 @@ public class AdminRepository { }); return lrw.getCounter(); } + + /** + * get all camps from the database + * + * @return a list of camps; an empty list at least + */ + public List getAllCamps() { + SelectWhereStep sql = jooq.selectFrom(T_CAMP); + LOGGER.debug(sql.toString()); + List list = new ArrayList<>(); + for (TCampRecord r : sql.fetch()) { + list.add(CampBean.of(r)); + } + return list; + } + + /** + * get the camp of id + * + * @param id the ID of the camp + * @return the camp or null + */ + public CampBean getCamp(Integer id) { + SelectConditionStep sql = jooq.selectFrom(T_CAMP).where(T_CAMP.PK.eq(id)); + LOGGER.debug(sql.toString()); + return CampBean.of(sql.fetchOne()); + } + + /** + * delete the camp and all of its dependencies + * + * @param id the ID of the camp + * @return error message + */ + public String deleteCamp(Integer id) { + LambdaResultWrapper lrw = new LambdaResultWrapper(); + jooq.transaction(t -> { + SelectConditionStep sql1 = DSL.using(t).selectFrom(T_PERSON).where(T_PERSON.FK_CAMP.eq(id)); + LOGGER.debug(sql1.toString()); + Integer registrations = sql1.fetch().size(); + if (registrations < 1) { + DeleteConditionStep sql2 = DSL.using(t).deleteFrom(T_CAMP).where(T_CAMP.PK.eq(id)); + LOGGER.debug(sql2.toString()); + sql2.execute(); + } else { + lrw.putString("error", String.format("Es gibt bereits %d Anmeldungen. Die Freizeit kann daher nicht gelöscht werden.", registrations)); + } + }); + return lrw.getString("error"); + } + + /** + * upsert the camp + * + * @param bean the bean + */ + public String upsertCamp(@Valid CampBean bean) { + if (bean.getPk() == null) { + // TODO: insert + } else { + // TODO: update + } + return "not yet implemented"; + } + + /** + * get all profiles from the db + * + * @return the profiles + */ + public List getProfiles() { + SelectWhereStep sql = jooq.selectFrom(T_PROFILE); + LOGGER.debug(sql.toString()); + List list = new ArrayList<>(); + for (TProfileRecord r : sql.fetch()) { + list.add(ProfileBean.of(r)); + } + return list; + } } 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 ffa39a9..d7b30d1 100644 --- a/src/main/java/de/jottyfan/camporganizer/module/admin/AdminService.java +++ b/src/main/java/de/jottyfan/camporganizer/module/admin/AdminService.java @@ -9,7 +9,6 @@ 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; @@ -115,7 +114,7 @@ public class AdminService { */ public void deleteLocation(Integer id) { // TODO: if a location is still in use by a camp, forbid deleting it - adminRepository.deleteLocation(id); + adminRepository.deleteLocation(id); } /** @@ -126,4 +125,61 @@ public class AdminService { public List getLocationDocuments() { return adminRepository.getAllDocumentsWith(T_DOCUMENT.DOCTYPE.eq(EnumDocument.location)); } + + /** + * get all documents that fit to the camp definitions + * + * @return the camp documents + */ + public List getCampDocuments() { + return adminRepository.getAllDocumentsWith(T_DOCUMENT.DOCTYPE.eq(EnumDocument.camp)); + } + + /** + * get all camp beans from the database + * + * @return all camp beans; an empty list at least + */ + public List getAllCamps() { + return adminRepository.getAllCamps(); + } + + /** + * get all profiles from the db + * + * @return the profiles + */ + public List getProfiles() { + return adminRepository.getProfiles(); + } + + /** + * get the camp of id + * + * @param id the ID of the camp + * @return the camp or null + */ + public CampBean getCamp(Integer id) { + return adminRepository.getCamp(id); + } + + /** + * upsert the camp + * + * @param bean the bean + * @return the error message, if any + */ + public String upsertCamp(@Valid CampBean bean) { + return adminRepository.upsertCamp(bean); + } + + /** + * delete the camp and all of its dependencies + * + * @param id the ID of the camp + * @return the error message, if any + */ + public String deleteCamp(Integer id) { + return adminRepository.deleteCamp(id); + } } diff --git a/src/main/java/de/jottyfan/camporganizer/module/admin/CampBean.java b/src/main/java/de/jottyfan/camporganizer/module/admin/CampBean.java new file mode 100644 index 0000000..ee38390 --- /dev/null +++ b/src/main/java/de/jottyfan/camporganizer/module/admin/CampBean.java @@ -0,0 +1,234 @@ +package de.jottyfan.camporganizer.module.admin; + +import java.io.Serializable; +import java.time.LocalDateTime; + +import javax.validation.constraints.NotBlank; +import javax.validation.constraints.NotNull; + +import de.jottyfan.camporganizer.db.jooq.tables.records.TCampRecord; + +/** + * + * @author jotty + * + */ +public class CampBean implements Serializable { + private static final long serialVersionUID = 1L; + + private Integer pk; + @NotBlank + private String name; + @NotNull + private Integer fkDocument; + @NotNull + private Integer fkLocation; + @NotNull + private Integer fkProfile; + private Boolean lockSales; + @NotNull + private Integer maxAge; + @NotNull + private Integer minAge; + @NotNull + private LocalDateTime arrive; + @NotNull + private LocalDateTime depart; + private String countries; + @NotNull + private String price; + + /** + * generate a camp bean out of r + * + * @param r the record + * @return the camp bean + */ + public static final CampBean of(TCampRecord r) { + if (r == null) { + return null; + } + CampBean bean = new CampBean(); + bean.setArrive(r.getArrive()); + bean.setCountries(r.getCountries()); + bean.setDepart(r.getDepart()); + bean.setFkDocument(r.getFkDocument()); + bean.setFkLocation(r.getFkLocation()); + bean.setFkProfile(r.getFkProfile()); + bean.setLockSales(r.getLockSales()); + bean.setMaxAge(r.getMaxAge()); + bean.setMinAge(r.getMinAge()); + bean.setName(r.getName()); + bean.setPk(r.getPk()); + bean.setPrice(r.getPrice()); + return bean; + } + + /** + * @return the pk + */ + public Integer getPk() { + return pk; + } + + /** + * @param pk the pk to set + */ + public void setPk(Integer pk) { + this.pk = pk; + } + + /** + * @return the name + */ + public String getName() { + return name; + } + + /** + * @param name the name to set + */ + public void setName(String name) { + this.name = name; + } + + /** + * @return the fkDocument + */ + public Integer getFkDocument() { + return fkDocument; + } + + /** + * @param fkDocument the fkDocument to set + */ + public void setFkDocument(Integer fkDocument) { + this.fkDocument = fkDocument; + } + + /** + * @return the fkLocation + */ + public Integer getFkLocation() { + return fkLocation; + } + + /** + * @param fkLocation the fkLocation to set + */ + public void setFkLocation(Integer fkLocation) { + this.fkLocation = fkLocation; + } + + /** + * @return the fkProfile + */ + public Integer getFkProfile() { + return fkProfile; + } + + /** + * @param fkProfile the fkProfile to set + */ + public void setFkProfile(Integer fkProfile) { + this.fkProfile = fkProfile; + } + + /** + * @return the lockSales + */ + public Boolean getLockSales() { + return lockSales; + } + + /** + * @param lockSales the lockSales to set + */ + public void setLockSales(Boolean lockSales) { + this.lockSales = lockSales; + } + + /** + * @return the maxAge + */ + public Integer getMaxAge() { + return maxAge; + } + + /** + * @param maxAge the maxAge to set + */ + public void setMaxAge(Integer maxAge) { + this.maxAge = maxAge; + } + + /** + * @return the minAge + */ + public Integer getMinAge() { + return minAge; + } + + /** + * @param minAge the minAge to set + */ + public void setMinAge(Integer minAge) { + this.minAge = minAge; + } + + /** + * @return the arrive + */ + public LocalDateTime getArrive() { + return arrive; + } + + /** + * @param arrive the arrive to set + */ + public void setArrive(LocalDateTime arrive) { + this.arrive = arrive; + } + + /** + * @return the depart + */ + public LocalDateTime getDepart() { + return depart; + } + + /** + * @param depart the depart to set + */ + public void setDepart(LocalDateTime depart) { + this.depart = depart; + } + + /** + * @return the countries + */ + public String getCountries() { + return countries; + } + + /** + * @param countries the countries to set + */ + public void setCountries(String countries) { + this.countries = countries; + } + + /** + * @return the price + */ + public String getPrice() { + return price; + } + + /** + * @param price the price to set + */ + public void setPrice(String price) { + this.price = price; + } +} diff --git a/src/main/java/de/jottyfan/camporganizer/module/admin/ProfileBean.java b/src/main/java/de/jottyfan/camporganizer/module/admin/ProfileBean.java new file mode 100644 index 0000000..e068abf --- /dev/null +++ b/src/main/java/de/jottyfan/camporganizer/module/admin/ProfileBean.java @@ -0,0 +1,71 @@ +package de.jottyfan.camporganizer.module.admin; + +import java.io.Serializable; + +import de.jottyfan.camporganizer.db.jooq.tables.records.TProfileRecord; + +/** + * + * @author jotty + * + */ +public class ProfileBean implements Serializable { + private static final long serialVersionUID = 1L; + + private Integer pk; + private String forename; + private String surname; + + public static final ProfileBean of(TProfileRecord r) { + if (r == null) { + return null; + } + ProfileBean bean = new ProfileBean(); + bean.setPk(r.getPk()); + bean.setForename(r.getForename()); + bean.setSurname(r.getSurname()); + return bean; + } + + /** + * @return the pk + */ + public Integer getPk() { + return pk; + } + + /** + * @param pk the pk to set + */ + public void setPk(Integer pk) { + this.pk = pk; + } + + /** + * @return the forename + */ + public String getForename() { + return forename; + } + + /** + * @param forename the forename to set + */ + public void setForename(String forename) { + this.forename = forename; + } + + /** + * @return the surname + */ + public String getSurname() { + return surname; + } + + /** + * @param surname the surname to set + */ + public void setSurname(String surname) { + this.surname = surname; + } +} diff --git a/src/main/resources/templates/admin/camp.html b/src/main/resources/templates/admin/camp.html new file mode 100644 index 0000000..a577f3a --- /dev/null +++ b/src/main/resources/templates/admin/camp.html @@ -0,0 +1,41 @@ + + + + +
+ + + + + + + + + + + + + + + + + + + + + + +
NameOrtZeitraumBestätigung
+ +  - 
neue Freizeit anlegen
+ +
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/admin/camp_edit.html b/src/main/resources/templates/admin/camp_edit.html new file mode 100644 index 0000000..7d89a76 --- /dev/null +++ b/src/main/resources/templates/admin/camp_edit.html @@ -0,0 +1,122 @@ + + + + +
+
+
+
+ +
+
+
+
+
+
+ +
+ [[${error}]]
+
+
+
+ +
+ [[${error}]]
+ +
+
+
+ +
+ [[${error}]]
+
+ +
+ [[${error}]]
+
+
+
+ +
+ [[${error}]]
+
+ +
+ [[${error}]]
+
+
+
+ +
+ [[${error}]]
+
+
+
+ +
+ [[${error}]]
+
+
+
+ +
+ [[${error}]]
+ +
+
+
+ +
+ [[${error}]]
+ +
+
+ +
+
+
+ + Abbrechen + +
+
+
+
+
+
+
+ + \ No newline at end of file diff --git a/src/main/resources/templates/template.html b/src/main/resources/templates/template.html index dfc7fff..439b591 100644 --- a/src/main/resources/templates/template.html +++ b/src/main/resources/templates/template.html @@ -123,6 +123,7 @@
  • Testmail
  • Dokumente
  • Freizeitheime
  • +
  • Freizeiten