Skip to content

Commit

Permalink
feat: 알림 상태 변경 API 개발 #8
Browse files Browse the repository at this point in the history
  • Loading branch information
PgmJun committed Jan 19, 2024
1 parent a758abb commit 0548e5a
Show file tree
Hide file tree
Showing 13 changed files with 150 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.nice.petudio.api.controller.auth.dto.request.LoginRequest;
import com.nice.petudio.api.controller.auth.dto.request.SignUpRequest;
import com.nice.petudio.api.controller.auth.service.AuthService;
import com.nice.petudio.api.controller.member.service.MemberService;
import com.nice.petudio.api.controller.member.service.MemberCommandService;
import com.nice.petudio.api.controller.member.service.MemberServiceUtils;
import com.nice.petudio.domain.member.Member;
import com.nice.petudio.domain.member.SocialType;
Expand All @@ -23,12 +23,12 @@ public class KakaoAuthService implements AuthService {

private final MemberRepository memberRepository;

private final MemberService memberService;
private final MemberCommandService memberCommandService;

@Override
public Long signUp(SignUpRequest request) {
KakaoProfileResponse response = kakaoApiCaller.getProfileInfo(request.getToken());
return memberService.registerMember(request.toCreateMemberDto(response));
return memberCommandService.registerMember(request.toCreateMemberDto(response));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.nice.petudio.api.controller.member;

import com.nice.petudio.api.controller.member.dto.ChangeNotificationStatusResponse;
import com.nice.petudio.api.controller.member.service.MemberCommandService;
import com.nice.petudio.api.dto.ApiResponse;
import com.nice.petudio.global.auth.auth.Auth;
import com.nice.petudio.global.auth.resolver.MemberId;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
public class MemberController {
private final MemberCommandService memberCommandService;

@Auth
@ResponseStatus(HttpStatus.OK)
@PatchMapping("/member/notification/status-change")
public ApiResponse<ChangeNotificationStatusResponse> changeMemberNotificationStatus(@MemberId final Long memberId,
@RequestParam("status") final boolean notificationStatus) {
return ApiResponse.success(memberCommandService.changeMemberNotificationStatus(memberId, notificationStatus));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.nice.petudio.api.controller.member.dto;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class ChangeNotificationStatusResponse {
private final boolean notificationStatus;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.nice.petudio.api.controller.member.service;

import com.nice.petudio.api.controller.member.dto.ChangeNotificationStatusResponse;
import com.nice.petudio.api.controller.member.dto.CreateMemberRequest;
import com.nice.petudio.domain.member.Member;
import com.nice.petudio.domain.member.point.Point;
import com.nice.petudio.domain.member.point.repository.PointRepository;
import com.nice.petudio.domain.member.repository.MemberRepository;
import com.nice.petudio.domain.member.setting.Setting;
import com.nice.petudio.domain.member.setting.repository.SettingRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class MemberCommandService {
private final MemberRepository memberRepository;
private final PointRepository pointRepository;
private final SettingRepository settingRepository;

public Long registerMember(CreateMemberRequest request) {
MemberServiceUtils.validateNotExistsMember(memberRepository, request.getSocialId(), request.getSocialType());

Member member = memberRepository.save(
Member.newInstance(request.getSocialId(), request.getSocialType()));
pointRepository.save(Point.fromMemberId(member.getId()));
settingRepository.save(Setting.fromMemberId(member.getId()));

return member.getId();
}

public ChangeNotificationStatusResponse changeMemberNotificationStatus(final Long memberId, final boolean notificationStatus) {
Setting setting = SettingServiceUtils.findSettingByMemberId(settingRepository, memberId);
setting.changeNotificationStatus(notificationStatus);

return new ChangeNotificationStatusResponse(notificationStatus);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static Member findMemberById(MemberRepository memberRepository, Long memb
String.format("존재하지 않는 memberId(%d) 입니다.", memberId)));
}

static void validateNotExistsMember(MemberRepository memberRepository, String socialId,
public static void validateNotExistsMember(MemberRepository memberRepository, String socialId,
SocialType socialType) {
if (memberRepository.existsBySocialIdAndSocialType(socialId, socialType)) {
throw new ConflictException(ErrorCode.CONFLICT_MEMBER_EXCEPTION, String.format("이미 존재하는 회원 소셜정보 (%s - %s) 입니다", socialId, socialType));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.nice.petudio.api.controller.member.service;

import com.nice.petudio.domain.member.setting.Setting;
import com.nice.petudio.domain.member.setting.repository.SettingRepository;
import com.nice.petudio.global.exception.error.ErrorCode;
import com.nice.petudio.global.exception.model.NotFoundException;

public class SettingServiceUtils {

public static Setting findSettingByMemberId(SettingRepository settingRepository, final Long memberId) {
return settingRepository.findByMemberId(memberId)
.orElseThrow(() -> new NotFoundException(
ErrorCode.NOT_FOUND_MEMBER_EXCEPTION,
String.format("memberId(%d)의 Setting 정보를 찾을 수 없습니다.", memberId)));
}
}
12 changes: 12 additions & 0 deletions src/main/java/com/nice/petudio/domain/member/point/Point.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "points")
@Builder
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -28,4 +30,14 @@ public class Point extends BaseEntity {

@Column(name = "point_amount", nullable = false)
private int amount;


private static final int INIT_POINT_AMOUNT = 0;

public static Point fromMemberId(Long memberId) {
return Point.builder()
.memberId(memberId)
.amount(INIT_POINT_AMOUNT)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Table(name = "settings")
@Builder
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@AllArgsConstructor(access = AccessLevel.PROTECTED)
Expand All @@ -24,8 +26,20 @@ public class Setting extends BaseEntity {
private Long id;

@Column(name = "member_id", nullable = false)
private Long member_id;
private Long memberId;

@Column(name = "notification_status", nullable = false)
private Boolean notificationStatus;


public static Setting fromMemberId(Long memberId) {
return Setting.builder()
.memberId(memberId)
.notificationStatus(false)
.build();
}

public void changeNotificationStatus(final boolean notificationStatus) {
this.notificationStatus = notificationStatus;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import com.nice.petudio.domain.member.setting.Setting;
import org.springframework.data.jpa.repository.JpaRepository;

public interface SettingRepository extends JpaRepository<Setting, Long> {
public interface SettingRepository extends SettingRepositoryCustom, JpaRepository<Setting, Long> {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
package com.nice.petudio.domain.member.setting.repository;

import com.nice.petudio.domain.member.setting.Setting;
import java.util.Optional;

public interface SettingRepositoryCustom {
Optional<Setting> findByMemberId(Long memberId);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
package com.nice.petudio.domain.member.setting.repository;

import static com.nice.petudio.domain.member.setting.QSetting.setting;

import com.nice.petudio.domain.member.setting.Setting;
import com.querydsl.jpa.impl.JPAQueryFactory;
import java.util.Optional;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class SettingRepositoryImpl implements SettingRepositoryCustom {
private final JPAQueryFactory queryFactory;

@Override
public Optional<Setting> findByMemberId(Long memberId) {
return Optional.ofNullable(queryFactory
.selectFrom(setting)
.where(setting.member_id.eq(memberId))
.fetchOne());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public enum ErrorCode {

// NotFound Exception
NOT_FOUND_EXCEPTION("N001", "존재하지 않는 요청입니다."),
NOT_FOUND_MEMBER_EXCEPTION("N002", "탈퇴했거나 존재하지 않는 회원입니다."),
NOT_FOUND_CONCEPT_EXCEPTION("N004", "존재하지 않는 컨셉입니다."),
NOT_FOUND_MEMBER_EXCEPTION("N002", "탈퇴했거나 존재하지 않는 회원 정보입니다."),
NOT_FOUND_CONCEPT_EXCEPTION("N003", "존재하지 않는 컨셉입니다."),

// Conflict Exception
CONFLICT_EXCEPTION("C001", "이미 존재하는 데이터입니다."),
Expand Down

0 comments on commit 0548e5a

Please sign in to comment.