Skip to content

Commit

Permalink
Merge pull request #132 from teamterning/refactor/#129
Browse files Browse the repository at this point in the history
[♻️ refactor/#129]: 공고 상세 정보 및 검색 결과 화면 response body 수정
  • Loading branch information
JungYoonShin authored Sep 11, 2024
2 parents 3c20eeb + 4e92e6d commit aef33a5
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 53 deletions.
10 changes: 9 additions & 1 deletion src/main/java/org/terning/terningserver/domain/enums/Color.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.terning.terningserver.exception.CustomException;
import org.terning.terningserver.exception.enums.ErrorMessage;

import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;

import static org.terning.terningserver.exception.enums.ErrorMessage.INVALID_SCRAP_COLOR;

@RequiredArgsConstructor
@Getter
public enum Color {
Expand Down Expand Up @@ -35,7 +39,11 @@ public String getColorValue() {
}

public static Color findByName(String name) {
return colorMap.get(name);
Color color = colorMap.get(name);
if (color == null) {
throw new CustomException(INVALID_SCRAP_COLOR);
}
return color;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,52 @@
import lombok.Builder;
import org.terning.terningserver.domain.Company;
import org.terning.terningserver.domain.InternshipAnnouncement;
import org.terning.terningserver.domain.Scrap;
import org.terning.terningserver.util.DateUtil;

@Builder
import static lombok.AccessLevel.PRIVATE;

@Builder(access = PRIVATE)
public record InternshipDetailResponseDto(
String companyImage,
String dDay,
String title,
String deadline,
String workingPeriod,
String startDate,
boolean isScrapped,
String color,
String deadline,
String startYearMonth,
int scrapCount,
int viewCount,
String company,
String companyCategory,
String companyImage,
String qualification,
String jobType,
String detail,
String url,
Long scrapId
String url
) {
public static InternshipDetailResponseDto of(InternshipAnnouncement announcement, Company company, Long scrapId) {
public static InternshipDetailResponseDto of(
final InternshipAnnouncement announcement,
final Company company,
final Long scrapId,
final String color
) {
return InternshipDetailResponseDto.builder()
.companyImage(company.getCompanyImage())
.dDay(DateUtil.convert(announcement.getDeadline()))
.title(announcement.getTitle())
.deadline(DateUtil.convertDeadline(announcement.getDeadline()))
.workingPeriod(announcement.getWorkingPeriod())
.startDate(announcement.getStartYear() + "년 " + announcement.getStartMonth() + "월")
.isScrapped(scrapId!=null)
.color(color)
.deadline(DateUtil.convertDeadline(announcement.getDeadline()))
.startYearMonth(announcement.getStartYear() + "년 " + announcement.getStartMonth() + "월")
.scrapCount(announcement.getScrapCount())
.viewCount(announcement.getViewCount())
.company(company.getCompanyInfo())
.companyCategory(company.getCompanyCategory().getValue())
.companyImage(company.getCompanyImage())
.qualification(announcement.getQualifications())
.jobType(announcement.getJobType())
.detail(announcement.getDetail())
.url(announcement.getUrl())
.scrapId(scrapId)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,52 @@

import java.util.List;

import static lombok.AccessLevel.PRIVATE;

public record SearchResultResponseDto(
int totalPages,
long totalCount,
Boolean hasNext,
List<SearchAnnouncementResponse> announcements
) {
@Builder
@Builder(access = PRIVATE)
public record SearchAnnouncementResponse(
Long internshipAnnouncementId,
Long scrapId,
String dDay,
String deadline,
long internshipAnnouncementId,
String companyImage,
String dDay,
String title,
String workingPeriod,
String startYearMonth,
String color
boolean isScrapped,
String color,
String deadline,
String startYearMonth

) {
public static SearchAnnouncementResponse from(final InternshipAnnouncement announcement, final Long scrapId, final String color) {
String startYearMonth = announcement.getStartYear() + "년 " + announcement.getStartMonth() + "월";
String deadline = DateUtil.convertDeadline(announcement.getDeadline());

public static SearchAnnouncementResponse from(
final InternshipAnnouncement announcement,
final Long scrapId,
final String color
) {
return SearchAnnouncementResponse.builder()
.internshipAnnouncementId(announcement.getId())
.scrapId(scrapId)
.dDay(DateUtil.convert(announcement.getDeadline()))
.deadline(deadline)
.companyImage(announcement.getCompany().getCompanyImage())
.dDay(DateUtil.convert(announcement.getDeadline()))
.title(announcement.getTitle())
.workingPeriod(announcement.getWorkingPeriod())
.startYearMonth(startYearMonth)
.isScrapped(scrapId!=null)
.color(color)
// .isScrapped(isScrapped)
.deadline(DateUtil.convertDeadline(announcement.getDeadline()))
.startYearMonth(announcement.getStartYear() + "년 " + announcement.getStartMonth() + "월")
.build();
}
}
public static SearchResultResponseDto of(int totalPages, Boolean hasNext, List<SearchAnnouncementResponse> announcements) {
return new SearchResultResponseDto(totalPages, hasNext, announcements);
public static SearchResultResponseDto of(
final int totalPages,
final long totalCount,
final Boolean hasNext,
final List<SearchAnnouncementResponse> announcements
) {
return new SearchResultResponseDto(totalPages, totalCount, hasNext, announcements);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public enum ErrorMessage {

// 스크랩
EXISTS_SCRAP_ALREADY(400, "이미 스크랩했습니다."),
INVALID_SCRAP_COLOR(401, "유효하지 않은 스크랩 색상입니다."),

// 로그 아웃
FAILED_SIGN_OUT(404, "로그아웃에 실패하였습니다"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@
@RequiredArgsConstructor
public class InternshipRepositoryImpl implements InternshipRepositoryCustom {

private static final int INTERNSHIP_CREATED_WITHIN_DAYS = 60;

private final JPAQueryFactory jpaQueryFactory;

@Override
public List<InternshipAnnouncement> getMostViewedInternship() {
return jpaQueryFactory
.selectFrom(internshipAnnouncement)
//지원 마감된 공고 및 60일 보다 오래된 공고 제외
.where(
internDeadlineGoe(),
internCreatedAtAfter()
) //지원 마감된 공고 및 30일 보다 오래된 공고 제외
)
.orderBy(internshipAnnouncement.viewCount.desc(), internshipAnnouncement.createdAt.desc())
.limit(5)
.fetch();
Expand All @@ -48,7 +51,7 @@ public List<InternshipAnnouncement> getMostScrappedInternship() {
.where(
internDeadlineGoe(),
internCreatedAtAfter()
) //지원 마감된 공고 및 30일 보다 오래된 공고 제외
) //지원 마감된 공고 및 60일 보다 오래된 공고 제외
.orderBy(internshipAnnouncement.scrapCount.desc(), internshipAnnouncement.createdAt.desc())
.limit(5)
.fetch();
Expand All @@ -74,8 +77,10 @@ public Page<InternshipAnnouncement> searchInternshipAnnouncement(String keyword,
.where(contentLike(keyword))
.fetchOne();

// 인턴공고가 없을 경우, 즉 count가 null일 경우 0L을 기본값으로 설정
long announcementCount = (count != null) ? count : 0L;

return new PageImpl<>(internshipAnnouncements, pageable, count);
return new PageImpl<>(internshipAnnouncements, pageable, announcementCount);
}

private BooleanExpression contentLike(String keyword) {
Expand Down Expand Up @@ -159,22 +164,16 @@ private NumberTemplate<Integer> getWorkingPeriodAsNumber() {
);
}

// private NumberTemplate<Integer> getWorkingPeriodAsNumber(){
// return Expressions.numberTemplate(
// Integer.class,
// "CAST(SUBSTRING({0}, 1, LENGTH({0}) - 2) AS INTEGER)",
// internshipAnnouncement.workingPeriod
// );
// }

// 지원 마감일이 지나지 않은 공고
private BooleanExpression internDeadlineGoe() {
return internshipAnnouncement.deadline.goe(LocalDate.now());
}

// 현재 시점으로부터 30일 이내의 공고
// 현재 시점으로부터 60일 이내의 공고
private BooleanExpression internCreatedAtAfter() {
return internshipAnnouncement.createdAt.after(LocalDate.now().minusDays(30).atStartOfDay());
return internshipAnnouncement.createdAt.after(
LocalDate.now().minusDays(INTERNSHIP_CREATED_WITHIN_DAYS).atStartOfDay()
);
}

// 서류 마감일이 지난 공고는 가장 아래로 보여주는 로직
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ public interface ScrapRepository extends JpaRepository<Scrap, Long>, ScrapReposi

Optional<Scrap> findByInternshipAnnouncementIdAndUserId(Long internshipId, Long userId);

// List<Scrap> findByUserIdAndInternshipAnnouncement_Deadline(Long userId, LocalDate deadline);

void deleteByInternshipAnnouncementIdAndUserId(Long internshipId, Long userId);

List<Scrap> findByUserIdAndInternshipAnnouncement_DeadlineBetween(Long userId, LocalDate start, LocalDate end);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@

public interface InternshipDetailService {

InternshipDetailResponseDto getInternshipDetail(Long internshipAnnouncementId, Long userId);
InternshipDetailResponseDto getInternshipDetail(long internshipAnnouncementId, long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,22 @@ public class InternshipDetailServiceImpl implements InternshipDetailService {

@Override
@Transactional
public InternshipDetailResponseDto getInternshipDetail(Long internshipAnnouncementId, Long userId) {
public InternshipDetailResponseDto getInternshipDetail(long internshipAnnouncementId, long userId) {
InternshipAnnouncement announcement = internshipRepository.findById(internshipAnnouncementId)
.orElseThrow(() -> new CustomException(ErrorMessage.NOT_FOUND_INTERN_EXCEPTION));


announcement.updateViewCount();
Optional<Scrap> scrap = scrapRepository.findByInternshipAnnouncementIdAndUserId(announcement.getId(), userId);

if (scrap.isPresent()) {
return InternshipDetailResponseDto.of(
announcement, announcement.getCompany(),
scrapRepository.findByInternshipAnnouncementIdAndUserId(announcement.getId(), userId).get().getId()
scrap.get().getId(), scrap.get().getColor().getColorValue()
);
} else {
return InternshipDetailResponseDto.of(
announcement, announcement.getCompany(),
null
null, null
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public SearchResultResponseDto searchInternshipAnnouncement(String keyword, Stri

List<InternshipAnnouncement> announcements = pageAnnouncements.getContent();

List<SearchResultResponseDto.SearchAnnouncementResponse> searchAnnouncementResponses = new ArrayList<>();
List<SearchResultResponseDto.SearchAnnouncementResponse> searchAnnouncementResponses;

List<Scrap> scraps = scrapRepository.findAllByInternshipAndUserId(announcements, userId);

Expand All @@ -57,12 +57,16 @@ public SearchResultResponseDto searchInternshipAnnouncement(String keyword, Stri
searchAnnouncementResponses = announcements.stream()
.map(a -> {
Scrap scrap = scrapMap.get(a.getId());
return SearchResultResponseDto.SearchAnnouncementResponse.from(a, scrap != null ? scrap.getId() : null, scrap != null ? scrap.getColor().getColorValue() : null);
return SearchResultResponseDto.SearchAnnouncementResponse.from(
a, scrap != null ? scrap.getId() : null,
scrap != null ? scrap.getColor().getColorValue() : null
);
})
.toList();

return new SearchResultResponseDto(
pageAnnouncements.getTotalPages(),
pageAnnouncements.getTotalElements(),
pageAnnouncements.hasNext(),
searchAnnouncementResponses
);
Expand Down

0 comments on commit aef33a5

Please sign in to comment.