Skip to content

Commit b589f0c

Browse files
authored
Merge pull request #104 from CommitField/feat/#81
feat : ์•Œ๋ฆผ ์›น ์†Œ์ผ“ ๊ตฌํ˜„
2 parents 28a856b + 650c560 commit b589f0c

File tree

7 files changed

+98
-36
lines changed

7 files changed

+98
-36
lines changed
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package cmf.commitField.domain.noti.noti.controller;
22

33
import cmf.commitField.domain.noti.noti.dto.NotiDto;
4-
import cmf.commitField.domain.noti.noti.entity.Noti;
54
import cmf.commitField.domain.noti.noti.service.NotiService;
65
import cmf.commitField.domain.user.entity.User;
76
import cmf.commitField.domain.user.repository.UserRepository;
8-
import cmf.commitField.domain.user.service.CustomOAuth2UserService;
97
import cmf.commitField.global.error.ErrorCode;
108
import cmf.commitField.global.exception.CustomException;
119
import cmf.commitField.global.globalDto.GlobalResponse;
@@ -16,40 +14,39 @@
1614
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
1715
import org.springframework.security.oauth2.core.user.OAuth2User;
1816
import org.springframework.web.bind.annotation.GetMapping;
17+
import org.springframework.web.bind.annotation.PostMapping;
1918
import org.springframework.web.bind.annotation.RequestMapping;
2019
import org.springframework.web.bind.annotation.RestController;
2120

2221
import java.util.List;
2322
import java.util.Map;
24-
import java.util.stream.Collectors;
2523

2624
@RestController
2725
@RequestMapping("/api/notifications")
2826
@RequiredArgsConstructor
2927
@Slf4j
3028
public class ApiV1NotiController {
3129
private final NotiService notiService;
32-
private final CustomOAuth2UserService customOAuth2UserService;
3330
private final UserRepository userRepository;
3431

3532
@GetMapping("")
3633
public GlobalResponse<List<NotiDto>> getNoti() {
3734
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
38-
log.info("getNoti - userRequest: {}", authentication);
3935

4036
if (authentication instanceof OAuth2AuthenticationToken) {
4137
OAuth2User principal = (OAuth2User) authentication.getPrincipal();
4238
Map<String, Object> attributes = principal.getAttributes();
4339
String username = (String) attributes.get("login"); // GitHub ID
4440
User user = userRepository.findByUsername(username).orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_USER));
45-
List<Noti> notis = notiService.getNotReadNoti(user);
46-
47-
List<NotiDto> notiDtos = notis.stream()
48-
.map(NotiDto::new)
49-
.collect(Collectors.toList());
50-
return GlobalResponse.success(notiDtos);
41+
List<NotiDto> notis = notiService.getNotReadNoti(user);
42+
return GlobalResponse.success(notis);
5143
}
5244

5345
return GlobalResponse.error(ErrorCode.LOGIN_REQUIRED);
5446
}
47+
48+
@PostMapping("")
49+
public void createNoti() {
50+
51+
}
5552
}

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package cmf.commitField.domain.noti.noti.dto;
22

3-
import cmf.commitField.domain.noti.noti.entity.Noti;
43
import lombok.Getter;
54

65
import java.time.LocalDateTime;
@@ -9,12 +8,15 @@
98

109
@Getter
1110
public class NotiDto {
11+
private Long id;
1212
private String message;
1313
private String formattedCreatedAt; // ๋ณ€ํ™˜๋œ ๋‚ ์งœ๋ฅผ ์ €์žฅํ•  ํ•„๋“œ
1414

15-
public NotiDto(Noti noti) {
16-
this.message = noti.getMessage();
17-
this.formattedCreatedAt = formatCreatedAt(noti.getCreatedAt()); // ๋ณ€ํ™˜๋œ ๋‚ ์งœ ์ €์žฅ
15+
// JPQL์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•„๋“œ ๊ฐ’ ์ง์ ‘ ๋ฐ›๋Š” ์ƒ์„ฑ์ž ์ถ”๊ฐ€
16+
public NotiDto(Long id, String message, LocalDateTime createdAt) {
17+
this.id = id;
18+
this.message = message;
19+
this.formattedCreatedAt = formatCreatedAt(createdAt); // ๋ณ€ํ™˜๋œ ๋‚ ์งœ ์ €์žฅ
1820
}
1921

2022
private String formatCreatedAt(LocalDateTime createdAt) {
@@ -25,12 +27,8 @@ private String formatCreatedAt(LocalDateTime createdAt) {
2527
return "์˜ค๋Š˜";
2628
} else if (daysBetween == 1) {
2729
return "์–ด์ œ";
28-
} else if (daysBetween == 2) {
29-
return "1์ผ ์ „";
30-
} else if (daysBetween == 3) {
31-
return "2์ผ ์ „";
32-
} else if (daysBetween == 4) {
33-
return "3์ผ ์ „";
30+
} else if (daysBetween <= 3) {
31+
return (daysBetween - 1) + "์ผ ์ „";
3432
} else {
3533
return createdAt.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
3634
}
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package cmf.commitField.domain.noti.noti.repository;
22

3+
import cmf.commitField.domain.noti.noti.dto.NotiDto;
34
import cmf.commitField.domain.noti.noti.entity.Noti;
45
import cmf.commitField.domain.user.entity.User;
6+
import io.lettuce.core.dynamic.annotation.Param;
57
import org.springframework.data.jpa.repository.JpaRepository;
8+
import org.springframework.data.jpa.repository.Query;
69
import org.springframework.stereotype.Repository;
710

811
import java.util.List;
@@ -11,5 +14,8 @@
1114
@Repository
1215
public interface NotiRepository extends JpaRepository<Noti, Long> {
1316
Optional<List<Noti>> findNotiByReceiverAndRelId(User receiver, long season);
14-
Optional<List<Noti>> findNotiByReceiverAndIsRead(User receiver, boolean read);
17+
@Query("SELECT new cmf.commitField.domain.noti.noti.dto.NotiDto(n.id, n.message, n.createdAt) " +
18+
"FROM Noti n JOIN n.receiver u WHERE u.id = :receiverId AND n.isRead = :isRead")
19+
Optional<List<NotiDto>> findNotiDtoByReceiverId(@Param("receiverId") Long receiverId, @Param("isRead") boolean isRead);
20+
1521
}

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

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package cmf.commitField.domain.noti.noti.service;
22

3+
import cmf.commitField.domain.noti.noti.dto.NotiDto;
34
import cmf.commitField.domain.noti.noti.entity.Noti;
45
import cmf.commitField.domain.noti.noti.entity.NotiDetailType;
56
import cmf.commitField.domain.noti.noti.entity.NotiMessageTemplates;
67
import cmf.commitField.domain.noti.noti.entity.NotiType;
78
import cmf.commitField.domain.noti.noti.repository.NotiRepository;
89
import cmf.commitField.domain.season.entity.Season;
9-
import cmf.commitField.domain.season.repository.SeasonRepository;
10-
import cmf.commitField.domain.season.service.SeasonService;
1110
import cmf.commitField.domain.user.entity.User;
1211
import cmf.commitField.domain.user.repository.UserRepository;
1312
import cmf.commitField.global.error.ErrorCode;
@@ -27,40 +26,48 @@
2726
public class NotiService {
2827
private final NotiRepository notiRepository;
2928
private final UserRepository userRepository;
30-
private final SeasonRepository seasonRepository;
31-
private final SeasonService seasonService;
3229

3330
// ์•Œ๋ฆผ ๋ฉ”์‹œ์ง€ ์ƒ์„ฑ
3431
public static String generateMessage(NotiDetailType type, Object... params) {
3532
String template = NotiMessageTemplates.getTemplate(type);
36-
log.info("generateMessage - params: {}", params);
37-
log.info("generateMessage - template: {}", template); // template ์ž์ฒด๋ฅผ ์ถœ๋ ฅ
3833
String message = MessageFormat.format(template, params); // params ๋ฐฐ์—ด์„ ๊ทธ๋Œ€๋กœ ์ „๋‹ฌ
39-
log.info("generateMessage - message: {}", message);
4034
return message;
4135
}
4236

37+
@Transactional
38+
public void createNoti(User receiver) {
39+
System.out.println("์•Œ๋ฆผ ์ƒ์„ฑ");
40+
String message = NotiService.generateMessage(NotiDetailType.STREAK_BROKEN, receiver.getNickname());
41+
42+
Noti noti = Noti.builder()
43+
.typeCode(NotiType.STREAK)
44+
.type2Code(NotiDetailType.STREAK_BROKEN)
45+
.receiver(receiver)
46+
.isRead(false)
47+
.message(message)
48+
.build();
49+
notiRepository.save(noti);
50+
}
51+
4352

44-
public List<Noti> getNotReadNoti(User receiver) {
45-
log.info("getNotReadNoti - receiver: {}", receiver);
46-
List<Noti> notis = notiRepository.findNotiByReceiverAndIsRead(receiver, false).orElse(null);
47-
log.info("getNotReadNoti - notis: {}", notis);
53+
public List<NotiDto> getNotReadNoti(User receiver) {
54+
System.out.println("์•Œ๋ฆผ ์กฐํšŒ");
55+
List<NotiDto> notis = notiRepository.findNotiDtoByReceiverId(receiver.getId(), false).orElse(null);
56+
System.out.println("์•Œ๋ฆผ ์กฐํšŒ ๋");
4857
return notis;
4958
}
5059

5160
public List<Noti> getSeasonNotiCheck(User receiver, long seasonId) {
52-
log.info("getSeasonNotiCheck - receiver: {}, seasonId: {}", receiver, seasonId);
5361
return notiRepository.findNotiByReceiverAndRelId(receiver, seasonId)
5462
.orElseThrow(() -> new CustomException(ErrorCode.ERROR_CHECK)); // ์•Œ๋ฆผ์ด ์—†์„ ๊ฒฝ์šฐ ์˜ˆ์™ธ ๋ฐœ์ƒ
5563
}
5664

5765
// ์ƒˆ ์‹œ์ฆŒ ์•Œ๋ฆผ ์ƒ์„ฑ
5866
@Transactional
5967
public void createNewSeason(Season season) {
60-
log.info("createNewSeason - season: {}", season.getName());
68+
System.out.println("์ƒˆ ์‹œ์ฆŒ ์•Œ๋ฆผ ์ƒ์„ฑ");
6169
// ๋ฉ”์‹œ์ง€ ์ƒ์„ฑ
6270
String message = NotiService.generateMessage(NotiDetailType.SEASON_START, season.getName());
63-
log.info("createNewSeason - message: {}", message);
6471

6572
// ๋ชจ๋“  ์‚ฌ์šฉ์ž ์กฐํšŒ
6673
Iterable<User> users = userRepository.findAll();
@@ -79,5 +86,6 @@ public void createNewSeason(Season season) {
7986

8087
notiRepository.save(noti);
8188
});
89+
System.out.println("์ƒˆ ์‹œ์ฆŒ ์•Œ๋ฆผ ์ƒ์„ฑ ๋");
8290
}
8391
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public OAuth2User loadUser(OAuth2UserRequest userRequest) {
9292
// ์•Œ๋ฆผ ํ…Œ์ด๋ธ”์—์„œ Active์ธ ์‹œ์ฆŒ์˜ ์•Œ๋ฆผ์„ ํ•ด๋‹น ์œ ์ €๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š”์ง€ ์ฒดํฌ
9393
String season_key = "season_active:" + user.getUsername();
9494
Season season = seasonService.getActiveSeason();
95+
log.info("Active season: {}", season);
9596
if(notiService.getSeasonNotiCheck(user, season.getId()).isEmpty()){
9697
log.info("User {} does not have season noti", user.getUsername());
9798
// ๊ฐ€์ง€๊ณ  ์žˆ์ง€ ์•Š๋‹ค๋ฉด ์•Œ๋ฆผ์„ ์ถ”๊ฐ€
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cmf.commitField.global.scheduler;
2+
3+
import cmf.commitField.domain.noti.noti.service.NotiService;
4+
import cmf.commitField.domain.user.repository.UserRepository;
5+
import lombok.RequiredArgsConstructor;
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
@RequiredArgsConstructor
10+
public class NotiTestScheduler {
11+
private final NotiService notiService;
12+
private final UserRepository userRepository;
13+
14+
// @Scheduled(cron = "0 44 * * * *")
15+
// public void test() {
16+
// System.out.println("test ์‹คํ–‰");
17+
// Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
18+
//
19+
// if (authentication instanceof OAuth2AuthenticationToken) {
20+
// OAuth2User principal = (OAuth2User) authentication.getPrincipal();
21+
// Map<String, Object> attributes = principal.getAttributes();
22+
// String username = (String) attributes.get("login"); // GitHub ID
23+
// User user = userRepository.findByUsername(username).orElseThrow(() -> new CustomException(ErrorCode.NOT_FOUND_USER));
24+
// notiService.createNoti(user);
25+
// }
26+
//
27+
// }
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package cmf.commitField.global.websocket;
2+
3+
import org.springframework.context.annotation.Configuration;
4+
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
5+
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
6+
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
7+
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
8+
9+
@Configuration
10+
@EnableWebSocketMessageBroker
11+
public class NotiWebsoketConfig implements WebSocketMessageBrokerConfigurer {
12+
@Override
13+
public void registerStompEndpoints(StompEndpointRegistry registry) {
14+
registry.addEndpoint("/ws") // WebSocket ์—”๋“œํฌ์ธํŠธ ์„ค์ •
15+
.setAllowedOriginPatterns("*") // CORS ํ—ˆ์šฉ
16+
.withSockJS(); // SockJS ์ง€์›
17+
}
18+
19+
@Override
20+
public void configureMessageBroker(MessageBrokerRegistry registry) {
21+
registry.enableSimpleBroker("/topic"); // ๋ฉ”์‹œ์ง€ ๋ธŒ๋กœ์ปค ํ™œ์„ฑํ™”
22+
registry.setApplicationDestinationPrefixes("/app"); // ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ณด๋‚ผ ๊ฒฝ๋กœ
23+
}
24+
}

0 commit comments

Comments
ย (0)