Skip to content

Commit

Permalink
Merge pull request #144 from depromeet/refactor/143-memory
Browse files Browse the repository at this point in the history
refactor: 수영 기둝 API ν”Œλ‘œμš° 및 μŠ€νŽ™ μˆ˜μ •
  • Loading branch information
its-sky authored Aug 2, 2024
2 parents 5d02a70 + fad3a6d commit 20f3956
Show file tree
Hide file tree
Showing 10 changed files with 67 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

public interface GetMemoryUseCase {
Memory findById(Long memoryId);

int findOrderInMonth(Long memberId, Long memoryId, int month);
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public interface MemoryPersistencePort {

Optional<Memory> update(Long memoryId, Memory memoryUpdate);

int findOrderInMonth(Long memberId, Long memoryId, int month);

Timeline findPrevMemoryByMemberId(Long memberId, LocalDate cursorRecordAt, LocalDate recordAt);

Timeline findNextMemoryByMemberId(Long memberId, LocalDate cursorRecordAt, LocalDate recordAt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public Memory findById(Long memoryId) {
.orElseThrow(() -> new NotFoundException(MemoryErrorType.NOT_FOUND));
}

@Override
public int findOrderInMonth(Long memberId, Long memoryId, int month) {
return memoryPersistencePort.findOrderInMonth(memberId, memoryId, month);
}

@Override
@Transactional
public Memory update(Long memoryId, UpdateMemoryCommand command, List<Stroke> strokes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public Optional<Memory> update(Long memoryId, Memory memoryUpdate) {
}
}

@Override
public int findOrderInMonth(Long memberId, Long memoryId, int month) {
return 0;
}

@Override
public Timeline findPrevMemoryByMemberId(
Long memberId, LocalDate cursorRecordAt, LocalDate recordAt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,18 @@ public Optional<Memory> update(Long memoryId, Memory memoryUpdate) {
.map(entity -> entity.update(MemoryEntity.from(memoryUpdate)).toModel());
}

@Override
public int findOrderInMonth(Long memberId, Long memoryId, int month) {
return queryFactory
.select(memory.id)
.from(memory)
.where(memberEq(memberId), memory.recordAt.month().eq(month))
.orderBy(memory.id.asc())
.fetch()
.indexOf(memoryId)
+ 1;
}

@Override
public Timeline findPrevMemoryByMemberId(
Long memberId, LocalDate cursorRecordAt, LocalDate recordAt) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.depromeet.memory.dto.request.MemoryCreateRequest;
import com.depromeet.memory.dto.request.MemoryUpdateRequest;
import com.depromeet.memory.dto.response.CalendarResponse;
import com.depromeet.memory.dto.response.MemoryCreateResponse;
import com.depromeet.memory.dto.response.MemoryResponse;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand All @@ -19,7 +20,7 @@
@Tag(name = "수영 기둝(Memory)")
public interface MemoryApi {
@Operation(summary = "수영 기둝 μ €μž₯")
ApiResponse<?> create(
ApiResponse<MemoryCreateResponse> create(
@LoginMember Long memberId,
@Valid @RequestBody MemoryCreateRequest memoryCreateRequest);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.depromeet.memory.dto.request.MemoryCreateRequest;
import com.depromeet.memory.dto.request.MemoryUpdateRequest;
import com.depromeet.memory.dto.response.CalendarResponse;
import com.depromeet.memory.dto.response.MemoryCreateResponse;
import com.depromeet.memory.dto.response.MemoryResponse;
import com.depromeet.memory.dto.response.TimelineSliceResponse;
import com.depromeet.memory.facade.MemoryFacade;
Expand All @@ -22,11 +23,11 @@ public class MemoryController implements MemoryApi {
private final MemoryFacade memoryFacade;

@PostMapping
public ApiResponse<?> create(
public ApiResponse<MemoryCreateResponse> create(
@LoginMember Long memberId,
@Valid @RequestBody MemoryCreateRequest memoryCreateRequest) {
memoryFacade.create(memberId, memoryCreateRequest);
return ApiResponse.success(MemorySuccessType.POST_RESULT_SUCCESS);
MemoryCreateResponse response = memoryFacade.create(memberId, memoryCreateRequest);
return ApiResponse.success(MemorySuccessType.POST_RESULT_SUCCESS, response);
}

@GetMapping("/{memoryId}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.depromeet.memory.dto.response;

public record MemoryCreateResponse(int rank, Long memoryId) {
public static MemoryCreateResponse of(int rank, Long memoryId) {
return new MemoryCreateResponse(rank, memoryId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,19 @@
import com.depromeet.memory.dto.request.MemoryCreateRequest;
import com.depromeet.memory.dto.request.MemoryUpdateRequest;
import com.depromeet.memory.dto.response.CalendarResponse;
import com.depromeet.memory.dto.response.MemoryCreateResponse;
import com.depromeet.memory.dto.response.MemoryResponse;
import com.depromeet.memory.dto.response.TimelineSliceResponse;
import com.depromeet.memory.mapper.MemoryMapper;
import com.depromeet.memory.port.in.command.CreateStrokeCommand;
import com.depromeet.memory.port.in.command.UpdateMemoryCommand;
import com.depromeet.memory.port.in.command.UpdateStrokeCommand;
import com.depromeet.memory.port.in.usecase.CalendarUseCase;
import com.depromeet.memory.port.in.usecase.CreateMemoryUseCase;
import com.depromeet.memory.port.in.usecase.GetMemoryUseCase;
import com.depromeet.memory.port.in.usecase.StrokeUseCase;
import com.depromeet.memory.port.in.usecase.TimelineUseCase;
import com.depromeet.memory.service.CalendarService;
import com.depromeet.memory.service.MemoryService;
import com.depromeet.memory.service.StrokeService;
import com.depromeet.memory.port.in.usecase.UpdateMemoryUseCase;
import com.depromeet.pool.port.in.usecase.SearchLogUseCase;
import java.time.LocalDate;
import java.time.YearMonth;
Expand All @@ -34,45 +37,52 @@
@Transactional(readOnly = true)
public class MemoryFacade {
private final MemberUseCase memberUseCase;
private final MemoryService memoryService;
private final StrokeService strokeService;
private final StrokeUseCase strokeUseCase;
private final CalendarUseCase calendarUseCase;
private final TimelineUseCase timelineUseCase;
private final CalendarService calendarService;
private final GetMemoryUseCase getMemoryUseCase;
private final ImageUploadUseCase imageUploadUseCase;
private final SearchLogUseCase poolSearchLogUseCase;
private final CreateMemoryUseCase createMemoryUseCase;
private final UpdateMemoryUseCase updateMemoryUseCase;

@Transactional
public void create(Long memberId, MemoryCreateRequest request) {
public MemoryCreateResponse create(Long memberId, MemoryCreateRequest request) {
Member writer = memberUseCase.findById(memberId);
Memory newMemory = memoryService.save(writer, MemoryMapper.toCommand(request));
Memory newMemory = createMemoryUseCase.save(writer, MemoryMapper.toCommand(request));
Long memoryId = newMemory.getId();
int month = request.getRecordAt().getMonth().getValue();
int rank = getMemoryUseCase.findOrderInMonth(memberId, memoryId, month);

List<CreateStrokeCommand> commands =
request.getStrokes().stream().map(MemoryMapper::toCommand).toList();
List<Stroke> strokes = strokeService.saveAll(newMemory, commands);
List<Stroke> strokes = strokeUseCase.saveAll(newMemory, commands);

imageUploadUseCase.changeImageStatusAndAddMemoryIdToImages(
newMemory, request.getImageIdList());

if (request.getPoolId() != null) {
poolSearchLogUseCase.createSearchLog(writer, request.getPoolId());
}

return MemoryCreateResponse.of(rank, memoryId);
}

@Transactional
public MemoryResponse update(Long memberId, Long memoryId, MemoryUpdateRequest request) {
Memory memory = memoryService.findById(memoryId);
Memory memory = getMemoryUseCase.findById(memoryId);
validatePermission(memory.getMember().getId(), memberId);

List<UpdateStrokeCommand> commands =
request.getStrokes().stream().map(MemoryMapper::toCommand).toList();
List<Stroke> strokes = strokeService.updateAll(memory, commands);
List<Stroke> strokes = strokeUseCase.updateAll(memory, commands);
UpdateMemoryCommand command = MemoryMapper.toCommand(request);

return MemoryResponse.from(memoryService.update(memoryId, command, strokes));
return MemoryResponse.from(updateMemoryUseCase.update(memoryId, command, strokes));
}

public MemoryResponse findById(Long memberId, Long memoryId) {
Memory memory = memoryService.findById(memoryId);
Memory memory = getMemoryUseCase.findById(memoryId);
validatePermission(memory.getMember().getId(), memberId);
return MemoryResponse.from(memory);
}
Expand All @@ -89,7 +99,7 @@ public TimelineSliceResponse getTimelineByMemberIdAndCursorAndDate(
public CalendarResponse getCalendar(Long memberId, Integer year, Short month) {
YearMonth yearMonth = YearMonth.of(year, month);
List<Memory> calendarMemories =
calendarService.getCalendarByYearAndMonth(memberId, yearMonth);
calendarUseCase.getCalendarByYearAndMonth(memberId, yearMonth);
return CalendarResponse.of(calendarMemories);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ ApiResponse<PoolSearchResponse> searchPoolsByNameQuery(
@LoginMember Long memberId,
@Schema(description = "수영μž₯ 검색 μž…λ ₯κ°’", example = "강남") @RequestParam(value = "nameQuery")
String nameQuery,
@Schema(description = "λ§ˆμ§€λ§‰ 수영μž₯ 아이디", example = "77")
@Schema(
description =
"λ§ˆμ§€λ§‰ 수영μž₯ 아이디, 이전 검색 μ‹œ cursorId ν•„λ“œλ₯Ό κ·ΈλŒ€λ‘œ λ„£μœΌλ©΄ 됨. 제일 μ²˜μŒμ—λŠ” cursorId ν•„λ“œ 없이 쑰회",
example = "77")
@RequestParam(value = "cursorId", required = false)
Long cursorId);

Expand Down

0 comments on commit 20f3956

Please sign in to comment.