Skip to content

Commit

Permalink
πŸš€ λ§ˆμ‹€ μ‚­μ œ API μΆ”κ°€
Browse files Browse the repository at this point in the history
πŸš€ λ§ˆμ‹€ μ‚­μ œ API μΆ”κ°€
  • Loading branch information
ASak1104 authored Aug 9, 2024
2 parents 9bc56c5 + 08e2757 commit 6cb0d8e
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import lombok.RequiredArgsConstructor;
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.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand Down Expand Up @@ -139,4 +140,19 @@ public ResponseEntity<MasilDetailResponse> getById(
return ResponseEntity.ok(response);
}

@DeleteMapping("/api/v1/masils/{id}")
@Operation(summary = "λ§ˆμ‹€ μ‚­μ œ")
@ApiResponse(responseCode = "204")
public ResponseEntity<Void> deleteById(
@AuthenticationPrincipal
Long userId,
@PathVariable
Long id
) {
masilService.deleteById(userId, id);

return ResponseEntity.noContent()
.build();
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package team.silvertown.masil.masil.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import team.silvertown.masil.masil.domain.Masil;
import team.silvertown.masil.masil.domain.MasilPin;

public interface MasilPinRepository extends JpaRepository<MasilPin, Long> {

@Modifying
@Query("DELETE FROM MasilPin mp WHERE mp.masil = :masil")
void deleteAllByMasil(Masil masil);

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public class MasilQueryRepositoryImpl implements MasilQueryRepository {
private static final int DEFAULT_RECENT_SIZE = 10;

private final JPAQueryFactory jpaQueryFactory;
private final QMasil masil = QMasil.masil;
private final QMasil qMasil = QMasil.masil;

@Override
public List<Masil> findRecent(User user, Integer size) {
int limit = DEFAULT_RECENT_SIZE;

Expand All @@ -36,10 +37,10 @@ public List<Masil> findRecent(User user, Integer size) {
}

return jpaQueryFactory
.selectFrom(masil)
.where(masil.user.eq(user))
.selectFrom(qMasil)
.where(qMasil.user.eq(user))
.limit(limit)
.orderBy(masil.id.desc())
.orderBy(qMasil.id.desc())
.fetch();
}

Expand All @@ -50,15 +51,15 @@ public List<MasilDailyDto> findInGivenPeriod(
OffsetDateTime endDateTime
) {
BooleanBuilder condition = new BooleanBuilder();
StringTemplate startDate = convertToLocalDate(masil.startedAt);
StringTemplate startDate = convertToLocalDate(qMasil.startedAt);

condition.and(masil.user.eq(user))
.and(masil.startedAt.between(startDateTime, endDateTime));
condition.and(qMasil.user.eq(user))
.and(qMasil.startedAt.between(startDateTime, endDateTime));

return jpaQueryFactory
.selectFrom(masil)
.selectFrom(qMasil)
.where(condition)
.orderBy(masil.startedAt.asc())
.orderBy(qMasil.startedAt.asc())
.transform(
GroupBy.groupBy(startDate)
.as(projectDailyDetail())
Expand All @@ -80,13 +81,13 @@ private GroupExpression<MasilDailyDetailDto, List<MasilDailyDetailDto>> projectD
return GroupBy.list(
Projections.constructor(
MasilDailyDetailDto.class,
masil.id,
masil.address,
masil.content,
masil.thumbnailUrl,
masil.distance,
masil.totalTime,
masil.calories
qMasil.id,
qMasil.address,
qMasil.content,
qMasil.thumbnailUrl,
qMasil.distance,
qMasil.totalTime,
qMasil.calories
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ public MasilDetailResponse getById(Long userId, Long id) {
return MasilDetailResponse.from(masil, pins);
}

@Transactional
public void deleteById(Long userId, Long id) {
User user = userRepository.findById(userId)
.orElseThrow(getNotFoundException(MasilErrorCode.USER_NOT_FOUND));
Masil masil = masilRepository.findById(id)
.orElseThrow(getNotFoundException(MasilErrorCode.MASIL_NOT_FOUND));

MasilValidator.validateMasilOwner(masil, user);

masilPinRepository.deleteAllByMasil(masil);
masilRepository.delete(masil);
}

@Transactional(readOnly = true)
public RecentMasilResponse getRecent(Long userId, Integer size) {
User user = userRepository.findById(userId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayNameGeneration;
Expand Down Expand Up @@ -213,6 +214,83 @@ void setUp() {
assertThat(response.isEmpty()).isTrue();
}

@Test
void λΉ„νšŒμ›_μ‚¬μš©μžμΌ_경우_λ§ˆμ‹€_μ‚­μ œμ—_μ‹€νŒ¨ν•œλ‹€() {
// given
long invalidUserId = MasilTexture.getRandomId();
Masil masil = masilRepository.save(MasilTexture.createDependentMasil(user, 10000));
List<MasilPin> masilPins = MasilTexture.createDependentMasilPins(masil, user.getId(), 10);

masilPinRepository.saveAll(masilPins);
entityManager.clear();

masilRepository.save(masil);
masilPinRepository.saveAll(masilPins);
entityManager.clear();

// when
ThrowingCallable deleteById = () -> masilService.deleteById(invalidUserId, masil.getId());

// then
assertThatExceptionOfType(DataNotFoundException.class)
.isThrownBy(deleteById)
.withMessage(MasilErrorCode.USER_NOT_FOUND.getMessage());
}

@Test
void μ‘΄μž¬ν•˜μ§€_μ•ŠλŠ”_λ§ˆμ‹€μΌ_경우_μ‚­μ œμ—_μ‹€νŒ¨ν•œλ‹€() {
// given
long invalidMasilId = MasilTexture.getRandomId();

// when
ThrowingCallable deleteById = () -> masilService.deleteById(user.getId(), invalidMasilId);

// then
assertThatExceptionOfType(DataNotFoundException.class)
.isThrownBy(deleteById)
.withMessage(MasilErrorCode.MASIL_NOT_FOUND.getMessage());
}

@Test
void λ‘œκ·ΈμΈν•œ_μ‚¬μš©μžμ™€_λ§ˆμ‹€_μ†Œμœ μžκ°€_λ‹€λ₯΄λ©΄_λ§ˆμ‹€_μ‚­μ œμ—_μ‹€νŒ¨ν•œλ‹€() {
// given
User differntUser = userRepository.save(UserTexture.createValidUser());
Masil masil = masilRepository.save(MasilTexture.createDependentMasil(user, 10000));
List<MasilPin> masilPins = MasilTexture.createDependentMasilPins(masil, user.getId(), 10);

masilPinRepository.saveAll(masilPins);
entityManager.clear();

masilPinRepository.saveAll(masilPins);
entityManager.clear();

// when
ThrowingCallable deleteById = () -> masilService.deleteById(differntUser.getId(), masil.getId());

// then
assertThatExceptionOfType(ForbiddenException.class)
.isThrownBy(deleteById)
.withMessage(MasilErrorCode.USER_NOT_AUTHORIZED_FOR_MASIL.getMessage());
}

@Test
void λ§ˆμ‹€_μ‚­μ œμ—_μ„±κ³΅ν•œλ‹€() {
// given
Masil masil = masilRepository.save(MasilTexture.createDependentMasil(user, 10000));
List<MasilPin> masilPins = MasilTexture.createDependentMasilPins(masil, user.getId(), 10);

masilPinRepository.saveAll(masilPins);
entityManager.clear();

// when
masilService.deleteById(user.getId(), masil.getId());

// then
Optional<Masil> actual = masilRepository.findById(masil.getId());

assertThat(actual).isEmpty();
}

@ParameterizedTest
@ValueSource(ints = {5, 10, 15})
void 졜근_λ§ˆμ‹€_쑰회λ₯Ό_μ„±κ³΅ν•œλ‹€(int masilSize) {
Expand Down

0 comments on commit 6cb0d8e

Please sign in to comment.