Skip to content

Commit be6f102

Browse files
authored
Merge branch 'dev' into refactor#106
2 parents 889c37e + ddedc24 commit be6f102

File tree

7 files changed

+155
-1
lines changed

7 files changed

+155
-1
lines changed

src/main/java/com/back/domain/notification/controller/NotificationController.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import com.back.domain.notification.dto.NotificationGoResponseDto;
44
import com.back.domain.notification.dto.NotificationListResponseDto;
5+
import com.back.domain.notification.dto.NotificationSettingDto;
6+
import com.back.domain.notification.service.NotificationSettingService;
7+
import com.back.domain.notification.dto.NotificationSettingUpdateRequestDto;
8+
import jakarta.validation.Valid;
59
import com.back.domain.notification.service.NotificationService;
610
import com.back.global.rsData.RsData;
711
import jakarta.validation.constraints.Max;
@@ -21,6 +25,7 @@
2125
public class NotificationController {
2226

2327
private final NotificationService notificationService;
28+
private final NotificationSettingService notificationSettingService;
2429

2530
@GetMapping("/notifications")
2631
public RsData<NotificationListResponseDto> getNotifications(
@@ -33,6 +38,23 @@ public RsData<NotificationListResponseDto> getNotifications(
3338
return RsData.successOf(body);
3439
}
3540

41+
@GetMapping("/notification-setting")
42+
public RsData<NotificationSettingDto> getMyNotificationSetting(
43+
@AuthenticationPrincipal(expression = "id") Long userId
44+
) {
45+
NotificationSettingDto body = notificationSettingService.getMySetting(userId);
46+
return RsData.successOf(body);
47+
}
48+
49+
@PatchMapping("/notification-setting")
50+
public RsData<NotificationSettingDto> setMyNotificationSetting(
51+
@AuthenticationPrincipal(expression = "id") Long userId,
52+
@Valid @RequestBody NotificationSettingUpdateRequestDto req
53+
) {
54+
NotificationSettingDto body = notificationSettingService.setMySetting(userId, req.enabled());
55+
return RsData.successOf(body);
56+
}
57+
3658
@PostMapping("/notifications/{id}")
3759
public RsData<NotificationGoResponseDto> goPostLink(
3860
@AuthenticationPrincipal(expression = "id") Long userId,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.back.domain.notification.dto;
2+
3+
import com.back.domain.notification.entity.NotificationSetting;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Getter;
6+
7+
@Getter
8+
@AllArgsConstructor
9+
public class NotificationSettingDto {
10+
private boolean enabled;
11+
12+
public static NotificationSettingDto from(NotificationSetting s) {
13+
return new NotificationSettingDto(s.isEnabled());
14+
}
15+
}
16+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.back.domain.notification.dto;
2+
3+
import jakarta.validation.constraints.NotNull;
4+
5+
public record NotificationSettingUpdateRequestDto(
6+
@NotNull Boolean enabled
7+
) {}
8+
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.back.domain.notification.entity;
2+
3+
import com.back.domain.user.entity.User;
4+
import jakarta.persistence.*;
5+
import lombok.*;
6+
import org.springframework.data.annotation.CreatedDate;
7+
import org.springframework.data.annotation.LastModifiedDate;
8+
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
9+
10+
import java.time.LocalDateTime;
11+
12+
@Entity
13+
@EntityListeners(AuditingEntityListener.class)
14+
@Getter
15+
@Setter
16+
@NoArgsConstructor(access = lombok.AccessLevel.PROTECTED)
17+
@AllArgsConstructor
18+
@Builder
19+
public class NotificationSetting {
20+
21+
@Id
22+
@GeneratedValue(strategy = GenerationType.IDENTITY)
23+
private Long id;
24+
25+
@Setter
26+
@OneToOne(fetch = FetchType.LAZY)
27+
@JoinColumn(name = "user_id", nullable = false, unique = true)
28+
private User user;
29+
30+
@Setter
31+
@Builder.Default
32+
private boolean enabled = true;
33+
34+
@CreatedDate
35+
private LocalDateTime createdAt;
36+
37+
@LastModifiedDate
38+
private LocalDateTime updatedAt;
39+
40+
public void toggle() {
41+
this.enabled = !this.enabled;
42+
}
43+
44+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.back.domain.notification.repository;
2+
3+
import com.back.domain.notification.entity.NotificationSetting;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
6+
import org.springframework.data.repository.query.Param;
7+
import org.springframework.stereotype.Repository;
8+
9+
@Repository
10+
public interface NotificationSettingRepository extends JpaRepository<NotificationSetting, Long> {
11+
12+
@Query("""
13+
select ns from NotificationSetting ns
14+
where ns.user.id = :userId
15+
""")
16+
NotificationSetting findByUserId(@Param("userId") Long userId);
17+
}
18+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.back.domain.notification.service;
2+
3+
import com.back.domain.notification.dto.NotificationSettingDto;
4+
import com.back.domain.notification.entity.NotificationSetting;
5+
import com.back.domain.notification.repository.NotificationSettingRepository;
6+
import com.back.domain.user.entity.User;
7+
import com.back.domain.user.repository.UserRepository;
8+
import com.back.global.exception.ServiceException;
9+
import lombok.RequiredArgsConstructor;
10+
import org.springframework.stereotype.Service;
11+
import org.springframework.transaction.annotation.Transactional;
12+
13+
@Service
14+
@RequiredArgsConstructor
15+
public class NotificationSettingService {
16+
17+
private final NotificationSettingRepository notificationSettingRepository;
18+
private final UserRepository userRepository;
19+
20+
@Transactional(readOnly = true)
21+
public NotificationSettingDto getMySetting(Long userId) {
22+
NotificationSetting s = notificationSettingRepository.findByUserId(userId);
23+
if (s == null) {
24+
// Default when not created yet
25+
return new NotificationSettingDto(true);
26+
}
27+
return NotificationSettingDto.from(s);
28+
}
29+
30+
@Transactional
31+
public NotificationSettingDto setMySetting(Long userId, boolean enabled) {
32+
NotificationSetting s = notificationSettingRepository.findByUserId(userId);
33+
if (s == null) {
34+
User user = userRepository.findById(userId)
35+
.orElseThrow(() -> new ServiceException(404, "사용자를 찾을 수 없습니다."));
36+
s = NotificationSetting.builder()
37+
.user(user)
38+
.enabled(enabled)
39+
.build();
40+
} else {
41+
s.setEnabled(enabled);
42+
}
43+
NotificationSetting saved = notificationSettingRepository.save(s);
44+
return NotificationSettingDto.from(saved);
45+
}
46+
}

src/main/java/com/back/global/security/CustomOAuth2LoginSuccessHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
2828
userAuthService.issueTokens(response, securityUser.getId(), securityUser.getEmail(), securityUser.getNickname());
2929

3030
// 프론트엔드로 리다이렉트
31-
String redirectUrl = frontendUrl;
31+
String redirectUrl = frontendUrl + "/oauth/success";
3232

3333
response.sendRedirect(redirectUrl);
3434
}

0 commit comments

Comments
 (0)