diff --git a/WEB-INF/web.xml b/WEB-INF/web.xml new file mode 100644 index 0000000..b734677 --- /dev/null +++ b/WEB-INF/web.xml @@ -0,0 +1,12 @@ + + + BiCO + + index.html + index.htm + index.jsp + default.html + default.htm + default.jsp + + diff --git a/build.gradle b/build.gradle index e3de66d..7f810ae 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ plugins { } group = 'de.jottyfan.bico' -version = '0.0.0' +version = '0.0.1' description = """BibleClassOrganizer""" @@ -41,7 +41,7 @@ war { } dependencies { - implementation 'de.jottyfan:bicolib:1' + implementation 'de.jottyfan:bicolib:2' implementation 'org.springframework.boot:spring-boot-starter-jooq' implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' diff --git a/src/main/java/de/jottyfan/bico/modules/CommonController.java b/src/main/java/de/jottyfan/bico/modules/CommonController.java index 24a2cf5..82b0718 100644 --- a/src/main/java/de/jottyfan/bico/modules/CommonController.java +++ b/src/main/java/de/jottyfan/bico/modules/CommonController.java @@ -1,5 +1,10 @@ package de.jottyfan.bico.modules; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.ui.Model; + +import de.jottyfan.bico.modules.profile.ProfileService; + /** * * @author jotty @@ -7,4 +12,18 @@ package de.jottyfan.bico.modules; */ public abstract class CommonController { + @Autowired + private ProfileService profileService; + + /** + * get the theme for the current session + * + * @return the theme; light or dark at the moment + */ + public Model useThemedModel(Model model) { + // TODO: add profile's user name + String username = "jotty"; + model.addAttribute("theme", profileService.getTheme(username)); + return model; + } } diff --git a/src/main/java/de/jottyfan/bico/modules/index/IndexController.java b/src/main/java/de/jottyfan/bico/modules/index/IndexController.java index 0bc19d6..3742aa6 100644 --- a/src/main/java/de/jottyfan/bico/modules/index/IndexController.java +++ b/src/main/java/de/jottyfan/bico/modules/index/IndexController.java @@ -1,6 +1,7 @@ package de.jottyfan.bico.modules.index; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import de.jottyfan.bico.modules.CommonController; @@ -13,7 +14,8 @@ import de.jottyfan.bico.modules.CommonController; @Controller public class IndexController extends CommonController { @GetMapping("/") - public String getIndex() { + public String getIndex(Model model) { + useThemedModel(model); return "redirect:/sheet"; } } diff --git a/src/main/java/de/jottyfan/bico/modules/profile/ProfileController.java b/src/main/java/de/jottyfan/bico/modules/profile/ProfileController.java new file mode 100644 index 0000000..6c78592 --- /dev/null +++ b/src/main/java/de/jottyfan/bico/modules/profile/ProfileController.java @@ -0,0 +1,35 @@ +package de.jottyfan.bico.modules.profile; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RestController; + +import de.jottyfan.bico.modules.CommonController; + +/** + * + * @author jotty + * + */ +@RestController +public class ProfileController extends CommonController { + + @Autowired + private ProfileService service; + + /** + * update the theme of the current user + * + * @param theme the theme + */ + @PostMapping("/updateTheme/{theme}") + public ResponseEntity updateTheme(@PathVariable String theme) { + // TODO: add profile's user name + String username = "jotty"; + service.updateTheme(username, theme); + return ResponseEntity.ok(null); + } + +} diff --git a/src/main/java/de/jottyfan/bico/modules/profile/ProfileRepository.java b/src/main/java/de/jottyfan/bico/modules/profile/ProfileRepository.java new file mode 100644 index 0000000..9dc2174 --- /dev/null +++ b/src/main/java/de/jottyfan/bico/modules/profile/ProfileRepository.java @@ -0,0 +1,70 @@ +package de.jottyfan.bico.modules.profile; + +import static de.jottyfan.bico.db.Tables.T_PROFILE; + +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.jooq.DSLContext; +import org.jooq.InsertOnDuplicateSetMoreStep; +import org.jooq.SelectConditionStep; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Repository; + +import de.jottyfan.bico.db.tables.records.TProfileRecord; +import de.jottyfan.bico.modules.profile.model.ProfileBean; + +/** + * + * @author jotty + * + */ +@Repository +public class ProfileRepository { + private static final Logger LOGGER = LogManager.getLogger(ProfileRepository.class); + + @Autowired + private DSLContext jooq; + + /** + * get the profile of the user + * + * @param username the name of the user + * @return the profile or null if not found + */ + public ProfileBean getProfile(String username) { + SelectConditionStep sql = jooq + // @formatter:off + .selectFrom(T_PROFILE) + .where(T_PROFILE.USERNAME.eq(username)); + // @formatter:on + LOGGER.trace(sql.toString()); + TProfileRecord r = sql.fetchOne(); + if (r != null) { + return ProfileBean.of(username).withTheme(r.getTheme()); + } else { + return null; + } + } + + /** + * upsert theme for username + * + * @param username the name of the user + * @param theme the selected theme; may be light or dark + * @return number of affected database rows; should be 1 + */ + public Integer upsertTheme(String username, String theme) { + InsertOnDuplicateSetMoreStep sql = jooq + // @formatter:off + .insertInto(T_PROFILE, + T_PROFILE.USERNAME, + T_PROFILE.THEME) + .values(username, theme) + .onConflict(T_PROFILE.USERNAME) + .doUpdate() + .setAllToExcluded(); + // @formatter:on + LOGGER.trace(sql.toString()); + return sql.execute(); + } +} diff --git a/src/main/java/de/jottyfan/bico/modules/profile/ProfileService.java b/src/main/java/de/jottyfan/bico/modules/profile/ProfileService.java new file mode 100644 index 0000000..62df40e --- /dev/null +++ b/src/main/java/de/jottyfan/bico/modules/profile/ProfileService.java @@ -0,0 +1,40 @@ +package de.jottyfan.bico.modules.profile; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import de.jottyfan.bico.modules.profile.model.ProfileBean; + +/** + * + * @author jotty + * + */ +@Service +public class ProfileService { + + @Autowired + private ProfileRepository repository; + + /** + * get the theme of the user + * + * @param username the name of the user + * @return the theme of the user + */ + public String getTheme(String username) { + ProfileBean bean = repository.getProfile(username); + return bean == null ? "light" : bean.getTheme(); + } + + /** + * update the theme of the user + * + * @param username the name of the user + * @param theme the theme; may be light or dark + */ + public void updateTheme(String username, String theme) { + repository.upsertTheme(username, theme); + } + +} diff --git a/src/main/java/de/jottyfan/bico/modules/profile/model/ProfileBean.java b/src/main/java/de/jottyfan/bico/modules/profile/model/ProfileBean.java new file mode 100644 index 0000000..25ac902 --- /dev/null +++ b/src/main/java/de/jottyfan/bico/modules/profile/model/ProfileBean.java @@ -0,0 +1,58 @@ +package de.jottyfan.bico.modules.profile.model; + +import java.io.Serializable; + +/** + * + * @author jotty + * + */ +public class ProfileBean implements Serializable { + private static final long serialVersionUID = 1L; + + private String username; + private String theme; + + public static final ProfileBean of(String username) { + ProfileBean bean = new ProfileBean(); + bean.setUsername(username); + return bean; + } + + public static final ProfileBean of(String username, String theme) { + return ProfileBean.of(username).withTheme(theme); + } + + public ProfileBean withTheme(String theme) { + this.theme = theme; + return this; + } + + /** + * @return the username + */ + public String getUsername() { + return username; + } + + /** + * @param username the username to set + */ + public void setUsername(String username) { + this.username = username; + } + + /** + * @return the theme + */ + public String getTheme() { + return theme; + } + + /** + * @param theme the theme to set + */ + public void setTheme(String theme) { + this.theme = theme; + } +} diff --git a/src/main/java/de/jottyfan/bico/modules/sheet/SheetController.java b/src/main/java/de/jottyfan/bico/modules/sheet/SheetController.java index c8fb955..d05a6cf 100644 --- a/src/main/java/de/jottyfan/bico/modules/sheet/SheetController.java +++ b/src/main/java/de/jottyfan/bico/modules/sheet/SheetController.java @@ -20,7 +20,7 @@ public class SheetController extends CommonController { @GetMapping("/sheet") public String getSheet(Model model) { - model.addAttribute("list", service.getList()); + useThemedModel(model).addAttribute("list", service.getList()); return "/sheet"; } } diff --git a/src/main/resources/static/js/stylehelp.js b/src/main/resources/static/js/stylehelp.js index 751465c..0002208 100644 --- a/src/main/resources/static/js/stylehelp.js +++ b/src/main/resources/static/js/stylehelp.js @@ -1,5 +1,21 @@ toggleDarkMode = function() { var oldValue = $("html").attr("data-bs-theme"); var newValue = oldValue == "dark" ? "light" : "dark"; + var updateUrl = /*[[@{/updateTheme}]]*/ 'updateTheme'; + updateUrl = updateUrl + "/" + newValue; $("html").attr("data-bs-theme", newValue); + $.ajax({ + url: updateUrl, + type: "POST", + contentType: "application/json", + dataType: 'json' + }); } + +/** + * only because th:data-bs-theme="${theme}" does not work + */ +$(document).ready(function(){ + var theme = /*[[${theme}]]*/ 'dark'; + $("html").attr("data-bs-theme", theme); +}); diff --git a/src/main/resources/templates/sheet.html b/src/main/resources/templates/sheet.html index 1c55eb0..d7cdbc9 100644 --- a/src/main/resources/templates/sheet.html +++ b/src/main/resources/templates/sheet.html @@ -1,9 +1,5 @@ - -Camp Organizer 2 - -
diff --git a/src/main/resources/templates/template.html b/src/main/resources/templates/template.html index 13d98d7..1cff495 100644 --- a/src/main/resources/templates/template.html +++ b/src/main/resources/templates/template.html @@ -1,7 +1,7 @@ - + -Camp Organizer 2 +Bible Class Organizer