Skip to content

Commit

Permalink
Merge pull request #85 from Princess-in-silvertown/feat/52
Browse files Browse the repository at this point in the history
Feat: ์ˆ˜์‹ ๋œ ๋กค๋งํŽ˜์ดํผ ๊ทธ๋ฃน ํ•„ํ„ฐ๋ง ๊ธฐ๋ฐ˜ ์กฐํšŒ ๊ธฐ๋Šฅ
  • Loading branch information
woosung1223 authored Sep 30, 2024
2 parents d6bdc41 + 2199404 commit 6b294da
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import slvtwn.khu.toyouserver.agent.modellabs.ModelLabsAgent;
Expand Down Expand Up @@ -62,6 +63,15 @@ public RollingPaperResponse findById(Long userId, Long rollingPaperId) {
return RollingPaperResponse.from(rollingPaper);
}

public List<RollingPaperResponse> findReceivedRollingPapers(Long userId, Long groupId,
Long targetId, Integer limit) {
List<Long> memberIds = getMemberIds(userId, groupId);
PageRequest pageRequest = PageRequest.ofSize(limit);
return rollingPaperRepository.findAllByMembersAfterCursor(memberIds, targetId, pageRequest).stream()
.map(RollingPaperResponse::from)
.toList();
}

private List<Sticker> parseStickers(RollingPaperRequest request, RollingPaper rollingPaper) {
return request.stickers().stream()
.map(each -> new Sticker(rollingPaper, each.imageUrl(), each.x(), each.y(),
Expand All @@ -74,4 +84,19 @@ private void assertUserIsRollingPaperOwner(User user, RollingPaper rollingPaper)
throw new ToyouException(ResponseType.UNAUTHORIZED_USER_ACCESS);
}
}

private List<Long> getMemberIds(Long userId, Long groupId) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new ToyouException(ResponseType.USER_NOT_FOUND));
if (groupId == null) {
List<Member> members = memberRepository.findByUser(user);
return members.stream()
.map(Member::getId)
.toList();
} else {
Member member = memberRepository.findByUserIdAndGroupId(userId, groupId)
.orElseThrow(() -> new ToyouException(ResponseType.MEMBER_NOT_FOUND));
return List.of(member.getId());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public enum ResponseType {
NOT_FOUND(HttpStatus.NOT_FOUND, "TYU-404", "์š”์ฒญํ•œ ์ž์›์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),
GROUP_NOT_FOUND(HttpStatus.NOT_FOUND, "TYU-4041", "๊ทธ๋ฃน์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),
USER_NOT_FOUND(HttpStatus.NOT_FOUND, "TYU-4042", "์‚ฌ์šฉ์ž๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),
MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND, "TYU-4043", "๊ฐ€์ž… ๊ธฐ๋ก์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค."),

// 500 Internal Server Error
INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "TYU-500", "์„œ๋ฒ„ ๋‚ด๋ถ€ ์˜ค๋ฅ˜");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
package slvtwn.khu.toyouserver.persistance;

import java.util.List;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import slvtwn.khu.toyouserver.domain.RollingPaper;

public interface RollingPaperRepository extends JpaRepository<RollingPaper, Long> {

@Query("SELECT r FROM RollingPaper r WHERE r.member.id IN :memberIds AND r.id < :targetId ORDER BY r.id DESC")
List<RollingPaper> findAllByMembersAfterCursor(@Param("memberIds") List<Long> memberIds,
@Param("targetId") Long targetId,
Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package slvtwn.khu.toyouserver.presentation;

import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
Expand All @@ -20,23 +21,32 @@ public class RollingPaperController {

private final RollingPaperService rollingPaperService;

@GetMapping("/rollingpapers")
@GetMapping("/rollingpapers/{rollingPaperId}")
public ToyouResponse<RollingPaperResponse> findById(@UserAuthentication Long userId,
@RequestParam Long rollingPaperId) {
@PathVariable Long rollingPaperId) {
return ToyouResponse.from(rollingPaperService.findById(userId, rollingPaperId));
}

@GetMapping("/rollingpapers")
public ToyouResponse<List<RollingPaperResponse>> findReceivedRollingPapers(@UserAuthentication Long userId,
@RequestParam Long groupId,
@RequestParam Long targetId,
@RequestParam(defaultValue = "10") int limit) {
return ToyouResponse.from(rollingPaperService.findReceivedRollingPapers(userId, groupId, targetId, limit));
}

@PostMapping("/users/{userId}/rollingpapers")
public ToyouResponse<Void> sendRollingPaper(@UserAuthentication Long RequestUserId,
@PathVariable(name = "userId") Long recipientUserId,
@RequestBody RollingPaperRequest rollingPaperRequest) {
@PathVariable(name = "userId") Long recipientUserId,
@RequestBody RollingPaperRequest rollingPaperRequest) {
rollingPaperService.sendRollingPaper(recipientUserId, rollingPaperRequest);
return ToyouResponse.noContent();
}

@PostMapping("/rollingpapers/{rollingPaperId}/generate-cover")
public ToyouResponse<Void> generateCoverImage(@UserAuthentication Long userId,
@PathVariable Long rollingPaperId, @RequestBody CoverRequest request) {
@PathVariable Long rollingPaperId,
@RequestBody CoverRequest request) {
rollingPaperService.generateCoverImageAndUpdateRollingPaper(request, rollingPaperId);
return ToyouResponse.noContent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import slvtwn.khu.toyouserver.domain.Group;
import slvtwn.khu.toyouserver.domain.Member;
import slvtwn.khu.toyouserver.domain.RollingPaper;
import slvtwn.khu.toyouserver.domain.SocialAuthProvider;
import slvtwn.khu.toyouserver.domain.User;
import slvtwn.khu.toyouserver.dto.CoverRequest;
import slvtwn.khu.toyouserver.dto.RollingPaperRequest;
Expand Down Expand Up @@ -111,4 +110,164 @@ class RollingPaperServiceTest {
assertThat(response).usingRecursiveComparison()
.isEqualTo(RollingPaperResponse.from(rollingPaper));
}

@Test
void ์‚ฌ์šฉ์ž๋Š”_์ž์‹ ์ด_์ˆ˜์‹ ํ•œ_๋กค๋งํŽ˜์ดํผ๋ฅผ_์กฐํšŒํ• _์ˆ˜_์žˆ๋‹ค() {
// given
Group group = new Group("name");
User user = new User("name", LocalDate.now(), "introduction", "profile_picture", null);
Member member = new Member(user, group);
RollingPaper rollingPaper = new RollingPaper(null, "title", "content", 1L, member);

entityManager.persist(user);
entityManager.persist(group);
entityManager.persist(member);
entityManager.persist(rollingPaper);

// when
List<RollingPaperResponse> response = rollingPaperService.findReceivedRollingPapers(user.getId(), group.getId(),
rollingPaper.getId() + 1, 10);

// then
assertThat(response).usingRecursiveComparison()
.isEqualTo(List.of(RollingPaperResponse.from(rollingPaper)));
}

@Test
void ์ˆ˜์‹ ํ•œ_๋กค๋งํŽ˜์ดํผ_์กฐํšŒ๋Š”_๊ทธ๋ฃน_์‹๋ณ„์ž๊ฐ€_null์ธ_๊ฒฝ์šฐ_์œ ์ €๊ฐ€_์ˆ˜์‹ ํ•œ_๋ชจ๋“ _๋กค๋งํŽ˜์ดํผ๋ฅผ_๋ฐ˜ํ™˜ํ•œ๋‹ค() {
// given
User user = new User("name", LocalDate.now(), "introduction", "profile_picture", null);
Group group1 = new Group("group1");
Group group2 = new Group("group2");
Member member1 = new Member(user, group1);
Member member2 = new Member(user, group2);
RollingPaper rollingPaper = new RollingPaper(null, "title", "rollingPaper", 1L, member1);
RollingPaper anotherRollingPaper = new RollingPaper(null, "title", "anotherRollingPaper", 1L, member2);

entityManager.persist(user);
entityManager.persist(group1);
entityManager.persist(group2);
entityManager.persist(member1);
entityManager.persist(member2);
entityManager.persist(rollingPaper);
entityManager.persist(anotherRollingPaper);

// when
List<RollingPaperResponse> responseWithoutGroupId = rollingPaperService.findReceivedRollingPapers(
user.getId(),
null,
anotherRollingPaper.getId() + 1, 10);

// then
assertThat(responseWithoutGroupId).usingRecursiveComparison()
.isEqualTo(List.of(
RollingPaperResponse.from(anotherRollingPaper),
RollingPaperResponse.from(rollingPaper)));

}

@Test
void ์ˆ˜์‹ ํ•œ_๋กค๋งํŽ˜์ดํผ_์กฐํšŒ๋Š”_๊ทธ๋ฃน_์‹๋ณ„์ž๊ฐ€_์กด์žฌํ• _๊ฒฝ์šฐ_ํ•ด๋‹น_๊ทธ๋ฃน์˜_๋กค๋งํŽ˜์ดํผ๋งŒ_๋ฐ˜ํ™˜ํ•œ๋‹ค() {
// given
User user = new User("name", LocalDate.now(), "introduction", "profile_picture", null);
Group group1 = new Group("group1");
Group group2 = new Group("group2");
Member member1 = new Member(user, group1);
Member member2 = new Member(user, group2);
RollingPaper rollingPaper = new RollingPaper(null, "title", "rollingPaper", 1L, member1);
RollingPaper anotherRollingPaper = new RollingPaper(null, "title", "anotherRollingPaper", 1L, member2);

entityManager.persist(user);
entityManager.persist(group1);
entityManager.persist(group2);
entityManager.persist(member1);
entityManager.persist(member2);
entityManager.persist(rollingPaper);
entityManager.persist(anotherRollingPaper);

// when
List<RollingPaperResponse> responseWithGroupId = rollingPaperService.findReceivedRollingPapers(
user.getId(),
group1.getId(),
rollingPaper.getId() + 1, 10);

// then
assertThat(responseWithGroupId).usingRecursiveComparison()
.isEqualTo(List.of(RollingPaperResponse.from(rollingPaper)));
}

@Test
void ์ˆ˜์‹ ํ•œ_๋กค๋งํŽ˜์ดํผ_์กฐํšŒ๋Š”_๋‹ค๋ฅธ_๊ทธ๋ฃน์˜_์ปค์„œ๋ฅผ_์‚ฌ์šฉํ•ด๋„_ํ•ด๋‹น_๊ทธ๋ฃน์˜_ํŽธ์ง€๋งŒ_์กฐํšŒ๋œ๋‹ค() {
// given
User user = new User("name", LocalDate.now(), "introduction", "profile_picture", null);
Group group1 = new Group("group1");
Group group2 = new Group("group2");
Member member1 = new Member(user, group1);
Member member2 = new Member(user, group2);

RollingPaper rollingPaper1 = new RollingPaper(null, "title1", "rollingPaper1", 1L, member1);
RollingPaper rollingPaper2 = new RollingPaper(null, "title2", "rollingPaper2", 1L, member1);
RollingPaper rollingPaper3 = new RollingPaper(null, "title3", "rollingPaper3", 1L, member2);

entityManager.persist(user);
entityManager.persist(group1);
entityManager.persist(group2);
entityManager.persist(member1);
entityManager.persist(member2);
entityManager.persist(rollingPaper1);
entityManager.persist(rollingPaper2);
entityManager.persist(rollingPaper3);

long totalRollingPapersCount = rollingPaper3.getId() + 1;

// when
List<RollingPaperResponse> response = rollingPaperService.findReceivedRollingPapers(
user.getId(),
group1.getId(),
totalRollingPapersCount, 10);

// then
List<RollingPaperResponse> expectedResponse = List.of(
RollingPaperResponse.from(rollingPaper2),
RollingPaperResponse.from(rollingPaper1)
);

assertThat(response).usingRecursiveComparison()
.isEqualTo(expectedResponse);
}

@Test
void ์ˆ˜์‹ ํ•œ_๋กค๋งํŽ˜์ดํผ_์กฐํšŒ๋Š”_์ƒ์„ฑ๋œ_์‹œ๊ฐ„์ด_์ตœ์‹ ์ธ_๋กค๋งํŽ˜์ดํผ๋ถ€ํ„ฐ_์กฐํšŒ๋œ๋‹ค() {
// given
User user = new User("name", LocalDate.now(), "introduction", "profile_picture", null);
Group group = new Group("group");
Member member = new Member(user, group);

RollingPaper rollingPaper1 = new RollingPaper(null, "title1", "rollingPaper1", 1L, member);
RollingPaper rollingPaper2 = new RollingPaper(null, "title2", "rollingPaper2", 1L, member);
RollingPaper rollingPaper3 = new RollingPaper(null, "title3", "rollingPaper3", 1L, member);

entityManager.persist(user);
entityManager.persist(group);
entityManager.persist(member);
entityManager.persist(rollingPaper1);
entityManager.persist(rollingPaper2);
entityManager.persist(rollingPaper3);

// when
List<RollingPaperResponse> response = rollingPaperService.findReceivedRollingPapers(
user.getId(),
group.getId(),
rollingPaper3.getId() + 1, 10);

// then
List<RollingPaperResponse> expectedResponse = List.of(
RollingPaperResponse.from(rollingPaper3),
RollingPaperResponse.from(rollingPaper2),
RollingPaperResponse.from(rollingPaper1)
);

assertThat(response).usingRecursiveComparison()
.isEqualTo(expectedResponse);
}
}

0 comments on commit 6b294da

Please sign in to comment.