Skip to content

Commit

Permalink
feature: 음료 기록 삭제 API 구현 (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeJejune committed Jul 19, 2023
1 parent c766ea1 commit 07c447a
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import com.depromeet.oversweet.record.dto.response.DrinkRecordSaveResponse;
import com.depromeet.oversweet.record.service.DrinkRecordSaveService;
import com.depromeet.oversweet.response.DataResponse;
import com.depromeet.oversweet.response.MessageResponse;
import com.depromeet.oversweet.security.service.CustomUserDetails;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
Expand All @@ -15,9 +17,12 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Tag(name = "기록", description = "마신 음료 당 기록 관련 API")
Expand All @@ -38,10 +43,21 @@ public class RecordController {
@PostMapping("/drink")
public ResponseEntity<DataResponse<DrinkRecordSaveResponse>> saveDrinkRecord(
@RequestBody @Valid final DrinkRecordSaveRequest drinkRecordSaveRequest,
@AuthenticationPrincipal CustomUserDetails userDetails
@AuthenticationPrincipal final CustomUserDetails userDetails
) {
DrinkRecordSaveResponse response = drinkRecordSaveService.saveDrinkRecord(userDetails.getId(), drinkRecordSaveRequest);
final DrinkRecordSaveResponse response = drinkRecordSaveService.saveDrinkRecord(userDetails.getId(), drinkRecordSaveRequest);
return ResponseEntity.ok().body(DataResponse.of(HttpStatus.CREATED, "마신 음료 당 기록 성공", response));
}

@Operation(summary = "마신 음료 삭제", description = "유저가 마신 음료를 삭제할 수 있는 기능입니다.")
@ApiResponses(@ApiResponse(responseCode = "201", description = "음료 기록 삭제 성공"))
@SecurityRequirement(name = "accessToken")
@DeleteMapping("/drink/{drinkId}")
public ResponseEntity<MessageResponse> deleteDrinkRecord(
@AuthenticationPrincipal final CustomUserDetails userDetails,
@PathVariable @Parameter(description = "해당 음료의 Id") final Long drinkId
) {
drinkRecordSaveService.deleteDrinkRecord(userDetails.getId(), drinkId);
return ResponseEntity.ok().body(MessageResponse.of(HttpStatus.OK, "마신 음료 기록 삭제 성공"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.depromeet.oversweet.domain.member.entity.MemberEntity;
import com.depromeet.oversweet.domain.member.repository.FindMemberRepository;
import com.depromeet.oversweet.domain.record.entity.RecordEntity;
import com.depromeet.oversweet.domain.record.repository.DeleteRecordRepository;
import com.depromeet.oversweet.domain.record.repository.FindRecordsRepository;
import com.depromeet.oversweet.domain.record.repository.SaveRecordRepository;
import com.depromeet.oversweet.drink.dto.response.DrinkDailySugarTotalStatisticsInfo;
Expand All @@ -25,6 +26,7 @@ public class DrinkRecordSaveService {
private final FindDrinkRepository findDrinkRepository;
private final FindRecordsRepository findRecordsRepository;
private final SaveRecordRepository saveRecordRepository;
private final DeleteRecordRepository deleteRecordRepository;

public DrinkRecordSaveResponse saveDrinkRecord(Long memberId, DrinkRecordSaveRequest drinkRecordSaveRequest) {

Expand Down Expand Up @@ -54,4 +56,18 @@ public DrinkRecordSaveResponse saveDrinkRecord(Long memberId, DrinkRecordSaveReq
// 기록 관련 response 로 반환
return DrinkRecordSaveResponse.of(record.getIntakeSugar(), dailyTotalStatisticsInfo);
}

/**
* 마신 당 기록 삭제
*/
public void deleteDrinkRecord(final Long memberId, final Long drinkId) {

final MemberEntity findMember = findMemberRepository.findMemberById(memberId);

final DrinkEntity findDrink = findDrinkRepository.findDrinkById(drinkId);

final RecordEntity findRecord = findRecordsRepository.findRecordByMemberIdAndDrinkId(memberId, drinkId);

deleteRecordRepository.deleteRecordByMemberIdAndDrinkId(memberId, drinkId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public enum ErrorCode {
INVALID_TOKEN_ERROR(HttpStatus.UNAUTHORIZED, "S002", "토큰이 유효하지 않습니다."),
ACCESS_DENIED(HttpStatus.FORBIDDEN, "S003", "토큰이 존재하지 않아 접근이 불가합니다."),

// Record
NOT_FOUND_RECORD(HttpStatus.NOT_FOUND, "R001", "존재하지 않는 기록입니다."),

// Test
TEST_EXCEPTION(HttpStatus.BAD_REQUEST, "T001", "테스트 에러");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.depromeet.oversweet.exception.record;

import com.depromeet.oversweet.exception.ErrorCode;
import com.depromeet.oversweet.exception.OverSweetException;

public class NotFoundRecordException extends OverSweetException {
public NotFoundRecordException(ErrorCode errorCode) {
super(errorCode);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.depromeet.oversweet.domain.record.repository;

public interface DeleteRecordRepository {
void deleteRecordByMemberIdAndDrinkId(Long memberId, Long drinkId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.depromeet.oversweet.domain.record.repository;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
@RequiredArgsConstructor
public class DeleteRecordRepositoryImpl implements DeleteRecordRepository{

private final RecordJpaRepository recordJpaRepository;

@Override
@Transactional
public void deleteRecordByMemberIdAndDrinkId(final Long memberId, final Long drinkId) {
recordJpaRepository.deleteByMemberIdAndDrinkId(memberId, drinkId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface FindRecordsRepository {
Optional<RecordEntity> findRecordById(Long id);

List<RankingDrink> findPopularDrinkRecordsByFranchiseId(Long franchiseId, LocalDateTime startDate, LocalDateTime endDate);

RecordEntity findRecordByMemberIdAndDrinkId(Long memberId, Long drinkId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.util.List;
import java.util.Optional;

import com.depromeet.oversweet.exception.ErrorCode;
import com.depromeet.oversweet.exception.record.NotFoundRecordException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -84,5 +86,12 @@ public List<RankingDrink> findPopularDrinkRecordsByFranchiseId(Long franchiseId,
.limit(3)
.fetch();
}

@Override
@Transactional
public RecordEntity findRecordByMemberIdAndDrinkId(final Long memberId, final Long drinkId) {
return recordJpaRepository.findByMemberIdAndDrinkId(memberId, drinkId)
.orElseThrow(() -> new NotFoundRecordException(ErrorCode.NOT_FOUND_RECORD));
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

import com.depromeet.oversweet.domain.record.dto.RankingDrink;
import com.depromeet.oversweet.domain.record.entity.RecordEntity;
Expand All @@ -24,4 +25,8 @@ List<RankingDrink> findTop3PopularDrinksByFranchiseIdAndBetweenDates(
@Param("startDate") LocalDateTime startDate,
@Param("endDate") LocalDateTime endDate,
Pageable pageable);

Optional<RecordEntity> findByMemberIdAndDrinkId(Long memberId, Long drinkId);

void deleteByMemberIdAndDrinkId(Long memberId, Long drinkId);
}

0 comments on commit 07c447a

Please sign in to comment.