diff --git a/src/main/java/com/example/swiftgathering_server/controller/FriendRequestController.java b/src/main/java/com/example/swiftgathering_server/controller/FriendRequestController.java index 364a2ce..497de8d 100644 --- a/src/main/java/com/example/swiftgathering_server/controller/FriendRequestController.java +++ b/src/main/java/com/example/swiftgathering_server/controller/FriendRequestController.java @@ -1,6 +1,6 @@ package com.example.swiftgathering_server.controller; -import com.example.swiftgathering_server.dto.FriendRequestInputDto; +import com.example.swiftgathering_server.dto.FriendRequestCreateDto; import com.example.swiftgathering_server.dto.FriendRequestOutputDto; import com.example.swiftgathering_server.dto.FriendRequestUpdateDto; import com.example.swiftgathering_server.service.FriendRequestService; @@ -18,8 +18,8 @@ public class FriendRequestController { private final FriendRequestService friendRequestService; @PostMapping - public ResponseEntity sendFriendRequest(@PathVariable Long memberId, @RequestBody FriendRequestInputDto friendRequestDto) { - friendRequestService.sendFriendRequest(memberId, friendRequestDto); + public ResponseEntity sendFriendRequest(@PathVariable Long memberId, @RequestBody FriendRequestCreateDto friendRequestCreateDto) { + friendRequestService.sendFriendRequest(memberId, friendRequestCreateDto); return ResponseEntity.ok().build(); } diff --git a/src/main/java/com/example/swiftgathering_server/dto/FriendRequestInputDto.java b/src/main/java/com/example/swiftgathering_server/dto/FriendRequestCreateDto.java similarity index 54% rename from src/main/java/com/example/swiftgathering_server/dto/FriendRequestInputDto.java rename to src/main/java/com/example/swiftgathering_server/dto/FriendRequestCreateDto.java index 8ecc782..a63e032 100644 --- a/src/main/java/com/example/swiftgathering_server/dto/FriendRequestInputDto.java +++ b/src/main/java/com/example/swiftgathering_server/dto/FriendRequestCreateDto.java @@ -1,8 +1,10 @@ package com.example.swiftgathering_server.dto; +import lombok.AllArgsConstructor; import lombok.Getter; @Getter -public class FriendRequestInputDto { +@AllArgsConstructor +public class FriendRequestCreateDto { private Long receiverId; } diff --git a/src/main/java/com/example/swiftgathering_server/dto/FriendRequestUpdateDto.java b/src/main/java/com/example/swiftgathering_server/dto/FriendRequestUpdateDto.java index dcb7f02..9fd6d25 100644 --- a/src/main/java/com/example/swiftgathering_server/dto/FriendRequestUpdateDto.java +++ b/src/main/java/com/example/swiftgathering_server/dto/FriendRequestUpdateDto.java @@ -1,8 +1,10 @@ package com.example.swiftgathering_server.dto; +import lombok.AllArgsConstructor; import lombok.Getter; @Getter +@AllArgsConstructor public class FriendRequestUpdateDto { private Long requestId; private Boolean isAccepted; diff --git a/src/main/java/com/example/swiftgathering_server/dto/RegisterDto.java b/src/main/java/com/example/swiftgathering_server/dto/RegisterDto.java index e95d370..63c00e1 100644 --- a/src/main/java/com/example/swiftgathering_server/dto/RegisterDto.java +++ b/src/main/java/com/example/swiftgathering_server/dto/RegisterDto.java @@ -1,10 +1,12 @@ package com.example.swiftgathering_server.dto; +import lombok.AllArgsConstructor; import lombok.Getter; @Getter +@AllArgsConstructor public class RegisterDto { - private String loginId; + private String loginUsername; private String loginPassword; private String name; } diff --git a/src/main/java/com/example/swiftgathering_server/dto/ResignDto.java b/src/main/java/com/example/swiftgathering_server/dto/ResignDto.java index 2865961..40b5959 100644 --- a/src/main/java/com/example/swiftgathering_server/dto/ResignDto.java +++ b/src/main/java/com/example/swiftgathering_server/dto/ResignDto.java @@ -4,6 +4,6 @@ @Getter public class ResignDto { - private String loginId; + private String loginUsername; private String loginPassword; } \ No newline at end of file diff --git a/src/main/java/com/example/swiftgathering_server/repository/MemberRepository.java b/src/main/java/com/example/swiftgathering_server/repository/MemberRepository.java index 2763c0c..82c2f02 100644 --- a/src/main/java/com/example/swiftgathering_server/repository/MemberRepository.java +++ b/src/main/java/com/example/swiftgathering_server/repository/MemberRepository.java @@ -23,9 +23,9 @@ public Optional findOne(Long id) { return Optional.ofNullable(member); } - public Optional findByLoginId(String loginId) { - return em.createQuery("select m from Member m where m.loginUsername = :loginId", Member.class) - .setParameter("loginId", loginId) + public Optional findByLoginUsername(String loginUsername) { + return em.createQuery("select m from Member m where m.loginUsername = :loginUsername", Member.class) + .setParameter("loginUsername", loginUsername) .getResultList() .stream() .findAny(); diff --git a/src/main/java/com/example/swiftgathering_server/service/AuthService.java b/src/main/java/com/example/swiftgathering_server/service/AuthService.java index e025254..446606a 100644 --- a/src/main/java/com/example/swiftgathering_server/service/AuthService.java +++ b/src/main/java/com/example/swiftgathering_server/service/AuthService.java @@ -11,8 +11,10 @@ import org.springframework.security.core.Authentication; import org.springframework.security.core.AuthenticationException; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; @Service +@Transactional @RequiredArgsConstructor public class AuthService { private final MemberRepository memberRepository; @@ -33,7 +35,7 @@ public LoginOutputDto authenticateAndGenerateToken(LoginInputDto loginInputDto) String token = jwtUtil.createJwt(username, role, 60 * 60 * 1000L); - Long memberId = memberRepository.findByLoginId(username) + Long memberId = memberRepository.findByLoginUsername(username) .orElseThrow(() -> new RuntimeException("Invalid login ID or password.")) .getId(); diff --git a/src/main/java/com/example/swiftgathering_server/service/CustomUserDetailsService.java b/src/main/java/com/example/swiftgathering_server/service/CustomUserDetailsService.java index ee8cde5..5f383f4 100644 --- a/src/main/java/com/example/swiftgathering_server/service/CustomUserDetailsService.java +++ b/src/main/java/com/example/swiftgathering_server/service/CustomUserDetailsService.java @@ -9,8 +9,10 @@ import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; @Service +@Transactional @RequiredArgsConstructor public class CustomUserDetailsService implements UserDetailsService { @@ -18,7 +20,7 @@ public class CustomUserDetailsService implements UserDetailsService { @Override public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { - Member member = memberRepository.findByLoginId(username) + Member member = memberRepository.findByLoginUsername(username) .orElseThrow(() -> new AuthenticationException("Invalid login ID or password.")); return new CustomUserDetails(member); } diff --git a/src/main/java/com/example/swiftgathering_server/service/FriendRequestService.java b/src/main/java/com/example/swiftgathering_server/service/FriendRequestService.java index 67d96eb..9b538d9 100644 --- a/src/main/java/com/example/swiftgathering_server/service/FriendRequestService.java +++ b/src/main/java/com/example/swiftgathering_server/service/FriendRequestService.java @@ -3,7 +3,7 @@ import com.example.swiftgathering_server.domain.FriendRequest; import com.example.swiftgathering_server.domain.Friendship; import com.example.swiftgathering_server.domain.Member; -import com.example.swiftgathering_server.dto.FriendRequestInputDto; +import com.example.swiftgathering_server.dto.FriendRequestCreateDto; import com.example.swiftgathering_server.dto.FriendRequestOutputDto; import com.example.swiftgathering_server.dto.FriendRequestUpdateDto; import com.example.swiftgathering_server.repository.FriendRequestRepository; @@ -25,8 +25,8 @@ public class FriendRequestService { private final MemberRepository memberRepository; private final FriendshipRepository friendshipRepository; - public void sendFriendRequest(Long memberId, FriendRequestInputDto friendRequestDto) { - Long receiverId = friendRequestDto.getReceiverId(); + public Long sendFriendRequest(Long memberId, FriendRequestCreateDto friendRequestCreateDto) { + Long receiverId = friendRequestCreateDto.getReceiverId(); Member sender = memberRepository.findOne(memberId) .orElseThrow(() -> new IllegalArgumentException("No member found with ID: " + memberId)); Member receiver = memberRepository.findOne(receiverId) @@ -36,7 +36,7 @@ public void sendFriendRequest(Long memberId, FriendRequestInputDto friendRequest .receiverMember(receiver) .requestStatus(FriendRequest.RequestStatus.PENDING) .build(); - friendRequestRepository.save(friendRequest); + return friendRequestRepository.save(friendRequest); } @Transactional(readOnly = true) diff --git a/src/main/java/com/example/swiftgathering_server/service/GatheringSessionService.java b/src/main/java/com/example/swiftgathering_server/service/GatheringSessionService.java index 967c402..a7400ba 100644 --- a/src/main/java/com/example/swiftgathering_server/service/GatheringSessionService.java +++ b/src/main/java/com/example/swiftgathering_server/service/GatheringSessionService.java @@ -30,7 +30,6 @@ public class GatheringSessionService { private final GatheringSessionRepository gatheringSessionRepository; private final FlagLocationRepository flagLocationRepository; -// private final RabbitAdmin rabbitAdmin; private final AmqpTemplate amqpTemplate; public void createSession(CreateSessionRequestDto requestDto) { diff --git a/src/main/java/com/example/swiftgathering_server/service/MemberService.java b/src/main/java/com/example/swiftgathering_server/service/MemberService.java index acde41c..bb5d1db 100644 --- a/src/main/java/com/example/swiftgathering_server/service/MemberService.java +++ b/src/main/java/com/example/swiftgathering_server/service/MemberService.java @@ -1,8 +1,6 @@ package com.example.swiftgathering_server.service; import com.example.swiftgathering_server.domain.Member; -import com.example.swiftgathering_server.dto.LoginInputDto; -import com.example.swiftgathering_server.dto.MyInfoDto; import com.example.swiftgathering_server.dto.RegisterDto; import com.example.swiftgathering_server.dto.ResignDto; import com.example.swiftgathering_server.repository.MemberRepository; @@ -22,14 +20,14 @@ public class MemberService { private final BCryptPasswordEncoder bCryptPasswordEncoder; public Long register(RegisterDto registerDto) { - memberRepository.findByLoginId(registerDto.getLoginId()) + memberRepository.findByLoginUsername(registerDto.getLoginUsername()) .ifPresent(m -> { - throw new EntityExistsException("Login ID already in use: " + registerDto.getLoginId()); + throw new EntityExistsException("Login ID already in use: " + registerDto.getLoginUsername()); }); String encodedPassword = bCryptPasswordEncoder.encode(registerDto.getLoginPassword()); Member member = Member.builder() - .loginUsername(registerDto.getLoginId()) + .loginUsername(registerDto.getLoginUsername()) .loginPassword(encodedPassword) .name(registerDto.getName()) .build(); @@ -37,20 +35,11 @@ public Long register(RegisterDto registerDto) { } public void resign(ResignDto resignDto) { - Member member = memberRepository.findByLoginId(resignDto.getLoginId()) + Member member = memberRepository.findByLoginUsername(resignDto.getLoginUsername()) .orElseThrow(() -> new AuthenticationException("Invalid login ID or password.")); if (!bCryptPasswordEncoder.matches(resignDto.getLoginPassword(), member.getLoginPassword())) { throw new AuthenticationException("Invalid login ID or password."); } memberRepository.remove(member); } - - public MyInfoDto verify(LoginInputDto loginDto) { - Member member = memberRepository.findByLoginId(loginDto.getLoginUsername()) - .orElseThrow(() -> new AuthenticationException("Invalid login ID or password.")); - if (!bCryptPasswordEncoder.matches(loginDto.getLoginPassword(), member.getLoginPassword())) { - throw new AuthenticationException("Invalid login ID or password."); - } - return new MyInfoDto(member.getId()); - } } \ No newline at end of file diff --git a/src/test/java/com/example/swiftgathering_server/FriendshipTest.java b/src/test/java/com/example/swiftgathering_server/FriendshipTest.java index 5c765e1..e358fba 100644 --- a/src/test/java/com/example/swiftgathering_server/FriendshipTest.java +++ b/src/test/java/com/example/swiftgathering_server/FriendshipTest.java @@ -2,8 +2,15 @@ import com.example.swiftgathering_server.domain.Friendship; import com.example.swiftgathering_server.domain.Member; +import com.example.swiftgathering_server.dto.FriendInfoDto; +import com.example.swiftgathering_server.dto.FriendRequestCreateDto; +import com.example.swiftgathering_server.dto.FriendRequestUpdateDto; +import com.example.swiftgathering_server.dto.RegisterDto; import com.example.swiftgathering_server.repository.FriendshipRepository; import com.example.swiftgathering_server.repository.MemberRepository; +import com.example.swiftgathering_server.service.FriendRequestService; +import com.example.swiftgathering_server.service.FriendshipService; +import com.example.swiftgathering_server.service.MemberService; import jakarta.persistence.EntityManager; import jakarta.transaction.Transactional; import org.junit.jupiter.api.Assertions; @@ -13,7 +20,10 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit.jupiter.SpringExtension; +import java.lang.reflect.Array; +import java.util.Arrays; import java.util.List; +import java.util.stream.Collectors; @ExtendWith(SpringExtension.class) @SpringBootTest @@ -21,10 +31,13 @@ public class FriendshipTest { @Autowired - private FriendshipRepository friendshipRepository; + private MemberService memberService; @Autowired - private MemberRepository memberRepository; + private FriendRequestService friendRequestService; + + @Autowired + private FriendshipService friendshipService; @Autowired private EntityManager em; @@ -32,45 +45,29 @@ public class FriendshipTest { @Test public void 친구목록() { // given - Member member1 = Member.builder() - .loginId("111") - .loginPassword("11111") - .name("firsty") - .build(); - Member member2 = Member.builder() - .loginId("222") - .loginPassword("22222") - .name("secondy") - .build(); - Member member3 = Member.builder() - .loginId("333") - .loginPassword("33333") - .name("thirdy") - .build(); - Long member1Id = memberRepository.save(member1); - Long member2Id = memberRepository.save(member2); - Long member3Id = memberRepository.save(member3); + RegisterDto registerDto0 = new RegisterDto("member0", "asdasdf", "mem0"); + Long member0Id = memberService.register(registerDto0); + RegisterDto registerDto1 = new RegisterDto("member1", "asdasdf", "mem1"); + Long member1Id = memberService.register(registerDto1); + RegisterDto registerDto2 = new RegisterDto("member2", "asdasdf", "mem2"); + Long member2Id = memberService.register(registerDto2); em.flush(); // when - Friendship friendshipOf1And2 = Friendship.builder() - .olderMember(member1) - .youngerMember(member2) - .build(); - Friendship friendshipOf2And3 = Friendship.builder() - .olderMember(member2) - .youngerMember(member3) - .build(); - friendshipRepository.save(friendshipOf1And2); - friendshipRepository.save(friendshipOf2And3); - em.flush(); + FriendRequestCreateDto friendRequestCreateDto0to1 = new FriendRequestCreateDto(member1Id); + Long request0to1Id = friendRequestService.sendFriendRequest(member0Id, friendRequestCreateDto0to1); + FriendRequestCreateDto friendRequestCreateDto0to2 = new FriendRequestCreateDto(member2Id); + Long request0to2Id = friendRequestService.sendFriendRequest(member0Id, friendRequestCreateDto0to2); + + FriendRequestUpdateDto friendRequestUpdateDto1to0 = new FriendRequestUpdateDto(request0to1Id, true); + friendRequestService.updateFriendRequestStatus(member1Id, friendRequestUpdateDto1to0); + FriendRequestUpdateDto friendRequestUpdateDto2to0 = new FriendRequestUpdateDto(request0to2Id, false); + friendRequestService.updateFriendRequestStatus(member2Id, friendRequestUpdateDto2to0); // then - List fetchedFriends = friendshipRepository.findAllFriendsOfUser(member2) - .stream() - .map(Member::getId) - .toList(); - List expectedFriends = List.of(member1Id, member3Id); - Assertions.assertArrayEquals(expectedFriends.toArray(), fetchedFriends.toArray()); + List resultIds = friendshipService.findAllFriendsOfUser(member0Id) + .stream().map(FriendInfoDto::getId).collect(Collectors.toList()); + List expectedIds = Arrays.asList(new Long[]{member1Id}); + Assertions.assertEquals(expectedIds, resultIds); } }