diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/TestInit.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/TestInit.java index 90cd8ed..e8195b6 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/TestInit.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/TestInit.java @@ -9,6 +9,7 @@ import MathCaptain.weakness.domain.Group.repository.RelationRepository; import MathCaptain.weakness.domain.Record.entity.ActivityRecord; import MathCaptain.weakness.domain.Record.repository.RecordRepository; +import MathCaptain.weakness.domain.Recruitment.dto.request.CreateRecruitmentRequest; import MathCaptain.weakness.domain.Recruitment.entity.Comment; import MathCaptain.weakness.domain.Recruitment.entity.Recruitment; import MathCaptain.weakness.domain.Recruitment.enums.RecruitmentStatus; @@ -90,7 +91,7 @@ public void init() { userRepository.save(user); } - log.info("======== ๐Ÿ‘คํ…Œ์ŠคํŠธ ์œ ์ € ์ƒ์„ฑ ์™„๋ฃŒ ========="); + log.info("======== ๐Ÿ‘คํ…Œ์ŠคํŠธ ์œ ์ € ๋ฐ์ดํ„ฐ ์ƒ์„ฑ ์™„๋ฃŒ ========="); Users leader = userRepository.findByUserId(1L) .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ์œ ์ €๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")); @@ -122,79 +123,41 @@ public void init() { group3.updateWeeklyGoalAchieveMap(DayOfWeek.SUNDAY, 0); groupRepository.save(group3); - log.info("======== ๐Ÿ‘ฅ ํ…Œ์ŠคํŠธ ๊ทธ๋ฃน ์ƒ์„ฑ ์™„๋ฃŒ ========="); - - RelationBetweenUserAndGroup join1 = RelationBetweenUserAndGroup.builder() - .member(users1) - .groupRole(GroupRole.LEADER) - .group(group1) - .personalDailyGoal(2) - .personalWeeklyGoal(3) - .build(); - - RelationBetweenUserAndGroup join2 = RelationBetweenUserAndGroup.builder() - .member(users2) - .groupRole(GroupRole.LEADER) - .group(group2) - .personalDailyGoal(2) - .personalWeeklyGoal(3) - .build(); - - RelationBetweenUserAndGroup join3 = RelationBetweenUserAndGroup.builder() - .member(users3) - .groupRole(GroupRole.LEADER) - .group(group3) - .personalDailyGoal(2) - .personalWeeklyGoal(3) - .build(); + log.info("======== ๐Ÿ‘ฅ ํ…Œ์ŠคํŠธ ๊ทธ๋ฃน ๋ฐ์ดํ„ฐ ์ƒ์„ฑ ์™„๋ฃŒ ========="); + + RelationBetweenUserAndGroup join1 = RelationBetweenUserAndGroup.of(users1, group1, groupCreateRequest1); + RelationBetweenUserAndGroup join2 = RelationBetweenUserAndGroup.of(users2, group2, groupCreateRequest2); + RelationBetweenUserAndGroup join3 = RelationBetweenUserAndGroup.of(users3, group3, groupCreateRequest3); relationRepository.save(join1); relationRepository.save(join2); relationRepository.save(join3); for (int i = 4; i <= 12; i++) { - RelationBetweenUserAndGroup join = RelationBetweenUserAndGroup.builder() - .member(userRepository.findByUserId((long) i) - .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ์œ ์ €๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")) - ) - .groupRole(GroupRole.MEMBER) - .group(group3) - .personalDailyGoal(3) - .personalWeeklyGoal(5) - .build(); + Users member = userRepository.findByUserId((long) i) + .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ์œ ์ €๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")); + RelationBetweenUserAndGroup join = RelationBetweenUserAndGroup.of(member, group3, 3, 5); relationRepository.save(join); } - /// ํ…Œ์ŠคํŠธ ๋ชจ์ง‘๊ธ€ ์ƒ์„ฑ - Recruitment recruitment = Recruitment.builder() - .postId(1L) - .author(users1) - .recruitGroup(group1) - .category(CategoryStatus.STUDY) - .title("testRecruitment") - .content("testContent") - .recruitmentStatus(RecruitmentStatus.RECRUITING) - .build(); + log.info("======== ๐Ÿ‘ฅ ํ…Œ์ŠคํŠธ ๊ด€๊ณ„ ๋ฐ์ดํ„ฐ ์ƒ์„ฑ ์™„๋ฃŒ ========="); + CreateRecruitmentRequest createRecruitmentRequest = CreateRecruitmentRequest.of(group1.getId(), "testRecruitment", "testContent"); + Recruitment recruitment = Recruitment.of(users1, group1, createRecruitmentRequest); recruitmentRepository.save(recruitment); + log.info("======== ๐Ÿ”–ํ…Œ์ŠคํŠธ ๋ชจ์ง‘๊ธ€ ์ƒ์„ฑ ์™„๋ฃŒ ========="); /// ํ…Œ์ŠคํŠธ ๋Œ“๊ธ€ ์ƒ์„ฑ - Comment comment = Comment.builder() - .commentId(1L) - .author(users1) - .post(recruitment) - .content("testComment") - .build(); - + Comment comment = Comment.of(recruitment, users1, "testComment"); commentRepository.save(comment); + log.info("======== ๐Ÿ’ฌํ…Œ์ŠคํŠธ ๋Œ“๊ธ€ ์ƒ์„ฑ ์™„๋ฃŒ ========="); // ActivityRecord ์ƒ์„ฑ (currentProgress ์„ค์ •: 10 ์ดํ•˜๋กœ ์กฐ์ •) // ์ด๋ฒˆ ์ฃผ์˜ ์‹œ์ž‘๊ณผ ๋ ์‹œ๊ฐ„ ๊ณ„์‚ฐ LocalDateTime startOfWeek = LocalDateTime.now().with(java.time.temporal.TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); - LocalDateTime endOfWeek = startOfWeek.plusWeeks(1); // User ID: 4 -> currentProgress: 5 createActivityRecords(userRepository.findByUserId(4L).orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ์œ ์ €๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.")), group3, startOfWeek, 5); @@ -217,14 +180,22 @@ public void init() { // Helper ๋ฉ”์„œ๋“œ: ActivityRecord ์ƒ์„ฑ (์ด๋ฒˆ ์ฃผ ๋‚ด์—์„œ๋งŒ ์ƒ์„ฑ๋˜๋„๋ก ์ˆ˜์ •) private void createActivityRecords(Users user, Group group, LocalDateTime startOfWeek, int recordCount) { for (int i = 0; i < recordCount; i++) { - ActivityRecord record = ActivityRecord.builder() - .user(user) - .group(group) - // ์ด๋ฒˆ ์ฃผ ๋‚ด์—์„œ๋งŒ startTime ์„ค์ • - .startTime(startOfWeek.plusDays(i % 7)) // ์›”์š”์ผ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์ˆœ์ฐจ์ ์œผ๋กœ ๋‚ ์งœ ์„ค์ • - .dailyGoalAchieved(true) // ์ผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ ์—ฌ๋ถ€ ์„ค์ • - .weeklyGoalAchieved(false) // ์ฃผ๊ฐ„ ๋ชฉํ‘œ๋Š” ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ false - .build(); + // ์‹œ์ž‘ ์‹œ๊ฐ„์€ ์ฃผ์–ด์ง„ startOfWeek์—์„œ i์ผ์„ ๋”ํ•œ ๊ฐ’ + LocalDateTime startTime = startOfWeek.plusDays(i % 7); + + // ์ข…๋ฃŒ ์‹œ๊ฐ„์€ ์‹œ์ž‘ ์‹œ๊ฐ„์—์„œ 1์‹œ๊ฐ„์„ ๋”ํ•œ ๊ฐ’์œผ๋กœ ์„ค์ • (์˜ˆ์‹œ) + LocalDateTime endTime = startTime.plusHours(1); + + // ํ™œ๋™ ์‹œ๊ฐ„์€ 60๋ถ„์œผ๋กœ ์„ค์ • (์˜ˆ์‹œ) + Long activityTime = 60L; + + // ์š”์ผ์€ ์‹œ์ž‘ ์‹œ๊ฐ„์˜ DayOfWeek๋ฅผ ์‚ฌ์šฉ + DayOfWeek dayOfWeek = startTime.getDayOfWeek(); + + // ActivityRecord ๊ฐ์ฒด ์ƒ์„ฑ + ActivityRecord record = ActivityRecord.of(user, group, startTime, endTime, activityTime, dayOfWeek); + + // ์ €์žฅ์†Œ์— ์ €์žฅ recordRepository.save(record); } } diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Group/entity/RelationBetweenUserAndGroup.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Group/entity/RelationBetweenUserAndGroup.java index e60cd89..de68e28 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Group/entity/RelationBetweenUserAndGroup.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Group/entity/RelationBetweenUserAndGroup.java @@ -11,6 +11,7 @@ import org.springframework.data.annotation.CreatedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; +import java.time.DayOfWeek; import java.time.LocalDate; @Entity @@ -76,9 +77,9 @@ private RelationBetweenUserAndGroup(Users member, Group group, GroupRole groupRo this.weeklyGoalAchieveStreak = 0; } - public static RelationBetweenUserAndGroup of(Users leader, Group group, int dailyGoal, int weeklyGoal) { + public static RelationBetweenUserAndGroup of(Users member, Group group, int dailyGoal, int weeklyGoal) { return RelationBetweenUserAndGroup.builder() - .member(leader) + .member(member) .groupRole(GroupRole.MEMBER) .group(group) .personalDailyGoal(dailyGoal) @@ -86,9 +87,9 @@ public static RelationBetweenUserAndGroup of(Users leader, Group group, int dail .build(); } - public static RelationBetweenUserAndGroup of(Users member, Group group, GroupCreateRequest groupCreateRequest) { + public static RelationBetweenUserAndGroup of(Users leader, Group group, GroupCreateRequest groupCreateRequest) { return RelationBetweenUserAndGroup.builder() - .member(member) + .member(leader) .groupRole(GroupRole.LEADER) .group(group) .personalDailyGoal(groupCreateRequest.getPersonalDailyGoal()) @@ -125,18 +126,18 @@ public void updatePersonalDailyGoalAchieved(Long personalDailyGoalAchieved) { } // ์ฃผ๊ฐ„ ๋ชฉํ‘œ ์—…๋ฐ์ดํŠธ - public void updatePersonalWeeklyGoalAchieved(int personalWeeklyGoalAchieve) { - this.personalWeeklyGoalAchieve = personalWeeklyGoalAchieve; + public void updatePersonalWeeklyGoalAchieved() { + this.personalWeeklyGoalAchieve += 1; } // ์ฃผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ ์—ฐ์† ํšŸ์ˆ˜ ์—…๋ฐ์ดํŠธ - public void updateWeeklyGoalAchieveStreak(int weeklyGoalAchieveStreak) { - this.weeklyGoalAchieveStreak = weeklyGoalAchieveStreak; + public void updateWeeklyGoalAchieveStreak() { + this.weeklyGoalAchieveStreak += 1; } // ์ผ๊ฐ„ ๋ชฉํ‘œ ์ดˆ๊ธฐํ™” public void resetPersonalDailyGoalAchieve() { - this.personalDailyGoalAchieve = (Long) 0L; + this.personalDailyGoalAchieve = 0L; } // ์ฃผ๊ฐ„ ๋ชฉํ‘œ ์ดˆ๊ธฐํ™” @@ -153,4 +154,24 @@ public void updateRequestStatus(RequestStatus requestStatus) { this.requestStatus = requestStatus; } + public void addPoint(Long point) { + this.group.addPoint(point); + this.member.addPoint(point); + } + + public int weeklyAchieveBase() { + return this.getWeeklyGoalAchieveStreak() + this.getPersonalWeeklyGoal(); + } + + public void increaseWeeklyGroupCountOf(DayOfWeek dayOfWeek) { + this.group.increaseWeeklyGoalAchieveMap(dayOfWeek); + } + + public Long remainingDailyGoalMinutes() { + return Math.max(this.getPersonalDailyGoal() * 60L - this.getPersonalDailyGoalAchieve(), 0L); + } + + public int remainingWeeklyGoalDays() { + return Math.max(this.getPersonalWeeklyGoal() - this.getPersonalWeeklyGoalAchieve(), 0); + } } diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Notification/entity/Notification.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Notification/entity/Notification.java index a8e2d04..641e9b9 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Notification/entity/Notification.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Notification/entity/Notification.java @@ -1,20 +1,16 @@ package MathCaptain.weakness.domain.Notification.entity; import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; import java.time.LocalDateTime; +import java.util.Map; @Entity @Getter -@Builder -@AllArgsConstructor -@NoArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) @EntityListeners(AuditingEntityListener.class) public class Notification { @@ -22,12 +18,27 @@ public class Notification { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; + @Column(nullable = false) private String sender; @CreatedDate private LocalDateTime createdAt; + @Column(nullable = false) private String contents; - + @Builder + private Notification(String sender, LocalDateTime createdAt, String contents) { + this.sender = sender; + this.createdAt = createdAt; + this.contents = contents; + } + + public static Notification of(Map eventData) { + return Notification.builder() + .sender(eventData.get("sender")) + .createdAt(LocalDateTime.parse(eventData.get("createdAt"))) + .contents(eventData.get("message")) + .build(); + } } diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Notification/service/NotificationService.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Notification/service/NotificationService.java index f6c00d6..8b373c1 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Notification/service/NotificationService.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Notification/service/NotificationService.java @@ -43,9 +43,7 @@ public SseEmitter subscribe(Long userId) { } catch (IOException e) { e.printStackTrace(); } - NotificationController.sseEmitters.put(userId, sseEmitter); - sseEmitter.onCompletion(() -> NotificationController.sseEmitters.remove(userId)); sseEmitter.onTimeout(() -> NotificationController.sseEmitters.remove(userId)); sseEmitter.onError((e) -> NotificationController.sseEmitters.remove(userId)); @@ -54,29 +52,22 @@ public SseEmitter subscribe(Long userId) { } public void notifyComment(Long recruitmentId, Long commentId) { - Recruitment recruitment = recruitmentRepository.findById(recruitmentId) - .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๋ชจ์ง‘๊ธ€์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); - - Comment comment = commentRepository.findById(commentId) - .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๋Œ“๊ธ€์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); + Recruitment recruitment = findRecruitmentBy(recruitmentId); + Comment comment = findCommentBy(commentId); Long userId = recruitment.getAuthor().getUserId(); if (NotificationController.sseEmitters.containsKey(userId)) { SseEmitter sseEmitter = NotificationController.sseEmitters.get(userId); try { Map eventData = buildNotificationData("๋Œ“๊ธ€์ด ๋‹ฌ๋ ธ์Šต๋‹ˆ๋‹ค.", comment.getAuthor().getNickname(), comment.getCommentTime().toString(), comment.getContent()); - sseEmitter.send(SseEmitter.event().name("addComment").data(eventData)); - - notificationRepository.save(buildNotification(eventData)); - + Notification notification = Notification.of(eventData); + notificationRepository.save(notification); // ์•Œ๋ฆผ ๊ฐœ์ˆ˜ ์ฆ๊ฐ€ notificationCounts.put(userId, notificationCounts.getOrDefault(userId, 0) + 1); - // ํ˜„์žฌ ์•Œ๋ฆผ ๊ฐœ์ˆ˜ ์ „์†ก sseEmitter.send(SseEmitter.event().name("notificationCount").data(notificationCounts.get(userId))); - } catch (IOException e) { NotificationController.sseEmitters.remove(userId); } @@ -85,30 +76,22 @@ public void notifyComment(Long recruitmentId, Long commentId) { public void notifyGroupJoinRequest(Long groupId, Users user) { - Group group = groupRepository.findById(groupId) - .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๊ทธ๋ฃน์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); - - Users leader = relationRepository.findLeaderByGroup(group) - .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๊ทธ๋ฃน์˜ ๋ฆฌ๋”๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); - + Group group = findGroupBy(groupId); + Users leader = findLeaderBy(group); Long leaderId = leader.getUserId(); if (NotificationController.sseEmitters.containsKey(leaderId)) { SseEmitter sseEmitter = NotificationController.sseEmitters.get(leaderId); try { Map eventData = buildNotificationData("๊ทธ๋ฃน ๊ฐ€์ž… ์š”์ฒญ์ด ์žˆ์Šต๋‹ˆ๋‹ค.", user.getNickname(), LocalDateTime.now().toString(), "๊ฐ€์ž… ์š”์ฒญ"); - sseEmitter.send(SseEmitter.event().name("groupJoinRequest").data(eventData)); - // DB ์ €์žฅ - notificationRepository.save(buildNotification(eventData)); - + Notification notification = Notification.of(eventData); + notificationRepository.save(notification); // ์•Œ๋ฆผ ๊ฐœ์ˆ˜ ์ฆ๊ฐ€ updateNotificationCount(leaderId); - // ํ˜„์žฌ ์•Œ๋ฆผ ๊ฐœ์ˆ˜ ์ „์†ก sseEmitter.send(SseEmitter.event().name("notificationCount").data(notificationCounts.get(leaderId))); - } catch (IOException e) { NotificationController.sseEmitters.remove(leaderId); } @@ -117,30 +100,22 @@ public void notifyGroupJoinRequest(Long groupId, Users user) { } public void notifyGroupJoinResult(Long groupId, Users user) { - Group group = groupRepository.findById(groupId) - .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๊ทธ๋ฃน์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); - - RelationBetweenUserAndGroup relation = relationRepository.findByMemberAndGroup(user, group) - .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๊ด€๊ณ„๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); - + Group group = findGroupBy(groupId); + RelationBetweenUserAndGroup relation = findRelationBy(user, group); Long userId = user.getUserId(); if (NotificationController.sseEmitters.containsKey(userId)) { SseEmitter sseEmitter = NotificationController.sseEmitters.get(userId); try { Map eventData = buildNotificationData("๊ทธ๋ฃน ๊ฐ€์ž… ์š”์ฒญ ๊ฒฐ๊ณผ๊ฐ€ ๋„์ฐฉํ–ˆ์Šต๋‹ˆ๋‹ค.", group.getName(), LocalDateTime.now().toString(), relation.getRequestStatus().toString()); - sseEmitter.send(SseEmitter.event().name("groupJoinResult").data(eventData)); - // DB ์ €์žฅ - notificationRepository.save(buildNotification(eventData)); - + Notification notification = Notification.of(eventData); + notificationRepository.save(notification); // ์•Œ๋ฆผ ๊ฐœ์ˆ˜ ์ฆ๊ฐ€ updateNotificationCount(userId); - // ํ˜„์žฌ ์•Œ๋ฆผ ๊ฐœ์ˆ˜ ์ „์†ก sseEmitter.send(SseEmitter.event().name("notificationCount").data(notificationCounts.get(userId))); - } catch (IOException e) { NotificationController.sseEmitters.remove(userId); } @@ -148,13 +123,9 @@ public void notifyGroupJoinResult(Long groupId, Users user) { } public ApiResponse deleteNotification(Users loginUser, Long notificationId) { - Notification notification = notificationRepository.findById(notificationId) - .orElseThrow(() -> new IllegalArgumentException("์•Œ๋ฆผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")); - + Notification notification = findNotificationBy(notificationId); Long userId = loginUser.getUserId(); - notificationRepository.delete(notification); - // ์•Œ๋ฆผ ๊ฐœ์ˆ˜ ๊ฐ์†Œ if (notificationCounts.containsKey(userId)) { int currentCount = notificationCounts.get(userId); @@ -170,7 +141,6 @@ public ApiResponse deleteNotification(Users loginUser, Long notificationId) { } catch (IOException e) { e.printStackTrace(); } - return ApiResponse.ok("์•Œ๋ฆผ์ด ์‚ญ์ œ๋˜์—ˆ์Šต๋‹ˆ๋‹ค."); } @@ -187,15 +157,35 @@ private void updateNotificationCount(Long userId) { notificationCounts.put(userId, notificationCounts.getOrDefault(userId, 0) + 1); } - private Notification buildNotification(Map eventData) { - return Notification.builder() - .sender(eventData.get("sender")) - .createdAt(LocalDateTime.parse(eventData.get("createdAt"))) - .contents(eventData.get("message")) - .build(); + private Comment findCommentBy(Long commentId) { + return commentRepository.findById(commentId) + .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๋Œ“๊ธ€์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); } + private Recruitment findRecruitmentBy(Long recruitmentId) { + return recruitmentRepository.findById(recruitmentId) + .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๋ชจ์ง‘๊ธ€์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); + } + private Notification findNotificationBy(Long notificationId) { + return notificationRepository.findById(notificationId) + .orElseThrow(() -> new IllegalArgumentException("์•Œ๋ฆผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.")); + } + + private RelationBetweenUserAndGroup findRelationBy(Users user, Group group) { + return relationRepository.findByMemberAndGroup(user, group) + .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๊ด€๊ณ„๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); + } + + private Group findGroupBy(Long groupId) { + return groupRepository.findById(groupId) + .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๊ทธ๋ฃน์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); + } + + private Users findLeaderBy(Group group) { + return relationRepository.findLeaderByGroup(group) + .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๊ทธ๋ฃน์˜ ๋ฆฌ๋”๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); + } // ์•Œ๋ฆผ ์‚ญ์ œ // public MsgResponseDto deleteNotification(Long id) throws IOException { diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Ranking/controller/RankingController.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Ranking/controller/RankingController.java index 75b2ab3..97391a8 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Ranking/controller/RankingController.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Ranking/controller/RankingController.java @@ -1,6 +1,6 @@ package MathCaptain.weakness.domain.Ranking.controller; -import MathCaptain.weakness.domain.Ranking.dto.response.GroupRankingResponseDto; +import MathCaptain.weakness.domain.Ranking.dto.response.GroupRankingResponse; import MathCaptain.weakness.domain.Ranking.service.RankingService; import MathCaptain.weakness.global.Api.ApiResponse; import lombok.RequiredArgsConstructor; @@ -19,7 +19,7 @@ public class RankingController { // ๊ทธ๋ฃน ๋žญํ‚น ์กฐํšŒ @GetMapping("/{page}") - public ApiResponse> getGroupRankings( + public ApiResponse> getGroupRankings( @PathVariable("page") int page) { // ํŽ˜์ด์ง€ ๋ฒˆํ˜ธ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ Pageable ์ƒ์„ฑ Pageable pageable = PageRequest.of(page - 1, 10, Sort.by(Sort.Direction.DESC, "groupPoint")); diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Ranking/dto/response/GroupRankingResponse.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Ranking/dto/response/GroupRankingResponse.java new file mode 100644 index 0000000..8cefaa8 --- /dev/null +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Ranking/dto/response/GroupRankingResponse.java @@ -0,0 +1,34 @@ +package MathCaptain.weakness.domain.Ranking.dto.response; + +import MathCaptain.weakness.domain.Group.entity.Group; +import lombok.*; + +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class GroupRankingResponse { + + private Long groupId; + + private String groupName; + + private Long groupPoint; + + private int ranking; + + @Builder + private GroupRankingResponse(Long groupId, String groupName, Long groupPoint, int ranking) { + this.groupId = groupId; + this.groupName = groupName; + this.groupPoint = groupPoint; + this.ranking = ranking; + } + + public static GroupRankingResponse of(Group group) { + return GroupRankingResponse.builder() + .groupId(group.getId()) + .groupName(group.getName()) + .groupPoint(group.getGroupPoint()) + .ranking(group.getGroupRanking()) + .build(); + } +} \ No newline at end of file diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Ranking/dto/response/GroupRankingResponseDto.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Ranking/dto/response/GroupRankingResponseDto.java deleted file mode 100644 index 351416f..0000000 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Ranking/dto/response/GroupRankingResponseDto.java +++ /dev/null @@ -1,18 +0,0 @@ -package MathCaptain.weakness.domain.Ranking.dto.response; - -import lombok.Builder; -import lombok.Data; - -@Data -@Builder -public class GroupRankingResponseDto { - - private Long groupId; - - private String groupName; - - private Long groupPoint; - - private int ranking; - -} \ No newline at end of file diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Ranking/service/RankingService.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Ranking/service/RankingService.java index 59846b9..30bbbe4 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Ranking/service/RankingService.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Ranking/service/RankingService.java @@ -2,7 +2,7 @@ import MathCaptain.weakness.domain.Group.entity.Group; import MathCaptain.weakness.domain.Group.repository.GroupRepository; -import MathCaptain.weakness.domain.Ranking.dto.response.GroupRankingResponseDto; +import MathCaptain.weakness.domain.Ranking.dto.response.GroupRankingResponse; import lombok.Builder; import lombok.RequiredArgsConstructor; import org.springframework.data.domain.Page; @@ -20,7 +20,7 @@ public class RankingService { private final GroupRepository groupRepository; - public Page getGroupRankings(Pageable pageable) { + public Page getGroupRankings(Pageable pageable) { // Repository ํ˜ธ์ถœํ•˜์—ฌ ํŽ˜์ด์ง•๋œ ๋ฐ์ดํ„ฐ ๊ฐ€์ ธ์˜ค๊ธฐ Page groupPage = groupRepository.findAllOrderByGroupPoint(pageable); @@ -34,16 +34,6 @@ public Page getGroupRankings(Pageable pageable) { }); // ๋ณ€๊ฒฝ ์‚ฌํ•ญ์„ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ๋ฐ˜์˜ - return groupPage.map(RankingService::buildGroupRankingResponse); + return groupPage.map(GroupRankingResponse::of); } - - private static GroupRankingResponseDto buildGroupRankingResponse(Group group) { - return GroupRankingResponseDto.builder() - .groupId(group.getId()) - .groupName(group.getName()) - .groupPoint(group.getGroupPoint()) - .ranking(group.getGroupRanking()) - .build(); - } - } \ No newline at end of file diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/controller/RecordController.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/controller/RecordController.java index 7e9478d..941b85d 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/controller/RecordController.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/controller/RecordController.java @@ -1,11 +1,12 @@ package MathCaptain.weakness.domain.Record.controller; -import MathCaptain.weakness.domain.Record.dto.request.recordEndRequestDto; -import MathCaptain.weakness.domain.Record.dto.response.recordSummaryResponseDto; +import MathCaptain.weakness.domain.Record.dto.request.recordEndRequest; +import MathCaptain.weakness.domain.Record.dto.response.RecordSummaryResponse; import MathCaptain.weakness.domain.Record.service.RecordService; import MathCaptain.weakness.domain.User.entity.Users; import MathCaptain.weakness.global.Api.ApiResponse; import MathCaptain.weakness.global.annotation.LoginUser; +import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.*; @@ -17,7 +18,7 @@ public class RecordController { private final RecordService recordService; @PostMapping("/end/{groupId}") - public ApiResponse endActivity(@LoginUser Users loginUser, @PathVariable Long groupId, @RequestBody recordEndRequestDto requestDto) { + public ApiResponse endActivity(@Valid @LoginUser Users loginUser, @PathVariable Long groupId, @RequestBody recordEndRequest requestDto) { return ApiResponse.ok(recordService.endActivity(loginUser, groupId, requestDto)); } } diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/dto/request/recordEndRequestDto.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/dto/request/recordEndRequest.java similarity index 53% rename from MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/dto/request/recordEndRequestDto.java rename to MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/dto/request/recordEndRequest.java index 292ea28..8f9fb22 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/dto/request/recordEndRequestDto.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/dto/request/recordEndRequest.java @@ -1,17 +1,12 @@ package MathCaptain.weakness.domain.Record.dto.request; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.*; import java.time.LocalDateTime; -@Data -@Builder -@NoArgsConstructor -@AllArgsConstructor -public class recordEndRequestDto { +@Getter +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class recordEndRequest { // ์ˆ˜ํ–‰ ์‹œ๊ฐ„ (๋ถ„) private Long activityTime; diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/dto/response/RecordSummaryResponse.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/dto/response/RecordSummaryResponse.java new file mode 100644 index 0000000..f4bd114 --- /dev/null +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/dto/response/RecordSummaryResponse.java @@ -0,0 +1,67 @@ +package MathCaptain.weakness.domain.Record.dto.response; + +import MathCaptain.weakness.domain.Group.entity.RelationBetweenUserAndGroup; +import MathCaptain.weakness.domain.Record.entity.ActivityRecord; +import lombok.*; + +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class RecordSummaryResponse { + + // ์œ ์ € ์ด๋ฆ„ + private String userName; + + // ๊ทธ๋ฃน ์ด๋ฆ„ + private String groupName; + + // ์ˆ˜ํ–‰ ์‹œ๊ฐ„ + private Long durationInMinutes; + + // ์ผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ ์—ฌ๋ถ€ + private boolean dailyGoalAchieved; + + // ์ฃผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ ์—ฌ๋ถ€ + private boolean weeklyGoalAchieved; + + // ์ผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ๊นŒ์ง€ ๋‚จ์€ ์‹œ๊ฐ„ (๋ถ„) (๋‹ฌ์„ฑ์‹œ 0) + private Long remainingDailyGoalMinutes; + + // ์ฃผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ๊นŒ์ง€ ๋‚จ์€ ์ผ ์ˆ˜ (์ผ) (๋‹ฌ์„ฑ์‹œ 0) + private int remainingWeeklyGoalDays; + + // ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•œ ์ผ๊ฐ„ ๋ชฉํ‘œ + private Long personalDailyGoal; + + // ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•œ ์ฃผ๊ฐ„ ๋ชฉํ‘œ + private int personalWeeklyGoal; + + @Builder + private RecordSummaryResponse(String userName, String groupName, Long durationInMinutes, + boolean dailyGoalAchieved, boolean weeklyGoalAchieved, + Long remainingDailyGoalMinutes, int remainingWeeklyGoalDays, + Long personalDailyGoal, int personalWeeklyGoal) { + this.userName = userName; + this.groupName = groupName; + this.durationInMinutes = durationInMinutes; + this.dailyGoalAchieved = dailyGoalAchieved; + this.weeklyGoalAchieved = weeklyGoalAchieved; + this.remainingDailyGoalMinutes = remainingDailyGoalMinutes; + this.remainingWeeklyGoalDays = remainingWeeklyGoalDays; + this.personalDailyGoal = personalDailyGoal; + this.personalWeeklyGoal = personalWeeklyGoal; + } + + public static RecordSummaryResponse of(ActivityRecord record, RelationBetweenUserAndGroup relation) { + return RecordSummaryResponse.builder() + .userName(record.getUser().getName()) + .groupName(record.getGroup().getName()) + .durationInMinutes(record.getDurationInMinutes()) + .dailyGoalAchieved(record.isDailyGoalAchieved()) + .weeklyGoalAchieved(record.isWeeklyGoalAchieved()) + .remainingDailyGoalMinutes(relation.remainingDailyGoalMinutes()) + .remainingWeeklyGoalDays(relation.remainingWeeklyGoalDays()) + .personalDailyGoal(relation.getPersonalDailyGoal() * 60L) + .personalWeeklyGoal(relation.getPersonalWeeklyGoal()) + .build(); + } +} diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/dto/response/recordSummaryResponseDto.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/dto/response/recordSummaryResponseDto.java deleted file mode 100644 index 8b4f178..0000000 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/dto/response/recordSummaryResponseDto.java +++ /dev/null @@ -1,38 +0,0 @@ -package MathCaptain.weakness.domain.Record.dto.response; - -import lombok.Builder; -import lombok.Data; - -import java.time.Duration; - -@Data -@Builder -public class recordSummaryResponseDto { - - // ์œ ์ € ์ด๋ฆ„ - private String userName; - - // ๊ทธ๋ฃน ์ด๋ฆ„ - private String groupName; - - // ์ˆ˜ํ–‰ ์‹œ๊ฐ„ - private Long durationInMinutes; - - // ์ผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ ์—ฌ๋ถ€ - private boolean dailyGoalAchieved; - - // ์ฃผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ ์—ฌ๋ถ€ - private boolean weeklyGoalAchieved; - - // ์ผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ๊นŒ์ง€ ๋‚จ์€ ์‹œ๊ฐ„ (๋ถ„) (๋‹ฌ์„ฑ์‹œ 0) - private Long remainingDailyGoalMinutes; - - // ์ฃผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ๊นŒ์ง€ ๋‚จ์€ ์ผ ์ˆ˜ (์ผ) (๋‹ฌ์„ฑ์‹œ 0) - private int remainingWeeklyGoalDays; - - // ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•œ ์ผ๊ฐ„ ๋ชฉํ‘œ - private Long personalDailyGoal; - - // ์‚ฌ์šฉ์ž๊ฐ€ ์„ค์ •ํ•œ ์ฃผ๊ฐ„ ๋ชฉํ‘œ - private int personalWeeklyGoal; -} diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/entity/ActivityRecord.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/entity/ActivityRecord.java index e182944..16f7cdd 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/entity/ActivityRecord.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/entity/ActivityRecord.java @@ -1,12 +1,11 @@ package MathCaptain.weakness.domain.Record.entity; import MathCaptain.weakness.domain.Group.entity.Group; +import MathCaptain.weakness.domain.Group.entity.RelationBetweenUserAndGroup; +import MathCaptain.weakness.domain.Record.dto.request.recordEndRequest; import MathCaptain.weakness.domain.User.entity.Users; import jakarta.persistence.*; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; -import lombok.NoArgsConstructor; +import lombok.*; import org.hibernate.validator.constraints.Range; import org.springframework.data.annotation.CreatedDate; import org.springframework.data.jpa.domain.support.AuditingEntityListener; @@ -16,9 +15,7 @@ @Getter @Entity(name = "activity_record") -@NoArgsConstructor -@AllArgsConstructor -@Builder +@NoArgsConstructor(access = AccessLevel.PROTECTED) @EntityListeners(AuditingEntityListener.class) public class ActivityRecord { @@ -40,38 +37,57 @@ public class ActivityRecord { private LocalDateTime endTime; // ์ธ์ฆ ์ข…๋ฃŒ ์‹œ๊ฐ„ @Range(min = 0) + @Column(nullable = false) private Long durationInMinutes; // ํ™œ๋™ ์‹œ๊ฐ„ (๋ถ„) private boolean dailyGoalAchieved; // ์ผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ ์—ฌ๋ถ€ private boolean weeklyGoalAchieved; // ์ฃผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ ์—ฌ๋ถ€ + @Column(nullable = false) private DayOfWeek dayOfWeek; // ์š”์ผ -// @PrePersist -// public void prePersist() { -// this.dailyGoalAchieved = false; -// this.weeklyGoalAchieved = false; -// this.durationInMinutes = 0L; -// } - - public void updateEndTime(LocalDateTime endTime) { + @Builder + private ActivityRecord(Users user, Group group, LocalDateTime startTime, LocalDateTime endTime, Long durationInMinutes, + boolean dailyGoalAchieved, boolean weeklyGoalAchieved, DayOfWeek dayOfWeek) { + this.user = user; + this.group = group; + this.startTime = startTime; this.endTime = endTime; + this.durationInMinutes = durationInMinutes; + this.dailyGoalAchieved = dailyGoalAchieved; + this.weeklyGoalAchieved = weeklyGoalAchieved; + this.dayOfWeek = dayOfWeek; } - public void updateDailyGoalAchieved(boolean dailyGoalAchieved) { - this.dailyGoalAchieved = dailyGoalAchieved; + public static ActivityRecord of(RelationBetweenUserAndGroup relation, recordEndRequest endRequest, DayOfWeek dayOfWeek) { + return ActivityRecord.builder() + .user(relation.getMember()) + .group(relation.getGroup()) + .startTime(endRequest.getStartTime()) + .endTime(endRequest.getEndTime()) + .durationInMinutes(endRequest.getActivityTime()) + .dayOfWeek(dayOfWeek) + .build(); } - public void updateWeeklyGoalAchieved(boolean weeklyGoalAchieved) { - this.weeklyGoalAchieved = weeklyGoalAchieved; + public static ActivityRecord of(Users user, Group group, LocalDateTime startTime, LocalDateTime endTime, + Long activityTime, DayOfWeek dayOfWeek) { + return ActivityRecord.builder() + .user(user) + .group(group) + .startTime(startTime) + .endTime(endTime) + .durationInMinutes(activityTime) + .dayOfWeek(dayOfWeek) + .build(); } - public void updateDayOfWeek(DayOfWeek dayOfWeek) { - this.dayOfWeek = dayOfWeek; + public void updateDailyGoalAchieved(boolean dailyGoalAchieved) { + this.dailyGoalAchieved = dailyGoalAchieved; } - public void updateDurationInMinutes(Long durationInMinutes) { - this.durationInMinutes = durationInMinutes; + public void updateWeeklyGoalAchieved(boolean weeklyGoalAchieved) { + this.weeklyGoalAchieved = weeklyGoalAchieved; } } diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/service/RecordService.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/service/RecordService.java index 38df108..967f4a0 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/service/RecordService.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Record/service/RecordService.java @@ -4,8 +4,8 @@ import MathCaptain.weakness.domain.Group.entity.RelationBetweenUserAndGroup; import MathCaptain.weakness.domain.Group.repository.RelationRepository; import MathCaptain.weakness.domain.Record.entity.ActivityRecord; -import MathCaptain.weakness.domain.Record.dto.request.recordEndRequestDto; -import MathCaptain.weakness.domain.Record.dto.response.recordSummaryResponseDto; +import MathCaptain.weakness.domain.Record.dto.request.recordEndRequest; +import MathCaptain.weakness.domain.Record.dto.response.RecordSummaryResponse; import MathCaptain.weakness.domain.Record.repository.RecordRepository; import MathCaptain.weakness.domain.User.entity.Users; import MathCaptain.weakness.global.PointSet; @@ -15,6 +15,7 @@ import org.springframework.transaction.annotation.Transactional; import java.time.DayOfWeek; +import java.time.LocalDate; import java.time.LocalDateTime; import java.time.temporal.TemporalAdjusters; import java.util.*; @@ -35,23 +36,14 @@ public class RecordService { /// ๊ธฐ๋ก // ๊ธฐ๋ก ์ €์žฅ - public recordSummaryResponseDto endActivity(Users user, Long groupId, recordEndRequestDto endRequest) { - - // ๊ด€๊ณ„ ์‹๋ณ„ - RelationBetweenUserAndGroup relation = relationRepository.findByMemberAndGroup_Id(user, groupId) - .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๊ด€๊ณ„๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); - - ActivityRecord record = buildRecord(user, relation, endRequest); - - // ๊ทธ๋ฃน ์‹๋ณ„ - Group group = relation.getGroup(); - - // ๋ชฉํ‘œ์— ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ ๋นˆ์˜ - updateGoalAchieve(user, relation, record, group); + public RecordSummaryResponse endActivity(Users user, Long groupId, recordEndRequest endRequest) { + RelationBetweenUserAndGroup relation = findRelationByMemberAndGroup(user, groupId); + DayOfWeek nowDay = LocalDate.now().getDayOfWeek(); + ActivityRecord record = ActivityRecord.of(relation, endRequest, nowDay); + updateGoalAchieve(relation, record, nowDay); recordRepository.save(record); - - return buildRecordSummaryResponseDto(record, relation, calculateRemainingDailyGoalMinutes(relation), calculateRemainingWeeklyGoal(relation)); + return RecordSummaryResponse.of(record, relation); } // ์ฃผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ ์—ฌ๋ถ€ ์กฐํšŒ @@ -62,75 +54,47 @@ public Map getWeeklyGoalStatus(Users user, Group group, Loca // ํ™œ๋™ ๊ธฐ๋ก์ด ์žˆ๋Š” ์š”์ผ ์กฐํšŒ List activeDays = recordRepository.findDaysWithActivity(user, group, startOfWeek, endOfWeek); - // ์š”์ผ๋ณ„ ํ™œ๋™ ๊ธฐ๋ก ์—ฌ๋ถ€ ๋งต ์ƒ์„ฑ return createWeeklyGoalStatusMap(activeDays); } + /// ๋กœ์ง // ์ผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ ์—ฌ๋ถ€ ํ™•์ธ private boolean isDailyGoalAchieved(RelationBetweenUserAndGroup relation) { - return relation.getPersonalDailyGoalAchieve() >= relation.getPersonalDailyGoal(); + return relation.isDailyGoalAchieved(); } - private boolean isWeeklyGoalAchieved(RelationBetweenUserAndGroup relation) { - return relation.getPersonalWeeklyGoalAchieve() >= relation.getPersonalWeeklyGoal(); + return relation.isWeeklyGoalAchieved(); } // ๊ทธ๋ฃน์˜ ์š”์ผ ๋ชฉํ‘œ ์ˆ˜ํ–‰ ์นด์šดํŠธ ์ฆ๊ฐ€ - - public void increaseWeeklyGoalAchieveCount(RelationBetweenUserAndGroup relation, ActivityRecord record) { - // ํ™œ๋™ ์š”์ผ - DayOfWeek dayOfWeek = record.getDayOfWeek(); - Group group = relation.getGroup(); - group.increaseWeeklyGoalAchieveMap(dayOfWeek); - } - private void addPoint(Users user, Group group, Long point) { - // ๊ฐœ์ธ ํฌ์ธํŠธ ํš๋“ (์ผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ) - user.addPoint(point); - - // ๊ทธ๋ฃน ํฌ์ธํŠธ ํš๋“ (์ผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ) - group.addPoint(point); - } - - private void updateGoalAchieve(Users user, RelationBetweenUserAndGroup relation, ActivityRecord record, Group group) { + private void updateGoalAchieve(RelationBetweenUserAndGroup relation, ActivityRecord record, DayOfWeek nowDay) { // ์ผ๊ฐ„ ๋‹ฌ์„ฑ ์‹œ๊ฐ„ ์—…๋ฐ์ดํŠธ (๋ถ„) relation.updatePersonalDailyGoalAchieved( Optional.ofNullable(relation.getPersonalDailyGoalAchieve()) .orElse(0L) + record.getDurationInMinutes()); - // ์ผ๊ฐ„ ๋ชฉํ‘œ ์‹œ๊ฐ„ ๋‹ฌ์„ฑ์‹œ if (isDailyGoalAchieved(relation)) { - // ์ผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ ์„ฑ๊ณต ์—…๋ฐ์ดํŠธ (๊ธฐ๋ก) record.updateDailyGoalAchieved(true); - - // ๊ทธ๋ฃน์˜ ์š”์ผ ๋ชฉํ‘œ ์ˆ˜ํ–‰ ์นด์šดํŠธ ์ฆ๊ฐ€ (๊ทธ๋ฃน) - increaseWeeklyGoalAchieveCount(relation, record); - - // ์ผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ ํฌ์ธํŠธ ํš๋“ - addPoint(user, group, DAILY_GOAL_ACHIEVE); - - // ์ฃผ๊ฐ„ ๋ชฉํ‘œ + 1 (์ผ๊ฐ„ ๋ชฉํ‘œ ์ถฉ์กฑ์‹œ ์—…๋ฐ์ดํŠธ) - relation.updatePersonalWeeklyGoalAchieved(relation.getPersonalWeeklyGoalAchieve() + 1); + relation.increaseWeeklyGroupCountOf(nowDay); + addPoint(relation, DAILY_GOAL_ACHIEVE); + relation.updatePersonalWeeklyGoalAchieved(); } - // ์ฃผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ์‹œ if (isWeeklyGoalAchieved(relation)) { - // ์ฃผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ ์„ฑ๊ณต ์—…๋ฐ์ดํŠธ (๊ธฐ๋ก) record.updateWeeklyGoalAchieved(true); - - // ์ฃผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ ์ŠคํŠธ๋ฆญ ์—…๋ฐ์ดํŠธ (๊ด€๊ณ„) - relation.updateWeeklyGoalAchieveStreak(relation.getWeeklyGoalAchieveStreak() + 1); - - // ์ฃผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ ํฌ์ธํŠธ ๊ณ„์‚ฐ - Long weeklyAchievePoint = WEEKLY_GOAL_ACHIEVE_BASE * (relation.getWeeklyGoalAchieveStreak() + relation.getPersonalWeeklyGoal()); - - // ์ฃผ๊ฐ„ ๋ชฉํ‘œ ๋‹ฌ์„ฑ ํฌ์ธํŠธ ํš๋“ - addPoint(user, group, weeklyAchievePoint); + relation.updateWeeklyGoalAchieveStreak(); + Long weeklyAchievePoint = WEEKLY_GOAL_ACHIEVE_BASE * relation.weeklyAchieveBase(); + addPoint(relation, weeklyAchievePoint); } } + private void addPoint(RelationBetweenUserAndGroup relation, Long point) { + relation.addPoint(point); + } + private LocalDateTime calculateStartOfWeek(LocalDateTime weekStart) { return weekStart.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); } @@ -139,56 +103,13 @@ private Map createWeeklyGoalStatusMap(List active // ๋ชจ๋“  ์š”์ผ์„ false๋กœ ์ดˆ๊ธฐํ™” Map weeklyGoalStatus = Arrays.stream(DayOfWeek.values()) .collect(Collectors.toMap(day -> day, day -> false, (a, b) -> b, () -> new EnumMap<>(DayOfWeek.class))); - // ํ™œ๋™ ๊ธฐ๋ก์ด ์žˆ๋Š” ์š”์ผ์„ true๋กœ ์„ค์ • activeDays.forEach(day -> weeklyGoalStatus.put(day, true)); - return weeklyGoalStatus; } - private void checkRemainRecord(Users user, Group group) { - // ๊ธฐ์กด ๋ฏธ์™„๋ฃŒ๋œ ํ™œ๋™์ด ์žˆ๋Š”์ง€ ํ™•์ธ - Optional existingActivity = recordRepository.findByUserAndGroupAndEndTimeIsNull( - user, group); - - if (existingActivity.isPresent()) { - recordRepository.delete(existingActivity.get()); - log.info("๊ธฐ์กด ๋ฏธ์™„๋ฃŒ๋œ ํ™œ๋™ ์‚ญ์ œ"); - } - } - - private Long calculateRemainingDailyGoalMinutes(RelationBetweenUserAndGroup relation) { - return Math.max(relation.getPersonalDailyGoal() * 60L - relation.getPersonalDailyGoalAchieve(), 0L); - } - - private int calculateRemainingWeeklyGoal(RelationBetweenUserAndGroup relation) { - return Math.max(relation.getPersonalWeeklyGoal() - relation.getPersonalWeeklyGoalAchieve(), 0); - } - - /// ๋นŒ๋” - - private recordSummaryResponseDto buildRecordSummaryResponseDto(ActivityRecord activityRecord, RelationBetweenUserAndGroup relation, Long remainingDailyGoalMinutes, int remainingWeeklyGoal) { - return recordSummaryResponseDto.builder() - .userName(activityRecord.getUser().getName()) - .groupName(activityRecord.getGroup().getName()) - .durationInMinutes(activityRecord.getDurationInMinutes()) - .dailyGoalAchieved(activityRecord.isDailyGoalAchieved()) - .weeklyGoalAchieved(activityRecord.isWeeklyGoalAchieved()) - .remainingDailyGoalMinutes(remainingDailyGoalMinutes) - .remainingWeeklyGoalDays(remainingWeeklyGoal) - .personalDailyGoal(relation.getPersonalDailyGoal() * 60L) - .personalWeeklyGoal(relation.getPersonalWeeklyGoal()) - .build(); - } - - private static ActivityRecord buildRecord(Users user, RelationBetweenUserAndGroup relation, recordEndRequestDto endRequest) { - return ActivityRecord.builder() - .user(user) - .group(relation.getGroup()) - .startTime(endRequest.getStartTime()) - .endTime(endRequest.getEndTime()) - .durationInMinutes(endRequest.getActivityTime()) - .dayOfWeek(LocalDateTime.now().getDayOfWeek()) - .build(); + private RelationBetweenUserAndGroup findRelationByMemberAndGroup(Users user, Long groupId) { + return relationRepository.findByMemberAndGroup_Id(user, groupId) + .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๊ด€๊ณ„๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.")); } } diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/controller/RecruitmentController.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/controller/RecruitmentController.java index 9d114d3..88a54bf 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/controller/RecruitmentController.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/controller/RecruitmentController.java @@ -1,10 +1,10 @@ package MathCaptain.weakness.domain.Recruitment.controller; import MathCaptain.weakness.domain.Notification.service.NotificationService; -import MathCaptain.weakness.domain.Recruitment.dto.request.CreateCommentRequestDto; -import MathCaptain.weakness.domain.Recruitment.dto.request.CreateRecruitmentRequestDto; -import MathCaptain.weakness.domain.Recruitment.dto.request.UpdateCommentRequestDto; -import MathCaptain.weakness.domain.Recruitment.dto.request.UpdateRecruitmentRequestDto; +import MathCaptain.weakness.domain.Recruitment.dto.request.CreateCommentRequest; +import MathCaptain.weakness.domain.Recruitment.dto.request.CreateRecruitmentRequest; +import MathCaptain.weakness.domain.Recruitment.dto.request.UpdateCommentRequest; +import MathCaptain.weakness.domain.Recruitment.dto.request.UpdateRecruitmentRequest; import MathCaptain.weakness.domain.Recruitment.dto.response.*; import MathCaptain.weakness.domain.Recruitment.service.CommentService; import MathCaptain.weakness.domain.Recruitment.service.RecruitmentService; @@ -32,61 +32,61 @@ public class RecruitmentController { // ๋ชจ์ง‘๊ธ€ ๋ฆฌ์ŠคํŠธ ์กฐํšŒ @GetMapping - public ApiResponse> recruitmentList() { + public ApiResponse> recruitmentList() { return recruitmentService.getAllRecruitments(); } // ๋ชจ์ง‘๊ธ€ ์ž‘์„ฑ ์š”์ฒญ @GetMapping("/create") - public ApiResponse createRecruitmentPage(@LoginUser Users loginUser) { + public ApiResponse createRecruitmentPage(@LoginUser Users loginUser) { return recruitmentService.createRequest(loginUser); } // ๋ชจ์ง‘๊ธ€ ์ƒ์„ฑ @PostMapping("/create") - public ApiResponse createRecruitment(@Valid @LoginUser Users loginUser, - @RequestBody CreateRecruitmentRequestDto createRecruitmentRequestDto) { - return recruitmentService.createRecruitment(loginUser, createRecruitmentRequestDto); + public ApiResponse createRecruitment(@Valid @LoginUser Users loginUser, + @RequestBody CreateRecruitmentRequest createRecruitmentRequest) { + return recruitmentService.createRecruitment(loginUser, createRecruitmentRequest); } // ๋ชจ์ง‘๊ธ€ ์ƒ์„ธ ์กฐํšŒ @GetMapping("/{recruitmentId}") - public ApiResponse recruitmentDetailInfo(@PathVariable Long recruitmentId) { + public ApiResponse recruitmentDetailInfo(@PathVariable Long recruitmentId) { return recruitmentService.getRecruitment(recruitmentId); } // ๋ชจ์ง‘๊ธ€ ์ˆ˜์ • @PutMapping("/{recruitmentId}") - public ApiResponse updateRecruitment(@Valid @PathVariable Long recruitmentId, @RequestBody UpdateRecruitmentRequestDto updateRecruitmentRequestDto) { - return recruitmentService.updateRecruitment(recruitmentId, updateRecruitmentRequestDto); + public ApiResponse updateRecruitment(@Valid @PathVariable Long recruitmentId, @RequestBody UpdateRecruitmentRequest updateRecruitmentRequest) { + return recruitmentService.updateRecruitment(recruitmentId, updateRecruitmentRequest); } // ๋ชจ์ง‘๊ธ€ ์‚ญ์ œ @DeleteMapping("/{recruitmentId}") - public ApiResponse deleteRecruitment(@PathVariable Long recruitmentId) { + public ApiResponse deleteRecruitment(@PathVariable Long recruitmentId) { return recruitmentService.deleteRecruitment(recruitmentId); } // ๋Œ“๊ธ€ ์ž‘์„ฑ @PostMapping("/comment/{recruitmentId}") - public ApiResponse createComment(@Valid @PathVariable Long recruitmentId, - @LoginUser Users loginUser, - @RequestBody CreateCommentRequestDto createCommentRequestDto) { - CommentSuccessDto commentSuccessDto = commentService.createComment(loginUser, recruitmentId, createCommentRequestDto); - notificationService.notifyComment(recruitmentId, commentSuccessDto.getCommentId()); - return ApiResponse.ok(commentSuccessDto); + public ApiResponse createComment(@Valid @PathVariable Long recruitmentId, + @LoginUser Users loginUser, + @RequestBody CreateCommentRequest createCommentRequest) { + CommentSuccessResponse commentSuccessResponse = commentService.createComment(loginUser, recruitmentId, createCommentRequest); + notificationService.notifyComment(recruitmentId, commentSuccessResponse.getCommentId()); + return ApiResponse.ok(commentSuccessResponse); } // ๋Œ“๊ธ€ ์ˆ˜์ • @PutMapping("/comment/{recruitmentId}/{commentId}") - public ApiResponse updateComment(@Valid @PathVariable Long recruitmentId, @PathVariable Long commentId, - @RequestBody UpdateCommentRequestDto updateCommentRequestDto) { - return commentService.updateComment(recruitmentId, commentId, updateCommentRequestDto); + public ApiResponse updateComment(@Valid @PathVariable Long recruitmentId, @PathVariable Long commentId, + @RequestBody UpdateCommentRequest updateCommentRequest) { + return commentService.updateComment(recruitmentId, commentId, updateCommentRequest); } // ๋Œ“๊ธ€ ์‚ญ์ œ @DeleteMapping("/comment/{recruitmentId}/{commentId}") - public ApiResponse deleteComment(@PathVariable Long recruitmentId, @PathVariable Long commentId) { + public ApiResponse deleteComment(@PathVariable Long recruitmentId, @PathVariable Long commentId) { return commentService.deleteComment(recruitmentId, commentId); } diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/CreateCommentRequestDto.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/CreateCommentRequest.java similarity index 68% rename from MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/CreateCommentRequestDto.java rename to MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/CreateCommentRequest.java index 62bb70c..8778898 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/CreateCommentRequestDto.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/CreateCommentRequest.java @@ -2,13 +2,11 @@ import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotNull; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.*; -@Data -@NoArgsConstructor -public class CreateCommentRequestDto { +@Getter +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class CreateCommentRequest { @NotNull(message = "๋‚ด์šฉ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”!") @NotEmpty(message = "๋‚ด์šฉ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”!") diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/CreateRecruitmentRequestDto.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/CreateRecruitmentRequest.java similarity index 58% rename from MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/CreateRecruitmentRequestDto.java rename to MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/CreateRecruitmentRequest.java index e066410..34fcee2 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/CreateRecruitmentRequestDto.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/CreateRecruitmentRequest.java @@ -3,12 +3,11 @@ import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; -import lombok.Builder; -import lombok.Data; +import lombok.*; -@Data -@Builder -public class CreateRecruitmentRequestDto { +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class CreateRecruitmentRequest { @NotNull(message = "๊ทธ๋ฃน์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”!") @NotEmpty(message = "๊ทธ๋ฃน์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”!") @@ -23,4 +22,13 @@ public class CreateRecruitmentRequestDto { @NotEmpty(message = "๋‚ด์šฉ์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”!") private String content; + private CreateRecruitmentRequest(Long recruitGroupId, String title, String content) { + this.recruitGroupId = recruitGroupId; + this.title = title; + this.content = content; + } + + public static CreateRecruitmentRequest of(Long recruitGroupId, String title, String content) { + return new CreateRecruitmentRequest(recruitGroupId, title, content); + } } diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/UpdateCommentRequestDto.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/UpdateCommentRequest.java similarity index 60% rename from MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/UpdateCommentRequestDto.java rename to MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/UpdateCommentRequest.java index 9793686..fbe6e24 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/UpdateCommentRequestDto.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/UpdateCommentRequest.java @@ -2,15 +2,11 @@ import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotNull; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.*; -@Data -@AllArgsConstructor -@NoArgsConstructor -public class UpdateCommentRequestDto { +@Getter +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class UpdateCommentRequest { @NotNull(message = "๋Œ“๊ธ€์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”") @NotEmpty(message = "๋Œ“๊ธ€์„ ์ž‘์„ฑํ•ด์ฃผ์„ธ์š”") diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/UpdateRecruitmentRequestDto.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/UpdateRecruitmentRequest.java similarity index 82% rename from MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/UpdateRecruitmentRequestDto.java rename to MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/UpdateRecruitmentRequest.java index 697240a..459fc97 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/UpdateRecruitmentRequestDto.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/request/UpdateRecruitmentRequest.java @@ -4,16 +4,11 @@ import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.Size; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Data; -import lombok.NoArgsConstructor; +import lombok.*; -@Builder -@Data -@AllArgsConstructor -@NoArgsConstructor -public class UpdateRecruitmentRequestDto { +@Getter +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public class UpdateRecruitmentRequest { @NotNull(message = "๊ทธ๋ฃน์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”!") @NotEmpty(message = "๊ทธ๋ฃน์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”!") diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/CommentResponse.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/CommentResponse.java new file mode 100644 index 0000000..fd254f1 --- /dev/null +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/CommentResponse.java @@ -0,0 +1,41 @@ +package MathCaptain.weakness.domain.Recruitment.dto.response; + +import MathCaptain.weakness.domain.Recruitment.entity.Comment; +import lombok.*; + +import java.time.LocalDateTime; + +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class CommentResponse { + + private Long commentId; + + private String authorName; + + private String content; + + private LocalDateTime createdAt; + + private LocalDateTime updatedAt; + + @Builder + private CommentResponse(Long commentId, String authorName, String content, LocalDateTime createdAt, LocalDateTime updatedAt) { + this.commentId = commentId; + this.authorName = authorName; + this.content = content; + this.createdAt = createdAt; + this.updatedAt = updatedAt; + } + + public static CommentResponse of(Comment comment) { + return CommentResponse.builder() + .commentId(comment.getCommentId()) + .authorName(comment.getAuthor().getName()) + .authorName(comment.getAuthor().getName()) + .content(comment.getContent()) + .createdAt(comment.getCommentTime()) + .updatedAt(comment.getLastModifiedTime()) + .build(); + } +} diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/CommentResponseDto.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/CommentResponseDto.java deleted file mode 100644 index 1626d27..0000000 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/CommentResponseDto.java +++ /dev/null @@ -1,21 +0,0 @@ -package MathCaptain.weakness.domain.Recruitment.dto.response; - -import lombok.Builder; -import lombok.Data; - -import java.time.LocalDateTime; - -@Data -@Builder -public class CommentResponseDto { - - private Long commentId; - - private String authorName; - - private String content; - - private LocalDateTime createdAt; - - private LocalDateTime updatedAt; -} diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/CommentSuccessDto.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/CommentSuccessDto.java deleted file mode 100644 index d86e185..0000000 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/CommentSuccessDto.java +++ /dev/null @@ -1,13 +0,0 @@ -package MathCaptain.weakness.domain.Recruitment.dto.response; - -import lombok.Builder; -import lombok.Data; - -@Data -@Builder -public class CommentSuccessDto { - - private Long recruitmentId; - - private Long commentId; -} diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/CommentSuccessResponse.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/CommentSuccessResponse.java new file mode 100644 index 0000000..6d17191 --- /dev/null +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/CommentSuccessResponse.java @@ -0,0 +1,26 @@ +package MathCaptain.weakness.domain.Recruitment.dto.response; + +import MathCaptain.weakness.domain.Recruitment.entity.Comment; +import lombok.*; + +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class CommentSuccessResponse { + + private Long recruitmentId; + + private Long commentId; + + private CommentSuccessResponse(Long recruitmentId, Long commentId) { + this.recruitmentId = recruitmentId; + this.commentId = commentId; + } + + public static CommentSuccessResponse of(Long recruitmentId, Long commentId) { + return new CommentSuccessResponse(recruitmentId, commentId); + } + + public static CommentSuccessResponse of(Long recruitmentId, Comment comment) { + return new CommentSuccessResponse(recruitmentId, comment.getCommentId()); + } +} diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentCreateResponse.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentCreateResponse.java new file mode 100644 index 0000000..41eef35 --- /dev/null +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentCreateResponse.java @@ -0,0 +1,25 @@ +package MathCaptain.weakness.domain.Recruitment.dto.response; + +import MathCaptain.weakness.domain.Group.entity.RelationBetweenUserAndGroup; +import lombok.*; + +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class RecruitmentCreateResponse { + + private Long groupId; + + private String leaderName; + + private String groupName; + + private RecruitmentCreateResponse(Long groupId, String leaderName, String groupName) { + this.groupId = groupId; + this.leaderName = leaderName; + this.groupName = groupName; + } + + public static RecruitmentCreateResponse of(RelationBetweenUserAndGroup relation) { + return new RecruitmentCreateResponse(relation.getGroup().getId(), relation.getGroup().getName(), relation.getMember().getName()); + } +} diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentCreateResponseDto.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentCreateResponseDto.java deleted file mode 100644 index 0464db7..0000000 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentCreateResponseDto.java +++ /dev/null @@ -1,15 +0,0 @@ -package MathCaptain.weakness.domain.Recruitment.dto.response; - -import lombok.Builder; -import lombok.Data; - -@Data -@Builder -public class RecruitmentCreateResponseDto { - - private final Long groupId; - - private final String leaderName; - - private final String groupName; -} diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentDetailResponse.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentDetailResponse.java new file mode 100644 index 0000000..34ac28d --- /dev/null +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentDetailResponse.java @@ -0,0 +1,61 @@ +package MathCaptain.weakness.domain.Recruitment.dto.response; + +import MathCaptain.weakness.domain.Group.enums.CategoryStatus; +import MathCaptain.weakness.domain.Recruitment.entity.Recruitment; +import MathCaptain.weakness.domain.Recruitment.enums.RecruitmentStatus; +import lombok.*; + +import java.time.LocalDateTime; +import java.util.List; + +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class RecruitmentDetailResponse { + + private String authorName; + + private String recruitGroupName; + + private String title; + + private CategoryStatus category; + + private String content; + + private RecruitmentStatus recruitmentStatus; + + private LocalDateTime createdAt; + + private LocalDateTime updatedAt; + + private List comments; + + @Builder + private RecruitmentDetailResponse(String authorName, String recruitGroupName, String title, + CategoryStatus category, String content, RecruitmentStatus recruitmentStatus, + LocalDateTime createdAt, LocalDateTime updatedAt, List comments) { + this.authorName = authorName; + this.recruitGroupName = recruitGroupName; + this.title = title; + this.category = category; + this.content = content; + this.recruitmentStatus = recruitmentStatus; + this.createdAt = createdAt; + this.updatedAt = updatedAt; + this.comments = comments; + } + + public static RecruitmentDetailResponse of(Recruitment recruitment, List comments) { + return RecruitmentDetailResponse.builder() + .authorName(recruitment.getAuthor().getName()) + .recruitGroupName(recruitment.getRecruitGroup().getName()) + .title(recruitment.getTitle()) + .category(recruitment.getCategory()) + .content(recruitment.getContent()) + .recruitmentStatus(recruitment.getRecruitmentStatus()) + .createdAt(recruitment.getPostTime()) + .updatedAt(recruitment.getLastModifiedTime()) + .comments(comments) + .build(); + } +} diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentDetailResponseDto.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentDetailResponseDto.java deleted file mode 100644 index 0b5be20..0000000 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentDetailResponseDto.java +++ /dev/null @@ -1,32 +0,0 @@ -package MathCaptain.weakness.domain.Recruitment.dto.response; - -import MathCaptain.weakness.domain.Group.enums.CategoryStatus; -import MathCaptain.weakness.domain.Recruitment.enums.RecruitmentStatus; -import lombok.Builder; -import lombok.Data; - -import java.time.LocalDateTime; -import java.util.List; - -@Data -@Builder -public class RecruitmentDetailResponseDto { - - private String authorName; - - private String recruitGroupName; - - private String title; - - private CategoryStatus category; - - private String content; - - private RecruitmentStatus recruitmentStatus; - - private LocalDateTime createdAt; - - private LocalDateTime updatedAt; - - private List comments; -} diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentResponse.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentResponse.java new file mode 100644 index 0000000..9f1a96f --- /dev/null +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentResponse.java @@ -0,0 +1,56 @@ +package MathCaptain.weakness.domain.Recruitment.dto.response; + +import MathCaptain.weakness.domain.Group.enums.CategoryStatus; +import MathCaptain.weakness.domain.Recruitment.entity.Recruitment; +import MathCaptain.weakness.domain.Recruitment.enums.RecruitmentStatus; +import lombok.*; + +import java.time.LocalDateTime; + +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class RecruitmentResponse { + + private String authorName; + + private String recruitGroupName; + + private String title; + + private CategoryStatus category; + + private String content; + + private RecruitmentStatus recruitmentStatus; + + private LocalDateTime createdAt; + + private LocalDateTime updatedAt; + + @Builder + private RecruitmentResponse(String authorName, String recruitGroupName, String title, + CategoryStatus category, String content, RecruitmentStatus recruitmentStatus, + LocalDateTime createdAt, LocalDateTime updatedAt) { + this.authorName = authorName; + this.recruitGroupName = recruitGroupName; + this.title = title; + this.category = category; + this.content = content; + this.recruitmentStatus = recruitmentStatus; + this.createdAt = createdAt; + this.updatedAt = updatedAt; + } + + public static RecruitmentResponse of(Recruitment recruitment) { + return RecruitmentResponse.builder() + .authorName(recruitment.getAuthor().getName()) + .recruitGroupName(recruitment.getRecruitGroup().getName()) + .title(recruitment.getTitle()) + .category(recruitment.getCategory()) + .content(recruitment.getContent()) + .recruitmentStatus(recruitment.getRecruitmentStatus()) + .createdAt(recruitment.getPostTime()) + .updatedAt(recruitment.getLastModifiedTime()) + .build(); + } +} diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentResponseDto.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentResponseDto.java deleted file mode 100644 index 0c9d3da..0000000 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentResponseDto.java +++ /dev/null @@ -1,29 +0,0 @@ -package MathCaptain.weakness.domain.Recruitment.dto.response; - -import MathCaptain.weakness.domain.Group.enums.CategoryStatus; -import MathCaptain.weakness.domain.Recruitment.enums.RecruitmentStatus; -import lombok.Builder; -import lombok.Data; - -import java.time.LocalDateTime; - -@Builder -@Data -public class RecruitmentResponseDto { - - private String authorName; - - private String recruitGroupName; - - private String title; - - private CategoryStatus category; - - private String content; - - private RecruitmentStatus recruitmentStatus; - - private LocalDateTime createdAt; - - private LocalDateTime updatedAt; -} diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentSuccessDto.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentSuccessDto.java deleted file mode 100644 index 9055d2c..0000000 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/dto/response/RecruitmentSuccessDto.java +++ /dev/null @@ -1,12 +0,0 @@ -package MathCaptain.weakness.domain.Recruitment.dto.response; - -import lombok.Builder; -import lombok.Data; - -@Data -@Builder -public class RecruitmentSuccessDto { - - private Long recruitmentId; - -} diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/entity/Comment.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/entity/Comment.java index c90df7f..0145c9f 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/entity/Comment.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/entity/Comment.java @@ -1,6 +1,7 @@ package MathCaptain.weakness.domain.Recruitment.entity; -import MathCaptain.weakness.domain.Recruitment.dto.request.UpdateCommentRequestDto; +import MathCaptain.weakness.domain.Recruitment.dto.request.CreateCommentRequest; +import MathCaptain.weakness.domain.Recruitment.dto.request.UpdateCommentRequest; import MathCaptain.weakness.domain.User.entity.Users; import jakarta.persistence.*; import lombok.*; @@ -12,9 +13,7 @@ @Entity @Getter -@Builder @NoArgsConstructor(access = AccessLevel.PROTECTED) -@AllArgsConstructor @EntityListeners(AuditingEntityListener.class) @Table(name = "COMMENT") public class Comment { @@ -41,16 +40,26 @@ public class Comment { @LastModifiedDate private LocalDateTime lastModifiedTime; - //==์ˆ˜์ •==// - public void updateContent(String content) { - if (content != null && !content.equals(this.content)) { - this.content = content; - this.lastModifiedTime = LocalDateTime.now(); - } + @Builder + private Comment(Recruitment post, Users author, String content) { + this.post = post; + this.author = author; + this.content = content; + this.commentTime = LocalDateTime.now(); + this.lastModifiedTime = LocalDateTime.now(); } - public void updateComment(UpdateCommentRequestDto requestDto) { - updateContent(requestDto.getContent()); + public static Comment of(Recruitment recruitment, Users author, CreateCommentRequest createCommentRequest) { + return new Comment(recruitment, author, createCommentRequest.getContent()); + } + + public static Comment of(Recruitment recruitment, Users author, String content) { + return new Comment(recruitment, author, content); + } + + public void updateComment(UpdateCommentRequest updateRequest) { + this.content = updateRequest.getContent(); + this.lastModifiedTime = LocalDateTime.now(); } public Boolean isBelongToPost(Long recruitmentId) { diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/entity/Recruitment.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/entity/Recruitment.java index 61236a3..ba71d36 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/entity/Recruitment.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/entity/Recruitment.java @@ -2,7 +2,8 @@ import MathCaptain.weakness.domain.Group.entity.Group; import MathCaptain.weakness.domain.Group.enums.CategoryStatus; -import MathCaptain.weakness.domain.Recruitment.dto.request.UpdateRecruitmentRequestDto; +import MathCaptain.weakness.domain.Recruitment.dto.request.CreateRecruitmentRequest; +import MathCaptain.weakness.domain.Recruitment.dto.request.UpdateRecruitmentRequest; import MathCaptain.weakness.domain.Recruitment.enums.RecruitmentStatus; import MathCaptain.weakness.domain.User.entity.Users; import jakarta.persistence.*; @@ -17,9 +18,7 @@ @Entity @Getter -@Builder @NoArgsConstructor(access = AccessLevel.PROTECTED) -@AllArgsConstructor @EntityListeners(AuditingEntityListener.class) public class Recruitment { @@ -62,52 +61,35 @@ public class Recruitment { @LastModifiedDate private LocalDateTime lastModifiedTime; - // ๊ธฐ๋ณธ๊ฐ’ ์„ค์ • - @PrePersist - protected void onCreate() { - this.postTime = LocalDateTime.now(); - this.lastModifiedTime = LocalDateTime.now(); + @Builder + private Recruitment(Users author, Group recruitGroup, CategoryStatus category, String title, String content, List comments) { + this.author = author; + this.recruitGroup = recruitGroup; + this.category = category; + this.title = title; + this.content = content; this.recruitmentStatus = RecruitmentStatus.RECRUITING; this.interestCount = 0L; - } - - //== ์ˆ˜์ • ๋กœ์ง ==// - @PreUpdate - protected void onUpdate() { + this.comments = comments; + this.postTime = LocalDateTime.now(); this.lastModifiedTime = LocalDateTime.now(); } - public void updateTitle(String title) { - if (title != null && !title.equals(this.title)) - { - this.title = title; - updateLastModifiedTime(); - } + public static Recruitment of(Users author, Group group, CreateRecruitmentRequest createRecruitmentRequest) { + return Recruitment.builder() + .author(author) + .recruitGroup(group) + .title(createRecruitmentRequest.getTitle()) + .content(createRecruitmentRequest.getContent()) + .category(group.getCategory()) + .build(); } - public void updateContent(String content) { - if (content != null && !content.equals(this.content)) - { - this.content = content; - updateLastModifiedTime(); - } - } - - public void updateRecruitmentStatus(RecruitmentStatus recruitmentStatus) { - if (recruitmentStatus != null && !recruitmentStatus.equals(this.recruitmentStatus)) - { - this.recruitmentStatus = recruitmentStatus; - updateLastModifiedTime(); - } - } - - private void updateLastModifiedTime() { + //== ์ˆ˜์ • ๋กœ์ง ==// + public void updateRecruitment(UpdateRecruitmentRequest requestDto) { + this.title = requestDto.getTitle(); + this.content = requestDto.getContent(); + this.recruitmentStatus = requestDto.getRecruitmentStatus(); this.lastModifiedTime = LocalDateTime.now(); } - - public void updateRecruitment(UpdateRecruitmentRequestDto requestDto) { - updateTitle(requestDto.getTitle()); - updateContent(requestDto.getContent()); - updateRecruitmentStatus(requestDto.getRecruitmentStatus()); - } } diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/service/CommentService.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/service/CommentService.java index c7f6a52..5b476b4 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/service/CommentService.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/service/CommentService.java @@ -2,10 +2,10 @@ import MathCaptain.weakness.domain.Recruitment.entity.Comment; import MathCaptain.weakness.domain.Recruitment.entity.Recruitment; -import MathCaptain.weakness.domain.Recruitment.dto.request.CreateCommentRequestDto; -import MathCaptain.weakness.domain.Recruitment.dto.request.UpdateCommentRequestDto; -import MathCaptain.weakness.domain.Recruitment.dto.response.CommentResponseDto; -import MathCaptain.weakness.domain.Recruitment.dto.response.CommentSuccessDto; +import MathCaptain.weakness.domain.Recruitment.dto.request.CreateCommentRequest; +import MathCaptain.weakness.domain.Recruitment.dto.request.UpdateCommentRequest; +import MathCaptain.weakness.domain.Recruitment.dto.response.CommentResponse; +import MathCaptain.weakness.domain.Recruitment.dto.response.CommentSuccessResponse; import MathCaptain.weakness.domain.Recruitment.repository.CommentRepository; import MathCaptain.weakness.domain.Recruitment.repository.RecruitmentRepository; import MathCaptain.weakness.domain.User.entity.Users; @@ -29,87 +29,49 @@ public class CommentService { /// ๋Œ“๊ธ€ CRUD // ๋Œ“๊ธ€ ์ƒ์„ฑ - public CommentSuccessDto createComment(Users user, Long recruitmentId, CreateCommentRequestDto createCommentRequestDto) { - - Recruitment recruitment = recruitmentRepository.findById(recruitmentId) - .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๋ชจ์ง‘๊ธ€์ด ์—†์Šต๋‹ˆ๋‹ค.")); - - String content = createCommentRequestDto.getContent(); - - Comment comment = Comment.builder() - .post(recruitment) - .author(user) - .content(content) - .build(); - + public CommentSuccessResponse createComment(Users user, Long recruitmentId, CreateCommentRequest createCommentRequest) { + Recruitment recruitment = findRecruitmentBy(recruitmentId); + Comment comment = Comment.of(recruitment, user, createCommentRequest); commentRepository.save(comment); - - return CommentSuccessDto.builder() - .commentId(comment.getCommentId()) - .recruitmentId(recruitmentId) - .build(); + return CommentSuccessResponse.of(recruitmentId, comment); } // ๋Œ“๊ธ€ ์ˆ˜์ • - public ApiResponse updateComment(Long recruitmentId, Long commentId, UpdateCommentRequestDto updateCommentRequestDto) { - - Comment comment = commentRepository.findById(commentId) - .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๋Œ“๊ธ€์ด ์—†์Šต๋‹ˆ๋‹ค.")); - - if (comment.isBelongToPost(recruitmentId)) { - throw new IllegalArgumentException("ํ•ด๋‹น ๋Œ“๊ธ€์ด ํ•ด๋‹น ๋ชจ์ง‘๊ธ€์— ์†ํ•ด์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); - } - - comment.updateComment(updateCommentRequestDto); - - return ApiResponse.ok(CommentSuccessDto.builder() - .commentId(commentId) - .recruitmentId(recruitmentId) - .build()); + public ApiResponse updateComment(Long recruitmentId, Long commentId, UpdateCommentRequest updateCommentRequest) { + Comment comment = findCommentBy(commentId, recruitmentId); + comment.updateComment(updateCommentRequest); + return ApiResponse.ok(CommentSuccessResponse.of(recruitmentId, commentId)); } // ๋Œ“๊ธ€ ์‚ญ์ œ - - public ApiResponse deleteComment(Long recruitmentId, Long commentId) { - Comment comment = commentRepository.findById(commentId) - .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๋Œ“๊ธ€์ด ์—†์Šต๋‹ˆ๋‹ค.")); - - if (comment.isBelongToPost(recruitmentId)) { - throw new IllegalArgumentException("ํ•ด๋‹น ๋Œ“๊ธ€์ด ํ•ด๋‹น ๋ชจ์ง‘๊ธ€์— ์†ํ•ด์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); - } - + public ApiResponse deleteComment(Long recruitmentId, Long commentId) { + Comment comment = findCommentBy(commentId, recruitmentId); commentRepository.delete(comment); - - return ApiResponse.ok(CommentSuccessDto.builder() - .commentId(commentId) - .recruitmentId(recruitmentId) - .build()); + return ApiResponse.ok(CommentSuccessResponse.of(recruitmentId, commentId)); } - // ๋Œ“๊ธ€ ์กฐํšŒ - - public List getComments(Long recruitmentId) { - Recruitment recruitment = recruitmentRepository.findById(recruitmentId) - .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๋ชจ์ง‘๊ธ€์ด ์—†์Šต๋‹ˆ๋‹ค.")); + // ๋Œ“๊ธ€ ์กฐํšŒ + public List getComments(Long recruitmentId) { + Recruitment recruitment = findRecruitmentBy(recruitmentId); List commentList = commentRepository.findAllByPost(recruitment); - return commentList.stream() - .map(this::buildCommentResponseDto) + .map(CommentResponse::of) .toList(); } - /// ๋นŒ๋” - - private CommentResponseDto buildCommentResponseDto(Comment comment) { - return CommentResponseDto.builder() - .commentId(comment.getCommentId()) - .authorName(comment.getAuthor().getName()) - .authorName(comment.getAuthor().getName()) - .content(comment.getContent()) - .createdAt(comment.getCommentTime()) - .updatedAt(comment.getLastModifiedTime()) - .build(); + private Recruitment findRecruitmentBy(Long recruitmentId) { + return recruitmentRepository.findById(recruitmentId) + .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๋ชจ์ง‘๊ธ€์ด ์—†์Šต๋‹ˆ๋‹ค.")); } + private Comment findCommentBy(Long commentId, Long recruitmentId) { + Comment comment = commentRepository.findById(commentId) + .orElseThrow(() -> new IllegalArgumentException("ํ•ด๋‹น ๋Œ“๊ธ€์ด ์—†์Šต๋‹ˆ๋‹ค.")); + if (comment.isBelongToPost(recruitmentId)) { + throw new IllegalArgumentException("ํ•ด๋‹น ๋Œ“๊ธ€์ด ํ•ด๋‹น ๋ชจ์ง‘๊ธ€์— ์†ํ•ด์žˆ์ง€ ์•Š์Šต๋‹ˆ๋‹ค."); + } + + return comment; + } } diff --git a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/service/RecruitmentService.java b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/service/RecruitmentService.java index d34a79e..7e339a0 100644 --- a/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/service/RecruitmentService.java +++ b/MathCaptain/weakness/src/main/java/MathCaptain/weakness/domain/Recruitment/service/RecruitmentService.java @@ -7,9 +7,8 @@ import MathCaptain.weakness.domain.Group.repository.RelationRepository; import MathCaptain.weakness.domain.Recruitment.dto.response.*; import MathCaptain.weakness.domain.Recruitment.entity.Recruitment; -import MathCaptain.weakness.domain.Recruitment.dto.request.CreateRecruitmentRequestDto; -import MathCaptain.weakness.domain.Recruitment.dto.request.UpdateRecruitmentRequestDto; -import MathCaptain.weakness.domain.Recruitment.dto.response.*; +import MathCaptain.weakness.domain.Recruitment.dto.request.CreateRecruitmentRequest; +import MathCaptain.weakness.domain.Recruitment.dto.request.UpdateRecruitmentRequest; import MathCaptain.weakness.domain.Recruitment.repository.RecruitmentRepository; import MathCaptain.weakness.domain.User.entity.Users; import MathCaptain.weakness.global.Api.ApiResponse; @@ -19,7 +18,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.time.LocalDateTime; import java.util.List; @Service @@ -35,117 +33,60 @@ public class RecruitmentService { /// ๋ชจ์ง‘ CRUD // ๋ชจ์ง‘๊ธ€ ์ž‘์„ฑ ์š”์ฒญ (๊ทธ๋ฃน ์ •๋ณด ๋ฐ˜ํ™˜) - public ApiResponse createRequest(Users user) { - - RelationBetweenUserAndGroup relation = relationRepository.findByMemberAndGroupRole(user, GroupRole.LEADER) - .orElseThrow(() -> new IllegalArgumentException("๊ทธ๋ฃน์žฅ๋งŒ ๋ชจ์ง‘๊ธ€์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.")); - - return ApiResponse.ok(RecruitmentCreateResponseDto.builder() - .groupId(relation.getGroup().getId()) - .groupName(relation.getGroup().getName()) - .leaderName(relation.getMember().getName()) - .build()); + public ApiResponse createRequest(Users user) { + RelationBetweenUserAndGroup relation = findLeaderRelationBy(user); + return ApiResponse.ok(RecruitmentCreateResponse.of(relation)); } // ๋ชจ์ง‘๊ธ€ ์ƒ์„ฑ - public ApiResponsecreateRecruitment(Users author, CreateRecruitmentRequestDto createRecruitmentRequestDto) { - - Recruitment recruitment = buildRecruitment(author, createRecruitmentRequestDto); - - return ApiResponse.ok(RecruitmentSuccessDto.builder() - .recruitmentId(recruitmentRepository.save(recruitment).getPostId()) - .build()); + public ApiResponsecreateRecruitment(Users author, CreateRecruitmentRequest createRecruitmentRequest) { + Group group = getGroupBy(createRecruitmentRequest); + Recruitment recruitment = Recruitment.of(author, group, createRecruitmentRequest); + Long recruitmentId = recruitmentRepository.save(recruitment).getPostId(); + return ApiResponse.ok(recruitmentId); } // ๋ชจ์ง‘๊ธ€ ์กฐํšŒ - public ApiResponse getRecruitment(Long recruitmentId) { - Recruitment recruitment = recruitmentRepository.findById(recruitmentId). - orElseThrow(() -> new ResourceNotFoundException("ํ•ด๋‹น ๋ชจ์ง‘๊ธ€์ด ์—†์Šต๋‹ˆ๋‹ค.")); - - List comments = commentService.getComments(recruitmentId); - - return ApiResponse.ok(buildRecruitmentDetailResponseDto(recruitment, comments)); + public ApiResponse getRecruitment(Long recruitmentId) { + Recruitment recruitment = findRecruitmentBy(recruitmentId); + List comments = commentService.getComments(recruitmentId); + return ApiResponse.ok(RecruitmentDetailResponse.of(recruitment, comments)); } // ๋ชจ์ง‘๊ธ€ ์ˆ˜์ • - public ApiResponse updateRecruitment(Long recruitmentId, UpdateRecruitmentRequestDto updateRecruitmentRequestDto) { - - Recruitment recruitment = recruitmentRepository.findById(recruitmentId). - orElseThrow(() -> new ResourceNotFoundException("ํ•ด๋‹น ๋ชจ์ง‘๊ธ€์ด ์—†์Šต๋‹ˆ๋‹ค.")); - - recruitment.updateRecruitment(updateRecruitmentRequestDto); - - log.info("๋ชจ์ง‘๊ธ€ ์ˆ˜์ • ์™„๋ฃŒ, ๋ชจ์ง‘๊ธ€ ID : {}", recruitmentId); - - return ApiResponse.ok(RecruitmentSuccessDto.builder() - .recruitmentId(recruitmentId) - .build()); + public ApiResponse updateRecruitment(Long recruitmentId, UpdateRecruitmentRequest updateRecruitmentRequest) { + Recruitment recruitment = findRecruitmentBy(recruitmentId); + recruitment.updateRecruitment(updateRecruitmentRequest); + return ApiResponse.ok(recruitmentId); } - public ApiResponse deleteRecruitment(Long recruitmentId) { - Recruitment recruitment = recruitmentRepository.findById(recruitmentId) - .orElseThrow(() -> new ResourceNotFoundException("ํ•ด๋‹น ๋ชจ์ง‘๊ธ€์ด ์—†์Šต๋‹ˆ๋‹ค.")); - + public ApiResponse deleteRecruitment(Long recruitmentId) { + Recruitment recruitment = findRecruitmentBy(recruitmentId); recruitmentRepository.delete(recruitment); - - return ApiResponse.ok(RecruitmentSuccessDto.builder() - .recruitmentId(recruitmentId) - .build()); + return ApiResponse.ok(recruitmentId); } // ๋ชจ์ง‘๊ธ€ ์ „์ฒด ์กฐํšŒ - public ApiResponse> getAllRecruitments() { + public ApiResponse> getAllRecruitments() { List recruitments = recruitmentRepository.findAll(); - return ApiResponse.ok(recruitments.stream() - .map(this::buildRecruitmentResponseDto) + .map(RecruitmentResponse::of) .toList()); } - /// ๋นŒ๋“œ - - private RecruitmentResponseDto buildRecruitmentResponseDto(Recruitment recruitment) { - return RecruitmentResponseDto.builder() - .authorName(recruitment.getAuthor().getName()) - .recruitGroupName(recruitment.getRecruitGroup().getName()) - .title(recruitment.getTitle()) - .category(recruitment.getCategory()) - .content(recruitment.getContent()) - .recruitmentStatus(recruitment.getRecruitmentStatus()) - .createdAt(recruitment.getPostTime()) - .updatedAt(recruitment.getLastModifiedTime()) - .build(); + private Group getGroupBy(CreateRecruitmentRequest createRecruitmentRequest) { + return groupRepository.findById(createRecruitmentRequest.getRecruitGroupId()). + orElseThrow(() -> new ResourceNotFoundException("ํ•ด๋‹น ๊ทธ๋ฃน์ด ์—†์Šต๋‹ˆ๋‹ค.")); } - private RecruitmentDetailResponseDto buildRecruitmentDetailResponseDto(Recruitment recruitment, List comments) { - - - return RecruitmentDetailResponseDto.builder() - .authorName(recruitment.getAuthor().getName()) - .recruitGroupName(recruitment.getRecruitGroup().getName()) - .title(recruitment.getTitle()) - .category(recruitment.getCategory()) - .content(recruitment.getContent()) - .recruitmentStatus(recruitment.getRecruitmentStatus()) - .createdAt(recruitment.getPostTime()) - .updatedAt(recruitment.getLastModifiedTime()) - .comments(comments) - .build(); + private RelationBetweenUserAndGroup findLeaderRelationBy(Users user) { + return relationRepository.findByMemberAndGroupRole(user, GroupRole.LEADER) + .orElseThrow(() -> new IllegalArgumentException("๊ทธ๋ฃน์žฅ๋งŒ ๋ชจ์ง‘๊ธ€์„ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.")); } - private Recruitment buildRecruitment(Users author, CreateRecruitmentRequestDto createRecruitmentRequestDto) { - - Group relatedGroup = groupRepository.findById(createRecruitmentRequestDto.getRecruitGroupId()). - orElseThrow(() -> new ResourceNotFoundException("ํ•ด๋‹น ๊ทธ๋ฃน์ด ์—†์Šต๋‹ˆ๋‹ค.")); - - return Recruitment.builder() - .author(author) - .recruitGroup(relatedGroup) - .title(createRecruitmentRequestDto.getTitle()) - .content(createRecruitmentRequestDto.getContent()) - .category(relatedGroup.getCategory()) - .lastModifiedTime(LocalDateTime.now()) - .build(); + private Recruitment findRecruitmentBy(Long recruitmentId) { + return recruitmentRepository.findById(recruitmentId). + orElseThrow(() -> new ResourceNotFoundException("ํ•ด๋‹น ๋ชจ์ง‘๊ธ€์ด ์—†์Šต๋‹ˆ๋‹ค.")); } }