Skip to content

Commit

Permalink
Merge pull request #29 from SWM-Flash/integration
Browse files Browse the repository at this point in the history
섹터 어드민 API 구현
  • Loading branch information
ChoiWonYu authored Jul 19, 2024
2 parents b5716d3 + de25198 commit a0d187d
Show file tree
Hide file tree
Showing 15 changed files with 121 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.first.flash.climbing.problem.application;

import com.first.flash.climbing.sector.domain.SectorExpiredEvent;
import com.first.flash.climbing.sector.domain.SectorInfoUpdatedEvent;
import com.first.flash.climbing.sector.domain.SectorRemovalDateUpdatedEvent;
import com.first.flash.climbing.solution.domain.SolutionSavedEvent;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -31,4 +32,11 @@ public void expireProblem(final SectorExpiredEvent event) {
public void updateProblemSolutionInfo(final SolutionSavedEvent event) {
problemsService.updateProblemSolutionInfo(event.getProblemId());
}

@EventListener
@Transactional
public void updateQueryProblemInfo(final SectorInfoUpdatedEvent event) {
problemsService.updateQueryProblemInfo(event.getId(), event.getSectorName(),
event.getSettingDate());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,10 @@ public void updateProblemSolutionInfo(final UUID problemId) {
QueryProblem queryProblem = problemReadService.findQueryProblemById(problemId);
queryProblem.addSolutionCount();
}

@Transactional
public void updateQueryProblemInfo(final Long sectorId, final String sectorName,
final LocalDate settingDate) {
queryProblemRepository.updateQueryProblemInfo(sectorId, sectorName, settingDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,7 @@ List<QueryProblem> findAll(final Cursor preCursor, final SortBy sortBy, final in
void updateRemovalDateBySectorId(final Long sectorId, final LocalDate removalDate);

void expireProblemsBySectorIds(final List<Long> expiredSectorsIds);

void updateQueryProblemInfo(final Long sectorId, final String sectorName,
final LocalDate settingDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public List<QueryProblem> findAll(final Cursor prevCursor, final SortBy sortBy,
public void updateRemovalDateBySectorId(final Long sectorId, final LocalDate removalDate) {
queryFactory.update(queryProblem)
.set(queryProblem.removalDate, removalDate)
.set(queryProblem.isFakeRemovalDate, false)
.where(queryProblem.sectorId.eq(sectorId))
.execute();
}
Expand All @@ -49,6 +50,15 @@ public void expireProblemsBySectorIds(final List<Long> expiredSectorsIds) {
.execute();
}

public void updateQueryProblemInfo(final Long sectorId, final String sectorName,
final LocalDate settingDate) {
queryFactory.update(queryProblem)
.set(queryProblem.sectorName, sectorName)
.set(queryProblem.settingDate, settingDate)
.where(queryProblem.sectorId.eq(sectorId))
.execute();
}

private BooleanExpression inGym(final Long gymId) {
return queryProblem.gymId.eq(gymId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,10 @@ public void updateRemovalDateBySectorId(final Long sectorId, final LocalDate rem
public void expireProblemsBySectorIds(final List<Long> expiredSectorsIds) {
queryProblemQueryDslRepository.expireProblemsBySectorIds(expiredSectorsIds);
}

@Override
public void updateQueryProblemInfo(final Long sectorId, final String sectorName,
final LocalDate settingDate) {
queryProblemQueryDslRepository.updateQueryProblemInfo(sectorId, sectorName, settingDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@

import com.first.flash.climbing.sector.application.dto.SectorCreateRequestDto;
import com.first.flash.climbing.sector.application.dto.SectorUpdateRequestDto;
import com.first.flash.climbing.sector.application.dto.SectorWriteDetailResponseDto;
import com.first.flash.climbing.sector.application.dto.SectorDetailResponseDto;
import com.first.flash.climbing.sector.application.dto.SectorUpdateRemovalDateRequestDto;
import com.first.flash.climbing.sector.application.dto.SectorsDetailResponseDto;
import com.first.flash.climbing.sector.domain.Sector;
import com.first.flash.climbing.sector.domain.SectorExpiredEvent;
import com.first.flash.climbing.sector.domain.SectorInfoUpdatedEvent;
import com.first.flash.climbing.sector.domain.SectorRemovalDateUpdatedEvent;
import com.first.flash.climbing.sector.domain.SectorRepository;
import com.first.flash.climbing.sector.exception.exceptions.SectorNotFoundException;
Expand All @@ -25,20 +27,20 @@ public class SectorService {
private final SectorRepository sectorRepository;

@Transactional
public SectorWriteDetailResponseDto saveSector(final Long gymId,
public SectorDetailResponseDto saveSector(final Long gymId,
final SectorCreateRequestDto createRequestDto) {
Sector sector = createSectorByDto(gymId, createRequestDto);
return SectorWriteDetailResponseDto.toDto(sectorRepository.save(sector));
return SectorDetailResponseDto.toDto(sectorRepository.save(sector));
}

@Transactional
public SectorWriteDetailResponseDto updateSectorRemovalDate(final Long sectorId,
public SectorDetailResponseDto updateSectorRemovalDate(final Long sectorId,
final SectorUpdateRemovalDateRequestDto sectorUpdateRemovalDateRequestDto) {
Sector sector = findById(sectorId);
LocalDate removalDate = sectorUpdateRemovalDateRequestDto.removalDate();
sector.updateRemovalDate(removalDate);
Events.raise(SectorRemovalDateUpdatedEvent.of(sectorId, removalDate));
return SectorWriteDetailResponseDto.toDto(sector);
return SectorDetailResponseDto.toDto(sector);
}

@Transactional
Expand All @@ -48,21 +50,33 @@ public void expireSector() {
}

@Transactional
public SectorWriteDetailResponseDto updateSector(
public SectorDetailResponseDto updateSector(
final Long sectorId,
final SectorUpdateRequestDto updateRequestDto) {
Sector foundSector = findById(sectorId);
foundSector.updateSector(updateRequestDto.sectorName(), updateRequestDto.adminSectorName(),
updateRequestDto.settingDate(),
updateRequestDto.removalDate(), updateRequestDto.gymId());
return SectorWriteDetailResponseDto.toDto(foundSector);
Events.raise(SectorInfoUpdatedEvent.of(foundSector.getId(), updateRequestDto.sectorName(),
updateRequestDto.settingDate()));
return SectorDetailResponseDto.toDto(foundSector);
}

public Sector findById(final Long id) {
return sectorRepository.findById(id)
.orElseThrow(() -> new SectorNotFoundException(id));
}

public SectorsDetailResponseDto findAllSectors() {
List<SectorDetailResponseDto> sectorsResponse = sectorRepository
.findAll()
.stream()
.map(
SectorDetailResponseDto::toDto)
.toList();
return new SectorsDetailResponseDto(sectorsResponse);
}

private Sector createSectorByDto(final Long gymId,
final SectorCreateRequestDto createRequestDto) {
if (hasNoRemovalDate(createRequestDto)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
import com.first.flash.climbing.sector.domain.vo.SectorName;
import java.time.LocalDate;

public record SectorWriteDetailResponseDto(Long id, SectorName sectorName, LocalDate settingDate,
RemovalInfo removalInfo, Long gymId) {
public record SectorDetailResponseDto(Long id, SectorName sectorName, LocalDate settingDate,
RemovalInfo removalInfo, Long gymId) {

public static SectorWriteDetailResponseDto toDto(final Sector sector) {
return new SectorWriteDetailResponseDto(sector.getId(), sector.getSectorName(),
public static SectorDetailResponseDto toDto(final Sector sector) {
return new SectorDetailResponseDto(sector.getId(), sector.getSectorName(),
sector.getSettingDate(), sector.getRemovalInfo(), sector.getGymId());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.first.flash.climbing.sector.application.dto;

import java.util.List;

public record SectorsDetailResponseDto(List<SectorDetailResponseDto> sectorsDetailResponseDtos) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.first.flash.climbing.sector.domain;

import java.time.LocalDate;
import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class SectorInfoUpdatedEvent {

private Long id;
private String sectorName;
private LocalDate settingDate;

public static SectorInfoUpdatedEvent of(final Long id, final String sectorName,
final LocalDate settingDate) {
return new SectorInfoUpdatedEvent(id, sectorName, settingDate);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface SectorRepository {
Optional<Sector> findById(final Long id);

List<Long> updateExpiredSector();

List<Sector> findAll();
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.first.flash.climbing.sector.infrastructure;

import com.first.flash.climbing.sector.domain.Sector;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

Expand All @@ -9,4 +10,6 @@ public interface SectorJpaRepository extends JpaRepository<Sector, Long> {
Sector save(final Sector sector);

Optional<Sector> findById(final Long id);

List<Sector> findAll();
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public Optional<Sector> findById(final Long id) {
public List<Long> updateExpiredSector() {
return sectorQueryDslRepository.updateExpiredSector();
}

@Override
public List<Sector> findAll() {
return sectorJpaRepository.findAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@
import com.first.flash.climbing.sector.application.dto.SectorCreateRequestDto;
import com.first.flash.climbing.sector.application.dto.SectorUpdateRemovalDateRequestDto;
import com.first.flash.climbing.sector.application.dto.SectorUpdateRequestDto;
import com.first.flash.climbing.sector.application.dto.SectorWriteDetailResponseDto;
import com.first.flash.climbing.sector.application.dto.SectorDetailResponseDto;
import com.first.flash.climbing.sector.application.dto.SectorsDetailResponseDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -28,13 +30,22 @@ public class SectorController {

private final SectorService sectorService;

@Operation(summary = "섹터 전체 조회", description = "모든 섹터의 정보를 반환")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "성공적으로 섹터를 조회"),
})
@GetMapping("sectors")
public ResponseEntity<SectorsDetailResponseDto> findAllSectors() {
return ResponseEntity.ok(sectorService.findAllSectors());
}

@Operation(summary = "섹터 갱신(생성)", description = "특정 클라이밍장에 새로운 섹터 생성")
@ApiResponses(value = {
@ApiResponse(responseCode = "201", description = "성공적으로 섹터를 생성함"),
@ApiResponse(responseCode = "400", description = "탈거일이 세팅일보다 빠름")
})
@PostMapping("gyms/{gymId}/sectors")
public ResponseEntity<SectorWriteDetailResponseDto> createSector(@PathVariable final Long gymId,
public ResponseEntity<SectorDetailResponseDto> createSector(@PathVariable final Long gymId,
@RequestBody final SectorCreateRequestDto sectorCreateRequestDto) {
return ResponseEntity.status(HttpStatus.CREATED)
.body(sectorService.saveSector(gymId, sectorCreateRequestDto));
Expand All @@ -47,20 +58,20 @@ public ResponseEntity<SectorWriteDetailResponseDto> createSector(@PathVariable f
@ApiResponse(responseCode = "404", description = "섹터를 찾을 수 없음")
})
@PatchMapping("sectors/{sectorId}")
public ResponseEntity<SectorWriteDetailResponseDto> updateSectorRemovalDate(
public ResponseEntity<SectorDetailResponseDto> updateSectorRemovalDate(
@PathVariable final Long sectorId,
@RequestBody final SectorUpdateRemovalDateRequestDto sectorUpdateRemovalDateRequestDto) {
SectorWriteDetailResponseDto response = sectorService.updateSectorRemovalDate(
SectorDetailResponseDto response = sectorService.updateSectorRemovalDate(
sectorId, sectorUpdateRemovalDateRequestDto);
return ResponseEntity.ok(response);
}

@Operation(summary = "섹터 전체 수정", description = "특정 섹터의 정보 수정")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "성공적으로 섹터 탈거일을 수정함"),
@ApiResponse(responseCode = "200", description = "성공적으로 섹터 정보 수정함"),
})
@PutMapping("sectors/{sectorId}")
public ResponseEntity<SectorWriteDetailResponseDto> updateSector(
public ResponseEntity<SectorDetailResponseDto> updateSector(
@PathVariable final Long sectorId,
@RequestBody final SectorUpdateRequestDto updateRequestDto) {
return ResponseEntity.ok(sectorService.updateSector(sectorId, updateRequestDto));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

import com.first.flash.climbing.sector.application.dto.SectorCreateRequestDto;
import com.first.flash.climbing.sector.application.dto.SectorWriteDetailResponseDto;
import com.first.flash.climbing.sector.application.dto.SectorDetailResponseDto;
import com.first.flash.climbing.sector.application.dto.SectorUpdateRemovalDateRequestDto;
import com.first.flash.climbing.sector.domain.Sector;
import com.first.flash.climbing.sector.domain.SectorRepository;
Expand Down Expand Up @@ -35,7 +35,7 @@ void init() {
SectorCreateRequestDto requestDto = createDefaultRequestDto(LocalDate.now());

// when
SectorWriteDetailResponseDto response = sectorService.saveSector(DEFAULT_GYM_ID, requestDto);
SectorDetailResponseDto response = sectorService.saveSector(DEFAULT_GYM_ID, requestDto);
Sector foundSector = sectorService.findById(response.id());

// then
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ public Optional<Sector> findById(final Long id) {
@Override
public List<Long> updateExpiredSector() {
List<Long> expiredIds = db.keySet().stream()
.filter(key -> db.get(key).getRemovalInfo().getRemovalDate()
.isBefore(db.get(key).getSettingDate()))
.toList();
.filter(key -> db.get(key).getRemovalInfo().getRemovalDate()
.isBefore(db.get(key).getSettingDate()))
.toList();
expiredIds.forEach(db::remove);
return expiredIds;
}

@Override
public List<Sector> findAll() {
return List.copyOf(db.values());
}
}

0 comments on commit a0d187d

Please sign in to comment.