Skip to content

Commit

Permalink
FEAT : Program 상세 정보 조회 메서드 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
yummygyudon committed Nov 25, 2023
1 parent 77374de commit ad103fd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.sopt.sopkerton.program.infrastructure;

import java.util.List;

import org.sopt.sopkerton.program.domain.Program;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -10,4 +11,5 @@ public interface ProgramRepository extends JpaRepository<Program, Long> {

@Query("select p from Program p where p.type = :type order by p.registerAt")
List<Program> findAllByProgramType(@Param("type")String programType);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.sopt.sopkerton.program.domain.Program;
import org.sopt.sopkerton.program.domain.exception.ProgramError;
import org.sopt.sopkerton.program.domain.exception.ProgramException;
import org.sopt.sopkerton.program.dto.response.ProgramDetailResponse;
import org.sopt.sopkerton.program.dto.response.ProgramListResponse;
import org.sopt.sopkerton.program.infrastructure.ProgramRepository;
import org.sopt.sopkerton.user.domain.Apply;
import org.sopt.sopkerton.user.domain.enums.ApplyStatus;
import org.sopt.sopkerton.user.infrastructure.ApplyRepository;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ProgramService {
private static final String VOLUNTEER_TYPE = "VOLUNTEERING";
private static final String EMPLOYMENT_TYPE = "EMPLOYMENT";

private final ProgramRepository programRepository;
private final ApplyRepository applyRepository;

public List<ProgramListResponse> getProgramListByProgramType(String programType) {
List<Program> programs = programRepository.findAllByProgramType(programType);
Expand All @@ -31,10 +41,40 @@ public List<ProgramListResponse> getProgramListByProgramType(String programType)
return programListResponses;
}

public Object getProgramDetail(Long userId, Long programId) {
Program program = programRepository.findById(programId)
.orElseThrow(() -> new ProgramException(ProgramError.PROGRAM_NOT_FOUND));
Apply apply = applyRepository.findByUserIdAndProgramId(userId, programId).get();
boolean isApply = convertToIsApply(apply.getIsApply());
if (program.getType().equals(VOLUNTEER_TYPE)) {
return new ProgramDetailResponse.VolunteerDetail(
program.getImageUrl(),
program.getContent(),
program.getOrganizationName(),
formatToLocalDate(program.getRegisterAt()),
program.getVolunteerHours(),
isApply
);
}
if (program.getType().equals(EMPLOYMENT_TYPE)){
return new ProgramDetailResponse.EmploymentDetail(
program.getImageUrl(),
program.getContent(),
program.getOrganizationName(),
formatToLocalDate(program.getRegisterAt()),
program.getSalary(),
isApply
);
}
return null;
}

private String formatToLocalDate(LocalDateTime localDateTime) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM월 dd일");
return localDateTime.format(formatter);
}


private boolean convertToIsApply(ApplyStatus status) {
return status.equals(ApplyStatus.APPLY);
}
}

0 comments on commit ad103fd

Please sign in to comment.