Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat]: 유저 닉네임 조회 API 구현 #57

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package io.sobok.SobokSobok.auth.application;

import io.sobok.SobokSobok.auth.application.util.UserServiceUtil;
import io.sobok.SobokSobok.auth.domain.User;
import io.sobok.SobokSobok.auth.infrastructure.UserRepository;
import io.sobok.SobokSobok.auth.ui.dto.UsernameResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -17,4 +22,24 @@ public Boolean duplicateNickname(String username) {

return userRepository.existsByUsername(username);
}

@Transactional
public List<UsernameResponse> getUsername(Long userId, String username) {
UserServiceUtil.existsUserById(userRepository, userId);

Optional<User> optionalMember = userRepository.findByUsername(username);

List<UsernameResponse> result = new ArrayList<>();

optionalMember.ifPresent(
member -> result.add(UsernameResponse.builder()
.memberId(member.getId())
.memberName(member.getUsername())
.deviceOS(member.getSocialInfo().getSocialType())
.selfCheck(member.getId().equals(userId))
.build())
);

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,6 @@ public interface UserRepository extends JpaRepository<User, Long> {
Boolean existsBySocialInfoSocialId(String socialId);

Boolean existsByUsername(String username);

Optional<User> findByUsername(String username);
}
21 changes: 21 additions & 0 deletions src/main/java/io/sobok/SobokSobok/auth/ui/UserController.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package io.sobok.SobokSobok.auth.ui;

import io.sobok.SobokSobok.auth.application.UserService;
import io.sobok.SobokSobok.auth.domain.User;
import io.sobok.SobokSobok.auth.ui.dto.UsernameResponse;
import io.sobok.SobokSobok.common.dto.ApiResponse;
import io.sobok.SobokSobok.exception.SuccessCode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@RestController
Expand All @@ -32,4 +36,21 @@ public ResponseEntity<ApiResponse<Boolean>> isNicknameDuplicate(@RequestParam fi
userService.duplicateNickname(username)
));
}

@GetMapping("/search")
@Operation(
summary = "유저 닉네임 조회 API 메서드",
description = "공유 멤버(유저) 닉네임을 조회하는 메서드입니다."
)
public ResponseEntity<ApiResponse<List<UsernameResponse>>> getUsername(
@AuthenticationPrincipal User user,
@RequestParam final String username
) {
return ResponseEntity
.status(HttpStatus.OK)
.body(ApiResponse.success(
SuccessCode.GET_USERNAME_SUCCESS,
userService.getUsername(user.getId(), username)
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.sobok.SobokSobok.auth.ui.dto;

import io.sobok.SobokSobok.auth.domain.SocialType;
import lombok.Builder;

@Builder
public record UsernameResponse(
Long memberId,
String memberName,
SocialType deviceOS,
Boolean selfCheck
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public enum SuccessCode {

// user
NICKNAME_CHECK_SUCCESS(HttpStatus.OK, "닉네임 중복 확인에 성공했습니다."),
GET_USERNAME_SUCCESS(HttpStatus.OK, "유저 이름 조회에 성공했습니다."),

// pill
ADD_PILL_SUCCESS(HttpStatus.CREATED, "약 추가에 성공했습니다."),
Expand Down