Skip to content

Commit

Permalink
[Feat] Our Shoot 조회 API 구현 (#67)
Browse files Browse the repository at this point in the history
* feat : ourShoot 조회 API 구현

* feat : default tab yet으로 지정

* feat : yet, doing, done 상수 처리

* feat : querydsl으로 전환

* feat : querydsl으로 전환

* feat : 에러 수정

* fix : dto of->from으로 수정

* fix : if-else문 object mapping으로 수정
  • Loading branch information
cowboysj authored Nov 22, 2024
1 parent 1abfb04 commit 93b4a75
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/main/java/gigedi/dev/domain/shoot/api/ShootController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@
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;

import gigedi.dev.domain.shoot.application.ShootService;
import gigedi.dev.domain.shoot.application.ShootStatusService;
import gigedi.dev.domain.shoot.dto.request.CreateShootRequest;
import gigedi.dev.domain.shoot.dto.request.UpdateShootStatusRequest;
import gigedi.dev.domain.shoot.dto.response.GetOurShootResponse;
import gigedi.dev.domain.shoot.dto.response.GetShootResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand Down Expand Up @@ -53,4 +55,12 @@ public GetShootResponse updateShootStatus(
@PathVariable Long shootId, @RequestBody UpdateShootStatusRequest request) {
return shootStatusService.updateShootStatus(shootId, request.status());
}

@Operation(
summary = "Our Shoot 조회 API",
description = "쿼리 파라미터로 yet, doing, done, mentioned을 구분해 Shoot을 조회하는 API")
@GetMapping("/status")
public List<GetOurShootResponse> getOurShoot(@RequestParam String tab) {
return shootService.getOurShoot(tab);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gigedi.dev.domain.shoot.application;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;
Expand All @@ -13,6 +15,7 @@
import gigedi.dev.domain.shoot.dao.ShootStatusRepository;
import gigedi.dev.domain.shoot.domain.Shoot;
import gigedi.dev.domain.shoot.domain.Status;
import gigedi.dev.domain.shoot.dto.response.GetOurShootResponse;
import gigedi.dev.domain.shoot.dto.response.GetShootResponse;
import gigedi.dev.global.error.exception.CustomException;
import gigedi.dev.global.error.exception.ErrorCode;
Expand All @@ -31,6 +34,11 @@ public class ShootService {
private final BlockService blockService;
private final ShootTagService shootTagService;

private static final String YET = "yet";
private static final String DOING = "doing";
private static final String DONE = "done";
private static final String MENTIONED = "mentioned";

@Transactional(readOnly = true)
public List<GetShootResponse> getShoot(Long blockId) {
List<Shoot> shoots = shootRepository.findAllByBlock_BlockIdAndDeletedAtIsNull(blockId);
Expand Down Expand Up @@ -68,6 +76,41 @@ private void processTags(String content, Shoot shoot) {
shootTagService.createShootTags(shoot, tags);
}

@Transactional(readOnly = true)
public List<GetOurShootResponse> getOurShoot(String tab) {
final Figma figma = figmaUtil.getCurrentFigma();
if (tab == null || tab.isBlank()) {
tab = YET;
}

Map<String, Function<Figma, List<GetOurShootResponse>>> tabMapping =
Map.of(
YET, f -> getShootByStatus(f, Status.YET),
DOING, f -> getShootByStatus(f, Status.DOING),
DONE, f -> getShootByStatus(f, Status.DONE),
MENTIONED, this::getMentionedShoot);

return tabMapping
.getOrDefault(
tab,
f -> {
throw new CustomException(ErrorCode.INVALID_TAB);
})
.apply(figma);
}

private List<GetOurShootResponse> getShootByStatus(Figma figma, Status status) {
return shootRepository.findByFigmaAndStatusAndDeletedAtIsNull(figma, status).stream()
.map(shoot -> GetOurShootResponse.from(shoot))
.toList();
}

private List<GetOurShootResponse> getMentionedShoot(Figma figma) {
return shootRepository.findMentionedShootsByFigma(figma).stream()
.map(shoot -> GetOurShootResponse.from(shoot))
.toList();
}

public List<GetShootResponse.User> getUsersByStatus(Shoot shoot, Status status) {
return shootStatusRepository
.findByShoot_ShootIdAndStatus(shoot.getShootId(), status)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import gigedi.dev.domain.shoot.domain.Shoot;

@Repository
public interface ShootRepository extends JpaRepository<Shoot, Long> {
public interface ShootRepository extends JpaRepository<Shoot, Long>, ShootRepositoryCustom {
List<Shoot> findAllByBlock_BlockIdAndDeletedAtIsNull(Long blockId);

Optional<Shoot> findByShootIdAndDeletedAtIsNull(Long shootId);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gigedi.dev.domain.shoot.dao;

import java.util.List;

import gigedi.dev.domain.auth.domain.Figma;
import gigedi.dev.domain.shoot.domain.Shoot;
import gigedi.dev.domain.shoot.domain.Status;

public interface ShootRepositoryCustom {
List<Shoot> findByFigmaAndStatusAndDeletedAtIsNull(Figma figma, Status status);

List<Shoot> findMentionedShootsByFigma(Figma figma);
}
42 changes: 42 additions & 0 deletions src/main/java/gigedi/dev/domain/shoot/dao/ShootRepositoryImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package gigedi.dev.domain.shoot.dao;

import static gigedi.dev.domain.shoot.domain.QShoot.shoot;
import static gigedi.dev.domain.shoot.domain.QShootStatus.shootStatus;
import static gigedi.dev.domain.shoot.domain.QShootTag.shootTag;

import java.util.List;

import com.querydsl.jpa.impl.JPAQueryFactory;

import gigedi.dev.domain.auth.domain.Figma;
import gigedi.dev.domain.shoot.domain.Shoot;
import gigedi.dev.domain.shoot.domain.Status;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
public class ShootRepositoryImpl implements ShootRepositoryCustom {

private final JPAQueryFactory queryFactory;

@Override
public List<Shoot> findByFigmaAndStatusAndDeletedAtIsNull(Figma figma, Status status) {
return queryFactory
.selectFrom(shoot)
.join(shootStatus)
.on(shoot.shootId.eq(shootStatus.shoot.shootId))
.where(
shoot.figma.eq(figma),
shootStatus.status.eq(status),
shoot.deletedAt.isNull())
.fetch();
}

@Override
public List<Shoot> findMentionedShootsByFigma(Figma figma) {
return queryFactory
.select(shootTag.shoot)
.from(shootTag)
.where(shootTag.figma.eq(figma), shootTag.shoot.deletedAt.isNull())
.fetch();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package gigedi.dev.domain.shoot.dto.response;

import gigedi.dev.domain.shoot.domain.Shoot;
import gigedi.dev.global.util.TimeUtil;

public record GetOurShootResponse(
Long shootId, String BlockTitle, String authorName, String content, String timeAgo) {
public static GetOurShootResponse from(Shoot shoot) {
return new GetOurShootResponse(
shoot.getShootId(),
shoot.getBlock().getTitle(),
shoot.getFigma().getFigmaName(),
shoot.getContent(),
TimeUtil.calculateTimeAgo(shoot.getCreatedAt()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public enum ErrorCode {
SHOOT_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 슛을 찾을 수 없습니다."),
SHOOT_STATUS_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 슛 상태를 찾을 수 없습니다."),
INVALID_STATUS(HttpStatus.BAD_REQUEST, "유효하지 않은 상태입니다."),
INVALID_TAB(HttpStatus.BAD_REQUEST, "유효하지 않은 탭입니다. yet, doing, done, mentioned 중 하나여야 합니다."),

// Figma
FIGMA_NOT_FOUND(HttpStatus.NOT_FOUND, "해당 Figma 정보를 찾을 수 없습니다."),
Expand Down

0 comments on commit 93b4a75

Please sign in to comment.