From 0c68193bff3d7de391e9249afe25a7e5e5b82d3b Mon Sep 17 00:00:00 2001 From: GeonH0 Date: Mon, 10 Jun 2024 17:44:47 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20RecipeDTO=20=EC=A0=95=EC=9D=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Data/Network/DTO/RecipeDTO.swift | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 HomeCafeRecipes/HomeCafeRecipes/Data/Network/DTO/RecipeDTO.swift diff --git a/HomeCafeRecipes/HomeCafeRecipes/Data/Network/DTO/RecipeDTO.swift b/HomeCafeRecipes/HomeCafeRecipes/Data/Network/DTO/RecipeDTO.swift new file mode 100644 index 0000000..b463a88 --- /dev/null +++ b/HomeCafeRecipes/HomeCafeRecipes/Data/Network/DTO/RecipeDTO.swift @@ -0,0 +1,62 @@ +// +// RecipeDTO.swift +// HomeCafeRecipes +// +// Created by 김건호 on 6/10/24. +// + +import Foundation + +struct RecipeDTO: Decodable { + private enum CodingKeys: String, CodingKey { + case recipeId = "recipeId" + case recipeType = "recipeType" + case recipeName = "recipeName" + case recipeDescription = "recipeDescription" + case recipeLikesCnt = "recipeLikesCnt" + case createdAt = "createdAt" + case recipeImgUrls = "recipeImgUrls" + case writer = "writer" + } + + let recipeId: Int + let recipeType: String + let recipeName: String + let recipeDescription: String + let recipeLikesCnt: Int + let createdAt: String + let recipeImgUrls: [RecipeImageDTO] + let writer: UserDTO +} + +struct RecipeImageDTO: Decodable { + private enum CodingKeys: String, CodingKey { + case recipeImgId = "recipeImgId" + case recipeImgUrl = "recipeImgUrl" + } + + let recipeImgId: Int + let recipeImgUrl: String +} + +extension RecipeDTO { + func toDomain() -> Recipe { + return Recipe( + id: recipeId, + type: RecipeType(rawValue: recipeType) ?? .dessert, + name: recipeName, + description: recipeDescription, + writer: writer.toDomain(), + imageUrls: recipeImgUrls.map { $0.recipeImgUrl }, + isLiked: false, + likeCount: recipeLikesCnt, + createdAt: DateFormatter.iso8601.date(from: createdAt) ?? Date() + ) + } +} + +extension RecipeImageDTO { + func toDomain() -> String { + return recipeImgUrl + } +}