Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
suw0n committed Aug 21, 2024
2 parents a51d04a + ad247dd commit b4f835d
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import b1nd.dodam.restapi.support.data.Response;
import b1nd.dodam.restapi.support.encrypt.Sha512PasswordEncoder;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -66,6 +67,7 @@ private void throwExceptionWhenMemberIsBroadcastClubMember(Member member) {
}
}

@CacheEvict(value = "members-cache", key = "'activeMembers'")
public Response delete(String id) {
Member member = getMemberById(id);
throwExceptionWhenAuthStatusIsActive(member);
Expand All @@ -79,16 +81,19 @@ private void throwExceptionWhenAuthStatusIsActive(Member member) {
}
}

@CacheEvict(value = "members-cache", key = "'activeMembers'")
public Response active(String id) {
updateStatus(id, ActiveStatus.ACTIVE);
return Response.ok("멤버 활성화 성공");
}

@CacheEvict(value = "members-cache", key = "'activeMembers'")
public Response deactivate(String id) {
updateStatus(id, ActiveStatus.DEACTIVATE);
return Response.ok("멤버 비활성화 성공");
}

@CacheEvict(value = "members-cache", key = "'activeMembers'")
public Response deactivate() {
Member member = memberAuthenticationHolder.current();
member.updateStatus(ActiveStatus.DEACTIVATE);
Expand All @@ -113,13 +118,15 @@ public Response updatePassword(UpdatePasswordReq req) {
return Response.noContent("비밀번호 수정 성공");
}

@CacheEvict(value = "members-cache", key = "'activeMembers'")
public Response updateMemberInfo(UpdateMemberInfoReq req) {
Member member = memberAuthenticationHolder.current();
member.updateInfo(req.name(), req.email(), req.phone(), req.profileImage());
service.save(member);
return Response.noContent("내 정보 수정 성공");
}

@CacheEvict(value = "members-cache", key = "'activeMembers'")
public Response updateStudentInfo(UpdateStudentInfoReq req) {
Student student = getStudentByMember(memberAuthenticationHolder.current());
student.updateInfo(req.grade(), req.room(), req.number());
Expand All @@ -131,4 +138,4 @@ private Student getStudentByMember(Member member) {
.orElseThrow(StudentNotFoundException::new);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import b1nd.dodam.restapi.member.application.data.res.MemberInfoRes;
import b1nd.dodam.restapi.support.data.ResponseData;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -42,6 +43,7 @@ public ResponseData<List<MemberInfoRes>> getDeactivateMembers() {
.toList());
}

@Cacheable(value = "members-cache", key = "'activeMembers'")
public ResponseData<List<MemberInfoRes>> getAll() {
return ResponseData.ok("모든 멤버 정보 조회 성공", service.getByStatus(ActiveStatus.ACTIVE).parallelStream()
.map(this::getMemberInfo)
Expand All @@ -65,4 +67,4 @@ public ResponseData<Boolean> checkBroadcastClubMember(String id) {
return ResponseData.ok("방송부원 확인 성공", service.checkBroadcastClubMember(member));
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import b1nd.dodam.domain.rds.member.entity.Teacher;
import b1nd.dodam.domain.rds.member.enumeration.ActiveStatus;
import b1nd.dodam.domain.rds.member.enumeration.MemberRole;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;

import java.time.LocalDateTime;

Expand All @@ -18,7 +22,11 @@ public record MemberInfoRes(
String phone,
StudentRes student,
TeacherRes teacher,
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
LocalDateTime createdAt,
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
LocalDateTime modifiedAt
) {
public static MemberInfoRes of(Member member, Student student, Teacher teacher) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private void savePoints(List<Student> students, PointReason reason, LocalDate is
}

private void saveScores(List<Student> students, PointReason reason) {
pointService.getScoresBy(students).forEach(
pointService.getScoresByStudentsForUpdate(students).forEach(
s -> s.issue(reason)
);
}
Expand All @@ -77,7 +77,7 @@ public Response cancel(Integer id) {
Point point = pointService.getPointBy(id);
PointReason reason = point.getReason();
Student student = point.getStudent();
PointScore score = pointService.getScoreBy(student);
PointScore score = pointService.getScoreByStudentForUpdate(student);
score.cancel(reason);

pointService.delete(point);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
package b1nd.dodam.restapi.support.data;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import lombok.Getter;
import org.springframework.http.HttpStatus;

import java.io.IOException;

@Getter
@JsonDeserialize(using = ResponseData.ResponseDataDeserializer.class)
public class ResponseData<T> extends Response {

private final T data;
Expand All @@ -25,4 +33,17 @@ public static <T> ResponseData<T> created(String message, T data) {
return new ResponseData<>(HttpStatus.CREATED, message, data);
}

public static class ResponseDataDeserializer extends JsonDeserializer<ResponseData> {
@Override
public ResponseData deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
JsonNode node = p.getCodec().readTree(p);
HttpStatus status = HttpStatus.valueOf(node.get("status").asInt());
String message = node.get("message").asText();
JsonNode dataNode = node.get("data");
Object data = p.getCodec().treeToValue(dataNode, Object.class);
return new ResponseData<>(status, message, data);
}
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import b1nd.dodam.domain.rds.member.entity.Member;
import b1nd.dodam.domain.rds.member.enumeration.ActiveStatus;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.time.LocalDateTime;
import java.util.List;
Expand All @@ -16,7 +17,12 @@ public interface MemberRepository extends JpaRepository<Member, String> {

List<Member> findByCreatedAtAfter(LocalDateTime createdAt);

List<Member> findByStatus(ActiveStatus status);
@Query("SELECT m FROM member m LEFT JOIN student s ON m.id = s.member.id " +
"WHERE m.status = :status " +
"ORDER BY CASE WHEN s.id IS NOT NULL THEN 0 ELSE 1 END ASC, " +
"s.grade ASC, s.room ASC, s.number ASC")
List<Member> findByStatusOrderByStudent(ActiveStatus status);


List<Member> findByNameContains(String name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public List<Member> searchByName(String name) {
}

public List<Member> getByStatus(ActiveStatus status) {
return memberRepository.findByStatus(status);
return memberRepository.findByStatusOrderByStudent(status);
}

public Optional<Student> getStudentByMember(Member member) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,31 @@
import b1nd.dodam.domain.rds.member.entity.Student;
import b1nd.dodam.domain.rds.point.entity.PointScore;
import b1nd.dodam.domain.rds.point.exception.PointScoreNotFoundException;
import jakarta.persistence.LockModeType;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Lock;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface PointScoreRepository extends JpaRepository<PointScore, Integer> {

@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("SELECT ps FROM PointScore ps WHERE ps.student = :student")
Optional<PointScore> findByStudentWithPessimisticLock(Student student);

Optional<PointScore> findByStudent(Student student);

List<PointScore> findByStudentIn(List<Student> students);
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("SELECT ps FROM PointScore ps WHERE ps.student IN :students")
List<PointScore> findByStudentInWithPessimisticLock(List<Student> students);

default List<PointScore> getByStudentIn(List<Student> students) {
List<PointScore> scores = findByStudentIn(students);
default List<PointScore> getByStudentInWithPessimisticLock(List<Student> students) {
List<PointScore> scores = findByStudentInWithPessimisticLock(students);

if(students.size() == scores.size()) {
if (students.size() == scores.size()) {
return scores;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,13 @@ public PointScore getScoreBy(Student student) {
.orElseThrow(PointScoreNotFoundException::new);
}

public List<PointScore> getScoresBy(List<Student> students) {
return scoreRepository.getByStudentIn(students);
public PointScore getScoreByStudentForUpdate(Student student) {
return scoreRepository.findByStudentWithPessimisticLock(student)
.orElseThrow(PointScoreNotFoundException::new);
}

public List<PointScore> getScoresByStudentsForUpdate(List<Student> students) {
return scoreRepository.getByStudentInWithPessimisticLock(students);
}

public List<PointScore> getAllScores() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public RedisCacheManager redisCacheManager() {
cacheConfigurations.put("meal-month", RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofSeconds(MONTH_EXPIRE_SECONDS)));

cacheConfigurations.put("members-cache", RedisCacheConfiguration.defaultCacheConfig());

return RedisCacheManager.RedisCacheManagerBuilder
.fromConnectionFactory(redisConnectionFactory())
.cacheDefaults(redisCacheConfiguration)
Expand Down

0 comments on commit b4f835d

Please sign in to comment.