Skip to content

Commit

Permalink
Merge pull request #31 from KU-Stacks/develop
Browse files Browse the repository at this point in the history
version 2.0.1
  • Loading branch information
zbqmgldjfh authored Mar 19, 2023
2 parents ea66d3e + 391db2e commit 5e037fa
Show file tree
Hide file tree
Showing 25 changed files with 228 additions and 183 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.kustacks.kuring.category.domain;

import com.kustacks.kuring.kuapi.CategoryName;
import com.kustacks.kuring.notice.domain.Notice;
import com.kustacks.kuring.user.domain.UserCategory;
import lombok.AccessLevel;
Expand Down Expand Up @@ -33,6 +34,10 @@ public Category(String name) {
this.name = name;
}

public boolean isSameName(CategoryName categoryName) {
return this.name.equals(categoryName);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/com/kustacks/kuring/config/MappedBeanConfig.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
package com.kustacks.kuring.config;

import com.kustacks.kuring.kuapi.CategoryName;
import com.kustacks.kuring.kuapi.api.notice.NoticeAPIClient;
import com.kustacks.kuring.kuapi.api.notice.NoticeApiClient;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@RequiredArgsConstructor
public class MappedBeanConfig {

private final NoticeAPIClient kuisNoticeAPIClient;
private final NoticeAPIClient libraryNoticeAPIClient;
private final NoticeApiClient kuisNoticeApiClient;

public MappedBeanConfig(NoticeAPIClient kuisNoticeAPIClient, NoticeAPIClient libraryNoticeAPIClient) {
this.kuisNoticeAPIClient = kuisNoticeAPIClient;
this.libraryNoticeAPIClient = libraryNoticeAPIClient;
}
private final NoticeApiClient libraryNoticeApiClient;

@Bean
public Map<CategoryName, NoticeAPIClient> noticeAPIClientMap() {
HashMap<CategoryName, NoticeAPIClient> map = new HashMap<>();
public Map<CategoryName, NoticeApiClient> noticeApiClientMap() {
HashMap<CategoryName, NoticeApiClient> map = new HashMap<>();

for (CategoryName categoryName : CategoryName.values()) {
if(categoryName.equals(CategoryName.LIBRARY)) {
map.put(categoryName, libraryNoticeAPIClient);
map.put(categoryName, libraryNoticeApiClient);
} else {
map.put(categoryName, kuisNoticeAPIClient);
map.put(categoryName, kuisNoticeApiClient);
}
}

return map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestConfig {
public class RestTemplateConfig {

@Bean
public RestTemplate restTemplate() {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/kustacks/kuring/kuapi/CategoryName.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public enum CategoryName {
this.korName = korName;
}

public boolean isSameName(String name) {
return this.name.equals(name);
}

public boolean isSameShortName(String shortName) {
return this.shortName.equals(shortName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.kustacks.kuring.kuapi.api;

public interface APIClient {
public interface ApiClient {
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

@Slf4j
@Component
public class KuisNoticeAPIClient implements NoticeAPIClient {
public class KuisNoticeApiClient implements NoticeApiClient {

@Value("${notice.referer}")
private String noticeReferer;
Expand All @@ -37,7 +37,7 @@ public class KuisNoticeAPIClient implements NoticeAPIClient {
private final KuisAuthManager kuisAuthManager;
private final Map<CategoryName, KuisNoticeRequestBody> noticeRequestBodies;

public KuisNoticeAPIClient(KuisAuthManager parsingKuisAuthManager,
public KuisNoticeApiClient(KuisAuthManager parsingKuisAuthManager,
KuisNoticeDTOToCommonFormatDTOConverter dtoConverter,

BachelorKuisNoticeRequestBody bachelorNoticeRequestBody,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.kustacks.kuring.kuapi.api.notice;

import com.kustacks.kuring.common.error.ErrorCode;
import com.kustacks.kuring.common.error.InternalLogicException;
import com.kustacks.kuring.common.utils.converter.DTOConverter;
import com.kustacks.kuring.common.utils.converter.LibraryNoticeDTOToCommonFormatDTOConverter;
import com.kustacks.kuring.kuapi.CategoryName;
import com.kustacks.kuring.kuapi.notice.dto.response.CommonNoticeFormatDTO;
import com.kustacks.kuring.kuapi.notice.dto.response.LibraryNoticeDTO;
import com.kustacks.kuring.kuapi.notice.dto.response.LibraryNoticeResponseDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Component
public class LibraryNoticeApiClient implements NoticeApiClient {

private static final int MAX_REQUEST_COUNT = 2;

private final DTOConverter dtoConverter;
private final RestTemplate restTemplate;

@Value("${library.request-url}")
private String libraryUrl;

public LibraryNoticeApiClient(LibraryNoticeDTOToCommonFormatDTOConverter dtoConverter, RestTemplate restTemplate) {
this.dtoConverter = dtoConverter;
this.restTemplate = restTemplate;
}

@Override
public List<CommonNoticeFormatDTO> getNotices(CategoryName categoryName) throws InternalLogicException {
List<LibraryNoticeDTO> libraryNoticeDtoList = scrapLibraryNoticeDtos();
return convertToCommonFormatDto(libraryNoticeDtoList);
}

private List<LibraryNoticeDTO> scrapLibraryNoticeDtos() {
int offset = 0;
int max = 20;

List<LibraryNoticeDTO> libraryNoticeDTOList = new LinkedList<>();
for (int requestIndex = 0; requestIndex < MAX_REQUEST_COUNT; requestIndex++) {
String completeLibraryUrl = buildUrl(libraryUrl, offset, max);
LibraryNoticeResponseDTO libraryNoticeResponseDTO = restTemplate
.getForEntity(completeLibraryUrl, LibraryNoticeResponseDTO.class)
.getBody();

validateResponse(requestIndex, libraryNoticeResponseDTO);

offset = max;
max = libraryNoticeResponseDTO.getTotalCount() - max;

libraryNoticeDTOList.addAll(libraryNoticeResponseDTO.getData().getList());
}

return libraryNoticeDTOList;
}

private String buildUrl(String url, int offset, int max) {
return UriComponentsBuilder.fromUriString(url)
.queryParam("offset", offset)
.queryParam("max", max)
.build().toString();
}

private List<CommonNoticeFormatDTO> convertToCommonFormatDto(List<LibraryNoticeDTO> libraryNoticeDtoList) {
return libraryNoticeDtoList.stream()
.map(dto -> (CommonNoticeFormatDTO) dtoConverter.convert(dto))
.collect(Collectors.toList());
}

private void validateResponse(int requestIndex, LibraryNoticeResponseDTO libraryNoticeResponseDTO) {
if (libraryNoticeResponseDTO == null) {
log.error("도서관 공지 {}번째 요청에 대한 응답의 body가 없습니다.", requestIndex + 1);
throw new InternalLogicException(ErrorCode.LIB_CANNOT_PARSE_JSON);
}

if (!libraryNoticeResponseDTO.isSuccess()) {
log.error("도서관 공지 {}번째 요청에 대한 응답이 fail입니다.", requestIndex + 1);
throw new InternalLogicException(ErrorCode.LIB_BAD_RESPONSE);
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import com.kustacks.kuring.common.error.InternalLogicException;
import com.kustacks.kuring.kuapi.CategoryName;
import com.kustacks.kuring.kuapi.api.APIClient;
import com.kustacks.kuring.kuapi.api.ApiClient;
import com.kustacks.kuring.kuapi.notice.dto.response.CommonNoticeFormatDTO;

import java.util.List;

// TODO: support(CategoryName) 필요
public interface NoticeAPIClient extends APIClient {
public interface NoticeApiClient extends ApiClient {

List<CommonNoticeFormatDTO> getNotices(CategoryName categoryName) throws InternalLogicException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
import java.util.Map;

@Component
public class EachDeptStaffAPIClient implements StaffAPIClient {
public class EachDeptStaffApiClient implements StaffApiClient {

@Value("${staff.each-dept-url}")
private String baseUrl;

private final JsoupClient jsoupClient;

public EachDeptStaffAPIClient(JsoupClient normalJsoupClient) {
public EachDeptStaffApiClient(JsoupClient normalJsoupClient) {
this.jsoupClient = normalJsoupClient;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@
import java.util.Map;

@Component
public class KuStaffAPIClient implements StaffAPIClient {
public class KuStaffApiClient implements StaffApiClient {

private final Map<DeptInfo, String> urlMap;
private final JsoupClient jsoupClient;

public KuStaffAPIClient(
public KuStaffApiClient(
@Value("${staff.living-design-url}") String livingDesignUrl,
@Value("${staff.communication-design-url}") String communicationDesignUrl,
DeptInfo communicationDesignDept,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
import java.util.List;

@Component
public class RealEstateStaffAPIClient implements StaffAPIClient {
public class RealEstateStaffApiClient implements StaffApiClient {

@Value("${staff.real-estate-url}")
private String baseUrl;

private final JsoupClient jsoupClient;

public RealEstateStaffAPIClient(JsoupClient proxyJsoupClient) {
public RealEstateStaffApiClient(JsoupClient proxyJsoupClient) {
this.jsoupClient = proxyJsoupClient;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.kustacks.kuring.kuapi.api.staff;

import com.kustacks.kuring.common.error.InternalLogicException;
import com.kustacks.kuring.kuapi.api.APIClient;
import com.kustacks.kuring.kuapi.api.ApiClient;
import com.kustacks.kuring.kuapi.staff.deptinfo.DeptInfo;
import org.jsoup.nodes.Document;

import java.util.List;

public interface StaffAPIClient extends APIClient {
public interface StaffApiClient extends ApiClient {

int SCRAP_TIMEOUT = 10000;

Expand Down
Loading

0 comments on commit 5e037fa

Please sign in to comment.