Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/feat/community' into dev-check
Browse files Browse the repository at this point in the history
  • Loading branch information
versatile0010 committed Nov 13, 2023
2 parents 51f9086 + a32c30b commit b307c0a
Show file tree
Hide file tree
Showing 38 changed files with 1,045 additions and 97 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.numberone.backend.domain.admin.dto.response.GetAddressResponse;
import com.numberone.backend.domain.shelter.dto.response.GetAllSheltersResponse;
import com.numberone.backend.domain.shelter.repository.ShelterRepository;
import com.numberone.backend.support.S3Provider;
import com.numberone.backend.support.s3.S3Provider;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.numberone.backend.domain.article.controller;

import com.numberone.backend.domain.article.dto.request.UploadArticleRequest;
import com.numberone.backend.domain.article.dto.response.*;
import com.numberone.backend.domain.article.service.ArticleService;
import com.numberone.backend.domain.comment.dto.request.CreateCommentRequest;
import com.numberone.backend.domain.comment.dto.response.CreateCommentResponse;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;

@Slf4j
@RequestMapping("/api/articles")
@RequiredArgsConstructor
@RestController
public class ArticleController {

private final ArticleService articleService;


@Operation(summary = "게시글 작성 API", description = """
동네생활 게시글 등록 api 입니다.
반드시 access token 을 헤더에 담아서 요청해주세요.
1. title 은 글 제목 입니다 (not null)
2. content 는 글 내용 입니다 (not null)
3. articleTag 는 게시글 태그 입니다. LIFE(일상), FRAUD(사기), SAFETY(안전), REPORT(제보)
4. imageList 는 이미지 (MultiPart) 리스트 입니다.
5. thumbNailImageIdx 는 썸네일 이미지의 인덱스 입니다. (0,1,2, ...
imageList 에 이미지를 담아서 보내는 경우,
idx 에 따라서 썸네일 이미지를 결정합니다.
""")

@PostMapping
public ResponseEntity<UploadArticleResponse> uploadArticle(@RequestBody @Valid UploadArticleRequest request) {
return ResponseEntity.created(URI.create("/api/articles"))
.body(articleService.uploadArticle(request));
}

@Operation(summary = "게시글을 삭제하는 API 입니다.", description = """
게시글 id 를 PathVariable 으로 넘겨주세요.
해당 게시글을 삭제 상태로 변경합니다.
""")
@PutMapping("{article-id}/delete")
public ResponseEntity<DeleteArticleResponse> deleteArticle(@PathVariable("article-id") Long articleId) {
return ResponseEntity.ok(articleService.deleteArticle(articleId));
}

@Operation(summary = "게시글 상세 조회 API 입니다.", description = """
게시글 id 를 PathVariable 으로 넘겨주세요.
""")
@GetMapping("{article-id}")
public ResponseEntity<GetArticleDetailResponse> getArticleDetails(@PathVariable("article-id") Long articleId) {
return ResponseEntity.ok(articleService.getArticleDetail(articleId));
}


@Operation(summary = "게시글 리스트 조회 no offset Paging API 입니다.", description = """
요청 예시 url 은 다음과 같습니다.
`/api/articles?size=5`
size 는 페이지의 사이즈를 의미하고, default 는 20 입니다.
정렬 순서는 articleId 순입니다. ( = 생성 시간 순 )
ModelAttribute 로 lastArticleId 와 tag 를 넘겨주세요 ( 둘 다 nullable )
tag 가 null 이면, tag 상관 없이 전체 조회를 수행합니다.
tag 가 null 이 아니면, 해당 tag 에 해당하는 게시글만 조회합니다.
lastArticleId 는 직전에 조회한 게시글 중 가장 먀지막(작은) articleId 를 의미합니다.
- 첫 페이지를 요청할 경우에는 lastArticleId 를 null 로 보내야합니다.
- 첫 페이지 이후에 대한 요청은, 직전 페이지 요청에서 얻어온 lastArticleId 를 넣어서 보내면 그 다음 페이지를 호출합니다.
""")
@GetMapping
public ResponseEntity<Slice<GetArticleListResponse>> getArticlePages(
Pageable pageable,
@ModelAttribute ArticleSearchParameter param) {
return ResponseEntity.ok(articleService.getArticleListPaging(param, pageable));
}

@Operation(summary = "게시글에 댓글 작성하기", description = """
게시글에 댓글을 작성하는 API 입니다.
게시글 아이디는 Path Parameter 으로 넘겨주세요 (article-id)
""")
@PostMapping("comments/{article-id}")
public ResponseEntity<CreateCommentResponse> createComment(
@PathVariable("article-id") Long articleId,
@RequestBody @Valid CreateCommentRequest request) {
return ResponseEntity.created(
URI.create(String.format("/api/articles/comment/%s", articleId)))
.body(articleService.createComment(articleId, request));

}



}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.numberone.backend.domain.article.dto.request;

import com.numberone.backend.domain.article.entity.ArticleTag;
import jakarta.validation.constraints.NotNull;
import lombok.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@ToString
@Builder
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class UploadArticleRequest {

// 글 관련
@NotNull(message = "글 제목은 null 일 수 없습니다.")
private String title; // 제목

@NotNull(message = "내용은 null 일 수 없습니다.")
private String content; // 내용

@NotNull(message = """
게시글의 태그를 하나 선택해주세요.
LIFE(일상), FRAUD(사기), SAFETY(안전), REPORT(제보)
""")
private ArticleTag articleTag; // 게시글 태그

// 이미지 관련
private List<MultipartFile> imageList; // 이미지 리스트
private Long thumbNailImageIdx; // 썸네일 이미지의 순서 (0,1,2,...)

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.numberone.backend.domain.article.dto.response;

import com.numberone.backend.domain.article.entity.ArticleTag;
import lombok.*;

@ToString
@Builder
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class ArticleSearchParameter {
private ArticleTag tag;
private Long lastArticleId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.numberone.backend.domain.article.dto.response;

import com.numberone.backend.domain.article.entity.Article;
import lombok.*;

import java.time.LocalDateTime;

@ToString
@Builder
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class DeleteArticleResponse {

private Long id;
private LocalDateTime deletedAt;

public static DeleteArticleResponse of(Article article){
return DeleteArticleResponse.builder()
.id(article.getId())
.deletedAt(article.getModifiedAt())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.numberone.backend.domain.article.dto.response;

import com.numberone.backend.domain.article.entity.Article;
import com.numberone.backend.domain.member.entity.Member;
import lombok.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

@ToString
@Builder
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class GetArticleDetailResponse {

// 게시글 관련
private Long articleId;
private Integer likeCount;
private LocalDateTime createdAt;
private LocalDateTime modifiedAt;
private String title;
private String content;

// 작성자 관련
private String memberName;
private String memberNickName;
private String address; // todo: 더미 데이터
private Long ownerMemberId;

// 이미지 관련
private List<String> imageUrls;
private String thumbNailImageUrl;

public static GetArticleDetailResponse of(Article article, List<String> imageUrls, String thumbNailImageUrl, Member member){
return GetArticleDetailResponse.builder()
.articleId(article.getId())
.title(article.getTitle())
.content(article.getContent())
.likeCount(
Optional.ofNullable(
article.getLikeCount()
).orElse(0)
)
.createdAt(article.getCreatedAt())
.modifiedAt(article.getModifiedAt())
.ownerMemberId(member.getId())
.memberName(member.getRealName())
.memberNickName(member.getNickName())
.imageUrls(imageUrls)
.thumbNailImageUrl(thumbNailImageUrl)
.address("서울시 광진구 자양동") // 교체
.build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.numberone.backend.domain.article.dto.response;

import com.numberone.backend.domain.article.entity.Article;
import com.numberone.backend.domain.article.entity.ArticleStatus;
import com.numberone.backend.domain.article.entity.ArticleTag;
import com.numberone.backend.domain.articleimage.entity.ArticleImage;
import com.numberone.backend.domain.member.entity.Member;
import com.querydsl.core.annotations.QueryProjection;
import lombok.*;

import java.time.LocalDateTime;
import java.util.Optional;

@ToString
@Builder
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class GetArticleListResponse {

private ArticleTag tag;
private Long id;
private String title;
private String content;
private String address;
private String ownerNickName;
private Long ownerId;
private LocalDateTime createdAt;
private ArticleStatus articleStatus;
private String thumbNailImageUrl;
private Long thumbNailImageId;

private Integer articleLikeCount;
private Integer commentCount;


@QueryProjection
public GetArticleListResponse(Article article, Long ownerId, Long thumbNailImageId) {
this.tag = article.getArticleTag();
this.id = article.getId();
this.title = article.getTitle();
this.content = article.getContent();
this.address = article.getAddress();
this.ownerId = ownerId;
this.createdAt = article.getCreatedAt();
this.articleStatus = article.getArticleStatus();
this.thumbNailImageId = thumbNailImageId;
this.articleLikeCount = article.getLikeCount();
this.commentCount = article.getCommentCount();
}

public void setOwnerNickName(String nickName){
this.ownerNickName = nickName;
}

public void setThumbNailImageUrl(String thumbNailImageUrl){
this.thumbNailImageUrl = thumbNailImageUrl;
}

public void updateInfo(Optional<Member> owner, Optional<ArticleImage> articleImage){
owner.ifPresentOrElse(
o -> setOwnerNickName(o.getNickName()),
() -> setOwnerNickName("알 수 없는 사용자")
);
articleImage.ifPresentOrElse(
image -> setThumbNailImageUrl(image.getImageUrl()),
() -> setThumbNailImageUrl("")
);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.numberone.backend.domain.article.dto.response;

import com.numberone.backend.domain.article.entity.Article;
import lombok.*;

import java.time.LocalDateTime;
import java.util.List;

@ToString
@Builder
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor
public class UploadArticleResponse {

private Long articleId;
private LocalDateTime createdAt;

// 이미지 관련
private List<String> imageUrls;
private String thumbNailImageUrl;

// 작성자 주소
private String address; // todo: 더미 데이터

public static UploadArticleResponse of(Article article, List<String> imageUrls, String thumbNailImageUrl){
return UploadArticleResponse.builder()
.articleId(article.getId())
.createdAt(article.getCreatedAt())
.imageUrls(imageUrls)
.thumbNailImageUrl(thumbNailImageUrl)
.address("서울시 광진구 자양동")
.build();
}

}
Loading

0 comments on commit b307c0a

Please sign in to comment.