Skip to content

Commit

Permalink
Add database
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimprinzbach committed Jun 23, 2024
1 parent c07170d commit d302b43
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 32 deletions.
16 changes: 0 additions & 16 deletions src/main/java/com/baloise/platformplanedemoapp/DemoController.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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<? extends GrantedAuthority> 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";
}
}
38 changes: 37 additions & 1 deletion src/main/java/com/baloise/platformplanedemoapp/todo/Todo.java
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -14,6 +18,7 @@ public Todo(String description, String details, boolean done) {
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String description;
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public Iterable<Todo> getTodos() {
return todoRepository.findAll();
}

@PostMapping("/create")
@GetMapping("/create")
public Todo create() {
return todoRepository.save(new Todo("New Todo", "Details", false));
}
Expand All @@ -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);
Expand Down
10 changes: 7 additions & 3 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions src/main/resources/schema.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
DROP TABLE IF EXISTS todo;
CREATE TABLE todo (id SERIAL PRIMARY KEY, description VARCHAR(255), details VARCHAR(4096), done BOOLEAN);
CREATE TABLE IF NOT EXISTS todo (id SERIAL PRIMARY KEY, description VARCHAR(255), details VARCHAR(4096), done BOOLEAN);
4 changes: 1 addition & 3 deletions src/main/resources/templates/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
<bal-navbar-brand href="/">Control Plane Demo</bal-navbar-brand>
<bal-navbar-menu>
<bal-navbar-menu-start>
<bal-button th:href="@{/demo}">
SSO Demo
</bal-button>
<bal-button th:href="@{/todos}">My Todos</bal-button>
</bal-navbar-menu-start>
<bal-navbar-menu-end>
<bal-button-group>
Expand Down
72 changes: 72 additions & 0 deletions src/main/resources/templates/todos.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="de-CH">
<head th:replace="~{header :: jscssinclude}"><title>Control Plane Demo</title></head>
<body>
<bal-app class="has-sticky-footer">
<header>
<div th:insert="~{navbar :: navbar}"></div>
</header>
<main>
<div class="container is-compact">
<bal-content>
<div th:switch="${todos}">
<bal-heading th:case="null">No todos yet!</bal-heading>
<bal-card style="margin-top: 40px;">
<bal-card-content>
<form id="todoForm" action="#" th:action="@{/addtodo}" th:object="${todo}" method="post">
<bal-form-grid>
<bal-form-col size="half">
<bal-field required="true">
<bal-field-label required="true">Description</bal-field-label>
<bal-field-control>
<bal-input th:field="*{description}" id="description" name="description" placeholder="Enter description" required="true"></bal-input>
</bal-field-control>
</bal-field>
</bal-form-col>

<bal-form-col size="half">
<bal-field required="true">
<bal-field-label required="true">Details</bal-field-label>
<bal-field-control>
<bal-input th:field="*{details}" id="details" name="details" placeholder="Enter details"></bal-input>
</bal-field-control>
</bal-field>
</bal-form-col>
</bal-form-grid>
</form>
</bal-card-content>
<bal-card-actions position="right">
<button class="button is-primary" form="todoForm" type="submit">Add todo</button>
</bal-card-actions>
</bal-card>
<bal-card style="margin-top: 40px;">
<bal-card-content>
<div th:case="*">
<bal-heading level="h2">Todos</bal-heading>
<table class="table w-full is-striped is-hoverable p-none">
<thead>
<tr>
<th>Description</th>
<th>Details</th>
<th>Done</th>
</tr>
</thead>
<tbody>
<tr th:each="todo : ${todos}">
<td th:text="${todo.description}"></td>
<td th:text="${todo.details}"></td>
<td th:text="${todo.done}"></td>
</tr>
</tbody>
</table>
</div>
</bal-card-content>
</bal-card>
</div>
</bal-content>
</div>
</main>
<bal-footer hide-links hide-language-selection/>
</bal-app>
</body>
</html>

0 comments on commit d302b43

Please sign in to comment.