WIP - Complete App refactoring

This commit is contained in:
VincentMeilinger
2025-05-26 15:52:24 +02:00
parent 29fd3c668b
commit 5acf3b9c4f
49 changed files with 1996 additions and 543 deletions

View File

@@ -12,7 +12,7 @@ import UIKit
class CookbookApiV1: CookbookApi {
static let basePath: String = "/index.php/apps/cookbook/api/v1"
static func importRecipe(auth: String, data: Data) async -> (RecipeDetail?, NetworkError?) {
static func importRecipe(auth: String, data: Data) async -> (Recipe?, NetworkError?) {
let request = ApiRequest(
path: basePath + "/import",
method: .POST,
@@ -22,10 +22,12 @@ class CookbookApiV1: CookbookApi {
let (data, error) = await request.send()
guard let data = data else { return (nil, error) }
return (JSONDecoder.safeDecode(data), nil)
let recipe: CookbookApiRecipeDetailV1? = JSONDecoder.safeDecode(data)
return (recipe?.toRecipe(), error)
}
static func getImage(auth: String, id: Int, size: RecipeImage.RecipeImageSize) async -> (UIImage?, NetworkError?) {
static func getImage(auth: String, id: String, size: RecipeImage.RecipeImageSize) async -> (UIImage?, NetworkError?) {
guard let id = Int(id) else {return (nil, .unknownError)}
let imageSize = (size == .FULL ? "full" : "thumb")
let request = ApiRequest(
path: basePath + "/recipes/\(id)/image?size=\(imageSize)",
@@ -39,7 +41,7 @@ class CookbookApiV1: CookbookApi {
return (UIImage(data: data), error)
}
static func getRecipes(auth: String) async -> ([Recipe]?, NetworkError?) {
static func getRecipes(auth: String) async -> ([RecipeStub]?, NetworkError?) {
let request = ApiRequest(
path: basePath + "/recipes",
method: .GET,
@@ -50,10 +52,12 @@ class CookbookApiV1: CookbookApi {
let (data, error) = await request.send()
guard let data = data else { return (nil, error) }
print("\n\nRECIPE: ", String(data: data, encoding: .utf8))
return (JSONDecoder.safeDecode(data), nil)
let recipes: [CookbookApiRecipeV1]? = JSONDecoder.safeDecode(data)
return (recipes?.map({ recipe in recipe.toRecipeStub() }), nil)
}
static func createRecipe(auth: String, recipe: RecipeDetail) async -> (NetworkError?) {
static func createRecipe(auth: String, recipe: Recipe) async -> (NetworkError?) {
let recipe = CookbookApiRecipeDetailV1.fromRecipe(recipe)
guard let recipeData = JSONEncoder.safeEncode(recipe) else {
return .dataError
}
@@ -81,7 +85,8 @@ class CookbookApiV1: CookbookApi {
return nil
}
static func getRecipe(auth: String, id: Int) async -> (RecipeDetail?, NetworkError?) {
static func getRecipe(auth: String, id: String) async -> (Recipe?, NetworkError?) {
guard let id = Int(id) else {return (nil, .unknownError)}
let request = ApiRequest(
path: basePath + "/recipes/\(id)",
method: .GET,
@@ -91,10 +96,13 @@ class CookbookApiV1: CookbookApi {
let (data, error) = await request.send()
guard let data = data else { return (nil, error) }
return (JSONDecoder.safeDecode(data), nil)
let recipe: CookbookApiRecipeDetailV1? = JSONDecoder.safeDecode(data)
return (recipe?.toRecipe(), nil)
}
static func updateRecipe(auth: String, recipe: RecipeDetail) async -> (NetworkError?) {
static func updateRecipe(auth: String, recipe: Recipe) async -> (NetworkError?) {
let cookbookRecipe = CookbookApiRecipeDetailV1.fromRecipe(recipe)
guard let recipeData = JSONEncoder.safeEncode(recipe) else {
return .dataError
}
@@ -121,7 +129,8 @@ class CookbookApiV1: CookbookApi {
return nil
}
static func deleteRecipe(auth: String, id: Int) async -> (NetworkError?) {
static func deleteRecipe(auth: String, id: String) async -> (NetworkError?) {
guard let id = Int(id) else {return .unknownError}
let request = ApiRequest(
path: basePath + "/recipes/\(id)",
method: .DELETE,
@@ -147,7 +156,7 @@ class CookbookApiV1: CookbookApi {
return (JSONDecoder.safeDecode(data), nil)
}
static func getCategory(auth: String, named categoryName: String) async -> ([Recipe]?, NetworkError?) {
static func getCategory(auth: String, named categoryName: String) async -> ([RecipeStub]?, NetworkError?) {
let request = ApiRequest(
path: basePath + "/category/\(categoryName)",
method: .GET,
@@ -157,7 +166,8 @@ class CookbookApiV1: CookbookApi {
let (data, error) = await request.send()
guard let data = data else { return (nil, error) }
return (JSONDecoder.safeDecode(data), nil)
let recipes: [CookbookApiRecipeV1]? = JSONDecoder.safeDecode(data)
return (recipes?.map({ recipe in recipe.toRecipeStub() }), nil)
}
static func renameCategory(auth: String, named categoryName: String, newName: String) async -> (NetworkError?) {
@@ -186,7 +196,7 @@ class CookbookApiV1: CookbookApi {
return (JSONDecoder.safeDecode(data), nil)
}
static func getRecipesTagged(auth: String, keyword: String) async -> ([Recipe]?, NetworkError?) {
static func getRecipesTagged(auth: String, keyword: String) async -> ([RecipeStub]?, NetworkError?) {
let request = ApiRequest(
path: basePath + "/tags/\(keyword)",
method: .GET,
@@ -196,7 +206,8 @@ class CookbookApiV1: CookbookApi {
let (data, error) = await request.send()
guard let data = data else { return (nil, error) }
return (JSONDecoder.safeDecode(data), nil)
let recipes: [CookbookApiRecipeV1]? = JSONDecoder.safeDecode(data)
return (recipes?.map({ recipe in recipe.toRecipeStub() }), nil)
}
static func getApiVersion(auth: String) async -> (NetworkError?) {
@@ -215,3 +226,4 @@ class CookbookApiV1: CookbookApi {
return .none
}
}