From d302b434e000d03dae878c8264adfa3bb352674a Mon Sep 17 00:00:00 2001 From: Joachim Prinzbach Date: Sun, 23 Jun 2024 16:38:51 +0200 Subject: [PATCH] Add database --- .../platformplanedemoapp/DemoController.java | 16 ----- .../platformplanedemoapp/SecurityConfig.java | 2 +- .../todo/ThymeleafTodoController.java | 51 +++++++++++++ .../platformplanedemoapp/todo/Todo.java | 38 +++++++++- .../todo/TodoRestController.java | 7 +- src/main/resources/application.yaml | 10 ++- src/main/resources/schema.sql | 3 +- src/main/resources/templates/navbar.html | 4 +- src/main/resources/templates/todos.html | 72 +++++++++++++++++++ 9 files changed, 171 insertions(+), 32 deletions(-) delete mode 100644 src/main/java/com/baloise/platformplanedemoapp/DemoController.java create mode 100644 src/main/java/com/baloise/platformplanedemoapp/todo/ThymeleafTodoController.java create mode 100644 src/main/resources/templates/todos.html diff --git a/src/main/java/com/baloise/platformplanedemoapp/DemoController.java b/src/main/java/com/baloise/platformplanedemoapp/DemoController.java deleted file mode 100644 index 1b2e5df..0000000 --- a/src/main/java/com/baloise/platformplanedemoapp/DemoController.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baloise.platformplanedemoapp; - -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.ResponseBody; -import org.springframework.web.bind.annotation.RestController; - -@RestController -public class DemoController { - - @GetMapping("demo") - @ResponseBody - public String Admin() { - return "Admin message"; - } - -} diff --git a/src/main/java/com/baloise/platformplanedemoapp/SecurityConfig.java b/src/main/java/com/baloise/platformplanedemoapp/SecurityConfig.java index cc35e7f..fb79127 100644 --- a/src/main/java/com/baloise/platformplanedemoapp/SecurityConfig.java +++ b/src/main/java/com/baloise/platformplanedemoapp/SecurityConfig.java @@ -21,7 +21,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http.apply(AadWebApplicationHttpSecurityConfigurer.aadWebApplication()) .and() .authorizeHttpRequests(auth -> auth - .requestMatchers("/demo").authenticated() + .requestMatchers("/todos").authenticated() .anyRequest().permitAll() ); return http.build(); diff --git a/src/main/java/com/baloise/platformplanedemoapp/todo/ThymeleafTodoController.java b/src/main/java/com/baloise/platformplanedemoapp/todo/ThymeleafTodoController.java new file mode 100644 index 0000000..9e413c5 --- /dev/null +++ b/src/main/java/com/baloise/platformplanedemoapp/todo/ThymeleafTodoController.java @@ -0,0 +1,51 @@ +package com.baloise.platformplanedemoapp.todo; + +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.springframework.security.core.GrantedAuthority; +import org.springframework.security.core.context.SecurityContext; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.validation.BindingResult; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; + +import java.util.Collection; + +@Controller +@RequiredArgsConstructor +public class ThymeleafTodoController { + + private final TodoRepository todoRepository; + + @GetMapping("/todos") + public String todos(final Model model) { + SecurityContext context = SecurityContextHolder.getContext(); + if(context.getAuthentication().getPrincipal() instanceof DefaultOidcUser user) { + String userName = user.getName(); + String mail = user.getPreferredUsername(); + model.addAttribute("currentUser", userName + " (" + mail + ")"); + Collection authorities = user.getAuthorities(); + model.addAttribute("role", authorities.iterator().next()); + model.addAttribute("issuer", "Azure EntraID"); + model.addAttribute("mail", mail); + model.addAttribute("name", userName); + } else { + model.addAttribute("currentUser", "Anonymous"); + } + model.addAttribute("todos", todoRepository.findAll()); + model.addAttribute("todo", new Todo(null, null, false)); + return "todos"; + } + + @PostMapping("/addtodo") + public String addTodo(@Valid Todo todo, BindingResult result, Model model) { + if (result.hasErrors()) { + return "todos"; + } + todoRepository.save(todo); + return "redirect:/todos"; + } +} diff --git a/src/main/java/com/baloise/platformplanedemoapp/todo/Todo.java b/src/main/java/com/baloise/platformplanedemoapp/todo/Todo.java index 3129255..7e73c04 100644 --- a/src/main/java/com/baloise/platformplanedemoapp/todo/Todo.java +++ b/src/main/java/com/baloise/platformplanedemoapp/todo/Todo.java @@ -1,7 +1,11 @@ package com.baloise.platformplanedemoapp.todo; -import org.springframework.data.annotation.Id; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +@Entity public class Todo { public Todo() { @@ -14,6 +18,7 @@ public Todo(String description, String details, boolean done) { } @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String description; @@ -22,4 +27,35 @@ public Todo(String description, String details, boolean done) { private boolean done; + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public String getDetails() { + return details; + } + + public void setDetails(String details) { + this.details = details; + } + + public boolean isDone() { + return done; + } + + public void setDone(boolean done) { + this.done = done; + } } \ No newline at end of file diff --git a/src/main/java/com/baloise/platformplanedemoapp/todo/TodoRestController.java b/src/main/java/com/baloise/platformplanedemoapp/todo/TodoRestController.java index 02406e9..6aae88d 100644 --- a/src/main/java/com/baloise/platformplanedemoapp/todo/TodoRestController.java +++ b/src/main/java/com/baloise/platformplanedemoapp/todo/TodoRestController.java @@ -22,7 +22,7 @@ public Iterable getTodos() { return todoRepository.findAll(); } - @PostMapping("/create") + @GetMapping("/create") public Todo create() { return todoRepository.save(new Todo("New Todo", "Details", false)); } @@ -32,11 +32,6 @@ public Todo create(@RequestBody Todo todo) { return todoRepository.save(todo); } - @PutMapping("/{id}") - public Todo update(@PathVariable Long id, @RequestBody Todo todo) { - return todoRepository.save(todo); - } - @DeleteMapping("/{id}") public void delete(@PathVariable Long id) { todoRepository.deleteById(id); diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 52da748..4e1894e 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -8,13 +8,17 @@ spring: credential: client-id: ${CLIENT_ID} client-secret: ${CLIENT_SECRET} + sql: + init: + mode: always datasource: hikari: connection-timeout: 30000 maximum-pool-size: 5 - url: jdbc:postgresql://bal-control-plane-postgres.postgres.database.azure.com:5432/control-plane-db - username: postgres + url: ${JDBC_URL} + username: ${JDBC_USER} azure: - passwordless-enabled: true + passwordless-enabled: ${PASSWORDLESS_ENAB} + password: ${JDBC_PASSWORD}? proxy: enabled: false \ No newline at end of file diff --git a/src/main/resources/schema.sql b/src/main/resources/schema.sql index 79e8922..a7e0544 100644 --- a/src/main/resources/schema.sql +++ b/src/main/resources/schema.sql @@ -1,2 +1 @@ -DROP TABLE IF EXISTS todo; -CREATE TABLE todo (id SERIAL PRIMARY KEY, description VARCHAR(255), details VARCHAR(4096), done BOOLEAN); \ No newline at end of file +CREATE TABLE IF NOT EXISTS todo (id SERIAL PRIMARY KEY, description VARCHAR(255), details VARCHAR(4096), done BOOLEAN); \ No newline at end of file diff --git a/src/main/resources/templates/navbar.html b/src/main/resources/templates/navbar.html index f9713b3..a43b201 100644 --- a/src/main/resources/templates/navbar.html +++ b/src/main/resources/templates/navbar.html @@ -5,9 +5,7 @@ Control Plane Demo - - SSO Demo - + My Todos diff --git a/src/main/resources/templates/todos.html b/src/main/resources/templates/todos.html new file mode 100644 index 0000000..d386b15 --- /dev/null +++ b/src/main/resources/templates/todos.html @@ -0,0 +1,72 @@ + + +Control Plane Demo + + +
+
+
+
+
+ +
+ No todos yet! + + +
+ + + + Description + + + + + + + + + Details + + + + + + +
+
+ + + +
+ + +
+ Todos + + + + + + + + + + + + + + + +
DescriptionDetailsDone
+
+
+
+
+
+
+
+ +
+ + \ No newline at end of file