Skip to content

Commit

Permalink
feat: 유저 프로필 조회기능추가
Browse files Browse the repository at this point in the history
  • Loading branch information
SangWoon123 committed May 9, 2024
1 parent 206f7f3 commit 4dcb103
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,17 @@

import com.tukorea.planding.common.CommonResponse;
import com.tukorea.planding.common.CommonUtils;
import com.tukorea.planding.domain.group.dto.response.GroupFavoriteResponse;
import com.tukorea.planding.domain.group.entity.GroupRoom;
import com.tukorea.planding.domain.schedule.dto.ScheduleRequest;
import com.tukorea.planding.domain.schedule.dto.ScheduleResponse;
import com.tukorea.planding.domain.user.dto.AndroidLoginRequest;
import com.tukorea.planding.domain.user.dto.AndroidLoginResponse;
import com.tukorea.planding.domain.user.dto.ProfileResponse;
import com.tukorea.planding.domain.user.dto.UserInfo;
import com.tukorea.planding.domain.user.entity.User;
import com.tukorea.planding.domain.user.service.AndroidLoginService;
import com.tukorea.planding.domain.user.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
Expand All @@ -14,13 +21,16 @@
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "User", description = "회원 API 문서")
@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
public class UserController {

private final AndroidLoginService androidLoginService;
private final UserService userService;

@Operation(summary = "유저 정보 가져오기")
@GetMapping("/userInfo")
Expand All @@ -34,4 +44,24 @@ public CommonResponse<AndroidLoginResponse> signupApp(@RequestBody AndroidLoginR
AndroidLoginResponse response = androidLoginService.signupApp(androidLoginRequest);
return CommonUtils.success(response);
}

@Operation(summary = "프로필 가져오기", description = "즐겨찾는 그룹, 그룹요청")
@GetMapping("/profile")
public CommonResponse<ProfileResponse> getProfile(@AuthenticationPrincipal UserInfo userInfo) {
ProfileResponse profile = userService.getProfile(userInfo);
return CommonUtils.success(profile);
}

@Operation(summary = "즐겨찾기 그룹 조회")
@GetMapping()
public CommonResponse<List<GroupRoom>> searchFavorite(@AuthenticationPrincipal UserInfo userInfo) {
return CommonUtils.success(userService.findFavoriteGroupsByUserId(userInfo));
}

@Operation(summary = "오늘 스케줄 조회")
@GetMapping("/today")
public CommonResponse<List<ScheduleResponse>> showTodaySchedule(@AuthenticationPrincipal UserInfo userInfo) {
List<ScheduleResponse> responses = userService.showTodaySchedule(userInfo);
return CommonUtils.success(responses);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.tukorea.planding.domain.user.dto;

import lombok.Builder;

@Builder
public record ProfileResponse(
Long groupFavorite,
Long groupRequest
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.tukorea.planding.domain.group.entity.GroupFavorite;
import com.tukorea.planding.domain.group.entity.UserGroup;
import com.tukorea.planding.domain.notify.entity.Notification;
import com.tukorea.planding.domain.schedule.entity.Schedule;
import com.tukorea.planding.global.audit.BaseEntity;
import com.tukorea.planding.global.oauth.details.Role;
import com.tukorea.planding.domain.schedule.entity.Schedule;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.tukorea.planding.domain.user.repository;

import org.springframework.stereotype.Repository;

@Repository
public interface UserCustomRepository {
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.tukorea.planding.domain.user.entity.SocialType;import com.tukorea.planding.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;
import java.util.Optional;

public interface UserRepository extends JpaRepository<User,Long> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
import com.tukorea.planding.global.error.BusinessException;
import com.tukorea.planding.global.error.ErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class UserQueryService {
private final UserRepository userRepository;
Expand All @@ -17,13 +22,8 @@ public User save(User user) {
return userRepository.save(user);
}

public User getByUserInfo(String userCode) {
public User getUserByUserCode(String userCode) {
return userRepository.findByUserCode(userCode)
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
}

public User getUserByUserCode(UserInfo userInfo) {
return userRepository.findByUserCode(userInfo.getUserCode())
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
package com.tukorea.planding.domain.user.service;

import com.tukorea.planding.domain.group.dto.response.GroupFavoriteResponse;
import com.tukorea.planding.domain.group.entity.GroupFavorite;
import com.tukorea.planding.domain.group.entity.GroupRoom;
import com.tukorea.planding.domain.group.entity.InviteStatus;
import com.tukorea.planding.domain.group.service.query.GroupFavoriteQueryService;
import com.tukorea.planding.domain.group.service.query.GroupInviteQueryService;
import com.tukorea.planding.domain.schedule.dto.ScheduleResponse;
import com.tukorea.planding.domain.schedule.entity.Schedule;
import com.tukorea.planding.domain.schedule.service.ScheduleQueryService;
import com.tukorea.planding.domain.user.dto.AndroidLoginRequest;
import com.tukorea.planding.domain.user.dto.ProfileResponse;
import com.tukorea.planding.domain.user.dto.UserInfo;
import com.tukorea.planding.domain.user.entity.SocialType;
import com.tukorea.planding.domain.user.entity.User;
import com.tukorea.planding.domain.user.repository.UserRepository;
import com.tukorea.planding.global.error.BusinessException;
import com.tukorea.planding.global.error.ErrorCode;
import com.tukorea.planding.global.oauth.details.Role;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class UserService {

private final UserRepository userRepository;
private final UserQueryService userQueryService;
private final ScheduleQueryService scheduleQueryService;
private final GroupInviteQueryService groupInviteQueryService;
private final GroupFavoriteQueryService groupFavoriteQueryService;

@Transactional(readOnly = true)
public List<GroupRoom> findFavoriteGroupsByUserId(UserInfo userInfo) {
User user = userQueryService.getUserByUserCode(userInfo.getUserCode());
// 즐겨찾기된 그룹 목록을 반환
return user.getGroupFavorites().stream()
.map(GroupFavorite::getGroupRoom) // GroupFavorite 엔티티에서 GroupRoom을 가져오는 메서드 가정
.collect(Collectors.toList());
}

public User createUserFromRequest(AndroidLoginRequest androidLoginRequest) {
User user = User.builder()
Expand All @@ -28,14 +52,24 @@ public User createUserFromRequest(AndroidLoginRequest androidLoginRequest) {
.role(Role.USER)
.build();

return userRepository.save(user);
return userQueryService.save(user);
}

//TODO 즐겨찾는 그룹, 그룹 요청
public ProfileResponse getProfile(UserInfo userInfo) {
Long groupInvite = groupInviteQueryService.countInvitation(userInfo.getUserCode(), InviteStatus.PENDING);
Long groupFavorite = groupFavoriteQueryService.countMyFavoriteGroup(userInfo.getUserCode());
return ProfileResponse.builder()
.groupRequest(groupInvite)
.groupFavorite(groupFavorite)
.build();
}

/*
DB 접근 메서드
*/
public User getByUserInfo(String userCode) {
return userRepository.findByUserCode(userCode)
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
public List<ScheduleResponse> showTodaySchedule(UserInfo userInfo) {
User user = userQueryService.getUserByUserCode(userInfo.getUserCode());
List<Schedule> schedules = scheduleQueryService.showTodaySchedule(user.getId());
return schedules.stream()
.map(ScheduleResponse::from)
.collect(Collectors.toList());
}
}

0 comments on commit 4dcb103

Please sign in to comment.