154 lines
4.3 KiB
Rust
154 lines
4.3 KiB
Rust
use model::api::OrderAddressInput;
|
|
use model::{AccessTokenString, AddressId, PaymentMethod, RefreshTokenString};
|
|
use seed::fetch::{Header, Method, Request};
|
|
|
|
use crate::api::perform;
|
|
use crate::NetRes;
|
|
|
|
pub async fn config() -> NetRes<model::api::Config> {
|
|
perform(Request::new("/config").method(Method::Get)).await
|
|
}
|
|
|
|
pub async fn fetch_products() -> NetRes<model::api::Products> {
|
|
perform(Request::new("/api/v1/products").method(Method::Get)).await
|
|
}
|
|
|
|
pub async fn fetch_product(product_id: model::ProductId) -> NetRes<model::api::Product> {
|
|
perform(Request::new(format!("/api/v1/product/{}", product_id)).method(Method::Get)).await
|
|
}
|
|
|
|
pub async fn fetch_me(access_token: AccessTokenString) -> NetRes<model::api::Account> {
|
|
perform(
|
|
Request::new("/api/v1/me")
|
|
.header(Header::bearer(access_token.as_str()))
|
|
.method(Method::Get),
|
|
)
|
|
.await
|
|
}
|
|
|
|
pub async fn sign_in(input: model::api::SignInInput) -> NetRes<model::api::SessionOutput> {
|
|
perform(
|
|
Request::new("/api/v1/sign-in")
|
|
.method(Method::Post)
|
|
.json(&input)
|
|
.map_err(crate::api::NetRes::Http)?,
|
|
)
|
|
.await
|
|
}
|
|
|
|
pub async fn verify_token(access_token: AccessTokenString) -> NetRes<String> {
|
|
perform(
|
|
Request::new("/api/v1/token/verify")
|
|
.method(Method::Post)
|
|
.header(Header::bearer(access_token.as_str())),
|
|
)
|
|
.await
|
|
}
|
|
|
|
pub async fn refresh_token(access_token: RefreshTokenString) -> NetRes<model::api::SessionOutput> {
|
|
perform(
|
|
Request::new("/api/v1/token/refresh")
|
|
.method(Method::Post)
|
|
.header(Header::bearer(access_token.as_str())),
|
|
)
|
|
.await
|
|
}
|
|
|
|
pub async fn sign_up(input: model::api::CreateAccountInput) -> NetRes<model::api::SessionOutput> {
|
|
perform(
|
|
Request::new("/api/v1/register")
|
|
.method(Method::Post)
|
|
.json(&input)
|
|
.map_err(NetRes::Http)?,
|
|
)
|
|
.await
|
|
}
|
|
|
|
pub async fn update_cart_item(
|
|
access_token: &AccessTokenString,
|
|
product_id: model::ProductId,
|
|
quantity: model::Quantity,
|
|
quantity_unit: model::QuantityUnit,
|
|
) -> NetRes<model::api::UpdateItemOutput> {
|
|
let input = model::api::UpdateItemInput {
|
|
product_id,
|
|
quantity,
|
|
quantity_unit,
|
|
};
|
|
perform(
|
|
Request::new("/api/v1/shopping-cart-item")
|
|
.method(Method::Put)
|
|
.header(Header::bearer(access_token.as_str()))
|
|
.json(&input)
|
|
.map_err(NetRes::Http)?,
|
|
)
|
|
.await
|
|
}
|
|
|
|
pub async fn update_cart(
|
|
access_token: AccessTokenString,
|
|
items: Vec<crate::shopping_cart::Item>,
|
|
notes: String,
|
|
payment_method: Option<model::PaymentMethod>,
|
|
) -> NetRes<model::api::UpdateCartOutput> {
|
|
let input = model::api::UpdateCartInput {
|
|
notes,
|
|
items: items
|
|
.into_iter()
|
|
.map(
|
|
|crate::shopping_cart::Item {
|
|
product_id,
|
|
quantity,
|
|
quantity_unit,
|
|
}| model::api::UpdateItemInput {
|
|
product_id,
|
|
quantity,
|
|
quantity_unit,
|
|
},
|
|
)
|
|
.collect(),
|
|
payment_method,
|
|
};
|
|
perform(
|
|
Request::new("/api/v1/shopping-cart")
|
|
.method(Method::Put)
|
|
.header(Header::bearer(access_token.as_str()))
|
|
.json(&input)
|
|
.map_err(NetRes::Http)?,
|
|
)
|
|
.await
|
|
}
|
|
|
|
pub async fn place_account_order(
|
|
access_token: AccessTokenString,
|
|
email: String,
|
|
phone: String,
|
|
first_name: String,
|
|
last_name: String,
|
|
language: String,
|
|
payment_method: PaymentMethod,
|
|
currency: String,
|
|
address_id: Option<AddressId>,
|
|
) -> NetRes<model::api::PlaceOrderResult> {
|
|
let input = model::api::CreateOrderInput {
|
|
email,
|
|
phone,
|
|
first_name,
|
|
last_name,
|
|
language,
|
|
payment_method,
|
|
currency,
|
|
address: address_id
|
|
.map(OrderAddressInput::AccountAddress)
|
|
.unwrap_or(OrderAddressInput::DefaultAccountAddress),
|
|
};
|
|
perform(
|
|
Request::new("/api/v1/order")
|
|
.method(Method::Post)
|
|
.header(Header::bearer(access_token.as_str()))
|
|
.json(&input)
|
|
.map_err(NetRes::Http)?,
|
|
)
|
|
.await
|
|
}
|