From 48f6e910af472bde72c6b2f307d5f3114a8d45d1 Mon Sep 17 00:00:00 2001 From: makinosp Date: Sun, 6 Oct 2024 20:12:46 +0900 Subject: [PATCH] refact: reimplement the favorites group update method --- .../Models/Favorite/FavoriteModel.swift | 3 ++- .../Protocols/FavoriteServiceProtocol.swift | 7 +++--- Sources/VRCKit/Services/FavoriteService.swift | 22 ++++++++++++------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Sources/VRCKit/Models/Favorite/FavoriteModel.swift b/Sources/VRCKit/Models/Favorite/FavoriteModel.swift index 650fb1b..4dad4aa 100644 --- a/Sources/VRCKit/Models/Favorite/FavoriteModel.swift +++ b/Sources/VRCKit/Models/Favorite/FavoriteModel.swift @@ -55,8 +55,9 @@ struct RequestToAddFavorite: Codable, Sendable { let tags: [String] } +@MemberwiseInit struct RequestToUpdateFavoriteGroup: Codable, Sendable { let displayName: String? let visibility: FavoriteGroup.Visibility? - let tags: [String] + @Init(default: []) let tags: [String] } diff --git a/Sources/VRCKit/Protocols/FavoriteServiceProtocol.swift b/Sources/VRCKit/Protocols/FavoriteServiceProtocol.swift index 9d13e83..8b11c6d 100644 --- a/Sources/VRCKit/Protocols/FavoriteServiceProtocol.swift +++ b/Sources/VRCKit/Protocols/FavoriteServiceProtocol.swift @@ -6,16 +6,15 @@ // public protocol FavoriteServiceProtocol: Sendable { + typealias FavoriteGroupParams = (type: FavoriteType, name: String, userId: String) func listFavoriteGroups() async throws -> [FavoriteGroup] func listFavorites(n: Int, type: FavoriteType, tag: String?) async throws -> [Favorite] func fetchFavoriteGroupDetails(favoriteGroups: [FavoriteGroup]) async throws -> [FavoriteDetail] func addFavorite(type: FavoriteType, favoriteId: String, tag: String) async throws -> Favorite func updateFavoriteGroup( - type: FavoriteType, + params: FavoriteGroupParams, displayName: String, - visibility: FavoriteGroup.Visibility, - userId: String, - tag: String + visibility: FavoriteGroup.Visibility ) async throws -> SuccessResponse func removeFavorite(favoriteId: String) async throws -> SuccessResponse } diff --git a/Sources/VRCKit/Services/FavoriteService.swift b/Sources/VRCKit/Services/FavoriteService.swift index 39ca280..2728e38 100644 --- a/Sources/VRCKit/Services/FavoriteService.swift +++ b/Sources/VRCKit/Services/FavoriteService.swift @@ -92,18 +92,24 @@ public final actor FavoriteService: APIService, FavoriteServiceProtocol { return try await Serializer.shared.decode(response.data) } + /// Updates a favorite group with the given parameters, display name, and visibility. + /// - Parameters: + /// - params: A tuple containing the favorite group's details: + /// - type: The type of the favorite group, defined by `FavoriteType`. + /// - name: The name of the favorite group. + /// - userId: The ID of the user associated with the favorite group. + /// - displayName: The new display name to update the favorite group with. + /// - visibility: The new visibility setting for the favorite group. + /// - Returns: A `SuccessResponse` if the update is successful. public func updateFavoriteGroup( - type: FavoriteType, + params: FavoriteGroupParams, displayName: String, - visibility: FavoriteGroup.Visibility, - userId: String, - tag: String + visibility: FavoriteGroup.Visibility ) async throws -> SuccessResponse { - let pathParams = ["favorite", "groups", type.rawValue, displayName, userId] + let pathParams = ["favorite", "groups", params.type.rawValue, params.name, params.userId] let path = pathParams.joined(separator: "/") - let requestData = try await Serializer.shared.encode( - RequestToUpdateFavoriteGroup(displayName: displayName, visibility: visibility, tags: [tag]) - ) + let body = RequestToUpdateFavoriteGroup(displayName: displayName, visibility: visibility) + let requestData = try await Serializer.shared.encode(body) let response = try await client.request(path: path, method: .put, body: requestData) return try await Serializer.shared.decode(response.data) }