Skip to content

Commit

Permalink
Merge pull request #318 from Team-Sopetit/refactor/#316-member-routine
Browse files Browse the repository at this point in the history
[REFACTOR] 데일리 루틴 다중 추가 api 수정
  • Loading branch information
Chan531 authored Aug 16, 2024
2 parents 08d559a + 6d62d3a commit 9867260
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package com.soptie.server.memberroutine.controller.v2;

import static com.soptie.server.common.dto.SuccessResponse.*;
import static com.soptie.server.memberroutine.message.MemberRoutineSuccessMassage.*;
import static com.soptie.server.common.dto.SuccessResponse.success;
import static com.soptie.server.memberroutine.message.MemberRoutineSuccessMassage.SUCCESS_CREATE_ROUTINE;
import static com.soptie.server.memberroutine.message.MemberRoutineSuccessMassage.SUCCESS_GET_ROUTINE;

import java.security.Principal;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import com.soptie.server.common.dto.BaseResponse;
import com.soptie.server.common.dto.SuccessResponse;
import com.soptie.server.memberroutine.controller.v1.dto.request.MemberDailyRoutinesCreateRequest;
import com.soptie.server.memberroutine.controller.v2.docs.MemberDailyRoutineControllerV2Docs;
import com.soptie.server.memberroutine.controller.v2.dto.response.MemberDailyRoutineListAcquireResponseV2;
import com.soptie.server.memberroutine.controller.v2.dto.response.MemberDailyRoutinesCreateResponse;
import com.soptie.server.memberroutine.service.MemberRoutineCreateService;
import com.soptie.server.memberroutine.service.MemberRoutineReadService;
import com.soptie.server.memberroutine.service.dto.request.MemberDailyRoutineListAcquireServiceRequest;
Expand All @@ -42,12 +45,14 @@ public ResponseEntity<SuccessResponse<MemberDailyRoutineListAcquireResponseV2>>
}

@PostMapping
public ResponseEntity<BaseResponse> createMemberDailyRoutines(
@ResponseStatus(HttpStatus.CREATED)
public SuccessResponse<MemberDailyRoutinesCreateResponse> createMemberDailyRoutines(
Principal principal,
MemberDailyRoutinesCreateRequest request
) {
val memberId = Long.parseLong(principal.getName());
memberRoutineCreateService.createDailyRoutines(memberId, request);
return ResponseEntity.ok(SuccessResponse.success(SUCCESS_CREATE_ROUTINE.getMessage()));
val response = MemberDailyRoutinesCreateResponse
.of(memberRoutineCreateService.createDailyRoutines(memberId, request));
return SuccessResponse.success(SUCCESS_CREATE_ROUTINE.getMessage(), response);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;

import com.soptie.server.common.dto.BaseResponse;
import com.soptie.server.common.dto.ErrorResponse;
import com.soptie.server.common.dto.SuccessResponse;
import com.soptie.server.memberroutine.controller.v1.dto.request.MemberDailyRoutinesCreateRequest;
import com.soptie.server.memberroutine.controller.v2.dto.response.MemberDailyRoutineListAcquireResponseV2;
import com.soptie.server.memberroutine.controller.v2.dto.response.MemberDailyRoutinesCreateResponse;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand Down Expand Up @@ -48,7 +48,7 @@ ResponseEntity<SuccessResponse<MemberDailyRoutineListAcquireResponseV2>> acquire
description = "회원의 데일리 루틴을 다중으로 추가한다.",
responses = {
@ApiResponse(
responseCode = "200",
responseCode = "201",
description = "성공",
content = @Content(schema = @Schema(implementation = SuccessResponse.class))),
@ApiResponse(
Expand All @@ -64,7 +64,7 @@ ResponseEntity<SuccessResponse<MemberDailyRoutineListAcquireResponseV2>> acquire
description = "서버 내부 오류",
content = @Content(schema = @Schema(implementation = ErrorResponse.class)))}
)
ResponseEntity<BaseResponse> createMemberDailyRoutines(
SuccessResponse<MemberDailyRoutinesCreateResponse> createMemberDailyRoutines(
@Parameter(hidden = true) Principal principal,
@RequestBody MemberDailyRoutinesCreateRequest request
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.soptie.server.memberroutine.controller.v2.dto.response;

import static lombok.AccessLevel.PRIVATE;

import java.util.List;

import com.soptie.server.memberroutine.service.dto.response.MemberDailyRoutinesCreateServiceResponse;

import lombok.Builder;
import lombok.NonNull;

@Builder(access = PRIVATE)
public record MemberDailyRoutinesCreateResponse(
@NonNull List<Long> ids
) {

public static MemberDailyRoutinesCreateResponse of(MemberDailyRoutinesCreateServiceResponse response) {
return MemberDailyRoutinesCreateResponse.builder()
.ids(response.ids())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.soptie.server.memberroutine.service;

import static com.soptie.server.routine.message.RoutineErrorCode.*;
import static com.soptie.server.routine.message.RoutineErrorCode.CANNOT_ADD_MEMBER_ROUTINE;
import static com.soptie.server.routine.message.RoutineErrorCode.DUPLICATED_ROUTINE;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -13,6 +14,7 @@
import com.soptie.server.memberroutine.service.dto.request.MemberDailyRoutineCreateServiceRequest;
import com.soptie.server.memberroutine.service.dto.request.MemberHappinessRoutineCreateServiceRequest;
import com.soptie.server.memberroutine.service.dto.response.MemberDailyRoutineCreateServiceResponse;
import com.soptie.server.memberroutine.service.dto.response.MemberDailyRoutinesCreateServiceResponse;
import com.soptie.server.memberroutine.service.dto.response.MemberHappinessRoutineCreateServiceResponse;
import com.soptie.server.routine.adapter.ChallengeFinder;
import com.soptie.server.routine.adapter.RoutineFinder;
Expand Down Expand Up @@ -41,10 +43,16 @@ public MemberDailyRoutineCreateServiceResponse createDailyRoutine(MemberDailyRou
return MemberDailyRoutineCreateServiceResponse.of(savedMemberRoutine);
}

public void createDailyRoutines(long memberId, MemberDailyRoutinesCreateRequest request) {
public MemberDailyRoutinesCreateServiceResponse createDailyRoutines(
long memberId, MemberDailyRoutinesCreateRequest request
) {
val member = memberFinder.findById(memberId);
val routines = routineFinder.findDailyByIds(request.routineIds());
routines.forEach(routine -> memberRoutineSaver.checkHasDeletedAndSave(member, routine));
routines.forEach(routine -> checkMemberHasSameRoutineAlready(member, routine));
val memberRoutines = routines.stream()
.map(routine -> memberRoutineSaver.checkHasDeletedAndSave(member, routine))
.toList();
return MemberDailyRoutinesCreateServiceResponse.of(memberRoutines);
}

public MemberHappinessRoutineCreateServiceResponse createHappinessRoutine(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.soptie.server.memberroutine.service.dto.response;

import static lombok.AccessLevel.PRIVATE;

import java.util.List;

import com.soptie.server.memberroutine.entity.MemberRoutine;

import lombok.Builder;
import lombok.NonNull;

@Builder(access = PRIVATE)
public record MemberDailyRoutinesCreateServiceResponse(
@NonNull List<Long> ids
) {

public static MemberDailyRoutinesCreateServiceResponse of(List<MemberRoutine> memberRoutines) {
return MemberDailyRoutinesCreateServiceResponse.builder()
.ids(memberRoutines.stream().map(MemberRoutine::getId).toList())
.build();
}
}

0 comments on commit 9867260

Please sign in to comment.