From 38c8ca923629c122dc7ceb29065c4fcec8165fc5 Mon Sep 17 00:00:00 2001 From: nohy6630 Date: Sat, 9 Sep 2023 16:37:43 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20crud=20=EC=B4=88=EC=95=88=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pcs/controller/ArticleController.java | 13 +++--- .../java/com/teamB/pcs/domain/Article.java | 14 ++++++ .../teamB/pcs/dto/request/ArticleRequest.java | 12 +++++ .../pcs/dto/response/ArticleListResponse.java | 25 +++++++++++ .../com/teamB/pcs/service/ArticleService.java | 45 +++++++++++++++++-- src/main/resources/application.properties | 1 - src/main/resources/application.yml | 14 ++++++ 7 files changed, 114 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/teamB/pcs/dto/response/ArticleListResponse.java delete mode 100644 src/main/resources/application.properties create mode 100644 src/main/resources/application.yml diff --git a/src/main/java/com/teamB/pcs/controller/ArticleController.java b/src/main/java/com/teamB/pcs/controller/ArticleController.java index 9e8ab24..2a1962e 100644 --- a/src/main/java/com/teamB/pcs/controller/ArticleController.java +++ b/src/main/java/com/teamB/pcs/controller/ArticleController.java @@ -1,6 +1,7 @@ package com.teamB.pcs.controller; import com.teamB.pcs.dto.request.ArticleRequest; +import com.teamB.pcs.dto.response.ArticleListResponse; import com.teamB.pcs.dto.response.ArticleResponse; import com.teamB.pcs.service.ArticleService; import lombok.RequiredArgsConstructor; @@ -16,13 +17,13 @@ public class ArticleController { private final ArticleService articleService; @GetMapping("") - public List getAll(){ + public ArticleListResponse getAll(){ return articleService.getAll(); } - @PostMapping("/{articleId}") - public void create(@PathVariable Long articleId, @RequestBody ArticleRequest articleRequest){ - return articleService.create(articleId, articleRequest); + @PostMapping("") + public void create(@RequestBody ArticleRequest articleRequest){ + articleService.create(articleRequest); } @GetMapping("/{articleId}") @@ -32,11 +33,11 @@ public ArticleResponse get(@PathVariable Long articleId){ @PutMapping("/{articleId}") public void update(@PathVariable Long articleId, @RequestBody ArticleRequest articleRequest){ - return articleService.update(articleId, articleRequest); + articleService.update(articleId, articleRequest); } @DeleteMapping("/{articleId}") public void delete(@PathVariable Long articleId){ - return articleService.delete(articleId); + articleService.delete(articleId); } } diff --git a/src/main/java/com/teamB/pcs/domain/Article.java b/src/main/java/com/teamB/pcs/domain/Article.java index 005781a..c8e7df8 100644 --- a/src/main/java/com/teamB/pcs/domain/Article.java +++ b/src/main/java/com/teamB/pcs/domain/Article.java @@ -4,6 +4,7 @@ import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; import org.hibernate.annotations.CreationTimestamp; @@ -33,4 +34,17 @@ public class Article { @UpdateTimestamp private LocalDateTime updatedAt; + + @Builder + public Article(String title, String description, String body) {//태그도 추가되게해야됨 + this.title = title; + this.description = description; + this.body = body; + } + + public void update(String title, String description, String body) { + this.title = title; + this.description = description; + this.body = body; + } } diff --git a/src/main/java/com/teamB/pcs/dto/request/ArticleRequest.java b/src/main/java/com/teamB/pcs/dto/request/ArticleRequest.java index 5cc689b..f19b2ce 100644 --- a/src/main/java/com/teamB/pcs/dto/request/ArticleRequest.java +++ b/src/main/java/com/teamB/pcs/dto/request/ArticleRequest.java @@ -1,4 +1,16 @@ package com.teamB.pcs.dto.request; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@NoArgsConstructor +@Getter public class ArticleRequest { + private String title; + + private String description; + + private String body; + + //태그리스트추가 } diff --git a/src/main/java/com/teamB/pcs/dto/response/ArticleListResponse.java b/src/main/java/com/teamB/pcs/dto/response/ArticleListResponse.java new file mode 100644 index 0000000..27a5b3d --- /dev/null +++ b/src/main/java/com/teamB/pcs/dto/response/ArticleListResponse.java @@ -0,0 +1,25 @@ +package com.teamB.pcs.dto.response; + + +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +import java.util.List; + +@NoArgsConstructor +@Getter +public class ArticleListResponse { + private boolean isSuccess; + + private String message; + + private List data; + + @Builder + public ArticleListResponse(boolean isSuccess, String message, List data) { + this.isSuccess = isSuccess; + this.message = message; + this.data = data; + } +} diff --git a/src/main/java/com/teamB/pcs/service/ArticleService.java b/src/main/java/com/teamB/pcs/service/ArticleService.java index d89fdac..3665013 100644 --- a/src/main/java/com/teamB/pcs/service/ArticleService.java +++ b/src/main/java/com/teamB/pcs/service/ArticleService.java @@ -1,19 +1,58 @@ package com.teamB.pcs.service; +import com.teamB.pcs.domain.Article; +import com.teamB.pcs.dto.request.ArticleRequest; +import com.teamB.pcs.dto.response.ArticleListResponse; import com.teamB.pcs.dto.response.ArticleResponse; import com.teamB.pcs.repository.ArticleRepository; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import java.util.List; +import java.util.stream.Collectors; @Service @RequiredArgsConstructor public class ArticleService { private final ArticleRepository articleRepository; - public List getAll() { - return articleRepository.findAll().stream() - .map() + public ArticleListResponse getAll() { + List articleResponses = articleRepository.findAll().stream() + .map(ArticleResponse::new) + .collect(Collectors.toList()); + return ArticleListResponse.builder() + .isSuccess(true) + .message("게시글 목록 조회에 성공하였습니다.") + .data(articleResponses) + .build(); + } + + public ArticleResponse get(Long articleId) { + Article article = articleRepository.findById(articleId) + .orElseThrow(() -> new IllegalArgumentException("존재하지 않는 게시글입니다.")); + return new ArticleResponse(article); + } + + @Transactional + public void update(Long articleId, ArticleRequest articleRequest) { + Article article = articleRepository.findById(articleId) + .orElseThrow(() -> new IllegalArgumentException("존재하지 않는 게시글입니다.")); + article.update(articleRequest.getTitle(), articleRequest.getDescription(), articleRequest.getBody()); + } + + public void delete(Long articleId) { + Article article = articleRepository.findById(articleId) + .orElseThrow(() -> new IllegalArgumentException("존재하지 않는 게시글입니다.")); + articleRepository.delete(article); + } + + public void create(ArticleRequest articleRequest) { + Article article = Article.builder() + .title(articleRequest.getTitle()) + .description(articleRequest.getDescription()) + .body(articleRequest.getBody()) + .build(); + articleRepository.save(article); } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index 8b13789..0000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..101ea8f --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,14 @@ +spring: + datasource: + url: jdbc:mysql://partcross.cpbaiimaqaar.ap-northeast-2.rds.amazonaws.com:3306/partcross?useSSL=false&allowPublicKeyRetrieval=true + username: partcross + password: partcross + driver-class-name: com.mysql.cj.jdbc.Driver + + jpa: + show-sql: true + hibernate: + ddl-auto: update + properties: + hibernate: + format_sql: true \ No newline at end of file