Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ public ApiResponse<RunningRecordDTO> getRunningRecord(
RunningRecordDTO record = recordService.getRunningRecordById(userDetails.getMemberId(), recordId);
return ApiResponse.ok(record);
}

@DeleteMapping("/{recordId}")
public ApiResponse<Void> deleteRecord(
@AuthenticationPrincipal UserDetailsImpl userDetails,
@PathVariable Long recordId) {
recordService.deleteRecord(userDetails.getMemberId(), recordId);
return ApiResponse.ok();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,16 @@ public static LocalDate getMonthStart(LocalDate date) {
public static LocalDate getMonthEnd(LocalDate date) {
return date.with(TemporalAdjusters.lastDayOfMonth());
}

@Transactional
public void deleteRecord(Long memberId, Long recordId) {
RunningRecord record = recordRepository.findById(recordId)
.orElseThrow(() -> new RecordNotFoundException("Running record not found with id: " + recordId));

if (!record.getMemberId().equals(memberId)) {
throw new RecordNotFoundException("Running record not found with id: " + recordId);
}

recordRepository.delete(record);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
import static com.epages.restdocs.apispec.ResourceDocumentation.resource;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.doNothing;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.get;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.delete;
import static org.springframework.restdocs.payload.PayloadDocumentation.fieldWithPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand Down Expand Up @@ -322,4 +324,44 @@ void getAllRunningRecordsTest() throws Exception {
)
));
}

@Test
void deleteRecordTest() throws Exception {
// given
doNothing().when(recordService).deleteRecord(anyLong(), anyLong());
given(jwtUtil.getMemberIdFromToken(anyString())).willReturn(1L);
given(jwtUtil.getSocialIdFromToken(anyString())).willReturn("kakao_123");

UserDetailsImpl mockUserDetails = UserDetailsImpl.builder()
.memberId(1L)
.socialId("kakao_123")
.roles(List.of(MemberRole.USER))
.build();
given(userDetailsService.loadUserByUsername("1")).willReturn(mockUserDetails);

// when
this.mockMvc.perform(delete("/api/records/{recordId}", 1L)
.header(AUTH_HEADER, TEST_ACCESS_TOKEN))
.andExpect(status().isOk())
.andDo(document("record-delete",
resource(
ResourceSnippetParameters.builder()
.tag("records")
.summary("러닝 기록 삭제")
.description("본인의 러닝 기록을 삭제합니다.")
.requestHeaders(
headerWithName("Authorization").description("엑세스 토큰")
)
.pathParameters(
parameterWithName("recordId").description("삭제할 러닝 기록 ID")
)
.responseFields(
fieldWithPath("status.statusCode").type(JsonFieldType.STRING).description("상태 코드"),
fieldWithPath("status.message").type(JsonFieldType.STRING).description("상태 메시지"),
fieldWithPath("status.description").type(JsonFieldType.STRING).description("상태 설명").optional()
)
.build()
)
));
}
}