-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd33360
commit dfc78af
Showing
27 changed files
with
784 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/main/java/com/book/book_aws/controller/HelloController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.book.book_aws.controller; | ||
|
||
|
||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import org.springframework.web.service.annotation.GetExchange; | ||
|
||
@RestController | ||
public class HelloController { | ||
|
||
@GetMapping("/hello") | ||
public String hello(){ | ||
return "hello"; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/main/java/com/book/book_aws/controller/IndexController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.book.book_aws.controller; | ||
|
||
import com.book.book_aws.dto.PostsResponseDto; | ||
import com.book.book_aws.service.PostsService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
|
||
@RequiredArgsConstructor | ||
@Controller | ||
public class IndexController { | ||
|
||
private final PostsService postsService; | ||
|
||
@GetMapping("/") | ||
public String index(Model model) { | ||
model.addAttribute("posts", postsService.findAllDesc()); | ||
return "index"; | ||
} | ||
|
||
@GetMapping("/posts/save") | ||
public String postsSave() { | ||
return "posts-save"; | ||
} | ||
|
||
@GetMapping("/posts/update/{id}") | ||
public String postsUpdate(@PathVariable Long id, Model model) { | ||
PostsResponseDto dto = postsService.findById(id); | ||
model.addAttribute("post", dto); | ||
|
||
return "posts-update"; | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/book/book_aws/controller/PostsApiController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.book.book_aws.controller; | ||
|
||
import com.book.book_aws.dto.PostsResponseDto; | ||
import com.book.book_aws.dto.PostsSaveRequestDto; | ||
import com.book.book_aws.dto.PostsUpdateRequestDto; | ||
import com.book.book_aws.service.PostsService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RequiredArgsConstructor | ||
@RestController | ||
public class PostsApiController { | ||
|
||
private final PostsService postsService; | ||
|
||
@PostMapping("/api/v1/posts") | ||
public Long save(@RequestBody PostsSaveRequestDto requestDto) { | ||
return postsService.save(requestDto); | ||
} | ||
|
||
@PutMapping("/api/v1/posts/{id}") | ||
public Long update(@PathVariable Long id, @RequestBody PostsUpdateRequestDto requestDto) { | ||
return postsService.update(id, requestDto); | ||
} | ||
|
||
@GetMapping("/api/v1/posts/{id}") | ||
public PostsResponseDto findById(@PathVariable Long id) { | ||
return postsService.findById(id); | ||
} | ||
|
||
@DeleteMapping("/api/v1/posts/{id}") | ||
public Long delete(@PathVariable Long id) { | ||
postsService.delete(id); | ||
return id; | ||
} | ||
|
||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/book/book_aws/domain/BaseTimeEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.book.book_aws.domain; | ||
|
||
import jakarta.persistence.EntityListeners; | ||
import jakarta.persistence.MappedSuperclass; | ||
import lombok.Getter; | ||
import org.springframework.data.annotation.CreatedDate; | ||
import org.springframework.data.annotation.LastModifiedDate; | ||
import org.springframework.data.jpa.domain.support.AuditingEntityListener; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
@MappedSuperclass | ||
@EntityListeners(AuditingEntityListener.class) | ||
public abstract class BaseTimeEntity { | ||
|
||
@CreatedDate | ||
private LocalDateTime createdDate; | ||
|
||
@LastModifiedDate | ||
private LocalDateTime modifiedDate; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package com.book.book_aws.domain.posts; | ||
|
||
import com.book.book_aws.domain.BaseTimeEntity; | ||
import jakarta.persistence.*; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
@Entity | ||
public class Posts extends BaseTimeEntity { | ||
|
||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) // 오토 인크리션 | ||
private Long id; | ||
|
||
@Column(length = 500, nullable = false) // 사용하지 않아도 되지만 | ||
private String title; | ||
|
||
@Column(columnDefinition = "TEXT", nullable = false) | ||
private String content; | ||
|
||
private String author; | ||
|
||
@Builder | ||
public Posts(String title, String content, String author) { | ||
this.title = title; | ||
this.content = content; | ||
this.author = author; | ||
} | ||
|
||
public void update(String title, String content) { | ||
this.title = title; | ||
this.content = content; | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
13 changes: 13 additions & 0 deletions
13
src/main/java/com/book/book_aws/domain/posts/PostsRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.book.book_aws.domain.posts; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.jpa.repository.Query; | ||
|
||
import java.util.List; | ||
|
||
public interface PostsRepository extends JpaRepository<Posts, Long> { | ||
|
||
@Query("SELECT p FROM Posts p ORDER BY p.id DESC") | ||
List<Posts> findAllDesc(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.book.book_aws.dto; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
@Getter | ||
@RequiredArgsConstructor | ||
public class HelloResponseDto { | ||
|
||
private final String name; | ||
private final int amount; | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/book/book_aws/dto/PostsListResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.book.book_aws.dto; | ||
|
||
import com.book.book_aws.domain.posts.Posts; | ||
import lombok.Getter; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Getter | ||
public class PostsListResponseDto { | ||
private Long id; | ||
private String title; | ||
private String author; | ||
private LocalDateTime modifiedDate; | ||
|
||
public PostsListResponseDto(Posts entity) { | ||
this.id = entity.getId(); | ||
this.title = entity.getTitle(); | ||
this.author = entity.getAuthor(); | ||
this.modifiedDate = entity.getModifiedDate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.book.book_aws.dto; | ||
|
||
import com.book.book_aws.domain.posts.Posts; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
public class PostsResponseDto { | ||
|
||
private Long id; | ||
private String title; | ||
private String content; | ||
private String author; | ||
|
||
public PostsResponseDto(Posts entity) { | ||
this.id = entity.getId(); | ||
this.title = entity.getTitle(); | ||
this.content = entity.getContent(); | ||
this.author = entity.getAuthor(); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/book/book_aws/dto/PostsSaveRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.book.book_aws.dto; | ||
|
||
import com.book.book_aws.domain.posts.Posts; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class PostsSaveRequestDto { | ||
private String title; | ||
private String content; | ||
private String author; | ||
|
||
@Builder | ||
public PostsSaveRequestDto(String title, String content, String author) { | ||
this.title = title; | ||
this.content = content; | ||
this.author = author; | ||
} | ||
|
||
public Posts toEntity() { | ||
return Posts.builder() | ||
.title(title) | ||
.content(content) | ||
.author(author) | ||
.build(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/book/book_aws/dto/PostsUpdateRequestDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.book.book_aws.dto; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class PostsUpdateRequestDto { | ||
private String title; | ||
private String content; | ||
|
||
|
||
@Builder | ||
public PostsUpdateRequestDto(String title, String content) { | ||
this.title = title; | ||
this.content = content; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.book.book_aws.service; | ||
|
||
import com.book.book_aws.domain.posts.Posts; | ||
import com.book.book_aws.domain.posts.PostsRepository; | ||
import com.book.book_aws.dto.PostsListResponseDto; | ||
import com.book.book_aws.dto.PostsResponseDto; | ||
import com.book.book_aws.dto.PostsSaveRequestDto; | ||
import com.book.book_aws.dto.PostsUpdateRequestDto; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@RequiredArgsConstructor | ||
@Service | ||
public class PostsService { | ||
|
||
private final PostsRepository postsRepository; | ||
|
||
@Transactional | ||
public Long save(PostsSaveRequestDto requestDto) { | ||
return postsRepository.save(requestDto.toEntity()).getId(); | ||
} | ||
|
||
@Transactional | ||
public Long update(Long id, PostsUpdateRequestDto requestDto) { | ||
Posts posts = postsRepository.findById(id) | ||
.orElseThrow(() -> new IllegalArgumentException("해당 사용자가 없습니다. id=" + id)); | ||
|
||
posts.update(requestDto.getTitle(), requestDto.getContent()); | ||
|
||
return id; | ||
} | ||
|
||
|
||
@Transactional(readOnly = true) | ||
public PostsResponseDto findById(Long id) { | ||
Posts entity = postsRepository.findById(id) | ||
.orElseThrow(() -> new IllegalArgumentException("해당 사용자가 없습니다. id=" + id)); | ||
|
||
return new PostsResponseDto(entity); | ||
} | ||
|
||
|
||
@Transactional | ||
public void delete (Long id) { | ||
Posts posts = postsRepository.findById(id) | ||
.orElseThrow(() -> new IllegalArgumentException("해당 게시글이 없습니다. id=" + id)); | ||
|
||
postsRepository.delete(posts); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public List<PostsListResponseDto> findAllDesc() { | ||
return postsRepository.findAllDesc().stream() | ||
.map(PostsListResponseDto::new) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
spring.jpa.show-sql=true | ||
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect | ||
spring.h2.console.enabled=true | ||
spring.h2.console.path=/h2-console | ||
spring.datasource.url=jdbc:h2:mem:testdb | ||
|
||
server.servlet.encoding.force-response=true |
Oops, something went wrong.