diff --git a/build.gradle b/build.gradle index b2fc2de..92d7ad9 100644 --- a/build.gradle +++ b/build.gradle @@ -21,7 +21,8 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-thymeleaf' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-data-jpa' - runtimeOnly 'com.h2database:h2' +// runtimeOnly 'com.h2database:h2' + runtimeOnly 'com.mysql:mysql-connector-j' testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } diff --git a/src/main/java/BE_Basic_Security_Web_Assignment/BE_Basic_Security_Web_Assignment/controller/RestAPIController.java b/src/main/java/BE_Basic_Security_Web_Assignment/BE_Basic_Security_Web_Assignment/controller/RestAPIController.java new file mode 100644 index 0000000..f871517 --- /dev/null +++ b/src/main/java/BE_Basic_Security_Web_Assignment/BE_Basic_Security_Web_Assignment/controller/RestAPIController.java @@ -0,0 +1,56 @@ +package BE_Basic_Security_Web_Assignment.BE_Basic_Security_Web_Assignment.controller; + +import BE_Basic_Security_Web_Assignment.BE_Basic_Security_Web_Assignment.entity.Post; +import BE_Basic_Security_Web_Assignment.BE_Basic_Security_Web_Assignment.service.PostService; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/api") +public class RestAPIController { + private final PostService postService; + + public RestAPIController(PostService postService) { + this.postService = postService; + } + + @GetMapping("/posts") + public ResponseEntity> getAllPosts() { + return ResponseEntity.ok(postService.findAll()); + } + + @GetMapping("/posts/{id}") + public ResponseEntity getPost(@PathVariable Long id) { + Post post = postService.findById(id); + return post != null ? ResponseEntity.ok(post) : ResponseEntity.notFound().build(); + } + + @PostMapping("/posts") + public ResponseEntity createPost(@RequestBody Post post) { + Post savedPost = postService.save(post); + return ResponseEntity.ok(savedPost); + } + + @PutMapping("/posts/{id}") + public ResponseEntity updatePost(@PathVariable Long id, @RequestBody Post updatedPost) { + Post existingPost = postService.findById(id); + if (existingPost == null) { + return ResponseEntity.notFound().build(); + } + updatedPost.setId(id); + Post savedPost = postService.save(updatedPost); + return ResponseEntity.ok(savedPost); + } + + @DeleteMapping("/posts/{id}") + public ResponseEntity deletePost(@PathVariable Long id) { + if (postService.findById(id) == null) { + return ResponseEntity.notFound().build(); + } + postService.deleteById(id); + return ResponseEntity.noContent().build(); + } + +} diff --git a/src/main/java/BE_Basic_Security_Web_Assignment/BE_Basic_Security_Web_Assignment/service/PostService.java b/src/main/java/BE_Basic_Security_Web_Assignment/BE_Basic_Security_Web_Assignment/service/PostService.java index 98d5f33..e0f8d6a 100644 --- a/src/main/java/BE_Basic_Security_Web_Assignment/BE_Basic_Security_Web_Assignment/service/PostService.java +++ b/src/main/java/BE_Basic_Security_Web_Assignment/BE_Basic_Security_Web_Assignment/service/PostService.java @@ -28,5 +28,6 @@ public Post findById(Long id) { public void deleteById(Long id) { Post post = postRepository.findById(id).orElse(null); + postRepository.delete(post); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index b869de0..88d8bef 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,12 +1,14 @@ spring.application.name=BE_Basic_Security_Web_Assignment -spring.datasource.url=jdbc:h2:tcp://localhost/~/test -spring.datasource.driver-class-name=org.h2.Driver -spring.datasource.username=sa -spring.datasource.password= +spring.datasource.url=jdbc:mysql://localhost:3306/testdb?useSSL=false&serverTimezone=Asia/Seoul +spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver +spring.datasource.username=root +spring.datasource.password=mysql:data!235711 spring.jpa.hibernate.ddl-auto=update -spring.jpa.database-platform=org.hibernate.dialect.H2Dialect +spring.jpa.show-sql=true +spring.jpa.properties.hibernate.format_sql=true +spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect -spring.h2.console.enabled=true -spring.h2.console.path=/h2-console +#spring.h2.console.enabled=true +#spring.h2.console.path=/h2-console diff --git a/src/main/resources/templates/detail.html b/src/main/resources/templates/detail.html index e77e4bd..c90689a 100644 --- a/src/main/resources/templates/detail.html +++ b/src/main/resources/templates/detail.html @@ -7,7 +7,7 @@

제λͺ©

λ‚΄μš©

-πŸ”™ λͺ©λ‘μœΌλ‘œ +πŸ”™ λͺ©λ‘μœΌλ‘œ ✏️ μˆ˜μ • πŸ—‘οΈ μ‚­μ œ