Skip to content

Commit

Permalink
[ADD] 루틴 추가 중 예전 루틴 체크 로직 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
thguss committed Jan 11, 2024
1 parent fbb3f65 commit f3142f9
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@NoArgsConstructor
@Getter
public class CompletedMemberDailyRoutine {

@Id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ public MemberDailyRoutine(Member member, DailyRoutine routine) {
this.routine = routine;
}

public MemberDailyRoutine(Member member, DailyRoutine routine, int achieveCount) {
this.achieveCount = achieveCount;
this.isAchieve = false;
setMember(member);
this.routine = routine;
}

private void setMember(Member member) {
if (Objects.nonNull(this.member)) {
this.member.getDailyRoutines().remove(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.soptie.server.memberRoutine.repository;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;

import com.soptie.server.member.entity.Member;
import com.soptie.server.memberRoutine.entity.daily.CompletedMemberDailyRoutine;
import com.soptie.server.routine.entity.daily.DailyRoutine;

public interface CompletedMemberDailyRoutineRepository extends JpaRepository<CompletedMemberDailyRoutine, Long> {
Optional<CompletedMemberDailyRoutine> findByMemberAndRoutine(Member member, DailyRoutine routine);
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,25 @@ public class MemberDailyRoutineServiceImpl implements MemberDailyRoutineService
public MemberDailyRoutineResponse createMemberDailyRoutine(long memberId, MemberDailyRoutineRequest request) {
val member = findMember(memberId);
val routine = findRoutine(request.routineId());
val memberRoutine = new MemberDailyRoutine(member, routine);
val savedMemberRoutine = memberDailyRoutineRepository.save(memberRoutine);
val savedMemberRoutine = getMemberDailyRoutine(member, routine);
return MemberDailyRoutineResponse.of(savedMemberRoutine.getId());
}

private MemberDailyRoutine getMemberDailyRoutine(Member member, DailyRoutine routine) {
return completedMemberDailyRoutineRepository.findByMemberAndRoutine(member, routine)
.map(completedRoutine -> createOldRoutines(member, routine, completedRoutine))
.orElseGet(() -> createNewRoutine(member, routine));
}

private MemberDailyRoutine createNewRoutine(Member member, DailyRoutine routine) {
return memberDailyRoutineRepository.save(new MemberDailyRoutine(member, routine));
}

private MemberDailyRoutine createOldRoutines(Member member, DailyRoutine routine, CompletedMemberDailyRoutine completedRoutine) {
completedMemberDailyRoutineRepository.delete(completedRoutine);
return memberDailyRoutineRepository.save(new MemberDailyRoutine(member, routine, completedRoutine.getAchieveCount()));
}

@Override
@Transactional
public void createMemberDailyRoutines(Member member, List<Long> routines) {
Expand Down
14 changes: 7 additions & 7 deletions src/main/resources/static/docs/open-api-3.0.1.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@
"content" : {
"application/json;charset=UTF-8" : {
"schema" : {
"$ref" : "#/components/schemas/api-v1-routines-daily-54531209"
"$ref" : "#/components/schemas/api-v1-routines-daily-theme-themeId-54531209"
},
"examples" : {
"get-themes-daily-routines-docs" : {
"value" : "{\n \"success\" : true,\n \"message\" : \"루틴 조회 성공\",\n \"data\" : {\n \"routines\" : [ {\n \"routineId\" : 0,\n \"content\" : \"content0\"\n }, {\n \"routineId\" : 1,\n \"content\" : \"content1\"\n }, {\n \"routineId\" : 2,\n \"content\" : \"content2\"\n }, {\n \"routineId\" : 3,\n \"content\" : \"content3\"\n }, {\n \"routineId\" : 4,\n \"content\" : \"content4\"\n } ]\n }\n}"
"value" : "{\n \"success\" : true,\n \"message\" : \"루틴 조회 성공\",\n \"data\" : {\n \"routines\" : [ {\n \"routineId\" : 1,\n \"content\" : \"Daily routine Content1\"\n }, {\n \"routineId\" : 2,\n \"content\" : \"Daily routine Content2\"\n }, {\n \"routineId\" : 3,\n \"content\" : \"Daily routine Content3\"\n }, {\n \"routineId\" : 4,\n \"content\" : \"Daily routine Content4\"\n }, {\n \"routineId\" : 5,\n \"content\" : \"Daily routine Content5\"\n } ]\n }\n}"
}
}
}
Expand Down Expand Up @@ -168,7 +168,7 @@
},
"examples" : {
"get-member-daily-routines-docs" : {
"value" : "{\n \"success\" : true,\n \"message\" : \"루틴 조회 성공\",\n \"data\" : {\n \"routines\" : [ {\n \"routineId\" : 1,\n \"content\" : \"content-test\",\n \"iconImageUrl\" : \"image-url\",\n \"achieveCount\" : 1,\n \"isAchieve\" : false\n }, {\n \"routineId\" : 2,\n \"content\" : \"content-test\",\n \"iconImageUrl\" : \"image-url\",\n \"achieveCount\" : 2,\n \"isAchieve\" : true\n }, {\n \"routineId\" : 3,\n \"content\" : \"content-test\",\n \"iconImageUrl\" : \"image-url\",\n \"achieveCount\" : 3,\n \"isAchieve\" : false\n }, {\n \"routineId\" : 4,\n \"content\" : \"content-test\",\n \"iconImageUrl\" : \"image-url\",\n \"achieveCount\" : 4,\n \"isAchieve\" : true\n }, {\n \"routineId\" : 5,\n \"content\" : \"content-test\",\n \"iconImageUrl\" : \"image-url\",\n \"achieveCount\" : 5,\n \"isAchieve\" : false\n } ]\n }\n}"
"value" : "{\n \"success\" : true,\n \"message\" : \"루틴 조회 성공\",\n \"data\" : {\n \"routines\" : [ {\n \"routineId\" : 1,\n \"content\" : \"Daily routine Content1\",\n \"iconImageUrl\" : \"icon_image_url1\",\n \"achieveCount\" : 1,\n \"isAchieve\" : false\n }, {\n \"routineId\" : 2,\n \"content\" : \"Daily routine Content2\",\n \"iconImageUrl\" : \"icon_image_url2\",\n \"achieveCount\" : 2,\n \"isAchieve\" : true\n }, {\n \"routineId\" : 3,\n \"content\" : \"Daily routine Content3\",\n \"iconImageUrl\" : \"icon_image_url3\",\n \"achieveCount\" : 3,\n \"isAchieve\" : false\n }, {\n \"routineId\" : 4,\n \"content\" : \"Daily routine Content4\",\n \"iconImageUrl\" : \"icon_image_url4\",\n \"achieveCount\" : 4,\n \"isAchieve\" : true\n }, {\n \"routineId\" : 5,\n \"content\" : \"Daily routine Content5\",\n \"iconImageUrl\" : \"icon_image_url5\",\n \"achieveCount\" : 5,\n \"isAchieve\" : false\n } ]\n }\n}"
}
}
}
Expand Down Expand Up @@ -238,7 +238,7 @@
},
"examples" : {
"get-daily-themes-docs" : {
"value" : "{\n \"success\" : true,\n \"message\" : \"테마 조회 성공\",\n \"data\" : {\n \"themes\" : [ {\n \"themeId\" : 0,\n \"name\" : \"Daily routine Theme0\",\n \"iconImageUrl\" : \"icon_image_url0\",\n \"backgroundImageUrl\" : \"background_image_url0\"\n }, {\n \"themeId\" : 1,\n \"name\" : \"Daily routine Theme1\",\n \"iconImageUrl\" : \"icon_image_url1\",\n \"backgroundImageUrl\" : \"background_image_url1\"\n }, {\n \"themeId\" : 2,\n \"name\" : \"Daily routine Theme2\",\n \"iconImageUrl\" : \"icon_image_url2\",\n \"backgroundImageUrl\" : \"background_image_url2\"\n }, {\n \"themeId\" : 3,\n \"name\" : \"Daily routine Theme3\",\n \"iconImageUrl\" : \"icon_image_url3\",\n \"backgroundImageUrl\" : \"background_image_url3\"\n }, {\n \"themeId\" : 4,\n \"name\" : \"Daily routine Theme4\",\n \"iconImageUrl\" : \"icon_image_url4\",\n \"backgroundImageUrl\" : \"background_image_url4\"\n } ]\n }\n}"
"value" : "{\n \"success\" : true,\n \"message\" : \"테마 조회 성공\",\n \"data\" : {\n \"themes\" : [ {\n \"themeId\" : 1,\n \"name\" : \"Daily routine Theme1\",\n \"iconImageUrl\" : \"icon_image_url1\",\n \"backgroundImageUrl\" : \"background_image_url1\"\n }, {\n \"themeId\" : 2,\n \"name\" : \"Daily routine Theme2\",\n \"iconImageUrl\" : \"icon_image_url2\",\n \"backgroundImageUrl\" : \"background_image_url2\"\n }, {\n \"themeId\" : 3,\n \"name\" : \"Daily routine Theme3\",\n \"iconImageUrl\" : \"icon_image_url3\",\n \"backgroundImageUrl\" : \"background_image_url3\"\n }, {\n \"themeId\" : 4,\n \"name\" : \"Daily routine Theme4\",\n \"iconImageUrl\" : \"icon_image_url4\",\n \"backgroundImageUrl\" : \"background_image_url4\"\n }, {\n \"themeId\" : 5,\n \"name\" : \"Daily routine Theme5\",\n \"iconImageUrl\" : \"icon_image_url5\",\n \"backgroundImageUrl\" : \"background_image_url5\"\n } ]\n }\n}"
}
}
}
Expand Down Expand Up @@ -268,11 +268,11 @@
"content" : {
"application/json;charset=UTF-8" : {
"schema" : {
"$ref" : "#/components/schemas/api-v1-routines-daily-54531209"
"$ref" : "#/components/schemas/api-v1-routines-daily-theme-themeId-54531209"
},
"examples" : {
"get-theme-daily-routines-docs" : {
"value" : "{\n \"success\" : true,\n \"message\" : \"루틴 조회 성공\",\n \"data\" : {\n \"routines\" : [ {\n \"routineId\" : 0,\n \"content\" : \"content0\"\n }, {\n \"routineId\" : 1,\n \"content\" : \"content1\"\n }, {\n \"routineId\" : 2,\n \"content\" : \"content2\"\n }, {\n \"routineId\" : 3,\n \"content\" : \"content3\"\n }, {\n \"routineId\" : 4,\n \"content\" : \"content4\"\n } ]\n }\n}"
"value" : "{\n \"success\" : true,\n \"message\" : \"루틴 조회 성공\",\n \"data\" : {\n \"routines\" : [ {\n \"routineId\" : 1,\n \"content\" : \"Daily routine Content1\"\n }, {\n \"routineId\" : 2,\n \"content\" : \"Daily routine Content2\"\n }, {\n \"routineId\" : 3,\n \"content\" : \"Daily routine Content3\"\n }, {\n \"routineId\" : 4,\n \"content\" : \"Daily routine Content4\"\n }, {\n \"routineId\" : 5,\n \"content\" : \"Daily routine Content5\"\n } ]\n }\n}"
}
}
}
Expand Down Expand Up @@ -377,7 +377,7 @@
}
}
},
"api-v1-routines-daily-54531209" : {
"api-v1-routines-daily-theme-themeId-54531209" : {
"type" : "object",
"properties" : {
"data" : {
Expand Down

0 comments on commit f3142f9

Please sign in to comment.