Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: crud 초안 구현 #4

Merged
merged 2 commits into from
Sep 9, 2023
Merged
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
13 changes: 7 additions & 6 deletions src/main/java/com/teamB/pcs/controller/ArticleController.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -16,13 +17,13 @@ public class ArticleController {
private final ArticleService articleService;

@GetMapping("")
public List<ArticleResponse> 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}")
Expand All @@ -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);
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/teamB/pcs/domain/Article.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/teamB/pcs/dto/request/ArticleRequest.java
Original file line number Diff line number Diff line change
@@ -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;

//태그리스트추가
}
25 changes: 25 additions & 0 deletions src/main/java/com/teamB/pcs/dto/response/ArticleListResponse.java
Original file line number Diff line number Diff line change
@@ -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<ArticleResponse> data;

@Builder
public ArticleListResponse(boolean isSuccess, String message, List<ArticleResponse> data) {
this.isSuccess = isSuccess;
this.message = message;
this.data = data;
}
}
45 changes: 42 additions & 3 deletions src/main/java/com/teamB/pcs/service/ArticleService.java
Original file line number Diff line number Diff line change
@@ -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<ArticleResponse> getAll() {
return articleRepository.findAll().stream()
.map()
public ArticleListResponse getAll() {
List<ArticleResponse> 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);
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ spring:
dev-env:
- dev
prod-env:
- prod
- prod
Loading