Skip to content

Commit

Permalink
Merge pull request #42 from Modagbul/feat/마이페이지
Browse files Browse the repository at this point in the history
feat: 마이페이지 조회 및 소모임 생성 에러 처리 추가
  • Loading branch information
minsu20 authored Oct 22, 2023
2 parents f4cdb94 + d413ef9 commit b40fc75
Show file tree
Hide file tree
Showing 17 changed files with 266 additions and 22 deletions.
4 changes: 4 additions & 0 deletions src/docs/asciidoc/Mypage-API.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ operation::mypage-controller-test/sign_out[snippets='http-request,http-response,
=== Mypage 회원탈퇴
operation::mypage-controller-test/withdraw[snippets='http-request,request-fields,http-response,response-fields']

[[Mypage-전체-조회]]
=== Mypage 전체 조회
operation::mypage-controller-test/get_mypage[snippets='http-request,http-response,response-fields']

[[Mypage-프로필-조회]]
=== Mypage 프로필 조회
operation::mypage-controller-test/get_profile[snippets='http-request,http-response,response-fields']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,18 @@ public GetAllBoardResponse findBoardAll(Long teamId, Long memberId) {
public Integer findUnReadBoardNum(Long teamId, Long memberId) {
// 전체 게시글 수
Long allBoards = queryFactory
.selectFrom(board)
.select(board.count())
.from(board)
.where(board.team.teamId.eq(teamId))
.fetchCount();
.fetchFirst();

// 멤버가 읽은 게시글 수
Long readBoards = queryFactory
.selectFrom(boardRead)
.select(boardRead.count())
.from(boardRead)
.where(boardRead.member.memberId.eq(memberId))
.where(boardRead.board.team.teamId.eq(teamId))
.fetchCount();
.fetchFirst();

return Math.toIntExact(allBoards - readBoards);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.moing.backend.domain.mypage.application.dto.response;

import com.moing.backend.domain.member.domain.entity.Member;
import com.moing.backend.domain.team.domain.constant.Category;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.List;

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
public class GetMyPageResponse {
private String profileImage;
private String nickName;
private String introduction;
private List<Category> categories=new ArrayList<>();
private List<GetMyPageTeamBlock> getMyPageTeamBlocks = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.moing.backend.domain.mypage.application.dto.response;

import com.moing.backend.domain.team.domain.constant.Category;
import lombok.*;

@Builder
@AllArgsConstructor
@NoArgsConstructor
@Getter
public class GetMyPageTeamBlock {
private Long teamId;
private String teamName;
private Category category;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.moing.backend.domain.mypage.application.mapper;

import com.moing.backend.domain.member.domain.entity.Member;
import com.moing.backend.domain.mypage.application.dto.response.GetMyPageResponse;
import com.moing.backend.domain.mypage.application.dto.response.GetMyPageTeamBlock;
import com.moing.backend.domain.team.domain.constant.Category;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class MyPageMapper {

public GetMyPageResponse toGetMyPageResponse(Member member, List<Category> categories, List<GetMyPageTeamBlock> blocks) {
return GetMyPageResponse.builder()
.profileImage(member.getProfileImage())
.nickName(member.getNickName())
.introduction(member.getIntroduction())
.categories(categories)
.getMyPageTeamBlocks(blocks)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.moing.backend.domain.mypage.application.service;

import com.moing.backend.domain.member.domain.entity.Member;
import com.moing.backend.domain.member.domain.service.MemberGetService;
import com.moing.backend.domain.mypage.application.dto.response.GetMyPageResponse;
import com.moing.backend.domain.mypage.application.dto.response.GetMyPageTeamBlock;
import com.moing.backend.domain.mypage.application.mapper.MyPageMapper;
import com.moing.backend.domain.team.domain.constant.Category;
import com.moing.backend.domain.team.domain.service.TeamGetService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
@RequiredArgsConstructor
public class GetMyPageUserCase {

private final MemberGetService memberGetService;
private final TeamGetService teamGetService;
private final MyPageMapper myPageMapper;

public GetMyPageResponse getMyPageResponse(String socialId) {
Member member = memberGetService.getMemberBySocialId(socialId);
List<GetMyPageTeamBlock> getMyPageTeamBlocks = teamGetService.getMyPageTeamBlockByMemberId(member.getMemberId());
return myPageMapper.toGetMyPageResponse(member, calculateCategory(getMyPageTeamBlocks), getMyPageTeamBlocks);
}

private List<Category> calculateCategory(List<GetMyPageTeamBlock> getMyPageTeamBlocks) {
return getMyPageTeamBlocks.stream()
.map(GetMyPageTeamBlock::getCategory)
.distinct()
.limit(2)
.collect(Collectors.toList());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import com.moing.backend.domain.mypage.application.dto.request.UpdateProfileRequest;
import com.moing.backend.domain.mypage.application.dto.request.WithdrawRequest;
import com.moing.backend.domain.mypage.application.dto.response.GetAlarmResponse;
import com.moing.backend.domain.mypage.application.dto.response.GetMyPageResponse;
import com.moing.backend.domain.mypage.application.dto.response.GetProfileResponse;
import com.moing.backend.domain.mypage.application.service.AlarmUserCase;
import com.moing.backend.domain.mypage.application.service.ProfileUserCase;
import com.moing.backend.domain.mypage.application.service.SignOutUserCase;
import com.moing.backend.domain.mypage.application.service.WithdrawUserCase;
import com.moing.backend.domain.mypage.application.service.*;
import com.moing.backend.global.config.security.dto.User;
import com.moing.backend.global.response.SuccessResponse;
import lombok.AllArgsConstructor;
Expand All @@ -28,8 +26,9 @@ public class MyPageController {
private final WithdrawUserCase withdrawService;
private final ProfileUserCase profileUserCase;
private final AlarmUserCase alarmUserCase;
private final GetMyPageUserCase getMyPageUserCase;

//TODO 마이페이지 조회, 알림설정 수정
//TODO 알림설정 수정

/**
* 로그아웃
Expand All @@ -54,6 +53,16 @@ public ResponseEntity<SuccessResponse> withdraw(@AuthenticationPrincipal User us
return ResponseEntity.ok(SuccessResponse.create(WITHDRAWAL_SUCCESS.getMessage()));
}

/**
* 마이페이지 조회
* [GET] api/mypage
* 작성자: 김민수
*/
@GetMapping
public ResponseEntity<SuccessResponse<GetMyPageResponse>> getMyPage(@AuthenticationPrincipal User user){
return ResponseEntity.ok(SuccessResponse.create(GET_MYPAGE_SUCCESS.getMessage(), this.getMyPageUserCase.getMyPageResponse(user.getSocialId())));
}

/**
* 프로필 조회
* [GET] api/mypage/profile
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
public enum MypageResponseMessage {
SIGN_OUT_SUCCESS("로그아웃을 했습니다"),
WITHDRAWAL_SUCCESS("회원탈퇴를 했습니다"),
GET_MYPAGE_SUCCESS("마이페이지를 조회했습니다"),
GET_PROFILE_SUCCESS("프로필을 조회했습니다"),
UPDATE_PROFILE_SUCCESS("프로필을 수정했습니다"),
GET_ALARM_SUCCESS("알람 정보를 조회했습니다"),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.moing.backend.domain.team.domain.repository;

import com.moing.backend.domain.mypage.application.dto.response.GetMyPageTeamBlock;
import com.moing.backend.domain.team.application.dto.response.GetTeamDetailResponse;
import com.moing.backend.domain.team.application.dto.response.GetTeamResponse;
import com.moing.backend.domain.team.domain.entity.Team;
Expand All @@ -11,4 +12,5 @@ public interface TeamCustomRepository {
GetTeamResponse findTeamByMemberId(Long memberId);
Optional<Team> findTeamByTeamId(Long TeamId);
List<Long> findTeamIdByMemberId(Long memberId);
List<GetMyPageTeamBlock> findMyPageTeamByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.moing.backend.domain.team.domain.repository;

import com.moing.backend.domain.member.domain.entity.QMember;
import com.moing.backend.domain.team.application.dto.response.*;
import com.moing.backend.domain.mypage.application.dto.response.GetMyPageTeamBlock;
import com.moing.backend.domain.team.application.dto.response.GetTeamResponse;
import com.moing.backend.domain.team.application.dto.response.QTeamBlock;
import com.moing.backend.domain.team.application.dto.response.TeamBlock;
import com.moing.backend.domain.team.domain.constant.ApprovalStatus;
import com.moing.backend.domain.team.domain.entity.Team;
import com.querydsl.jpa.JPAExpressions;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;

import javax.persistence.EntityManager;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -55,10 +56,25 @@ public List<Long> findTeamIdByMemberId(Long memberId){
.where(teamMember.isDeleted.eq(false)) // 탈퇴하지 않았다면
.where(team.isDeleted.eq(false) // 강제종료되지 않았거나
.or(team.deletionTime.after(threeDaysAgo))) // 강제종료된 경우 3일이 지나지 않았다면
.groupBy(team.teamId)
.orderBy(team.approvalTime.asc())
.fetch();
}

@Override
public List<GetMyPageTeamBlock> findMyPageTeamByMemberId(Long memberId) {
return queryFactory
.select(Projections.constructor(GetMyPageTeamBlock.class,
team.teamId, team.name, team.category))
.from(teamMember)
.innerJoin(teamMember.team, team)
.on(teamMember.member.memberId.eq(memberId))
.where(team.approvalStatus.eq(ApprovalStatus.APPROVAL)) // 승인 되었고
.orderBy(team.missions.size().desc())
.groupBy(team.teamId)
.fetch();
}

private List<TeamBlock> getTeamBlock(Long memberId) {
LocalDateTime threeDaysAgo = LocalDateTime.now().minusDays(3);

Expand All @@ -78,6 +94,7 @@ private List<TeamBlock> getTeamBlock(Long memberId) {
.or(team.deletionTime.isNotNull()
.or(team.deletionTime.after(threeDaysAgo)))) // 강제종료된 경우 3일이 지나지 않았다면
.orderBy(team.approvalTime.asc())
.groupBy(team.teamId)
.fetch();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.moing.backend.domain.team.domain.service;

import com.moing.backend.domain.member.domain.entity.Member;
import com.moing.backend.domain.mypage.application.dto.response.GetMyPageTeamBlock;
import com.moing.backend.domain.team.application.dto.response.GetTeamResponse;
import com.moing.backend.domain.team.domain.entity.Team;
import com.moing.backend.domain.team.domain.repository.TeamRepository;
Expand Down Expand Up @@ -28,4 +29,8 @@ public List<Long> getTeamIdByMemberId(Long memberId) {
public Team getTeamByTeamId(Long teamId){
return teamRepository.findTeamByTeamId(teamId).orElseThrow(()->new NotFoundByTeamIdException());
}

public List<GetMyPageTeamBlock> getMyPageTeamBlockByMemberId(Long memberId){
return teamRepository.findMyPageTeamByMemberId(memberId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.moing.backend.domain.team.exception;

import com.moing.backend.global.response.ErrorCode;
import org.springframework.http.HttpStatus;

public class AlreadyJoinTeamException extends TeamException{
public AlreadyJoinTeamException(){
super(ErrorCode.ALREADY_JOIN_ERROR,
HttpStatus.UNAUTHORIZED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.moing.backend.domain.team.exception;

import com.moing.backend.global.response.ErrorCode;
import org.springframework.http.HttpStatus;

public class AlreadyWithdrawTeamException extends TeamException{
public AlreadyWithdrawTeamException(){
super(ErrorCode.ALREADY_WITHDRAW_ERROR,
HttpStatus.NOT_FOUND);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public List<TeamMemberInfo> findTeamMemberInfoByTeamId(Long teamId){
.innerJoin(teamMember.team, team) // innerJoin을 사용하여 최적화
.where(teamMember.team.teamId.eq(teamId) // where 절을 하나로 합침
.and(teamMember.isDeleted.eq(false)))
.groupBy(teamMember.member.memberId)
.fetch();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,36 @@

import com.moing.backend.domain.member.domain.entity.Member;
import com.moing.backend.domain.team.domain.entity.Team;
import com.moing.backend.domain.team.exception.AlreadyJoinTeamException;
import com.moing.backend.domain.team.exception.AlreadyWithdrawTeamException;
import com.moing.backend.domain.teamMember.domain.entity.TeamMember;
import com.moing.backend.domain.teamMember.domain.repository.TeamMemberRepository;
import com.moing.backend.global.annotation.DomainService;
import lombok.RequiredArgsConstructor;

import javax.transaction.Transactional;
import java.util.Optional;

@DomainService
@RequiredArgsConstructor
@Transactional
public class TeamMemberSaveService {
private final TeamMemberRepository teamMemberRepository;
public void addTeamMember(Team team, Member member){
TeamMember teamMember=new TeamMember();
teamMember.updateMember(member);
teamMember.updateTeam(team);
team.addTeamMember();
this.teamMemberRepository.save(teamMember);
Optional<TeamMember> existingTeamMemberOpt = teamMemberRepository.findTeamMemberByTeamAndMember(team, member);

TeamMember teamMember = existingTeamMemberOpt.orElseGet(() -> {
TeamMember newMember = new TeamMember();
newMember.updateMember(member);
newMember.updateTeam(team);
team.addTeamMember();
return teamMemberRepository.save(newMember);
});

if (teamMember.isDeleted()) {
throw new AlreadyWithdrawTeamException();
} else {
throw new AlreadyJoinTeamException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public enum ErrorCode {
//팀 관련 에러 코드
NOT_FOUND_BY_TEAM_ID_ERROR("T0001", "해당 teamId인 팀이 존재하지 않습니다."),
NOT_AUTH_BY_TEAM_ERROR("T0002","권한이 없습니다."),
ALREADY_WITHDRAW_ERROR("T0003","이미 탈퇴한 회원입니다."),
ALREADY_JOIN_ERROR("T0004","이미 가입한 회원입니다."),

//게시글 관련 에러 코드
NOT_FOUND_BY_BOARD_ID_ERROR("B0001","해당 boardId인 게시글이 존재하지 않습니다."),
Expand Down
Loading

0 comments on commit b40fc75

Please sign in to comment.