Skip to content

Commit

Permalink
Merge pull request #348 from Team-Sopetit/hotfix/#346-init-routine
Browse files Browse the repository at this point in the history
hotfix: 하루 루틴 달성 초기화 기능 수정
  • Loading branch information
thguss authored Oct 16, 2024
2 parents 9a53764 + 0a64d6b commit 852ed9f
Show file tree
Hide file tree
Showing 31 changed files with 315 additions and 43 deletions.
19 changes: 19 additions & 0 deletions src/main/java/com/soptie/server/batch/BatchProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.soptie.server.batch;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "batch")
public record BatchProperties(
Cron cron
) {

public record Cron(
Init init
) {

public record Init(
String routine
) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class MemberRoutineScheduler {
private final MemberRoutineService memberRoutineService;

@Scheduled(cron = "${softie.cron.init.routine}")
@Scheduled(cron = "${batch.cron.init.routine}")
public void initMemberDailyRoutines() {
memberRoutineService.initAchievement();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.soptie.server.persistence.config;
package com.soptie.server.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.soptie.server.persistence.config;
package com.soptie.server.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/soptie/server/config/PropertyConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.soptie.server.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import com.soptie.server.batch.BatchProperties;

@Configuration
@EnableConfigurationProperties({BatchProperties.class})
public class PropertyConfig {
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.soptie.server.api.config;
package com.soptie.server.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.soptie.server.api.config;
package com.soptie.server.config;

import static io.swagger.v3.oas.models.security.SecurityScheme.Type.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.soptie.server.persistence.config;
package com.soptie.server.config;

import java.util.TimeZone;

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/soptie/server/domain/member/Member.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package com.soptie.server.domain.member;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class Member {
private Long id;
private Social socialInfo;
private String refreshToken;
private MemberCotton cottonInfo;

public Member(SocialType socialType, String socialId) {
this.socialInfo = new Social(socialType, socialId);
this.cottonInfo = new MemberCotton(0, 0);
}

public void resetRefreshToken() {
this.refreshToken = null;
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/soptie/server/domain/member/Social.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import jakarta.validation.constraints.NotNull;
import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
public class Social {
@NotNull
protected SocialType socialType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.soptie.server.domain.memberroutine;

import com.soptie.server.domain.member.Member;
import com.soptie.server.domain.routine.Routine;

import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class MemberRoutine {
private Long id;
private boolean isAchieved;
Expand All @@ -13,14 +19,17 @@ public class MemberRoutine {
private long memberId;
private long routineId;

public MemberRoutine(Member member, Routine routine) {
this.isAchieved = false;
this.isAchievedToday = false;
this.achievementCount = 0;
this.memberId = member.getId();
this.routineId = routine.getId();
}

public void achieve() {
this.isAchievedToday = true;
this.achievementCount += !this.isAchieved ? 1 : -1;
this.isAchieved = !this.isAchieved;
}

public void initAchievement() {
this.isAchievedToday = false;
this.isAchieved = false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,7 @@ public AchieveMemberRoutineResponse achieveMemberRoutine(long memberId, long mem

@Transactional
public void initAchievement() {
val memberRoutines = memberRoutineAdapter.findAchieved();
for (val memberRoutine : memberRoutines) {
memberRoutine.initAchievement();
}
memberRoutineAdapter.initAllAchievement();
}

private Map<Theme, Map<Routine, MemberRoutine>> toRoutinesByTheme(
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/soptie/server/domain/routine/Routine.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package com.soptie.server.domain.routine;

import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
@AllArgsConstructor(access = AccessLevel.PROTECTED)
public class Routine {
private Long id;
@NotNull
private String content;
private long themeId;

public Routine(String content, long themeId) {
this.content = content;
this.themeId = themeId;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.soptie.server.persistence.adapter;

import java.util.ArrayList;
import java.util.List;

import com.soptie.server.common.exception.ExceptionCode;
Expand All @@ -10,7 +9,7 @@
import com.soptie.server.domain.memberroutine.MemberRoutine;
import com.soptie.server.domain.routine.Routine;
import com.soptie.server.persistence.entity.MemberRoutineEntity;
import com.soptie.server.persistence.repository.MemberRoutineRepository;
import com.soptie.server.persistence.repository.routine.MemberRoutineRepository;

import lombok.RequiredArgsConstructor;
import lombok.val;
Expand Down Expand Up @@ -60,14 +59,8 @@ public void update(MemberRoutine memberRoutine) {
memberRoutineEntity.update(memberRoutine);
}

public List<MemberRoutine> findAchieved() {
val memberRoutines = new ArrayList<MemberRoutine>();
memberRoutines.addAll(memberRoutineRepository.findByIsAchieved(true).stream()
.map(MemberRoutineEntity::toDomain)
.toList());
memberRoutines.addAll(memberRoutineRepository.findDeletedByIsAchieved(true).stream().map(
MemberRoutineEntity::toDomain).toList());
return memberRoutines;
public void initAllAchievement() {
memberRoutineRepository.bulkInitAchievement();
}

private MemberRoutineEntity restore(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.soptie.server.common.support.RepositoryAdapter;
import com.soptie.server.domain.routine.Routine;
import com.soptie.server.persistence.entity.RoutineEntity;
import com.soptie.server.persistence.repository.RoutineRepository;
import com.soptie.server.persistence.repository.routine.RoutineRepository;

import lombok.RequiredArgsConstructor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,22 @@ public class MemberEntity extends BaseEntity {
@Column(nullable = false)
private int rainbowCottonCount;

//TODO: 2번째 생성자 활용(Entity 보호)
public MemberEntity(SocialType socialType, String socialId) {
this.socialType = socialType;
this.socialId = socialId;
this.basicCottonCount = 0;
this.rainbowCottonCount = 0;
}

public MemberEntity(Member member) {
this.socialType = member.getSocialInfo().getSocialType();
this.socialId = member.getSocialInfo().getSocialId();
this.refreshToken = member.getRefreshToken();
this.basicCottonCount = member.getCottonInfo().getBasicCottonCount();
this.rainbowCottonCount = member.getCottonInfo().getRainbowCottonCount();
}

public void update(Member member) {
this.refreshToken = member.getRefreshToken();
this.basicCottonCount = member.getCottonInfo().getBasicCottonCount();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public class MemberRoutineEntity extends BaseEntity {
@Column(nullable = false)
private long routineId;

//TODO: 아래 생성자 활용 (Entity 보호)
public MemberRoutineEntity(Member member, Routine routine) {
this.isAchieved = false;
this.isAchievedToday = false;
Expand All @@ -47,6 +48,15 @@ public MemberRoutineEntity(Member member, Routine routine) {
this.routineId = routine.getId();
}

public MemberRoutineEntity(MemberRoutine memberRoutine) {
this.isAchieved = memberRoutine.isAchieved();
this.isAchievedToday = memberRoutine.isAchievedToday();
this.achievementCount = memberRoutine.getAchievementCount();
this.isDeleted = false;
this.memberId = memberRoutine.getMemberId();
this.routineId = memberRoutine.getRoutineId();
}

public MemberRoutine toDomain() {
return MemberRoutine.builder()
.id(this.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "routine", schema = "softie")
public class RoutineEntity extends BaseEntity {
@Column(nullable = false)
private String content;
@Column(nullable = false)
private long themeId;

public RoutineEntity(Routine routine) {
this.content = routine.getContent();
this.themeId = routine.getThemeId();
}

public Routine toDomain() {
return Routine.builder()
.id(this.id)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.soptie.server.persistence.repository;
package com.soptie.server.persistence.repository.routine;

import java.util.List;

Expand All @@ -7,25 +7,20 @@
import org.springframework.data.jpa.repository.Query;

import com.soptie.server.persistence.entity.MemberRoutineEntity;
import com.soptie.server.persistence.repository.routine.custom.MemberRoutineCustomRepository;

public interface MemberRoutineRepository
extends JpaRepository<MemberRoutineEntity, Long>, MemberRoutineCustomRepository {

public interface MemberRoutineRepository extends JpaRepository<MemberRoutineEntity, Long> {
@Query(
value = "SELECT * FROM member_routine WHERE member_id = :memberId AND is_deleted = true",
nativeQuery = true)
List<MemberRoutineEntity> findDeletedByMemberId(long memberId);

@Query(
value = "SELECT * FROM member_routine WHERE is_achieved = :isAchieved AND is_deleted = true",
nativeQuery = true)
List<MemberRoutineEntity> findDeletedByIsAchieved(boolean isAchieved);

List<MemberRoutineEntity> findByMemberId(long memberId);

List<MemberRoutineEntity> findByIdIn(List<Long> ids);

@SuppressWarnings("SpringDataMethodInconsistencyInspection")
List<MemberRoutineEntity> findByIsAchieved(boolean isAchieved);

@Modifying
@Query(
value = "DELETE FROM member_routine WHERE member_id = :memberId",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.soptie.server.persistence.repository;
package com.soptie.server.persistence.repository.routine;

import java.util.List;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.soptie.server.persistence.repository.routine.custom;

public interface MemberRoutineCustomRepository {
void bulkInitAchievement();

long countByAchieved(boolean isAchieved);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.soptie.server.persistence.repository.routine.custom;

import static com.soptie.server.persistence.entity.QMemberRoutineEntity.*;

import org.springframework.stereotype.Repository;

import com.querydsl.jpa.impl.JPAQueryFactory;

import lombok.RequiredArgsConstructor;

@Repository
@RequiredArgsConstructor
public class MemberRoutineRepositoryImpl implements MemberRoutineCustomRepository {

private final JPAQueryFactory queryFactory;

@Override
public void bulkInitAchievement() {
queryFactory.update(memberRoutineEntity)
.set(memberRoutineEntity.isAchieved, false)
.set(memberRoutineEntity.isAchievedToday, false)
.where(memberRoutineEntity.isAchieved.isTrue())
.execute();
}

@Override
public long countByAchieved(boolean isAchieved) {
return queryFactory
.selectFrom(memberRoutineEntity)
.where(memberRoutineEntity.isAchieved.eq(isAchieved))
.stream().count();
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jwt:
ACCESS_TOKEN_EXPIRED: 7200000
REFRESH_TOKEN_EXPIRED: 1209600000

softie:
batch:
cron:
init:
routine: "0 0 0 * * *"
2 changes: 1 addition & 1 deletion src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jwt:
ACCESS_TOKEN_EXPIRED: 7200000
REFRESH_TOKEN_EXPIRED: 1209600000

softie:
batch:
cron:
init:
routine: "0 0 0 * * *"
Loading

0 comments on commit 852ed9f

Please sign in to comment.