Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<List<Post>> getAllPosts() {
return ResponseEntity.ok(postService.findAll());
}

@GetMapping("/posts/{id}")
public ResponseEntity<Post> getPost(@PathVariable Long id) {
Post post = postService.findById(id);
return post != null ? ResponseEntity.ok(post) : ResponseEntity.notFound().build();
}

@PostMapping("/posts")
public ResponseEntity<Post> createPost(@RequestBody Post post) {
Post savedPost = postService.save(post);
return ResponseEntity.ok(savedPost);
}

@PutMapping("/posts/{id}")
public ResponseEntity<Post> 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<Void> deletePost(@PathVariable Long id) {
if (postService.findById(id) == null) {
return ResponseEntity.notFound().build();
}
postService.deleteById(id);
return ResponseEntity.noContent().build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ public Post findById(Long id) {

public void deleteById(Long id) {
Post post = postRepository.findById(id).orElse(null);
postRepository.delete(post);
}
}
16 changes: 9 additions & 7 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion src/main/resources/templates/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<body>
<h1 th:text="${post.title}">제목</h1>
<p th:text="${post.content}">내용</p>
<a href="/static">🔙 목록으로</a>
<a href="/">🔙 목록으로</a>
<a th:href="@{/post/edit/{id}(id=${post.id})}">✏️ 수정</a>
<a th:href="@{/post/delete/{id}(id=${post.id})}">🗑️ 삭제</a>
</body>
Expand Down