Skip to content

Commit

Permalink
PICK-O 프렌즈 관련 API 개발 (#760)
Browse files Browse the repository at this point in the history
* chore: 대문자 연속 허용하도록 수정

* refactor: 세부 구현 코드 캡슐화

* feat: PICK-O 프렌즈 생성 API 개발

* feat: PICK-O 프렌즈 이미지 목록 조회 API 개발

* style: 사용되지 않는 import문 제거

* chore: swagger dto description 수정

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* chore: swagger 메서드 description 변경

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* refactor: 기본 생성자 접근제어자를 private으로 변경

* fix: 메서드 네이밍 수정

* fix: 누락된 메서드 추가

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
  • Loading branch information
Hanjaemo and coderabbitai[bot] authored Nov 26, 2024
1 parent 4407085 commit e6410df
Show file tree
Hide file tree
Showing 10 changed files with 218 additions and 14 deletions.
8 changes: 0 additions & 8 deletions config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -285,14 +285,6 @@
<property name="lineWrappingIndentation" value="4"/>
<property name="arrayInitIndent" value="4"/>
</module>
<module name="AbbreviationAsWordInName">
<property name="ignoreFinal" value="false"/>
<property name="allowedAbbreviationLength" value="0"/>
<property name="tokens"
value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, ANNOTATION_DEF, ANNOTATION_FIELD_DEF,
PARAMETER_DEF, VARIABLE_DEF, METHOD_DEF, PATTERN_VARIABLE_DEF, RECORD_DEF,
RECORD_COMPONENT_DEF"/>
</module>
<module name="NoWhitespaceBeforeCaseDefaultColon"/>
<module name="OverloadMethodsDeclarationOrder"/>
<module name="VariableDeclarationUsageDistance"/>
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/balancetalk/file/domain/FileType.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ public enum FileType {
TEMP_TALK_PICK("temp-talks/", 10),
GAME_OPTION("game-options/", 1),
TEMP_GAME_OPTION("temp-game-options/", 1),
MEMBER("members/", 1);
MEMBER("members/", 1),
FRIENDS("friends/", 1);

private final String uploadDir;
private final int maxCount;
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/balancetalk/friends/application/FriendsService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package balancetalk.friends.application;

import static balancetalk.friends.dto.FriendsDto.FriendsImageResponse;

import balancetalk.file.domain.File;
import balancetalk.file.domain.FileHandler;
import balancetalk.file.domain.FileType;
import balancetalk.file.domain.repository.FileRepository;
import balancetalk.friends.domain.Friends;
import balancetalk.friends.domain.FriendsRepository;
import balancetalk.friends.dto.FriendsDto.CreateFriendsRequest;
import balancetalk.global.exception.BalanceTalkException;
import balancetalk.global.exception.ErrorCode;
import balancetalk.member.domain.Member;
import balancetalk.member.domain.MemberRepository;
import balancetalk.member.dto.ApiMember;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class FriendsService {

private final MemberRepository memberRepository;
private final FriendsRepository friendsRepository;
private final FileRepository fileRepository;
private final FileHandler fileHandler;

@Transactional
public void createFriends(CreateFriendsRequest request, ApiMember apiMember) {
Member member = apiMember.toMember(memberRepository);
if (member.isRoleUser()) {
throw new BalanceTalkException(ErrorCode.FORBIDDEN_PICK_O_FRIENDS_OPERATION);
}
Friends savedFriends = friendsRepository.save(request.toEntity());

File file = fileRepository.findById(request.getImgId())
.orElseThrow(() -> new BalanceTalkException(ErrorCode.NOT_FOUND_FILE));
fileHandler.relocateFile(file, savedFriends.getId(), FileType.FRIENDS);
}

public List<FriendsImageResponse> findAllFriendsImages() {
return fileRepository.findAllById(getFriendsImgIds())
.stream()
.map(FriendsImageResponse::from)
.toList();
}

private List<Long> getFriendsImgIds() {
return friendsRepository.findAll()
.stream()
.map(Friends::getImgId)
.toList();
}
}
32 changes: 32 additions & 0 deletions src/main/java/balancetalk/friends/domain/Friends.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package balancetalk.friends.domain;

import balancetalk.global.common.BaseTimeEntity;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Builder
@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Friends extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@NotBlank
private String name;

@NotNull
private Long imgId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package balancetalk.friends.domain;

import org.springframework.data.jpa.repository.JpaRepository;

public interface FriendsRepository extends JpaRepository<Friends, Long> {
}
58 changes: 58 additions & 0 deletions src/main/java/balancetalk/friends/dto/FriendsDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package balancetalk.friends.dto;

import balancetalk.file.domain.File;
import balancetalk.friends.domain.Friends;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class FriendsDto {

@Schema(description = "PICK-O 프렌즈 캐릭터 생성 요청")
@Data
@AllArgsConstructor
public static class CreateFriendsRequest {

@Schema(description = "프렌즈 이름", example = "꺼북이")
@NotBlank(message = "프렌즈 이름은 필수입니다.")
private String name;

@Schema(description = "첨부한 이미지 파일 ID", example = "41")
@NotNull(message = "프렌즈 이미지 ID는 필수입니다.")
private Long imgId;

public Friends toEntity() {
return Friends.builder()
.name(name)
.imgId(imgId)
.build();
}
}

@Schema(description = "PICK-O 프렌즈 이미지 응답")
@Data
@Builder
@AllArgsConstructor
public static class FriendsImageResponse {

@Schema(description = "프렌즈 이미지 파일 ID", example = "3")
private Long fileId;

@Schema(description = "프렌즈 이미지 URL",
example = "https://picko-image.amazonaws.com/friends/ad80-a94e08-3301d2_대해파리.png")
private String imgUrl;

public static FriendsImageResponse from(File file) {
return FriendsImageResponse.builder()
.fileId(file.getId())
.imgUrl(file.getImgUrl())
.build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package balancetalk.friends.presentation;

import static balancetalk.friends.dto.FriendsDto.FriendsImageResponse;

import balancetalk.friends.application.FriendsService;
import balancetalk.friends.dto.FriendsDto.CreateFriendsRequest;
import balancetalk.global.utils.AuthPrincipal;
import balancetalk.member.dto.ApiMember;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
@RequestMapping("/friends")
@Tag(name = "friends", description = "PICK-O 프렌즈 API")
public class FriendsController {

private final FriendsService friendsService;

@Operation(summary = "PICK-O 프렌즈 캐릭터 생성", description = "PICK-O 프렌즈 캐릭터를 생성합니다.")
@PostMapping
public void createCharacter(@RequestBody final CreateFriendsRequest request,
@Parameter(hidden = true) @AuthPrincipal final ApiMember apiMember) {
friendsService.createFriends(request, apiMember);
}

@Operation(summary = "PICK-O 프렌즈 이미지 목록 조회", description = "PICK-O 프렌즈 이미지 목록을 조회합니다.")
@GetMapping("/images")
public List<FriendsImageResponse> findAllFriendsImages() {
return friendsService.findAllFriendsImages();
}
}
3 changes: 1 addition & 2 deletions src/main/java/balancetalk/game/application/GameService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import balancetalk.global.exception.ErrorCode;
import balancetalk.member.domain.Member;
import balancetalk.member.domain.MemberRepository;
import balancetalk.member.domain.Role;
import balancetalk.member.dto.ApiMember;
import balancetalk.member.dto.GuestOrApiMember;
import balancetalk.vote.domain.GameVote;
Expand Down Expand Up @@ -199,7 +198,7 @@ private List<GameSetResponse> gameSetResponses(GuestOrApiMember guestOrApiMember
@Transactional
public void createGameMainTag(final CreateGameMainTagRequest request, final ApiMember apiMember) {
Member member = apiMember.toMember(memberRepository);
if (member.getRole() == Role.USER) {
if (member.isRoleUser()) {
throw new BalanceTalkException(ErrorCode.FORBIDDEN_MAIN_TAG_CREATE);
}

Expand Down
1 change: 1 addition & 0 deletions src/main/java/balancetalk/global/exception/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public enum ErrorCode {
FORBIDDEN_MEMBER_DELETE(FORBIDDEN, "사용자 탈퇴 권한이 없습니다."),
FORBIDDEN_LIKE_OWN_COMMENT(FORBIDDEN, "본인 댓글에는 좋아요를 누를 수 없습니다."),
FORBIDDEN_MAIN_TAG_CREATE(FORBIDDEN, "메인 태그 작성 권한이 없습니다."),
FORBIDDEN_PICK_O_FRIENDS_OPERATION(FORBIDDEN, "PICK-O 프렌즈 작업을 수행할 권한이 없습니다."),

// 404
NOT_FOUND_TALK_PICK(NOT_FOUND, "존재하지 않는 톡픽입니다."),
Expand Down
24 changes: 21 additions & 3 deletions src/main/java/balancetalk/member/domain/Member.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package balancetalk.member.domain;

import balancetalk.bookmark.domain.TalkPickBookmark;
import static balancetalk.member.domain.Role.USER;

import balancetalk.bookmark.domain.GameBookmark;
import balancetalk.bookmark.domain.TalkPickBookmark;
import balancetalk.game.domain.Game;
import balancetalk.game.domain.GameSet;
import balancetalk.game.domain.TempGameSet;
Expand All @@ -13,14 +15,26 @@
import balancetalk.talkpick.domain.TempTalkPick;
import balancetalk.vote.domain.GameVote;
import balancetalk.vote.domain.TalkPickVote;
import jakarta.persistence.*;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OneToOne;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;
import lombok.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Builder
Expand Down Expand Up @@ -197,6 +211,10 @@ public boolean isWriterOf(TalkPick talkPick) {
return talkPicks.contains(talkPick);
}

public boolean isRoleUser() {
return role == USER;
}

public boolean hasProfileImgId() {
return profileImgId != null;
}
Expand Down

0 comments on commit e6410df

Please sign in to comment.