Skip to content

Commit

Permalink
Merge pull request #47 from yht0827/feature/#46
Browse files Browse the repository at this point in the history
create, update 로직 리펙토링
  • Loading branch information
yht0827 authored Jan 4, 2024
2 parents 3ab19a1 + 841974e commit 0935fee
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 111 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
Expand Down Expand Up @@ -54,16 +54,15 @@ public ResponseEntity<PageResponseDto<BoardResponseDto>> getBoardList(
@PostMapping
public ResponseEntity<ResponseDto<?>> createBoard(@CurrentUser final Long memberId,
@Valid @RequestBody final BoardCreateRequestDto boardCreateRequestDto) {
boardService.create(memberId, boardCreateRequestDto);
return success(SuccessMessage.CREATE_BOARD_SUCCESS);
return create(boardService.create(memberId, boardCreateRequestDto));
}

@LoginCheck
@PutMapping
public ResponseEntity<ResponseDto<BoardResponseDto>> updateBoard(
@CurrentUser final Long memberId, @Valid @RequestBody final BoardUpdateRequestDto boardUpdateRequestDto) {
return success(boardService.update(memberId, boardUpdateRequestDto),
SuccessMessage.UPDATE_BOARD_SUCCESS);
@PatchMapping
public ResponseEntity<ResponseDto<?>> updateBoard(@CurrentUser final Long memberId,
@Valid @RequestBody final BoardUpdateRequestDto boardUpdateRequestDto) {
boardService.update(memberId, boardUpdateRequestDto);
return success(SuccessMessage.UPDATE_BOARD_SUCCESS);
}

@LoginCheck
Expand All @@ -83,8 +82,9 @@ public ResponseEntity<ResponseDto<LikeResponseDto>> getLike(@CurrentUser Long me

@LoginCheck
@PostMapping("/like")
public ResponseEntity<ResponseDto<LikeResponseDto>> updateLike(@CurrentUser final Long memberId,
public ResponseEntity<ResponseDto<?>> updateLike(@CurrentUser final Long memberId,
@Valid @RequestBody final LikeRequestDto likeRequestDto) {
return success(likeService.updateLike(memberId, likeRequestDto.boardId()));
likeService.updateLike(memberId, likeRequestDto.boardId());
return success(SuccessMessage.UPDATE_LIKE_SUCCESS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,9 @@ public Board(Long boardId, String title, String content, Long memberId, Long cat
this.deletedAt = deletedAt;
}

public Board updateBoard(BoardUpdateRequestDto boardUpdateRequestDto, Long categoryId) {
public void updateBoard(BoardUpdateRequestDto boardUpdateRequestDto, Long categoryId) {
this.title = boardUpdateRequestDto.title();
this.content = boardUpdateRequestDto.content();
this.categoryId = categoryId;
return this;
}
}
37 changes: 17 additions & 20 deletions src/main/java/com/example/kfanboy/board/service/BoardService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.kfanboy.board.service;

import java.util.Optional;
import java.util.Objects;

import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -37,34 +37,37 @@ public BoardResponseDto findById(final Long boardId) {
.orElseThrow(() -> new CustomException(ErrorMessage.BOARD_NOT_FOUND));
}

@Transactional(readOnly = true)
public PageResponseDto<BoardResponseDto> getBoardList(final BoardSearchCondition boardSearchCondition,
final Pageable pageable) {
return boardRepository.getBoardList(boardSearchCondition, pageable);
}

@Transactional
public void create(final Long memberId, final BoardCreateRequestDto boardCreateRequestDto) {
public Long create(final Long memberId, final BoardCreateRequestDto boardCreateRequestDto) {
Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new CustomException(ErrorMessage.USER_NOT_FOUND));

Category category = categoryRepository.findById(boardCreateRequestDto.categoryId())
.orElseThrow(() -> new CustomException(ErrorMessage.CATEGORY_NOT_FOUND));

Board board = boardRepository.save(boardCreateRequestDto.toEntity(member, category));

Optional.of(board)
.filter(b -> b.getBoardId() > 0)
.orElseThrow(() -> new CustomException(ErrorMessage.BOARD_NOT_CREATED));
return boardRepository.save(boardCreateRequestDto.toEntity(member, category)).getBoardId();
}

@Transactional
public BoardResponseDto update(final Long memberId, final BoardUpdateRequestDto boardUpdateRequestDto) {
Category category = categoryRepository.findById(boardUpdateRequestDto.categoryId())
.orElseThrow(() -> new CustomException(ErrorMessage.CATEGORY_NOT_FOUND));
public void update(final Long memberId, final BoardUpdateRequestDto boardUpdateRequestDto) {

Board board = boardRepository.findById(boardUpdateRequestDto.boardId())
.orElseThrow(() -> new CustomException(ErrorMessage.BOARD_NOT_FOUND));

Member member = memberRepository.findById(memberId)
.orElseThrow(() -> new CustomException(ErrorMessage.USER_NOT_FOUND));
if (!Objects.equals(board.getMemberId(), memberId)) {
throw new CustomException(ErrorMessage.BOARD_WRITER_NOT_MATCHED);
}

return BoardResponseDto.toDto(
board.updateBoard(boardUpdateRequestDto, category.getCategoryId()), member, category);
Category category = categoryRepository.findById(boardUpdateRequestDto.categoryId())
.orElseThrow(() -> new CustomException(ErrorMessage.CATEGORY_NOT_FOUND));

board.updateBoard(boardUpdateRequestDto, category.getCategoryId());
}

@Transactional
Expand All @@ -74,10 +77,4 @@ public void delete(final BoardDeleteRequestDto boardDeleteRequestDto) {

boardRepository.delete(board);
}

@Transactional(readOnly = true)
public PageResponseDto<BoardResponseDto> getBoardList(final BoardSearchCondition boardSearchCondition,
final Pageable pageable) {
return boardRepository.getBoardList(boardSearchCondition, pageable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@
public enum SuccessMessage {

//Common
OK("성공"),
OK("요청 성공"),
CREATE("리소스 작성 성공"),

// User
LOGIN_SUCCESS("로그인 성공"),
LOGOUT_SUCCESS("로그 아웃 성공"),
AVAILABLE_EMAIL("사용 가능한 이메일"),
CREATE_USER_SUCCESS("계정 생성 성공"),
UPDATE_USER_SUCCESS("계정 업데이트 성공"),
DELETE_USER_SUCCESS("계정 삭제 성공"),

// Board
CREATE_BOARD_SUCCESS("게시글 생성 성공"),
UPDATE_BOARD_SUCCESS("게시글 업데이트 성공"),
DELETE_BOARD_SUCCESS("게시글 삭제 성공"),

// Like
UPDATE_LIKE_SUCCESS("좋아요 업데이트 성공"),

DUMMY("");

private final String message;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.kfanboy.global.common.response;

import lombok.Builder;

@Builder
public record CreateDto(Long id) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@ public static <T> ResponseEntity<ResponseDto<T>> success(final T data, final Suc
.build());
}

public static ResponseEntity<ResponseDto<?>> create(final Long id) {
return ResponseEntity.status(HttpStatus.CREATED)
.body(ResponseDto.builder()
.data(CreateDto.builder().id(id).build())
.message(SuccessMessage.CREATE.getMessage())
.build());
}

public static ResponseEntity<ErrorResponseDto> error(final CustomException exception) {
return ResponseEntity.status(exception.getStatus())
.body(ErrorResponseDto.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,11 @@ public LikeResponseDto getLike(final Long memberId, final Long boardId) {

boolean isLiked = like.isPresent() && like.get().getDeletedAt() == null;

return LikeResponseDto.builder()
.isLiked(isLiked)
.build();
return LikeResponseDto.builder().isLiked(isLiked).build();
}

@Transactional
public LikeResponseDto updateLike(final Long memberId, final Long boardId) {
public void updateLike(final Long memberId, final Long boardId) {
Optional<Like> like = likeRepository.findByMemberIdAndBoardId(memberId, boardId);

boolean isLiked = false;
Expand All @@ -58,8 +56,5 @@ public LikeResponseDto updateLike(final Long memberId, final Long boardId) {
.orElseThrow(() -> new CustomException(ErrorMessage.BOARD_NOT_FOUND));

board.getBoardCount().changeLike(isLiked); // 좋아요 개수 카운트 업데이트
return LikeResponseDto.builder()
.isLiked(isLiked)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ public ResponseEntity<ResponseDto<UserResponseDto>> getProfile(@CurrentUser fina

@PostMapping(value = "/join")
public ResponseEntity<ResponseDto<?>> join(@RequestBody @Valid final JoinDto joinDto) {
memberService.join(joinDto);
return success(SuccessMessage.CREATE_USER_SUCCESS);
return create(memberService.join(joinDto));
}

@PostMapping(value = "/login")
Expand All @@ -80,9 +79,10 @@ public ResponseEntity<ResponseDto<?>> logout() {

@LoginCheck
@PutMapping
public ResponseEntity<ResponseDto<UserResponseDto>> update(@CurrentUser final Long id,
public ResponseEntity<ResponseDto<?>> update(@CurrentUser final Long id,
@RequestBody @Valid final UserUpdateRequestDto requestDto) {
return success(memberService.update(id, requestDto), SuccessMessage.UPDATE_USER_SUCCESS);
memberService.update(id, requestDto);
return success(SuccessMessage.UPDATE_USER_SUCCESS);
}

@LoginCheck
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public class Member extends BaseTimeEntity {
private String password;

@NotBlank
@Column(name = "nick_name", nullable = false, unique = true, length = 30)
@Column(name = "nick_name", nullable = false, length = 30)
@Length(min = 1, max = 30)
private String nickName;

Expand Down Expand Up @@ -85,11 +85,9 @@ public void updateLastLogin() {
this.lastLoginAt = LocalDateTime.now();
}

public Member updateMember(UserUpdateRequestDto userUpdateRequestDto, String encryptPassword) {
public void updateMember(UserUpdateRequestDto userUpdateRequestDto, String encryptPassword) {
this.nickName = userUpdateRequestDto.nickName();
this.password = encryptPassword;

return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,21 @@ public UserResponseDto getProfile(final Long id) {
}

@Transactional
public void join(final JoinDto joinDto) {
public Long join(final JoinDto joinDto) {
isExistsEmail(joinDto.email());
isEqualPassword(joinDto.password(), joinDto.password2());

// 비밀번호 암호화 후 저장
Member member = memberRepository.save(joinDto.toEntity(passwordEncryptor.encrypt(joinDto.password())));

Optional.of(member)
.filter(m -> m.getMemberId() > 0)
.orElseThrow(() -> new CustomException(ErrorMessage.USER_NOT_CREATED));
return memberRepository.save(joinDto.toEntity(passwordEncryptor.encrypt(joinDto.password()))).getMemberId();
}

@Transactional
public UserResponseDto update(final Long id, final UserUpdateRequestDto userUpdateRequestDto) {
public void update(final Long id, final UserUpdateRequestDto userUpdateRequestDto) {
isEqualPassword(userUpdateRequestDto.password(), userUpdateRequestDto.password2());

Member oldMember = findById(id);
return UserResponseDto.toDto(
oldMember.updateMember(userUpdateRequestDto, passwordEncryptor.encrypt(userUpdateRequestDto.password())));
Member member = findById(id);

member.updateMember(userUpdateRequestDto, passwordEncryptor.encrypt(userUpdateRequestDto.password()));
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ create table member
member_id bigint not null auto_increment,
email varchar(50) not null unique,
password varchar(100) not null,
nick_name varchar(30) not null unique,
nick_name varchar(30) not null,
created_at datetime(6) not null default now(6),
updated_at datetime(6) not null default now(6),
last_login_at datetime(6),
Expand Down
Loading

0 comments on commit 0935fee

Please sign in to comment.