Skip to content

Commit

Permalink
Merge pull request #165 from lemonssoju/develop-v2
Browse files Browse the repository at this point in the history
[develop-v2] main merge
  • Loading branch information
JoongHyun-Kim authored Jun 3, 2024
2 parents 9e9bc5a + c5b3686 commit be610f2
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 23 deletions.
6 changes: 4 additions & 2 deletions src/main/java/com/lesso/neverland/album/domain/Album.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,17 @@ public class Album extends BaseEntity {

private String albumImage;

@Column(nullable = false)
@Column(nullable = false, length = 1000)
private String content;

@OneToMany(mappedBy = "album")
private List<Comment> comments = new ArrayList<>();

@Builder
public Album(Puzzle puzzle, String content) {
public Album(Puzzle puzzle, String albumImage, Team team, String content) {
this.puzzle = puzzle;
this.albumImage = albumImage;
this.team = team;
this.content = content;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.lesso.neverland.album.domain.Album;
import com.lesso.neverland.group.domain.Team;
import com.lesso.neverland.puzzle.domain.Puzzle;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface AlbumRepository extends JpaRepository<Album, Long> {
List<Album> findByTeamOrderByCreatedDateDesc(Team team);
boolean existsByPuzzle(Puzzle puzzle);
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ public BaseResponse<GroupListResponse> getGroupList() {
User user = userRepository.findById(userService.getUserIdxWithValidation()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));

List<Team> groupList_admin = groupRepository.findByAdminAndStatusEquals(user, ACTIVE);
List<Team> groupList_member = user.getUserTeams().stream().map(UserTeam::getTeam).toList();
List<Team> groupList_member = user.getUserTeams().stream()
.filter(userTeam -> "active".equals(userTeam.getStatus()))
.map(UserTeam::getTeam).toList();
List<Team> combinedGroupList = Stream.concat(groupList_admin.stream(), groupList_member.stream()).distinct().toList();

List<GroupListDto> groupListDto = combinedGroupList.stream()
Expand Down Expand Up @@ -99,7 +101,7 @@ public BaseResponse<GroupProfileResponse> getGroupProfile(Long groupIdx) {

long dayCount = ChronoUnit.DAYS.between(startLocalDate, today);

GroupProfileResponse profile = new GroupProfileResponse(group.getName(), group.getStartDate().getYear(), memberImageList,
GroupProfileResponse profile = new GroupProfileResponse(group.getName(), group.getAdmin().getProfile().getNickname(), group.getStartDate().getYear(), memberImageList,
group.getUserTeams().size(), puzzleCount, dayCount);
return new BaseResponse<>(profile);
}
Expand Down Expand Up @@ -186,6 +188,8 @@ public BaseResponse<String> withdrawGroup(Long groupIdx) {

UserTeam userTeam = validateMember(user, group);
userTeam.delete();
userTeam.removeTeam(group);
userTeam.removeUser(user);
userTeamRepository.save(userTeam);

return new BaseResponse<>(SUCCESS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

public record GroupProfileResponse(String groupName,
String admin,
Integer startYear,
List<String> memberImageList, // 3개만 조회
Integer memberCount, // 멤버수
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.lesso.neverland.gpt.application.GptService;
import com.lesso.neverland.gpt.dto.GptResponseDto;
import com.lesso.neverland.puzzle.domain.PuzzleLocation;
import com.lesso.neverland.puzzle.dto.CompletePuzzleRequest;
import com.lesso.neverland.puzzle.dto.CompletePuzzleResponse;
import com.lesso.neverland.gpt.dto.GptResponse;
import com.lesso.neverland.group.domain.Team;
Expand Down Expand Up @@ -42,8 +41,10 @@
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

import static com.lesso.neverland.common.base.BaseResponseStatus.*;
import static com.lesso.neverland.common.constants.Constants.ACTIVE;
Expand Down Expand Up @@ -101,10 +102,11 @@ public BaseResponse<PuzzleDetailResponse> getPuzzleDetail(Long groupIdx, Long pu

boolean isWriter = puzzle.getUser().equals(user);
boolean hasWrite = puzzlePieceRepository.existsByPuzzleAndUser(puzzle, user);
boolean hasAlbum = albumRepository.existsByPuzzle(puzzle);

PuzzleDetailResponse puzzleDetail = new PuzzleDetailResponse(puzzle.getLocation().getLocation(), puzzle.getPuzzleImage(),
puzzle.getPuzzleDate().toString(), puzzle.getUser().getProfile().getNickname(), puzzle.getTitle(), puzzle.getContent(),
getMemberImageList(puzzle), puzzle.getPuzzleMembers().size(), puzzle.getPuzzlePieces().size()+1, isWriter, hasWrite,
puzzle.getPuzzleDate().toString(), puzzle.getCreatedDate().toString(), puzzle.getUser().getProfile().getNickname(), puzzle.getTitle(), puzzle.getContent(),
getMemberImageList(puzzle), getMemberNicknameList(puzzle), puzzle.getPuzzleMembers().size(), puzzle.getPuzzlePieces().size()+1, isWriter, hasWrite, hasAlbum,
getPuzzlePieceList(puzzle));
return new BaseResponse<>(puzzleDetail);
}
Expand All @@ -121,15 +123,24 @@ private List<PuzzlePieceDto> getPuzzlePieceList(Puzzle puzzle) {

// puzzleMember 3명의 프로필 이미지 조회
private List<String> getMemberImageList(Puzzle puzzle) {
return puzzle.getPuzzleMembers().stream()
List<String> imageList = new ArrayList<>();
imageList.add(puzzle.getUser().getProfile().getProfileImage());

List<String> memberImages = puzzle.getPuzzleMembers().stream()
.map(puzzleMember -> puzzleMember.getUser().getProfile().getProfileImage())
.limit(3)
.toList();
.limit(2).toList();
imageList.addAll(memberImages);
return imageList;
}

private List<String> getMemberNicknameList(Puzzle puzzle) {
return puzzle.getPuzzleMembers().stream()
.map(puzzleMember -> puzzleMember.getUser().getProfile().getNickname()).toList();
}

// 퍼즐 생성
@Transactional(rollbackFor = Exception.class)
public BaseResponse<String> createPuzzle(Long groupIdx, MultipartFile image, CreatePuzzleRequest createPuzzleRequest) throws IOException {
public BaseResponse<CreatePuzzleResponse> createPuzzle(Long groupIdx, MultipartFile image, CreatePuzzleRequest createPuzzleRequest) throws IOException {
Team group = groupRepository.findById(groupIdx).orElseThrow(() -> new BaseException(INVALID_GROUP_IDX));
User writer = userRepository.findById(userService.getUserIdxWithValidation()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));

Expand All @@ -139,7 +150,7 @@ public BaseResponse<String> createPuzzle(Long groupIdx, MultipartFile image, Cre
Puzzle newPuzzle = createPuzzle(createPuzzleRequest, group, writer, imagePath, puzzleDate);
addPuzzleMember(createPuzzleRequest, newPuzzle);

return new BaseResponse<>(SUCCESS);
return new BaseResponse<>(new CreatePuzzleResponse(newPuzzle.getPuzzleIdx()));
}

// Puzzle 생성 시 PuzzleMember entity 함께 생성
Expand Down Expand Up @@ -278,18 +289,24 @@ private void validatePuzzler(User user, Puzzle puzzle) {

// [작성자] 퍼즐 완성하기
@Transactional(rollbackFor = Exception.class)
public BaseResponse<CompletePuzzleResponse> completePuzzle(Long groupIdx, Long puzzleIdx, CompletePuzzleRequest completePuzzleRequest) {
public BaseResponse<CompletePuzzleResponse> completePuzzle(Long groupIdx, Long puzzleIdx) {
User user = userRepository.findById(userService.getUserIdxWithValidation()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
Puzzle puzzle = puzzleRepository.findById(puzzleIdx).orElseThrow(() -> new BaseException(INVALID_PUZZLE_IDX));
validateWriter(user, puzzle);

List<String> puzzleTextList = puzzle.getPuzzlePieces().stream()
.map(PuzzlePiece::getContent).collect(Collectors.toList());
puzzleTextList.add(puzzle.getContent());

// GPT 요약 수행
GptResponse response = gptService.completion(gptService.toText(completePuzzleRequest.puzzleTextList()));
GptResponse response = gptService.completion(gptService.toText(puzzleTextList));
GptResponseDto gptResponseDto = gptService.parseResponse(response.messages().get(0).message());

// Album 생성
Album newAlbum = Album.builder()
.puzzle(puzzle)
.albumImage("")
.team(puzzle.getTeam())
.content(gptResponseDto.description()).build();
albumRepository.save(newAlbum);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.lesso.neverland.puzzle.dto;

public record CreatePuzzleResponse(Long puzzleIdx) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
public record PuzzleDetailResponse(String location,
String puzzleImage,
String puzzleDate,
String createdDate,
String writer,
String title,
String content,
List<String> memberImageList, // 3명만
List<String> memberNicknameList,
Integer memberCount,
Integer writeCount, // PuzzlePieceCount+1
boolean isWriter,
boolean hasWrite, // 해당 user가 퍼즐피스 작성했는지 여부
boolean hasAlbum,
List<PuzzlePieceDto> puzzlePieces) {}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import com.lesso.neverland.common.base.BaseException;
import com.lesso.neverland.common.base.BaseResponse;
import com.lesso.neverland.gpt.application.GptService;
import com.lesso.neverland.puzzle.dto.CompletePuzzleRequest;
import com.lesso.neverland.puzzle.dto.CompletePuzzleResponse;
import com.lesso.neverland.group.dto.GroupPuzzleListResponse;
import com.lesso.neverland.puzzle.application.PuzzleService;
Expand All @@ -22,7 +20,6 @@
@RequestMapping(puzzle)
public class PuzzleController {
private final PuzzleService puzzleService;
private final GptService gptService;

// 퍼즐 목록 조회
@GetMapping("")
Expand All @@ -38,7 +35,7 @@ public BaseResponse<PuzzleDetailResponse> getPuzzleDetail(@PathVariable("groupId

// 퍼즐 생성
@PostMapping("")
public BaseResponse<String> createPuzzle(@PathVariable Long groupIdx, @RequestPart MultipartFile image, @RequestPart CreatePuzzleRequest createPuzzleRequest) {
public BaseResponse<CreatePuzzleResponse> createPuzzle(@PathVariable Long groupIdx, @RequestPart MultipartFile image, @RequestPart CreatePuzzleRequest createPuzzleRequest) {
try {
return puzzleService.createPuzzle(groupIdx, image, createPuzzleRequest);
} catch (IOException e) {
Expand Down Expand Up @@ -72,11 +69,8 @@ public BaseResponse<String> addPuzzlePiece(@PathVariable("groupIdx") Long groupI

// [작성자] 퍼즐 완성하기
@PostMapping("/{puzzleIdx}")
public BaseResponse<CompletePuzzleResponse> completePuzzle(@PathVariable("groupIdx") Long groupIdx,
@PathVariable("puzzleIdx") Long puzzleIdx,
@RequestBody CompletePuzzleRequest completePuzzleRequest)
{
return puzzleService.completePuzzle(groupIdx, puzzleIdx, completePuzzleRequest);
public BaseResponse<CompletePuzzleResponse> completePuzzle(@PathVariable("groupIdx") Long groupIdx, @PathVariable("puzzleIdx") Long puzzleIdx) {
return puzzleService.completePuzzle(groupIdx, puzzleIdx);
}

}
10 changes: 10 additions & 0 deletions src/main/java/com/lesso/neverland/user/domain/UserTeam.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public void setTeam(Team team) {
team.getUserTeams().add(this);
}

public void removeUser(User user) {
this.user = user;
user.getUserTeams().remove(this);
}

public void removeTeam(Team team) {
this.team = team;
team.getUserTeams().remove(this);
}

@Builder
public UserTeam(User user, Team team) {
this.user = user;
Expand Down

0 comments on commit be610f2

Please sign in to comment.