Skip to content

Commit

Permalink
release: 0.2.6
Browse files Browse the repository at this point in the history
* feat: 리뷰 등록시 회원 매너 온도 반영 기능 추가 및 리뷰 등록 리팩토링 (#158)

* refactor: 회원 리뷰 등록 API 리팩토링 (#157)

* feat: 회원 리뷰 등록시, 온도 반영 및 리뷰 피드백 반영 (#157)

* feat: User 엔티티 메소드 추가 (#157)

* feat: Review enum 필드 추가 (#157)

* test: 리뷰 등록시 온도 업데이트에 대한 단위 테스트 (#157)

* test: 리뷰 등록 기능 통합 테스트 (#157)

* refactor: 회원 리뷰 등록 메소드 수정 (#157)

* test: 불필요한 테스트 제거 및 CI 오류 수정 (#157)

* test: MeetingRepository 테스트에서 시간과 id비교 비활서화

* refactor: 회원 탈퇴 URI 변경 (#162)

* refactor: 회원 탈퇴 URI 수정 (#161)

* test: 회원 탈퇴 URI 수정에 대한 테스트 수정 (#161)

* fix: CI 에러 수정 (#161)

* fix: 필드 값 비교를 위해 deprecated 된 메소드을 대체 (#161)

* fix: 필드 값 비교를 위해 deprecated 된 메소드을 대체 (#161)

* feat: fcm token 업데이트 api 추가 (#166)

* fix: user_alert ddl의 pk에 auto_increment를 추가한다

* feat: token 업데이트 api를 추가한다

---------

Co-authored-by: ChoiDongKuen <[email protected]>
  • Loading branch information
devxb and choidongkuen authored Jan 30, 2024
1 parent bbb5e73 commit c334ced
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 4 deletions.
19 changes: 19 additions & 0 deletions src/main/java/net/teumteum/alert/controller/AlertController.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
package net.teumteum.alert.controller;

import io.sentry.Sentry;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import net.teumteum.alert.domain.AlertService;
import net.teumteum.alert.domain.request.RegisterAlertRequest;
import net.teumteum.alert.domain.request.UpdateAlertTokenRequest;
import net.teumteum.core.error.ErrorResponse;
import net.teumteum.core.security.service.SecurityService;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseStatus;
Expand All @@ -24,4 +29,18 @@ public void registerAlert(@Valid @RequestBody RegisterAlertRequest registerAlert
var loginUserId = securityService.getCurrentUserId();
alertService.registerAlert(loginUserId, registerAlertRequest);
}

@PatchMapping("/alerts")
@ResponseStatus(HttpStatus.OK)
public void updateAlert(@Valid @RequestBody UpdateAlertTokenRequest updateAlertTokenRequest) {
var loginUserId = securityService.getCurrentUserId();
alertService.updateAlertToken(loginUserId, updateAlertTokenRequest);
}

@ExceptionHandler(IllegalArgumentException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public ErrorResponse handleIllegalArgumentException(IllegalArgumentException illegalArgumentException) {
Sentry.captureException(illegalArgumentException);
return ErrorResponse.of(illegalArgumentException);
}
}
10 changes: 10 additions & 0 deletions src/main/java/net/teumteum/alert/domain/AlertRepository.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
package net.teumteum.alert.domain;

import jakarta.persistence.LockModeType;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

public interface AlertRepository extends JpaRepository<UserAlert, Long> {

@Query("select u from user_alert as u where u.userId in :userIds")
List<UserAlert> findAllByUserId(@Param("userIds") Iterable<Long> userIds);

@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("select u from user_alert as u where u.userId = :userId")
Optional<UserAlert> findByUserIdWithLock(@Param("userId") Long userId);

Optional<UserAlert> findByUserId(@Param("userId") Long userId);

}
18 changes: 16 additions & 2 deletions src/main/java/net/teumteum/alert/domain/AlertService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.Set;
import lombok.RequiredArgsConstructor;
import net.teumteum.alert.domain.request.RegisterAlertRequest;
import net.teumteum.alert.domain.request.UpdateAlertTokenRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -16,8 +17,21 @@ public class AlertService {

@Transactional
public void registerAlert(Long userId, RegisterAlertRequest registerAlertRequest) {
var alert = new UserAlert(null, userId, registerAlertRequest.token());
alertRepository.save(alert);
alertRepository.findByUserId(userId)
.ifPresentOrElse(userAlert -> {
throw new IllegalArgumentException("이미 토큰이 생성된 user입니다. \"" + userId +"\"");
}, () -> {
var alert = new UserAlert(null, userId, registerAlertRequest.token());
alertRepository.save(alert);
});
}

@Transactional
public void updateAlertToken(Long userId, UpdateAlertTokenRequest updateAlertTokenRequest) {
var userAlert = alertRepository.findByUserIdWithLock(userId)
.orElseThrow(() -> new IllegalArgumentException("userId에 해당하는 토큰을 찾을 수 없습니다."));

userAlert.updateToken(updateAlertTokenRequest.token());
}

public List<UserAlert> findAllByUserId(Set<Long> userIds) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/net/teumteum/alert/domain/UserAlert.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public class UserAlert {
public String getToken() {
return token;
}

public void updateToken(String token) {
this.token = token;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package net.teumteum.alert.domain.request;

import jakarta.validation.constraints.NotNull;

public record UpdateAlertTokenRequest(
@NotNull
String token
) {

}
2 changes: 1 addition & 1 deletion src/main/resources/db/migration/V10__create_alert.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
create table if not exists user_alert(
id bigint primary key,
id bigint primary key auto_increment,
user_id bigint unique not null,
token text not null
);
Expand Down
46 changes: 46 additions & 0 deletions src/test/java/net/teumteum/alert/domain/AlertRepositoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package net.teumteum.alert.domain;

import jakarta.persistence.EntityManager;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;

@DataJpaTest
@ExtendWith(SpringExtension.class)
@DisplayName("AlertRepository 클래스의")
class AlertRepositoryTest {

@Autowired
private AlertRepository alertRepository;

@Autowired
private EntityManager entityManager;

@Nested
@DisplayName("findByUserIdWithLock 메소드는")
class findByUserIdWithLock_method {

@Test
@DisplayName("userId로 userAlert를 조회한다.")
void find_userAlert_by_userId() {
// given
var userId = 1L;
var userAlert = new UserAlert(1L, userId, "token");

alertRepository.saveAndFlush(userAlert);
entityManager.clear();

// when
var result = alertRepository.findByUserIdWithLock(userId);

// then
Assertions.assertThat(result).isPresent();
}
}

}
2 changes: 1 addition & 1 deletion src/test/resources/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ create table if not exists withdraw_reasons
);

create table if not exists user_alert(
id bigint primary key,
id bigint primary key auto_increment,
user_id bigint unique not null,
token text not null
);
Expand Down

0 comments on commit c334ced

Please sign in to comment.