Skip to content

Commit

Permalink
refactor: Mod BanchanListDTO+Mapping Class
Browse files Browse the repository at this point in the history
반찬리스트DTO 클래스의 코딩키 삭제 & ConvertfromSnakeCase 적용 및 변수명 변경

issue: #27
  • Loading branch information
youngminshim-de committed Apr 23, 2021
1 parent ce2df8b commit 1dc0e54
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 31 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,25 @@ struct BanchanListDTO: Codable {

extension BanchanListDTO {
struct BanchanListItemDTO: Codable {
let hash: String
let detailHash: String
let image: String
let alt: String
let deliveryType: [String]
let description: String
let title: String
let normalPrice: String?
let salePrice: String
let nPrice: String?
let sPrice: String
let badge: [String]?

private enum CodingKeys: String, CodingKey {
case hash = "detail_hash"
case image
case alt
case deliveryType = "delivery_type"
case description
case title
case normalPrice = "n_price"
case salePrice = "s_price"
case badge
func toDomain() -> Banchan {
return .init(detailHash: detailHash, image: image, alt: alt, title: title, description: description, nPrice: nPrice, sPrice: sPrice, badge: badge, deliveryType: deliveryType)
}
}
}

extension BanchanListDTO {

func toDomain() -> [Banchan] {
let banchans = self.body.map {
$0.toDomain()
let banchans = self.body.map { banchanListitemDTO in
banchanListitemDTO.toDomain()
}
return banchans
}
}

extension BanchanListDTO.BanchanListItemDTO {
func toDomain() -> Banchan {
return .init(hash: hash, image: image, alt: alt, title: title, description: description, netPrice: normalPrice, salePrice: salePrice, badge: badge, delivery_type: deliveryType)
}
}
12 changes: 6 additions & 6 deletions iOS/SideDish/SideDish/Domain/Entities/Banchan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ enum PriceType {
}

struct Banchan: Hashable {
private (set) var hash: String
private (set) var detailHash: String
private (set) var image: String
private (set) var alt: String
private (set) var title: String
private (set) var description: String
private (set) var netPrice: String?
private (set) var salePrice: String
private (set) var nPrice: String?
private (set) var sPrice: String
private (set) var badge: [String]?
private (set) var delivery_type: [String]
private (set) var deliveryType: [String]

func hash(into hasher: inout Hasher) {
hasher.combine(hash)
hasher.combine(detailHash)
}
static func == (lhs: Banchan, rhs: Banchan) -> Bool {
lhs.hash == rhs.hash
lhs.detailHash == rhs.detailHash
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ struct FetchBanchanListUseCase {

static func fetchBanchanList(network: NetworkRequest,section: String, completion: @escaping ([Banchan]?) -> Void) {
let url = baseURL+section
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
network.request(url: url, httpMethod: .get) { dataDummy in
guard let data = dataDummy.data else { return }

guard let banchans = try? JSONDecoder().decode(BanchanListDTO.self, from: data) else { return }
guard let banchans = try? decoder.decode(BanchanListDTO.self, from: data) else { return }
completion(banchans.toDomain())
}
}
Expand Down

0 comments on commit 1dc0e54

Please sign in to comment.