Skip to content

Commit

Permalink
Merge pull request #68 from 4bujak-4bujak/feature/branch_spaceinfo
Browse files Browse the repository at this point in the history
Feature/branch spaceinfo
  • Loading branch information
KimSangwoo1259 authored Jun 3, 2024
2 parents eb1938a + ef20992 commit 675571d
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package com.example.sabujak.branch.controller;

import com.example.sabujak.branch.dto.response.BranchResponseDto;
import com.example.sabujak.branch.dto.response.BranchWithSpaceDto;
import com.example.sabujak.branch.service.BranchService;
import com.example.sabujak.common.response.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
Expand All @@ -13,6 +16,7 @@
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@Tag(name = "branches", description = "지점 API")
Expand Down Expand Up @@ -41,5 +45,19 @@ public ResponseEntity<Response<List<BranchResponseDto>>> getAllBranches() {
return ResponseEntity.ok(Response.success(branchService.findAll()));
}

@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "조회 성공", content = @Content(schema = @Schema(implementation = Response.class))),
@ApiResponse(responseCode = "404", description = "조회 실패", content = @Content(schema = @Schema(implementation = Response.class)))})
@Operation(summary = "지점 단건 조회", description = "지점의 이름과 현재 시간을 통해 지점 정보와 사용 가능한 회의실 개수 반환")
@Parameters({
@Parameter(name = "name", description = "지점명", example = "구로점"),
@Parameter(name = "now", description = "현재 시간", example = "2024-06-03T09:00:00")
})
@GetMapping("/{name}/space")
public ResponseEntity<Response<BranchWithSpaceDto>> getBranchWithSpace(@PathVariable String name,
@RequestParam LocalDateTime now) {
return ResponseEntity.ok(Response.success(branchService.getBranchWithSpace(now, name)));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
@Getter
@AllArgsConstructor
public class BranchResponseDto {
private Long branchId;
private String branchName;
private String branchAddress;

Expand All @@ -21,6 +22,7 @@ public class BranchResponseDto {

public static BranchResponseDto fromEntity(Branch branch) {
return new BranchResponseDto(
branch.getBranchId(),
branch.getBranchName(),
branch.getBranchAddress(),
branch.getBranchLatitude(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.example.sabujak.branch.dto.response;

import lombok.AllArgsConstructor;
import lombok.Getter;

@Getter
@AllArgsConstructor
public class BranchWithSpaceDto {
private Long id;
private String branchName;
private String branchAddress;

private int branchTotalMeetingRoomCount; // 전체 회의실
private int branchActiveMeetingRoomCount; // 사용중인 회의실


}
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package com.example.sabujak.branch.service;
import com.example.sabujak.branch.dto.response.BranchResponseDto;
import com.example.sabujak.branch.dto.response.BranchWithSpaceDto;
import com.example.sabujak.branch.entity.Branch;
import com.example.sabujak.branch.exception.BranchException;
import com.example.sabujak.branch.repository.BranchRepository;
import com.example.sabujak.space.repository.meetingroom.MeetingRoomRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import static com.example.sabujak.branch.exception.BranchErrorCode.ENTITY_NOT_FOUND_BY_NAME;
Expand All @@ -18,7 +23,7 @@
public class BranchService {

private final BranchRepository branchRepository;

private final MeetingRoomRepository meetingRoomRepository;

public BranchResponseDto findByBranchName(String branchName) {
log.info("[BranchService findByBranchName] branchName: {}", branchName);
Expand All @@ -32,5 +37,16 @@ public List<BranchResponseDto> findAll() {
.stream().map(BranchResponseDto::fromEntity).toList();
}

public BranchWithSpaceDto getBranchWithSpace(LocalDateTime now, String branchName) {
log.info("[BranchService getBranchWithSpace] branchName: {}, time: {}", branchName, now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
Branch branch = branchRepository.findByBranchName(branchName).orElseThrow(() -> new BranchException(ENTITY_NOT_FOUND_BY_NAME));

int branchTotalMeetingRoomCount = meetingRoomRepository.countTotalMeetingRoom(branchName);
int branchActiveMeetingRoomCount = meetingRoomRepository.countActiveMeetingRoom(now, branchName);

return new BranchWithSpaceDto(branch.getBranchId(), branch.getBranchName(),branch.getBranchAddress(),branchTotalMeetingRoomCount,branchActiveMeetingRoomCount);
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@

public interface MeetingRoomRepositoryCustom {
List<MeetingRoom> findMeetingRoomList(LocalDateTime startAt, LocalDateTime endAt, String branchName, List<MeetingRoomType> meetingRoomTypes, boolean projectorExists, boolean canVideoConference, boolean isPrivate, String sortTarget, String sortDirection);

Integer countTotalMeetingRoom(String branchName);
Integer countActiveMeetingRoom(LocalDateTime now, String branchName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ public List<MeetingRoom> findMeetingRoomList(LocalDateTime startAt, LocalDateTim
.fetch();
}

@Override
public Integer countTotalMeetingRoom(String branchName) {
return Math.toIntExact(queryFactory.select(meetingRoom.count())
.from(meetingRoom)
.where(meetingRoom.branch.branchName.eq(branchName))
.fetchFirst());

}

@Override
public Integer countActiveMeetingRoom(LocalDateTime now, String branchName) {
return Math.toIntExact(queryFactory.select(meetingRoom.count())
.from(meetingRoom)
.where(meetingRoom.branch.branchName.eq(branchName),
reservationConditionV2(now))
.fetchFirst());
}

private BooleanExpression reservationCondition(LocalDateTime startAt, LocalDateTime endAt) {
return JPAExpressions.selectOne()
Expand All @@ -49,6 +66,15 @@ private BooleanExpression reservationCondition(LocalDateTime startAt, LocalDateT
.notExists();
}

private BooleanExpression reservationConditionV2(LocalDateTime now) { // 현재 기준 사용중인 회의실 개수 반환
return JPAExpressions.selectOne()
.from(reservation)
.where(reservation.space.spaceId.eq(meetingRoom.spaceId),
reservation.reservationStartDateTime.after(now),
reservation.reservationEndDateTime.before(now))
.exists();
}


private BooleanExpression projectorCondition(boolean projectorExists) {
if (projectorExists) {
Expand Down

0 comments on commit 675571d

Please sign in to comment.