Skip to content

Commit

Permalink
🚧 Update data models due to API changes
Browse files Browse the repository at this point in the history
- Adjusted data models to align with the updated api structure.
  • Loading branch information
WhiteHyun committed Feb 23, 2024
1 parent 0bbb4fb commit 9fa5e34
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 106 deletions.
20 changes: 15 additions & 5 deletions APIService/Sources/HomeAPI/HomeService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Network
// MARK: - HomeServiceRepresentable

public protocol HomeServiceRepresentable {
func fetchProductList(request: ProductRequest) async throws -> [Product]
func fetchProductList(request: ProductRequest) async throws -> Paginated<Product>
func fetchProductCount(request: ProductCountRequest) async throws -> Int
}

Expand All @@ -29,9 +29,9 @@ public struct HomeService {
// MARK: HomeServiceRepresentable

extension HomeService: HomeServiceRepresentable {
public func fetchProductList(request: ProductRequest) async throws -> [Product] {
let products: [ProductResponse] = try await network.request(with: HomeEndPoint.fetchProducts(request))
return products.map(Product.init)
public func fetchProductList(request: ProductRequest) async throws -> Paginated<Product> {
let response: ProductResponse = try await network.request(with: HomeEndPoint.fetchProducts(request))
return Paginated<Product>(dto: response)
}

public func fetchProductCount(request: ProductCountRequest) async throws -> Int {
Expand All @@ -40,8 +40,18 @@ extension HomeService: HomeServiceRepresentable {
}
}

private extension Product {
private extension Paginated where Model == Product {
init(dto: ProductResponse) {
self.init(
count: dto.count,
hasMore: dto.hasMore,
results: dto.results.map(Product.init)
)
}
}

private extension Product {
init(dto: ProductItemResponse) {
self.init(
id: dto.id,
imageURL: dto.imageURL,
Expand Down
25 changes: 21 additions & 4 deletions APIService/Sources/HomeAPI/Responses/ProductResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,37 @@ import Foundation

// MARK: - ProductResponse

struct ProductResponse: Decodable {
struct ProductResponse: Decodable, Paginatable {
let count: Int
let hasMore: Bool

let results: [ProductItemResponse]

enum CodingKeys: String, CodingKey {
case count
case hasMore = "has_more"
case results
}
}

// MARK: - ProductItemResponse

struct ProductItemResponse: Decodable {
let id: Int
let imageURL: URL
let date: Date
let price: Int
let name: String
let promotion: Promotion
let convenienceStore: ConvenienceStore

enum CodingKeys: String, CodingKey {
case id
case imageURL = "image_url"
case imageURL = "img"
case price
case date
case name
case promotion
case convenienceStore
case promotion = "tag"
case convenienceStore = "store"
}
}
128 changes: 36 additions & 92 deletions APIService/Sources/HomeAPISupport/Mocks/HomeProductResponse.json
Original file line number Diff line number Diff line change
@@ -1,92 +1,36 @@
[
{
"id": 1,
"date": "2024:01",
"image_url": "https://image.woodongs.com/imgsvr/item/GD_8809288634967_001.jpg",
"price": 2500,
"name": "BR)레인보우샤베트과즙워터500ML",
"promotion": "1+1",
"convenienceStore": "GS25"
},
{
"id": 2,
"date": "2024:01",
"image_url": "https://image.woodongs.com/imgsvr/item/GD_8809288635315_003.jpg",
"price": 2500,
"name": "BR)망고탱고과즙워터500ML",
"promotion": "1+1",
"convenienceStore": "GS25"
},
{
"id": 3,
"date": "2024:01",
"image_url": "https://image.woodongs.com/imgsvr/item/GD_8809288634974_001.jpg",
"price": 2500,
"name": "BR)피치요거트과즙워터500ML",
"promotion": "1+1",
"convenienceStore": "GS25"
},
{
"id": 4,
"date": "2024:01",
"image_url": "https://image.woodongs.com/imgsvr/item/GD_8806002010861_865.jpg",
"price": 5000,
"name": "광동)헛개파워100ML",
"promotion": "1+1",
"convenienceStore": "GS25"
},
{
"id": 5,
"date": "2024:01",
"image_url": "https://image.woodongs.com/imgsvr/item/GD_8806011416005_001.JPG",
"price": 5000,
"name": "동아)모닝케어D100ML",
"promotion": "1+1",
"convenienceStore": "GS25"
},
{
"id": 6,
"date": "2024:01",
"image_url": "https://image.woodongs.com/imgsvr/item/GD_8806011415992_001.JPG",
"price": 5000,
"name": "동아)모닝케어H100ML",
"promotion": "1+1",
"convenienceStore": "GS25"
},
{
"id": 7,
"date": "2024:01",
"image_url": "https://image.woodongs.com/imgsvr/item/GD_8809556566891_001.jpg",
"price": 5000,
"name": "서영)모닝이즈백100ML",
"promotion": "1+1",
"convenienceStore": "GS25"
},
{
"id": 8,
"date": "2024:01",
"image_url": "https://image.woodongs.com/imgsvr/item/GD_8809125061857_002.jpg",
"price": 5000,
"name": "종근당)헛개땡큐골드100ML",
"promotion": "1+1",
"convenienceStore": "GS25"
},
{
"id": 9,
"date": "2024:01",
"image_url": "https://image.woodongs.com/imgsvr/item/GD_8809329050015_018.jpg",
"price": 5000,
"name": "천지개벽)숙취해소음료100ML",
"promotion": "1+1",
"convenienceStore": "GS25"
},
{
"id": 10,
"date": "2024:01",
"image_url": "https://image.woodongs.com/imgsvr/item/GD_8801013777260_001.jpg",
"price": 5000,
"name": "큐원)상쾌환부스터100ML",
"promotion": "1+1",
"convenienceStore": "GS25"
}
]
{
"count": 3,
"has_more": true,
"results": [
{
"name": "MDS)베이컨까르보나라파스타",
"img": "https://www.7-eleven.co.kr/upload/product/8805684/009026.1.jpg",
"price": 4100,
"store": "7-ELEVEn",
"tag": "2+1",
"proinfo": 0,
"date": "2024-02-01T10:11:47Z",
"id": 84683
},
{
"name": "MDS)투움바파스타230g",
"img": "https://www.7-eleven.co.kr/upload/product/8805684/003307.1.jpg",
"price": 4100,
"store": "7-ELEVEn",
"tag": "2+1",
"proinfo": 0,
"date": "2024-02-01T10:11:47Z",
"id": 84684
},
{
"name": "MDS)미트파스타230g",
"img": "https://www.7-eleven.co.kr/upload/product/8805684/005813.1.jpg",
"price": 4100,
"store": "7-ELEVEn",
"tag": "2+1",
"proinfo": 0,
"date": "2024-02-01T10:11:47Z",
"id": 84685
}
]
}
14 changes: 14 additions & 0 deletions Entity/Sources/Entity/Common/Paginatable.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//
// Paginatable.swift
//
//
// Created by 홍승현 on 2/23/24.
//

public protocol Paginatable {
associatedtype Model
var count: Int { get }
var hasMore: Bool { get }

var results: [Model] { get }
}
19 changes: 19 additions & 0 deletions Entity/Sources/Entity/Common/Paginated.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// Paginated.swift
//
//
// Created by 홍승현 on 2/23/24.
//

/// 페이징 처리가 들어가는 모델의 경우 해당 모델을 사용합니다.
public struct Paginated<Model>: Paginatable {
public let count: Int
public let hasMore: Bool
public let results: [Model]

public init(count: Int, hasMore: Bool, results: [Model]) {
self.count = count
self.hasMore = hasMore
self.results = results
}
}
2 changes: 1 addition & 1 deletion Entity/Sources/Entity/ConvenienceStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import SwiftUI
public enum ConvenienceStore: String, Codable {
case cu = "CU"
case gs25 = "GS25"
case _7Eleven = "7-ElEVEN"
case _7Eleven = "7-ELEVEn"
case emart24
case ministop = "MINISTOP"
}
9 changes: 7 additions & 2 deletions PyeonHaeng-iOS/Sources/AppRootComponent.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ struct AppRootComponent: HomeDependency {

init() {
let homeNetworking: Networking = {
let configuration = URLSessionConfiguration.ephemeral
configuration.protocolClasses = [HomeURLProtocol.self]
let configuration: URLSessionConfiguration
#if DEBUG
configuration = .ephemeral
configuration.protocolClasses = [HomeURLProtocol.self]
#else
configuration = .default
#endif
let provider = NetworkProvider(session: URLSession(configuration: configuration))
return provider
}()
Expand Down
11 changes: 9 additions & 2 deletions PyeonHaeng-iOS/Sources/Scenes/HomeScene/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import Combine
import Entity
import Foundation
import HomeAPI
import Log

// MARK: - HomeAction

Expand Down Expand Up @@ -64,7 +65,11 @@ final class HomeViewModel: HomeViewModelRepresentable {
switch action {
case .fetchProducts:
Task {
try await fetchProducts()
do {
try await fetchProducts()
} catch {
Log.make(with: .viewModel)?.error("\(String(describing: error))")
}
}
case .fetchCount:
Task {
Expand All @@ -87,7 +92,9 @@ final class HomeViewModel: HomeViewModelRepresentable {
pageSize: state.pageSize,
offset: state.offset
)
try await state.products = service.fetchProductList(request: request)

let paginatedModel = try await service.fetchProductList(request: request)
state.products = paginatedModel.results
}

private func fetchProductCounts() async throws {
Expand Down

0 comments on commit 9fa5e34

Please sign in to comment.