Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package com.backendboard.domain.auth.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.backendboard.domain.auth.dto.JoinRequest;
import com.backendboard.domain.auth.dto.JoinResponse;
import com.backendboard.domain.auth.dto.PasswordUpdateRequest;
import com.backendboard.domain.auth.dto.RefreshTokenDto;
import com.backendboard.domain.auth.service.AuthService;
import com.backendboard.global.error.CustomError;
import com.backendboard.global.error.CustomException;
import com.backendboard.global.error.dto.ErrorResponse;
import com.backendboard.global.security.dto.CustomUserDetails;
import com.backendboard.global.util.JwtUtil;

import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -33,7 +35,6 @@
@RestController
@RequiredArgsConstructor
public class AuthController {
private static final Logger log = LoggerFactory.getLogger(AuthController.class);
private final AuthService authService;
private final JwtUtil jwtUtil;

Expand All @@ -56,6 +57,23 @@ public ResponseEntity<JoinResponse> join(@RequestBody @Valid JoinRequest request
return ResponseEntity.status(HttpStatus.CREATED).body(response);
}

@Operation(
summary = "비밀번호 변경 API",
description = "비밀번호를 변경합니다.",
security = {@SecurityRequirement(name = "bearerAuth")}
)
@ApiResponses({
@ApiResponse(responseCode = "204", description = "204 성공", content = @Content()),
@ApiResponse(responseCode = "403", description = "본인이 아닙니다.",
content = @Content(mediaType = "application/json", schema = @Schema(implementation = ErrorResponse.class))),
})
@PatchMapping("/users/password")
public ResponseEntity<Void> updatePassword(PasswordUpdateRequest request,
@AuthenticationPrincipal CustomUserDetails customUserDetails) {
authService.updatePassword(request, customUserDetails.getId());
return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}

@Operation(
summary = "토큰 재발급 API - Swagger ui 에서 테스트 X",
description = "새로운 토큰을 발급합니다.",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.backendboard.domain.auth.dto;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.Builder;
import lombok.Getter;

@Schema(description = "비밀번호 변경 요청 DTO")
@Getter
public class PasswordUpdateRequest {
@Schema(description = "비밀번호", example = "1234")
@NotBlank
@Size(max = 30)
private final String password;

@Builder
private PasswordUpdateRequest(String password) {
this.password = password;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public User createUser(String username, String nickname) {
.build();
}

public void updatePassword(String password) {
this.password = password;
}

public void deactivate() {
this.status = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.backendboard.domain.auth.dto.JoinRequest;
import com.backendboard.domain.auth.dto.JoinResponse;
import com.backendboard.domain.auth.dto.PasswordUpdateRequest;
import com.backendboard.domain.auth.dto.RefreshTokenDto;

public interface AuthService {
Expand All @@ -12,4 +13,6 @@ public interface AuthService {
void deleteRefreshToken(String refreshToken);

boolean isValidRefreshToken(String refreshToken);

void updatePassword(PasswordUpdateRequest request, Long authUserId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import com.backendboard.domain.auth.dto.JoinRequest;
import com.backendboard.domain.auth.dto.JoinResponse;
import com.backendboard.domain.auth.dto.PasswordUpdateRequest;
import com.backendboard.domain.auth.dto.RefreshTokenDto;
import com.backendboard.domain.auth.entity.AuthUser;
import com.backendboard.domain.auth.entity.RefreshToken;
Expand Down Expand Up @@ -58,6 +59,13 @@ public JoinResponse joinProcess(JoinRequest request) {
return JoinResponse.toDto(user);
}

@Transactional
@Override
public void updatePassword(PasswordUpdateRequest request, Long authUserId) {
AuthUser user = authUserRepository.getReferenceById(authUserId);
user.updatePassword(bCryptPasswordEncoder.encode(request.getPassword()));
}

public void validateDuplicationNickname(String nickname) {
if (userRepository.existsByNickname(nickname)) {
throw new CustomException(CustomError.USER_DUPLICATION_NICKNAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public ResponseEntity<CommentUpdateResponse> updateComment(
@Operation(
summary = "댓글 보기 API",
description = "댓글을 보여줍니다.",
security = {@SecurityRequirement(name = "bearerAuth")}
security = {}
)
@ApiResponses({
@ApiResponse(responseCode = "200", description = "200 성공",
Expand Down Expand Up @@ -123,7 +123,7 @@ public ResponseEntity<Void> deleteComment(
@Operation(
summary = "댓글 슬라이스 보기 API",
description = "댓글을 슬라이스로 보여줍니다.",
security = {@SecurityRequirement(name = "bearerAuth")}
security = {}
)
@ApiResponses({
@ApiResponse(responseCode = "200", description = "200 성공",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.backendboard.domain.comment.dto;

import java.time.LocalDateTime;

import com.backendboard.domain.comment.entity.Comment;

import io.swagger.v3.oas.annotations.media.Schema;
Expand All @@ -21,19 +23,31 @@ public class CommentReadResponse {
@Schema(description = "댓글 내용", example = "안녕하세요.")
private final String content;

@Schema(description = "생성 시간")
private final LocalDateTime createdDate;

@Schema(description = "최근 수정 시간")
private final LocalDateTime lastModifiedDate;

@Builder
private CommentReadResponse(Long id, Long postId, String author, String content) {
private CommentReadResponse(Long id, Long postId, String author, String content, LocalDateTime createdDate,
LocalDateTime lastModifiedDate
) {
this.id = id;
this.postId = postId;
this.author = author;
this.content = content;
this.createdDate = createdDate;
this.lastModifiedDate = lastModifiedDate;
}

public static CommentReadResponse toDto(Comment comment, String author) {
return CommentReadResponse.builder()
.id(comment.getId())
.postId(comment.getPostId())
.content(comment.getContent())
.createdDate(comment.getCreatedDate())
.lastModifiedDate(comment.getLastModifiedDate())
.author(author)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.backendboard.domain.comment.dto;

import java.time.LocalDateTime;

import com.backendboard.domain.comment.entity.Comment;

import io.swagger.v3.oas.annotations.media.Schema;
Expand All @@ -21,19 +23,30 @@ public class CommentSliceResponse {
@Schema(description = "댓글 내용", example = "안녕하세요.")
private final String content;

@Schema(description = "생성 시간")
private final LocalDateTime createdDate;

@Schema(description = "최근 수정 시간")
private final LocalDateTime lastModifiedDate;

@Builder
private CommentSliceResponse(Long id, Long postId, String author, String content) {
private CommentSliceResponse(Long id, Long postId, String author, String content, LocalDateTime createdDate,
LocalDateTime lastModifiedDate) {
this.id = id;
this.postId = postId;
this.author = author;
this.content = content;
this.createdDate = createdDate;
this.lastModifiedDate = lastModifiedDate;
}

public static CommentSliceResponse toDto(Comment comment, String author) {
return CommentSliceResponse.builder()
.id(comment.getId())
.postId(comment.getPostId())
.content(comment.getContent())
.createdDate(comment.getCreatedDate())
.lastModifiedDate(comment.getLastModifiedDate())
.author(author)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public ResponseEntity<PostUpdateResponse> updatePost(
@Operation(
summary = "게시글 보기 API",
description = "게시글을 보여줍니다.",
security = {@SecurityRequirement(name = "bearerAuth")}
security = {}
)
@ApiResponses({
@ApiResponse(responseCode = "200", description = "200 성공",
Expand Down Expand Up @@ -123,7 +123,7 @@ public ResponseEntity<Void> deletePost(
@Operation(
summary = "게시글 슬라이스 API",
description = "게시글을 슬라이스로 보여줍니다.",
security = {@SecurityRequirement(name = "bearerAuth")}
security = {}
)
@ApiResponses({
@ApiResponse(responseCode = "200", description = "200 성공",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.backendboard.domain.post.dto;

import java.util.List;

import com.backendboard.domain.post.entity.Post;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -21,10 +24,15 @@ public class PostCreateRequest {
@NotBlank
private final String content;

@Schema(description = "이미지 아이디", example = "[1, 2, 3]")
@NotNull
private final List<Long> imageIds;

@Builder
private PostCreateRequest(String title, String content) {
private PostCreateRequest(String title, String content, List<Long> imageIds) {
this.title = title;
this.content = content;
this.imageIds = imageIds;
}

public static Post toEntity(PostCreateRequest dto, Long userId) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.backendboard.domain.post.dto;

import java.util.List;

import com.backendboard.domain.post.entity.Post;

import io.swagger.v3.oas.annotations.media.Schema;
Expand Down Expand Up @@ -27,26 +29,32 @@ public class PostCreateResponse {
@Schema(description = "조회수", example = "0")
private final Long viewCount;

@Schema(description = "이미지 아이디", example = "[1, 2, 3]")
private final List<Long> imageIds;

@Builder
private PostCreateResponse(Long id, String author, String title, String content, Long likeCount, Long viewCount) {
private PostCreateResponse(Long id, String author, String title, String content, Long likeCount, Long viewCount,
List<Long> imageIds) {
this.id = id;
this.author = author;
this.title = title;
this.content = content;
this.likeCount = likeCount;
this.viewCount = viewCount;
this.imageIds = imageIds;
}

@Builder

public static PostCreateResponse toDto(Post post, String author) {
public static PostCreateResponse toDto(Post post, String author, List<Long> imageIds) {
return PostCreateResponse.builder()
.id(post.getId())
.author(author)
.title(post.getTitle())
.content(post.getContent())
.likeCount(post.getLikeCount())
.viewCount(post.getViewCount())
.imageIds(imageIds)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package com.backendboard.domain.post.dto;

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

import com.backendboard.domain.post.entity.Post;
import com.backendboard.domain.postimage.dto.PostImageReadResponse;
import com.backendboard.domain.postimage.entity.PostImage;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Builder;
Expand All @@ -27,26 +32,42 @@ public class PostReadResponse {
@Schema(description = "조회수", example = "0")
private final Long viewCount;

@Schema(description = "이미지 아이디", example = "[1, 2, 3]")
private final List<PostImageReadResponse> images;

@Schema(description = "생성 시간")
private final LocalDateTime createdDate;

@Schema(description = "최근 수정 시간")
private final LocalDateTime lastModifiedDate;

@Builder
private PostReadResponse(Long id, String author, String title, String content, Long likeCount, Long viewCount) {
private PostReadResponse(Long id, String author, String title, String content, Long likeCount, Long viewCount,
List<PostImageReadResponse> images, LocalDateTime createdDate, LocalDateTime lastModifiedDate) {
this.id = id;
this.author = author;
this.title = title;
this.content = content;
this.likeCount = likeCount;
this.viewCount = viewCount;
this.images = images;
this.createdDate = createdDate;
this.lastModifiedDate = lastModifiedDate;
}

@Builder

public static PostReadResponse toDto(Post post, String author) {
public static PostReadResponse toDto(Post post, String author, List<PostImage> images) {
return PostReadResponse.builder()
.id(post.getId())
.author(author)
.title(post.getTitle())
.content(post.getContent())
.likeCount(post.getLikeCount())
.viewCount(post.getViewCount())
.images(images.stream().map(PostImageReadResponse::toDto).toList())
.createdDate(post.getCreatedDate())
.lastModifiedDate(post.getLastModifiedDate())
.build();
}
}
Loading