Skip to content

Commit 63e37ae

Browse files
authored
Merge pull request #145 from CommitField/feat/#81
docs : ์•Œ๋ฆผ ๋ฉ”์„œ๋“œ ์„ค๋ช… ์ฃผ์„ ์ถ”๊ฐ€
2 parents 664c19d + 0eac67f commit 63e37ae

File tree

9 files changed

+105
-39
lines changed

9 files changed

+105
-39
lines changed

โ€Žsrc/main/java/cmf/commitField/domain/commit/scheduler/CommitScheduler.javaโ€Ž

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
package cmf.commitField.domain.commit.scheduler;
22

33
import cmf.commitField.domain.commit.totalCommit.service.TotalCommitService;
4-
import cmf.commitField.domain.noti.noti.entity.NotiDetailType;
5-
import cmf.commitField.domain.noti.noti.entity.NotiType;
6-
import cmf.commitField.domain.noti.noti.event.NotiEvent;
74
import cmf.commitField.domain.noti.noti.service.CommitSteakNotiService;
85
import cmf.commitField.domain.noti.noti.service.NotiService;
96
import cmf.commitField.domain.user.entity.User;
@@ -29,8 +26,6 @@ public class CommitScheduler {
2926
private final UserRepository userRepository;
3027
private final StringRedisTemplate redisTemplate;
3128
private final AtomicInteger counter = new AtomicInteger(0);
32-
private final SimpMessagingTemplate messagingTemplate;
33-
private final NotiService notiService;
3429
private final CommitSteakNotiService commitSteakNotiService;
3530

3631
private final ApplicationEventPublisher eventPublisher;
@@ -110,6 +105,7 @@ private void processUserCommit(String username) {
110105
CommitUpdateEvent event = new CommitUpdateEvent(this, username, newCommitCount);
111106
eventPublisher.publishEvent(event); // ์ด๋ฒคํŠธ ๋ฐœ์ƒ
112107

108+
// ์ปค๋ฐ‹ ์—…๋ฐ์ดํŠธ๊ฐ€ ์žˆ๊ณ , ์—ฐ์† ์ปค๋ฐ‹์ด 3์ผ ์ด์ƒ, 10์˜ ๋ฐฐ์ˆ˜ ์ด์ƒ์ธ ๊ฒฝ์šฐ ์•Œ๋ฆผ ์ƒ์„ฑ
113109
commitSteakNotiService.checkAndCreateSteakNoti(user, currentStreakCommit);
114110

115111
System.out.println("CommitCreatedEvent published for user: " + username);

โ€Žsrc/main/java/cmf/commitField/domain/noti/noti/controller/ApiV1NotiController.javaโ€Ž

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import cmf.commitField.domain.noti.noti.service.NotiService;
55
import cmf.commitField.domain.user.entity.User;
66
import cmf.commitField.domain.user.repository.UserRepository;
7+
import cmf.commitField.domain.user.service.UserService;
78
import cmf.commitField.global.error.ErrorCode;
89
import cmf.commitField.global.exception.CustomException;
910
import cmf.commitField.global.globalDto.GlobalResponse;
@@ -29,32 +30,22 @@
2930
@Slf4j
3031
public class ApiV1NotiController {
3132
private final NotiService notiService;
32-
private final UserRepository userRepository;
33-
34-
// private final NotiWebSocketHandler notiWebSocketHandler;
35-
private final ApplicationEventPublisher eventPublisher;
33+
private final UserService userService;
3634

3735

3836
@GetMapping("")
3937
public GlobalResponse<List<NotiDto>> getNoti(@AuthenticationPrincipal OAuth2User oAuth2User) {
4038
String username = oAuth2User.getName();
41-
User user = userRepository.findByUsername(username).orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_USER));
39+
User user = userService.findByUsername(username);
4240
List<NotiDto> notis = notiService.getNotReadNoti(user);
4341
return GlobalResponse.success(notis);
4442
}
4543

4644
@PostMapping("/read")
47-
public GlobalResponse<Object> readNoti() {
48-
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
49-
50-
if (authentication instanceof OAuth2AuthenticationToken) {
51-
OAuth2User principal = (OAuth2User) authentication.getPrincipal();
52-
Map<String, Object> attributes = principal.getAttributes();
53-
String username = (String) attributes.get("login"); // GitHub ID
54-
User user = userRepository.findByUsername(username).orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_USER));
55-
notiService.read(user);
56-
return GlobalResponse.success("์•Œ๋ฆผ์„ ์ฝ์Œ ์ฒ˜๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.");
57-
}
58-
return GlobalResponse.error(ErrorCode.LOGIN_REQUIRED);
45+
public GlobalResponse<Object> readNoti(@AuthenticationPrincipal OAuth2User oAuth2User) {
46+
String username = oAuth2User.getName();
47+
User user = userService.findByUsername(username);
48+
notiService.read(user);
49+
return GlobalResponse.success("์•Œ๋ฆผ์„ ์ฝ์Œ ์ฒ˜๋ฆฌํ–ˆ์Šต๋‹ˆ๋‹ค.");
5950
}
6051
}

โ€Žsrc/main/java/cmf/commitField/domain/noti/noti/dto/NotiDto.javaโ€Ž

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@ public class NotiDto {
1212
private String message;
1313
private String formattedCreatedAt; // ๋ณ€ํ™˜๋œ ๋‚ ์งœ๋ฅผ ์ €์žฅํ•  ํ•„๋“œ
1414

15-
// JPQL์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•„๋“œ ๊ฐ’ ์ง์ ‘ ๋ฐ›๋Š” ์ƒ์„ฑ์ž ์ถ”๊ฐ€
15+
// ํ•„๋“œ ๊ฐ’ ์ง์ ‘ ๋ฐ›๋Š” ์ƒ์„ฑ์ž ์ถ”๊ฐ€
1616
public NotiDto(Long id, String message, LocalDateTime createdAt) {
1717
this.id = id;
1818
this.message = message;
1919
this.formattedCreatedAt = formatCreatedAt(createdAt); // ๋ณ€ํ™˜๋œ ๋‚ ์งœ ์ €์žฅ
2020
}
2121

22+
// ๋‚ ์งœ ๋ณ€ํ™˜ ๋ฉ”์„œ๋“œ (์˜ค๋Š˜, ์–ด์ œ, 3์ผ ์ „๊นŒ์ง€ ์ดํ›„ yyyy-MM-dd)
2223
private String formatCreatedAt(LocalDateTime createdAt) {
2324
LocalDateTime today = LocalDateTime.now();
2425
long daysBetween = ChronoUnit.DAYS.between(createdAt, today);

โ€Žsrc/main/java/cmf/commitField/domain/noti/noti/entity/NotiDetailType.javaโ€Ž

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ public String[] getParamNames() {
2727
return paramNames;
2828
}
2929

30+
/**
31+
* ๋ฉ”์‹œ์ง€ ํฌ๋งทํŒ…์ž…๋‹ˆ๋‹ค.
32+
* ์ž˜๋ชป๋œ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐœ์ˆ˜์ผ ๊ฒฝ์šฐ IllegalArgumentException์„ ๋˜์ง‘๋‹ˆ๋‹ค.
33+
* @param params
34+
* @return template์— ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ ์šฉํ•œ ๋ฉ”์‹œ์ง€
35+
*/
3036
public String formatMessage(Object... params) {
3137
if (paramNames.length != params.length) {
3238
throw new IllegalArgumentException("๐Ÿšจ ์ž˜๋ชป๋œ ํŒŒ๋ผ๋ฏธํ„ฐ ๊ฐœ์ˆ˜! ํ•„์š”: " +

โ€Žsrc/main/java/cmf/commitField/domain/noti/noti/repository/NotiRepository.javaโ€Ž

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,6 @@ public interface NotiRepository extends JpaRepository<Noti, Long> {
2525
// ์ตœ๊ทผ 10์ผ ๋‚ด ๋™์ผํ•œ ์ปค๋ฐ‹ ๋ถ€์žฌ ์•Œ๋ฆผ์ด ์žˆ๋Š”์ง€ ํ™•์ธ
2626
boolean existsByReceiverAndTypeCodeAndType2CodeAndCreatedAtAfter(User receiver, NotiType type, NotiDetailType detailType, LocalDateTime after);
2727

28+
// ํ•ด๋‹น ํƒ€์ž…์˜ ์•Œ๋ฆผ์ด ์˜ค๋Š˜ ์ดํ›„์— ์žˆ๋Š”์ง€ ํ™•์ธ
2829
boolean existsByReceiverAndType2CodeAndCreatedAtAfter(User receiver, NotiDetailType notiDetailType, LocalDateTime todayStart);
2930
}

โ€Žsrc/main/java/cmf/commitField/domain/noti/noti/service/CommitAbsenceNotiService.javaโ€Ž

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ public void sendCommitAbsenceNoti() {
4242
}
4343
}
4444

45-
// ์ตœ๊ทผ 10์ผ ๋‚ด ์ปค๋ฐ‹ ๋ถ€์žฌ ์•Œ๋ฆผ์ด ์žˆ์—ˆ๋Š”์ง€ ํ™•์ธ
45+
/**
46+
* ์ตœ๊ทผ 10์ผ ๋‚ด ์ปค๋ฐ‹ ๋ถ€์žฌ ์•Œ๋ฆผ์ด ์žˆ์—ˆ๋Š”์ง€ ํ™•์ธ
47+
* @param user
48+
* @return
49+
*/
4650
private boolean hasRecentAbsenceNoti(User user) {
4751
log.info("์ปค๋ฐ‹ ๋ถ€์žฌ ์•Œ๋ฆผ ํ™•์ธ: {}", user.getUsername());
4852
LocalDateTime checkDate = LocalDateTime.now().minusDays(10);

โ€Žsrc/main/java/cmf/commitField/domain/noti/noti/service/CommitSteakNotiService.javaโ€Ž

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public void sendCommitSteakNoti() {
4242
}
4343
}
4444

45+
/**
46+
* ์—ฐ์† ์ปค๋ฐ‹ ์•Œ๋ฆผ์„ ์ƒ์„ฑ
47+
* @param user
48+
* @param currentStreakCommit
49+
*/
4550
public void checkAndCreateSteakNoti(User user, int currentStreakCommit) {
4651
boolean alreadyNotified = notiRepository.existsByReceiverAndType2CodeAndCreatedAtAfter(
4752
user, NotiDetailType.STREAK_CONTINUED, LocalDate.now().atStartOfDay()
@@ -57,7 +62,7 @@ public void checkAndCreateSteakNoti(User user, int currentStreakCommit) {
5762

5863

5964
/**
60-
* ํŠน์ • ์—ฐ์† ์ปค๋ฐ‹ ํšŸ์ˆ˜์— ๋„๋‹ฌํ–ˆ๋Š”์ง€ ํ™•์ธ
65+
* ํŠน์ • ์—ฐ์† ์ปค๋ฐ‹ ํšŸ์ˆ˜(3ํšŒ, 10์˜ ๋ฐฐ์ˆ˜ ํšŸ์ˆ˜)์— ๋„๋‹ฌํ–ˆ๋Š”์ง€ ํ™•์ธ
6166
*/
6267
public boolean shouldNotify(int streak) {
6368
return streak == 3 || (streak % 10 == 0);

โ€Žsrc/main/java/cmf/commitField/domain/noti/noti/service/NotiService.javaโ€Ž

Lines changed: 69 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,19 @@ public class NotiService {
2727
private final NotiRepository notiRepository;
2828
private final ApplicationEventPublisher eventPublisher;
2929

30-
// ์•Œ๋ฆผ ์ƒ์„ฑ
30+
/**
31+
* ์•Œ๋ฆผ ์ƒ์„ฑ ๊ณตํ†ต ๋กœ์ง์ž…๋‹ˆ๋‹ค.
32+
* ๋ฐ›์•„์˜จ params๋ฅผ ์ด์šฉํ•˜์—ฌ ๋ฉ”์‹œ์ง€๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์•Œ๋ฆผ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
33+
* ๋ฉ”์‹œ์ง€์— ํ•„์š”ํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” NotiDetailType์— ์ •์˜๋œ ์ˆœ์„œ๋Œ€๋กœ ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
34+
* ๋งŒ์•ฝ NotiDetailType์— ์ •์˜๋œ ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ์—๋Š” ๋นˆ ๋ฐฐ์—ด์„ ์ „๋‹ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
35+
* ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์•Œ๋ฆผ ์—”ํ‹ฐํ‹ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  WebSocket ์ด๋ฒคํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ต๋‹ˆ๋‹ค.
36+
* @param receiver
37+
* @param notiType
38+
* @param notiDetailType
39+
* @param relId
40+
* @param relTypeCode
41+
* @param params
42+
*/
3143
@Transactional
3244
public void createNoti(User receiver, NotiType notiType, NotiDetailType notiDetailType, Long relId, String relTypeCode, Object... params) {
3345
// ๋ฉ”์‹œ์ง€ ์ƒ์„ฑ
@@ -53,66 +65,109 @@ public void createNoti(User receiver, NotiType notiType, NotiDetailType notiDeta
5365
eventPublisher.publishEvent(event);
5466
}
5567

56-
// ์•Œ๋ฆผ ์กฐํšŒ
68+
/**
69+
* ์•Œ๋ฆผ ์กฐํšŒ
70+
* @param receiver
71+
* @return
72+
*/
5773
public List<NotiDto> getNotReadNoti(User receiver) {
5874
List<NotiDto> notis = notiRepository.findNotiDtoByReceiverId(receiver.getId(), false).orElse(null);
5975
return notis;
6076
}
6177

62-
// ์‹œ์ฆŒ ์•Œ๋ฆผ ํ™•์ธ
78+
/**
79+
* ์‹œ์ฆŒ ์•Œ๋ฆผ ํ™•์ธ
80+
* ์•Œ๋ฆผ์ด ์—†์„ ๊ฒฝ์šฐ ์˜ˆ์™ธ ๋ฐœ์ƒ
81+
* @param receiver
82+
* @param seasonId
83+
* @return List
84+
*/
6385
public List<Noti> getSeasonNotiCheck(User receiver, long seasonId) {
6486
return notiRepository.findNotiByReceiverAndRelId(receiver, seasonId)
6587
.orElseThrow(() -> new CustomException(ErrorCode.ERROR_CHECK)); // ์•Œ๋ฆผ์ด ์—†์„ ๊ฒฝ์šฐ ์˜ˆ์™ธ ๋ฐœ์ƒ
6688
}
6789

68-
// ์ƒˆ ์‹œ์ฆŒ ์•Œ๋ฆผ ์ƒ์„ฑ
90+
/**
91+
* ์ƒˆ ์‹œ์ฆŒ ์•Œ๋ฆผ ์ƒ์„ฑ
92+
* @param season
93+
* @param user
94+
*/
6995
@Transactional
7096
public void createNewSeasonNoti(Season season, User user) {
7197
createNoti(user, NotiType.SEASON, NotiDetailType.SEASON_START, season.getId(), season.getModelName(), season.getName());
7298
}
7399

74-
// ๋žญํ‚น ์—… ์•Œ๋ฆผ ์ƒ์„ฑ
100+
/**
101+
* ๋žญํ‚น ์—… ์•Œ๋ฆผ ์ƒ์„ฑ
102+
* @param user
103+
*/
75104
@Transactional
76105
public void createRankUpNoti(User user) {
77106
createNoti(user, NotiType.RANK, NotiDetailType.RANK_UP, 0L, null, getDisplayName(user), user.getTier().name());
78107
}
79108

80-
// ์—ฐ์† ์ปค๋ฐ‹ ์ถ•ํ•˜ ์•Œ๋ฆผ ์ƒ์„ฑ
109+
/**
110+
* ์—ฐ์† ์ปค๋ฐ‹ ์ถ•ํ•˜ ์•Œ๋ฆผ ์ƒ์„ฑ
111+
* @param user
112+
* @param days
113+
*/
81114
@Transactional
82115
public void createStreakCommitNoti(User user, String days) {
83116
createNoti(user, NotiType.STREAK, NotiDetailType.STREAK_CONTINUED, 0L, null, getDisplayName(user), days);
84117
}
85118

86-
// ์ปค๋ฐ‹ ๋ถ€์žฌ ์•Œ๋ฆผ ์ƒ์„ฑ
119+
/**
120+
* ์ปค๋ฐ‹ ๋ถ€์žฌ ์•Œ๋ฆผ ์ƒ์„ฑ
121+
* @param user
122+
*/
87123
@Transactional
88124
public void createStreakBrokenNoti(User user) {
89125
createNoti(user, NotiType.STREAK, NotiDetailType.STREAK_BROKEN, 0L, null, getDisplayName(user));
90126
}
91127

92-
// ์—…์  ์•Œ๋ฆผ ์ƒ์„ฑ
128+
/**
129+
* ์—…์  ์•Œ๋ฆผ ์ƒ์„ฑ
130+
* @param user
131+
* @param achievementName
132+
*/
133+
// TODO: ์—…์  ๋กœ์ง ๊ตฌํ˜„ ํ›„ ์‚ฌ์šฉ
93134
@Transactional
94135
public void createAchievementNoti(User user, String achievementName) {
95136
createNoti(user, NotiType.ACHIEVEMENT, NotiDetailType.ACHIEVEMENT_COMPLETED, 0L, null, getDisplayName(user), achievementName);
96137
}
97138

98-
// ๊ณต์ง€์‚ฌํ•ญ ์•Œ๋ฆผ ์ƒ์„ฑ
139+
/**
140+
* ๊ณต์ง€์‚ฌํ•ญ ์•Œ๋ฆผ ์ƒ์„ฑ
141+
* @param user
142+
* @param noticeTitle
143+
*/
144+
// TODO: ๊ณต์ง€์‚ฌํ•ญ ๋กœ์ง ๊ตฌํ˜„ ํ›„ ์‚ฌ์šฉ
99145
@Transactional
100146
public void createNoticeNoti(User user, String noticeTitle) {
101147
createNoti(user, NotiType.NOTICE, NotiDetailType.NOTICE_CREATED, 0L, null, noticeTitle);
102148
}
103149

104-
// ์ฝ์Œ ์ฒ˜๋ฆฌ
150+
/**
151+
* ์•Œ๋ฆผ ์ฝ์Œ ์ฒ˜๋ฆฌ:
152+
* ์•Œ๋ฆผ ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ๋ชจ๋“  ์•Œ๋ฆผ์ด ์ฝ์Œ ์ƒํƒœ๋กœ ๋ณ€๊ฒฝ๋˜๊ธฐ ๋•Œ๋ฌธ์—
153+
* ์‚ฌ์šฉ์ž์˜ ๋ชจ๋“  ์•Œ๋ฆผ์˜ ์ƒํƒœ๋ฅผ true๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ๋ฉ”์„œ๋“œ
154+
* @param receiver
155+
*/
105156
@Transactional
106-
public List<Noti> read(User receiver) {
107-
System.out.println("์•Œ๋ฆผ ์ฝ์Œ ์ฒ˜๋ฆฌ");
157+
public void read(User receiver) {
158+
log.info("โœ… read notis");
108159
List<Noti> notis = notiRepository.findNotiByReceiver(receiver).orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_USER));
109160
notis.forEach(noti -> {
110161
noti.setRead(true);
111162
});
112-
System.out.println("์•Œ๋ฆผ ์ฝ์Œ ์ฒ˜๋ฆฌ ๋");
113-
return notis;
114163
}
115164

165+
/**
166+
* ๋‹‰๋„ค์ž„ ์—ฌ๋ถ€์— ๋”ฐ๋ผ ํ‘œ์‹œ ์ด๋ฆ„ ๋ฐ˜ํ™˜:
167+
* ๋‹‰๋„ค์ž„์ด ์žˆ์œผ๋ฉด ๋‹‰๋„ค์ž„, ์—†์œผ๋ฉด ์œ ์ €๋„ค์ž„ ๋ฐ˜ํ™˜
168+
* @param user
169+
* @return
170+
*/
116171
private String getDisplayName(User user) {
117172
return user.getNickname() != null ? user.getNickname() : user.getUsername();
118173
}

โ€Žsrc/main/java/cmf/commitField/domain/user/service/UserService.javaโ€Ž

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import cmf.commitField.domain.user.entity.User;
1515
import cmf.commitField.domain.user.repository.TierRegacyRepository;
1616
import cmf.commitField.domain.user.repository.UserRepository;
17+
import cmf.commitField.global.error.ErrorCode;
18+
import cmf.commitField.global.exception.CustomException;
1719
import lombok.RequiredArgsConstructor;
1820
import org.springframework.data.redis.core.StringRedisTemplate;
1921
import org.springframework.stereotype.Service;
@@ -120,4 +122,9 @@ public void updateUserCommitCount(String username, long count){
120122
user.addCommitCount(count);
121123
userRepository.save(user);
122124
}
125+
126+
public User findByUsername(String username) {
127+
return userRepository.findByUsername(username).orElseThrow(() -> new
128+
CustomException(ErrorCode.NOT_FOUND_USER));
129+
}
123130
}

0 commit comments

Comments
ย (0)