From 45e673df2b62518772dfca119428aa657f7914b7 Mon Sep 17 00:00:00 2001 From: Jyustin Date: Tue, 30 Jan 2024 17:36:12 -0800 Subject: [PATCH 1/2] added read to backend --- .../controllers/readcontroller.java | 41 +++++++++++++++---- src/main/resources/templates/layouts/nav.html | 2 +- src/main/resources/templates/person/read.html | 2 +- src/main/resources/templates/reading.html | 8 +--- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/nighthawk/spring_portfolio/controllers/readcontroller.java b/src/main/java/com/nighthawk/spring_portfolio/controllers/readcontroller.java index 9c93334..34ec32f 100644 --- a/src/main/java/com/nighthawk/spring_portfolio/controllers/readcontroller.java +++ b/src/main/java/com/nighthawk/spring_portfolio/controllers/readcontroller.java @@ -7,21 +7,44 @@ import org.springframework.ui.Model; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import com.nighthawk.spring_portfolio.mvc.person.Person; +import com.nighthawk.spring_portfolio.mvc.person.PersonDetailsService; +import com.nighthawk.spring_portfolio.mvc.person.PersonJpaRepository; @Controller // HTTP requests are handled as a controller, using the @Controller annotation public class readcontroller { + @Autowired + private PersonJpaRepository repository; + // @GetMapping handles GET request for /greet, maps it to greeting() method @GetMapping("/reading") + @PreAuthorize("isAuthenticated()") // @RequestParam handles variables binding to frontend, defaults, etc - public String read(@RequestParam(name="name", required=false, defaultValue="World") String name, Model model) { - - // model attributes are visible to Thymeleaf when HTML is "pre-processed" - model.addAttribute("person", name); - - // load HTML VIEW (greet.html) - return "reading"; - - } + public String person(Model model) { + List persons = repository.findAllByOrderByNameAsc(); + model.addAttribute("persons", persons); + // System.out.println(persons.toString()); for testing purposes + return "reading"; + } + } \ No newline at end of file diff --git a/src/main/resources/templates/layouts/nav.html b/src/main/resources/templates/layouts/nav.html index bfa456c..2015d96 100644 --- a/src/main/resources/templates/layouts/nav.html +++ b/src/main/resources/templates/layouts/nav.html @@ -34,7 +34,7 @@ diff --git a/src/main/resources/templates/person/read.html b/src/main/resources/templates/person/read.html index 455b836..70d0349 100644 --- a/src/main/resources/templates/person/read.html +++ b/src/main/resources/templates/person/read.html @@ -35,7 +35,7 @@

Person Viewer

- + Person ID Birth Date Name diff --git a/src/main/resources/templates/reading.html b/src/main/resources/templates/reading.html index 455b836..fdc4d42 100644 --- a/src/main/resources/templates/reading.html +++ b/src/main/resources/templates/reading.html @@ -30,21 +30,15 @@

Person Viewer

ID User ID Person - Age - Action - + Person ID Birth Date Name - Age - Unknown Age - Update - Delete From 3f90bfc188b14ddd9a07cc038dbf2198c6d1526c Mon Sep 17 00:00:00 2001 From: Jyustin Date: Thu, 1 Feb 2024 09:12:31 -0800 Subject: [PATCH 2/2] delete method for backend --- .../spring_portfolio/SecurityConfig.java | 5 +- .../controllers/readcontroller.java | 18 +++- .../controllers/updatecontroller.java | 51 +++++++++++ src/main/resources/templates/reading.html | 13 ++- src/main/resources/templates/updating.html | 89 +++++++++++++++++++ 5 files changed, 169 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/nighthawk/spring_portfolio/controllers/updatecontroller.java create mode 100644 src/main/resources/templates/updating.html diff --git a/src/main/java/com/nighthawk/spring_portfolio/SecurityConfig.java b/src/main/java/com/nighthawk/spring_portfolio/SecurityConfig.java index 1ad2572..3abe2e7 100644 --- a/src/main/java/com/nighthawk/spring_portfolio/SecurityConfig.java +++ b/src/main/java/com/nighthawk/spring_portfolio/SecurityConfig.java @@ -69,8 +69,9 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { // list the requests/endpoints need to be authenticated .authorizeHttpRequests(auth -> auth .requestMatchers("/authenticate").permitAll() - .requestMatchers("/mvc/person/update/**", "/mvc/person/delete/**").hasAnyAuthority("ROLE_PLAYER") - .requestMatchers("/api/person/delete/**").hasAnyAuthority("ROLE_PLAYER") + .requestMatchers("/reading").hasAnyAuthority("ROLE_ADMIN") + .requestMatchers("/mvc/person/update/**", "/mvc/person/delete/**").hasAnyAuthority("ROLE_ADMIN") + .requestMatchers("/api/person/delete/**").hasAnyAuthority("ROLE_ADMIN") .requestMatchers("/**").permitAll() ) // support cors diff --git a/src/main/java/com/nighthawk/spring_portfolio/controllers/readcontroller.java b/src/main/java/com/nighthawk/spring_portfolio/controllers/readcontroller.java index 34ec32f..373ce9d 100644 --- a/src/main/java/com/nighthawk/spring_portfolio/controllers/readcontroller.java +++ b/src/main/java/com/nighthawk/spring_portfolio/controllers/readcontroller.java @@ -38,6 +38,8 @@ public class readcontroller { // @GetMapping handles GET request for /greet, maps it to greeting() method @GetMapping("/reading") @PreAuthorize("isAuthenticated()") + + //@PreAuthorize("hasRole('ROLE_ADMIN')") // @RequestParam handles variables binding to frontend, defaults, etc public String person(Model model) { List persons = repository.findAllByOrderByNameAsc(); @@ -45,6 +47,20 @@ public String person(Model model) { // System.out.println(persons.toString()); for testing purposes return "reading"; } - + + @GetMapping("/delete/{id}") + public String deletePerson(@PathVariable Long id) { + if(id < 7) + { + System.out.println("can't delete admins!"); + return "redirect:/reading"; + } + else { + repository.deleteById(id); + return "redirect:/reading"; + } + } + + } \ No newline at end of file diff --git a/src/main/java/com/nighthawk/spring_portfolio/controllers/updatecontroller.java b/src/main/java/com/nighthawk/spring_portfolio/controllers/updatecontroller.java new file mode 100644 index 0000000..c1d0360 --- /dev/null +++ b/src/main/java/com/nighthawk/spring_portfolio/controllers/updatecontroller.java @@ -0,0 +1,51 @@ +package com.nighthawk.spring_portfolio.controllers; +/* MVC code that shows defining a simple Model, calling View, and this file serving as Controller + * Web Content with Spring MVCSpring Example: https://spring.io/guides/gs/serving-web-con + */ + +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; +import com.nighthawk.spring_portfolio.mvc.person.Person; +import com.nighthawk.spring_portfolio.mvc.person.PersonDetailsService; +import com.nighthawk.spring_portfolio.mvc.person.PersonJpaRepository; +@Controller // HTTP requests are handled as a controller, using the @Controller annotation +public class updatecontroller { + + @Autowired + private PersonJpaRepository repository; + + // @GetMapping handles GET request for /greet, maps it to greeting() method + @GetMapping("/updating") + @PreAuthorize("isAuthenticated()") + //@PreAuthorize("hasRole('ROLE_ADMIN')") + // @RequestParam handles variables binding to frontend, defaults, etc + public String person(Model model) { + List persons = repository.findAllByOrderByNameAsc(); + model.addAttribute("persons", persons); + // System.out.println(persons.toString()); for testing purposes + return "reading"; + } + + +} \ No newline at end of file diff --git a/src/main/resources/templates/reading.html b/src/main/resources/templates/reading.html index fdc4d42..104be4d 100644 --- a/src/main/resources/templates/reading.html +++ b/src/main/resources/templates/reading.html @@ -21,23 +21,27 @@
-

Person Viewer

- Create Person +

Person Viewer + console (cannot delete admins)

+ Create Person + Update Person + Delete Person
- + - + @@ -51,4 +55,5 @@

Person Viewer

+ diff --git a/src/main/resources/templates/updating.html b/src/main/resources/templates/updating.html new file mode 100644 index 0000000..9ab4083 --- /dev/null +++ b/src/main/resources/templates/updating.html @@ -0,0 +1,89 @@ + + + + + + + Person Update + + + + + +
+ +
+

Welcome, Update The Persons Details

+
+ +
+
+
+ + +

+
IDUser IDEmail Person
Person IDBirth DateBirth Email Name + Delete + updating
+ + + + + + +
No Roles
Name
+ +
+ +
+ + + + +
+ +
+ + + Email Error +
+
+ + + Password Error +
+
+ + + Name Error +
+
+ + + Birth Date Error +
+ +
+
+
+ + + + + + + +