Skip to content

Commit

Permalink
[SAMBAD-274] 마지막 접속 모임 정보 갱신 로직 구현 (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkjsw17 authored and nahyeon99 committed Aug 24, 2024
1 parent 3c59dba commit fe65e38
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
import org.depromeet.sambad.moring.meeting.meeting.domain.MeetingCode;
import org.depromeet.sambad.moring.meeting.meeting.domain.TypesPerMeeting;
import org.depromeet.sambad.moring.meeting.meeting.presentation.exception.MeetingNotFoundException;
import org.depromeet.sambad.moring.meeting.meeting.presentation.exception.NotJoinedAnyMeetingException;
import org.depromeet.sambad.moring.meeting.meeting.presentation.request.MeetingPersistRequest;
import org.depromeet.sambad.moring.meeting.meeting.presentation.response.MeetingResponse;
import org.depromeet.sambad.moring.meeting.meeting.presentation.response.MeetingNameResponse;
import org.depromeet.sambad.moring.meeting.member.application.MeetingMemberRepository;
import org.depromeet.sambad.moring.meeting.member.domain.MeetingMember;
import org.depromeet.sambad.moring.meeting.member.domain.MeetingMemberValidator;
import org.depromeet.sambad.moring.user.domain.User;
import org.depromeet.sambad.moring.user.domain.UserRepository;
import org.depromeet.sambad.moring.user.presentation.exception.NotFoundUserException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -22,6 +26,7 @@
@Transactional(readOnly = true)
public class MeetingService {

private final UserRepository userRepository;
private final MeetingRepository meetingRepository;
private final MeetingMemberRepository meetingMemberRepository;
private final MeetingTypeRepository meetingTypeRepository;
Expand All @@ -35,19 +40,21 @@ public Meeting createMeeting(Long userId, MeetingPersistRequest request) {
Meeting meeting = generateMeeting(request);
addTypesToMeeting(request, meeting);

// NOTE: 모임 생성 시점엔 아직 모임장도 모임원 가입이 안된 상태이므로, 오류 발생
// 해당 로직 추가한 이유가 궁금합니다.
// meetingQuestionService.createActiveQuestion(meeting, meeting.getOwner(), null);
return meeting;
}

public MeetingResponse getMeetingResponse(Long userId) {
List<MeetingMember> membersOfUser = meetingMemberRepository.findByUserId(userId);

if (membersOfUser.isEmpty()) {
throw new NotJoinedAnyMeetingException();
}

List<Meeting> meetings = membersOfUser.stream()
.map(MeetingMember::getMeeting)
.toList();

return MeetingResponse.from(meetings);
return MeetingResponse.of(meetings, MeetingMember.getLastMeetingId(membersOfUser));
}

public MeetingNameResponse getMeetingNameByCode(String code) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public enum MeetingExceptionCode implements ExceptionCode {
EXCEED_MAX_MEETING_COUNT(BAD_REQUEST, "모임 최대 참여 및 개설 횟수를 초과했습니다."),

MEETING_NOT_FOUND(NOT_FOUND, "모임을 찾을 수 없습니다."),
NOT_JOINED_ANY_MEETING(NOT_FOUND, "모임에 전혀 가입되어 있지 않습니다."),
;

private final HttpStatus status;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.depromeet.sambad.moring.meeting.meeting.presentation.exception;

import static org.depromeet.sambad.moring.meeting.meeting.presentation.exception.MeetingExceptionCode.*;

import org.depromeet.sambad.moring.common.exception.BusinessException;

public class NotJoinedAnyMeetingException extends BusinessException {
public NotJoinedAnyMeetingException() {
super(NOT_JOINED_ANY_MEETING);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@

public record MeetingResponse(
@Schema(description = "가입 되어 있는 모임의 정보 목록", requiredMode = REQUIRED)
List<MeetingResponseDetail> meetings
List<MeetingResponseDetail> meetings,

@Schema(description = "마지막으로 접근한 모임 ID", example = "1", requiredMode = REQUIRED)
Long lastMeetingId
) {
public static MeetingResponse from(List<Meeting> meetings) {
public static MeetingResponse of(List<Meeting> meetings, Long lastMeetingId) {
return new MeetingResponse(
meetings.stream()
.map(meeting -> new MeetingResponseDetail(meeting.getId(), meeting.getName()))
.toList()
.toList(),
lastMeetingId == null
? meetings.get(0).getId()
: lastMeetingId
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,15 @@ public void update(MeetingMemberPersistRequest request) {
this.mbti = request.mbti();
this.introduction = request.introduction();
}

public Long getUserId() {
return user.getId();
}

public static Long getLastMeetingId(List<MeetingMember> meetingMembers) {
return meetingMembers.stream()
.findFirst()
.map(MeetingMember::getUserId)
.orElse(null);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.depromeet.sambad.moring.user.application;

import org.depromeet.sambad.moring.meeting.member.domain.MeetingMemberValidator;
import org.depromeet.sambad.moring.user.domain.User;
import org.depromeet.sambad.moring.user.domain.UserRepository;
import org.depromeet.sambad.moring.user.presentation.exception.NotFoundUserException;
Expand All @@ -16,6 +17,7 @@
public class UserService {

private final UserRepository userRepository;
private final MeetingMemberValidator meetingMemberValidator;

public UserResponse findByUserId(Long userId) {
return userRepository.findById(userId)
Expand All @@ -32,4 +34,13 @@ public OnboardingResponse completeOnboarding(Long userId) {

return OnboardingResponse.from(user);
}

@Transactional
public void updateLastMeeting(Long userId, Long meetingId) {
User user = userRepository.findById(userId)
.orElseThrow(NotFoundUserException::new);

meetingMemberValidator.validateUserIsMemberOfMeeting(userId, meetingId);
user.updateLastAccessedMeeting(meetingId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public class User extends BaseTimeEntity {
@Column(columnDefinition = "TINYINT")
private Boolean onboardingCompleted;

private Long lastMeetingId;

@OneToMany(mappedBy = "user")
private List<MeetingMember> meetingMember = new ArrayList<>();

Expand Down Expand Up @@ -97,4 +99,8 @@ public void completeOnboarding() {
public boolean isNotEnteredAnyMeeting() {
return meetingMember.isEmpty();
}

public void updateLastAccessedMeeting(Long meetingId) {
this.lastMeetingId = meetingId;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@

import org.depromeet.sambad.moring.user.application.UserService;
import org.depromeet.sambad.moring.user.domain.User;
import org.depromeet.sambad.moring.user.presentation.request.LastMeetingRequest;
import org.depromeet.sambad.moring.user.presentation.resolver.UserId;
import org.depromeet.sambad.moring.user.presentation.response.OnboardingResponse;
import org.depromeet.sambad.moring.user.presentation.response.UserResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

Expand Down Expand Up @@ -45,4 +48,15 @@ public ResponseEntity<OnboardingResponse> completeOnboarding(@UserId Long userId
OnboardingResponse response = userService.completeOnboarding(userId);
return ResponseEntity.ok(response);
}

@Operation(summary = "가장 최근 접속 모임 수정", description = "가장 최근 접속한 모임 정보를 수정합니다.")
@ApiResponses({
@ApiResponse(responseCode = "200", description = "성공"),
@ApiResponse(responseCode = "403", description = "USER_NOT_MEMBER_OF_MEETING")
})
@PatchMapping("/last-meeting")
public ResponseEntity<Void> updateLastMeeting(@UserId Long userId, @RequestBody LastMeetingRequest request) {
userService.updateLastMeeting(userId, request.meetingId());
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.depromeet.sambad.moring.user.presentation.request;

import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.*;

import io.swagger.v3.oas.annotations.media.Schema;

public record LastMeetingRequest(
@Schema(description = "마지막으로 접속한 모임의 ID", example = "1", requiredMode = REQUIRED)
Long meetingId
) {
}
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,4 @@ caching:
event-templates-ttl: ${EVENT_TEMPLATES_TTL:1800}

event:
keep-days: ${EVENT_KEEP_DAYS:7}
keep-days: ${EVENT_KEEP_DAYS:30}

0 comments on commit fe65e38

Please sign in to comment.