Files
suixinkan_uikit/suixinkan/Features/TravelAlbum/API/TravelAlbumAPI.swift

164 lines
5.6 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// TravelAlbumAPI.swift
// suixinkan
//
import Foundation
@MainActor
///
protocol TravelAlbumServing {
///
func availableOrders() async throws -> [TravelAlbumAvailableOrder]
///
func create(_ request: TravelAlbumCreateRequest) async throws -> TravelAlbumCreateResponse
///
func list(page: Int, pageSize: Int) async throws -> TravelAlbumListResponse<TravelAlbum>
///
func info(id: Int) async throws -> TravelAlbum
///
func materialList(
userEquityTravelId: Int,
page: Int,
pageSize: Int,
orderBy: Int,
isPurchased: Int?
) async throws -> TravelAlbumListResponse<TravelAlbumMaterial>
///
func uploadMaterial(_ request: TravelAlbumUploadMaterialRequest) async throws -> TravelAlbumMaterial
/// ID
func materialClientPhotoIds(userEquityTravelId: Int) async throws -> TravelAlbumMaterialClientPhotoIDsResponse
///
func deleteAlbum(id: Int) async throws
///
func deleteMaterial(id: Int) async throws
///
func mpCode(id: Int) async throws -> TravelAlbumMpCodeResponse
}
@MainActor
/// API Android `photog/travel-album`
final class TravelAlbumAPI: TravelAlbumServing {
private let client: APIClient
private let basePath = "/api/yf-handset-app/photog/travel-album"
/// API
init(client: APIClient) {
self.client = client
}
///
func availableOrders() async throws -> [TravelAlbumAvailableOrder] {
try await client.send(APIRequest(method: .get, path: "\(basePath)/available-order"))
}
///
func create(_ request: TravelAlbumCreateRequest) async throws -> TravelAlbumCreateResponse {
try await client.send(APIRequest(method: .post, path: "\(basePath)/create", body: request))
}
///
func list(page: Int = 1, pageSize: Int = 20) async throws -> TravelAlbumListResponse<TravelAlbum> {
try await client.send(
APIRequest(
method: .get,
path: "\(basePath)/list",
queryItems: [
URLQueryItem(name: "page", value: String(max(page, 1))),
URLQueryItem(name: "page_size", value: String(max(pageSize, 1))),
]
)
)
}
///
func info(id: Int) async throws -> TravelAlbum {
try await client.send(
APIRequest(
method: .get,
path: "\(basePath)/info",
queryItems: [URLQueryItem(name: "id", value: String(id))]
)
)
}
///
func materialList(
userEquityTravelId: Int,
page: Int = 1,
pageSize: Int = 20,
orderBy: Int = 2,
isPurchased: Int? = nil
) async throws -> TravelAlbumListResponse<TravelAlbumMaterial> {
var items = [
URLQueryItem(name: "user_equity_travel_id", value: String(userEquityTravelId)),
URLQueryItem(name: "page", value: String(max(page, 1))),
URLQueryItem(name: "page_size", value: String(max(pageSize, 1))),
URLQueryItem(name: "order_by", value: String(orderBy)),
]
if let isPurchased {
items.append(URLQueryItem(name: "is_purchased", value: String(isPurchased)))
}
return try await client.send(
APIRequest(method: .get, path: "\(basePath)/material-list", queryItems: items)
)
}
///
func uploadMaterial(_ request: TravelAlbumUploadMaterialRequest) async throws -> TravelAlbumMaterial {
try await client.send(APIRequest(method: .post, path: "\(basePath)/upload-material", body: request))
}
/// ID
func materialClientPhotoIds(userEquityTravelId: Int) async throws -> TravelAlbumMaterialClientPhotoIDsResponse {
try await client.send(
APIRequest(
method: .get,
path: "\(basePath)/material-client-photo-ids",
queryItems: [
URLQueryItem(name: "user_equity_travel_id", value: String(userEquityTravelId)),
]
)
)
}
///
func deleteAlbum(id: Int) async throws {
let _: EmptyPayload = try await client.send(
APIRequest(method: .post, path: "\(basePath)/delete", body: TravelAlbumIDRequest(id: id))
)
}
///
func deleteMaterial(id: Int) async throws {
let _: EmptyPayload = try await client.send(
APIRequest(method: .post, path: "\(basePath)/delete-material", body: TravelAlbumIDRequest(id: id))
)
}
///
func mpCode(id: Int) async throws -> TravelAlbumMpCodeResponse {
try await client.send(
APIRequest(
method: .get,
path: "\(basePath)/mp-code",
queryItems: [URLQueryItem(name: "id", value: String(id))]
)
)
}
}
/// ID
private struct TravelAlbumIDRequest: Encodable, Sendable {
let id: Int
}