-
Notifications
You must be signed in to change notification settings - Fork 0
[Feat] #271 - 서비스 레이어 추가했습니다. #272
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
Conversation
- SearchService(API 3개) - MypageService(API 2개) - AnnouncementsService(API 1개)
| protocol SearchServiceProtocol { | ||
| func getSearchResult(keyword: String, sortBy: String, page: Int, size: Int) -> Observable<SearchResultModel> | ||
| func getMostViewDatas() -> Observable<[RecommendAnnouncement]> | ||
| func getMostScrapDatas() -> Observable<[RecommendAnnouncement]> | ||
| func getAdvertiseDatas() -> Observable<[BannerModel]> | ||
| } | ||
|
|
||
| final class SearchService: SearchServiceProtocol { | ||
|
|
||
| private let provider: MoyaProvider<SearchTargetType> | ||
|
|
||
| init(provider: MoyaProvider<SearchTargetType>) { | ||
| self.provider = provider | ||
| } | ||
|
|
||
| func getSearchResult(keyword: String, sortBy: String, page: Int, size: Int) -> Observable<SearchResultModel> { | ||
| return provider.rx.request(.getSearchResult(keyword: keyword, sortBy: sortBy, page: page, size: size)) | ||
| .filterSuccessfulStatusCodes() | ||
| .map(BaseResponse<SearchResultModel>.self) | ||
| .compactMap { $0.result } | ||
| .asObservable() | ||
| } | ||
|
|
||
| func getMostViewDatas() -> Observable<[RecommendAnnouncement]> { | ||
| return provider.rx.request(.getMostViewDatas) | ||
| .filterSuccessfulStatusCodes() | ||
| .map(BaseResponse<RecommendAnnouncementModel>.self) | ||
| .compactMap { $0.result?.announcements } | ||
| .asObservable() | ||
| } | ||
|
|
||
| func getMostScrapDatas() -> Observable<[RecommendAnnouncement]> { | ||
| return provider.rx.request(.getMostScrapDatas) | ||
| .filterSuccessfulStatusCodes() | ||
| .map(BaseResponse<RecommendAnnouncementModel>.self) | ||
| .compactMap { $0.result?.announcements } | ||
| .asObservable() | ||
| } | ||
|
|
||
| func getAdvertiseDatas() -> Observable<[BannerModel]> { | ||
| return provider.rx.request(.getAdvertiseDatas) | ||
| .filterSuccessfulStatusCodes() | ||
| .map(BaseResponse<AdvertisementModel>.self) | ||
| .compactMap { $0.result?.banners } | ||
| .asObservable() | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Service로 정리한 이 코드와, 변경하기 전(PR에 적은 긴 서버 통신 코드) 코드는 어떤 차이점이 있을까요?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ViewModel에 존재하던 이전 서버 통신 코드에서 데이터 가공이나 UI 업데이트 코드 등을 제외하고 네트워크 호출 부분만 가져와서 Service에 넣었어요..!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아 제가 말한 거는 예외 처리에서 차이가 있다!라는 것을 말씀드리려고 했습니다.
현재 코드는 filterSuccessfulStatusCodes
200에서 299번까지 성공 코드일 때만 필터가 되기 때문에
mapper 문제인지, 서버 문제인지 어디서 문제가 발생하는지 구분하는 데에 어려울 수 있다는 점 말하려고 했어요!
저도 어떻게 해야 서버 통신을 좋게 할 수 있을지 고민하고 있기 때문에 회의 때 이 부분도 말하면 좋을 것 같습니다~
thingineeer
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
고생하셨습니다~
| protocol SearchServiceProtocol { | ||
| func getSearchResult(keyword: String, sortBy: String, page: Int, size: Int) -> Observable<SearchResultModel> | ||
| func getMostViewDatas() -> Observable<[RecommendAnnouncement]> | ||
| func getMostScrapDatas() -> Observable<[RecommendAnnouncement]> | ||
| func getAdvertiseDatas() -> Observable<[BannerModel]> | ||
| } | ||
|
|
||
| final class SearchService: SearchServiceProtocol { | ||
|
|
||
| private let provider: MoyaProvider<SearchTargetType> | ||
|
|
||
| init(provider: MoyaProvider<SearchTargetType>) { | ||
| self.provider = provider | ||
| } | ||
|
|
||
| func getSearchResult(keyword: String, sortBy: String, page: Int, size: Int) -> Observable<SearchResultModel> { | ||
| return provider.rx.request(.getSearchResult(keyword: keyword, sortBy: sortBy, page: page, size: size)) | ||
| .filterSuccessfulStatusCodes() | ||
| .map(BaseResponse<SearchResultModel>.self) | ||
| .compactMap { $0.result } | ||
| .asObservable() | ||
| } | ||
|
|
||
| func getMostViewDatas() -> Observable<[RecommendAnnouncement]> { | ||
| return provider.rx.request(.getMostViewDatas) | ||
| .filterSuccessfulStatusCodes() | ||
| .map(BaseResponse<RecommendAnnouncementModel>.self) | ||
| .compactMap { $0.result?.announcements } | ||
| .asObservable() | ||
| } | ||
|
|
||
| func getMostScrapDatas() -> Observable<[RecommendAnnouncement]> { | ||
| return provider.rx.request(.getMostScrapDatas) | ||
| .filterSuccessfulStatusCodes() | ||
| .map(BaseResponse<RecommendAnnouncementModel>.self) | ||
| .compactMap { $0.result?.announcements } | ||
| .asObservable() | ||
| } | ||
|
|
||
| func getAdvertiseDatas() -> Observable<[BannerModel]> { | ||
| return provider.rx.request(.getAdvertiseDatas) | ||
| .filterSuccessfulStatusCodes() | ||
| .map(BaseResponse<AdvertisementModel>.self) | ||
| .compactMap { $0.result?.banners } | ||
| .asObservable() | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
아 제가 말한 거는 예외 처리에서 차이가 있다!라는 것을 말씀드리려고 했습니다.
현재 코드는 filterSuccessfulStatusCodes
200에서 299번까지 성공 코드일 때만 필터가 되기 때문에
mapper 문제인지, 서버 문제인지 어디서 문제가 발생하는지 구분하는 데에 어려울 수 있다는 점 말하려고 했어요!
저도 어떻게 해야 서버 통신을 좋게 할 수 있을지 고민하고 있기 때문에 회의 때 이 부분도 말하면 좋을 것 같습니다~
🩵 Issue
close #271
💙 변경된 내용
서비스 레이어 추가했습니다.
각각의 ServiceProtocol의 func의 파라미터와 반환값은 기존에 있던 API 호출 코드 그대로 반영해서 작성했습니다!
Service안의 func은 전부 API 호출한 후 응답 DTO 변환하여 반환하는 모습으로 작성되었습니다.
서비스는 호출만 하는 역할이라고 생각해서, 딱히 다른 코드를 추가하지 않았는데 더 필요한게 있을까요?
Service안의 func 이름들은 전부 TargetType의 case이름과 통일 시켰습니다. 공고 상세 조회하기의 경우에만 TargetType의 case이름이 getAnnouncements이길래, 다른 공고 조회 API과 헷갈릴 것 같아서 getDetailAnnouncements로 바꿔서 작성했습니다.
예시) 기존 코드
예시) 서비스 코드