Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[농장] 농장 생성시 시작날짜/종료날짜/시작시간/종료날짜 #211

Merged
merged 3 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
package poomasi.domain.farm._schedule.dto;

import java.time.LocalDate;

import lombok.Builder;
import poomasi.domain.farm._schedule.entity.FarmSchedule;
import poomasi.domain.farm._schedule.entity.ScheduleStatus;

import java.time.LocalDate;
import java.time.LocalTime;

@Builder
public record FarmScheduleResponse(
Long scheduleId,
LocalDate date,
LocalTime startTime,
LocalTime endTime,
ScheduleStatus status
LocalTime endTime
) {

public static FarmScheduleResponse fromEntity(FarmSchedule farmSchedule) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import jakarta.validation.constraints.NotNull;
import poomasi.domain.farm._schedule.entity.FarmSchedule;
import poomasi.domain.farm._schedule.entity.ScheduleStatus;

import java.time.LocalDate;
import java.time.LocalTime;
Expand All @@ -22,7 +21,6 @@ public FarmSchedule toEntity() {
.startTime(startTime)
.endTime(endTime)
.date(date)
.status(ScheduleStatus.PENDING)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class FarmSchedule {
private LocalTime endTime;

@Builder
public FarmSchedule(Long farmId, LocalDate date, LocalTime startTime, LocalTime endTime, ScheduleStatus status) {
public FarmSchedule(Long farmId, LocalDate date, LocalTime startTime, LocalTime endTime) {
this.farmId = farmId;
this.date = date;
this.startTime = startTime;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public interface FarmScheduleRepository extends JpaRepository<FarmSchedule, Long

List<FarmSchedule> findByFarmIdAndDate(Long aLong, LocalDate date);

List<FarmSchedule> findByFarmId(Long farmId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import poomasi.global.error.BusinessException;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.Collection;
import java.util.List;

import static poomasi.global.error.BusinessError.*;
Expand All @@ -19,6 +21,28 @@
public class FarmScheduleService {
private final FarmScheduleRepository farmScheduleRepository;

public void addFarmSchedule(LocalDate startDate, LocalDate endDate, LocalTime startTime, LocalTime endTime, Long farmId) {
List<FarmSchedule> farmSchedules = farmScheduleRepository.findByFarmIdAndDateRange(farmId, startDate, endDate);

// 같은 날짜에 동일한 시간에 스케줄이 있는지 확인
for (FarmSchedule farmSchedule : farmSchedules) {
if (startTime.isBefore(farmSchedule.getEndTime()) && endTime.isAfter(farmSchedule.getStartTime())) {
throw new BusinessException(FARM_SCHEDULE_ALREADY_EXISTS);
}
}
// 등록
for (LocalDate date = startDate; date.isBefore(endDate.plusDays(1)); date = date.plusDays(1)) {
FarmSchedule farmSchedule = FarmSchedule.builder()
.farmId(farmId)
.date(date)
.startTime(startTime)
.endTime(endTime)
.build();

farmScheduleRepository.save(farmSchedule);
}
}

public void addFarmSchedule(FarmScheduleUpdateRequest request) {
if (request.startTime().isAfter(request.endTime())) {
throw new BusinessException(START_TIME_SHOULD_BE_BEFORE_END_TIME);
Expand Down Expand Up @@ -55,4 +79,8 @@ public FarmSchedule getFarmScheduleByScheduleId(Long id) {
() -> new BusinessException(FARM_SCHEDULE_NOT_FOUND)
);
}

public List<FarmSchedule> getFarmScheduleByFarmId(Long farmId) {
return farmScheduleRepository.findByFarmId(farmId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import lombok.Builder;
import poomasi.domain.farm.entity.Farm;

import java.time.LocalDate;
import java.time.LocalTime;

@Builder
public record FarmRegisterRequest(
@NotNull
Expand Down Expand Up @@ -34,6 +37,18 @@ public record FarmRegisterRequest(
@NotNull
int price,

@NotNull
LocalDate startDate,

@NotNull
LocalDate endDate,

@NotNull
LocalTime startTime,

@NotNull
LocalTime endTime,

@NotNull
String businessNumber,

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package poomasi.domain.farm.dto.response;

import lombok.Builder;
import poomasi.domain.farm._schedule.dto.FarmScheduleResponse;

import java.util.List;

@Builder
public record FarmDetailResponse(
FarmResponse farmResponse,
FarmInfoAggregateResponse info
FarmInfoAggregateResponse info,
List<FarmScheduleResponse> schedules
) {


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import poomasi.domain.farm._schedule.service.FarmScheduleService;
import poomasi.domain.farm.dto.request.FarmInfoRegisterRequest;
import poomasi.domain.farm.dto.request.FarmInfoUpdateRequest;
import poomasi.domain.farm.dto.request.FarmRegisterRequest;
Expand All @@ -24,6 +25,7 @@ public class FarmFarmerService {
private final FarmRepository farmRepository;
private final FarmService farmService;
private final FarmInfoService farmInfoService;
private final FarmScheduleService farmScheduleService;

private final int MAX_FARM_INFO_COUNT = 4;

Expand All @@ -40,6 +42,8 @@ public Long registerFarm(Member member, FarmRegisterRequest request) {
registerFarmInfo(member, farmInfoRegisterRequest);
});

farmScheduleService.addFarmSchedule(request.startDate(), request.endDate(), request.startTime(), request.endTime(), id);

return id;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import poomasi.domain.farm.repository.FarmInfoRepository;
import poomasi.global.error.BusinessException;

import java.util.Collection;
import java.util.List;

import static poomasi.global.error.BusinessError.FARM_INFO_MAIN_REQUIRED_NO_CONTENT;
Expand Down Expand Up @@ -42,4 +43,8 @@ public void deleteFarmInfoById(Long farmInfoId) {
public void deleteFarmInfo(Long farmId) {
farmInfoRepository.deleteAllByFarmId(farmId);
}

public List<FarmInfo> getFarmSchedulesByFarmId(Long farmId) {
return farmInfoRepository.findAllByFarmId(farmId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import poomasi.domain.farm._schedule.dto.FarmScheduleResponse;
import poomasi.domain.farm._schedule.service.FarmScheduleService;
import poomasi.domain.farm.dto.response.FarmDetailResponse;
import poomasi.domain.farm.dto.response.FarmInfoAggregateResponse;
import poomasi.domain.farm.dto.response.FarmInfoResponse;
Expand All @@ -16,11 +18,15 @@
public class FarmPlatformService {
private final FarmService farmService;
private final FarmInfoService farmInfoService;
private final FarmScheduleService farmScheduleService;

public FarmDetailResponse getFarmDetailByFarmId(Long farmId) {
return FarmDetailResponse.builder()
.farmResponse(FarmResponse.fromEntity(farmService.getFarmByFarmId(farmId)))
.info(FarmInfoAggregateResponse.fromEntity(farmInfoService.getFarmInfoByFarmId(farmId)))
.schedules(farmScheduleService.getFarmScheduleByFarmId(farmId).stream()
.map(FarmScheduleResponse::fromEntity)
.collect(Collectors.toList()))
.build();
}

Expand Down
Loading