Skip to content

Commit

Permalink
feat(be): Added recently played calls
Browse files Browse the repository at this point in the history
  • Loading branch information
MendyBerger committed Jun 17, 2024
1 parent cd1142a commit 4325591
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
26 changes: 26 additions & 0 deletions backend/api/src/db/jig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,32 @@ pub async fn list_liked(
Ok(rows.into_iter().map(|row| JigId(row.jig_id)).collect())
}

pub async fn list_played(
db: &PgPool,
user_id: UserId,
page: u32,
page_limit: u32,
) -> sqlx::Result<Vec<JigId>> {
let rows = sqlx::query!(
r#"
select jig_id
from jig_play
where user_id = $1
order by at desc
offset $2
limit $3
"#,
user_id.0,
(page * page_limit) as i32,
page_limit as i32,
)
.fetch_all(db)
.await?;

Ok(rows.into_iter().map(|row| JigId(row.jig_id)).collect())
}

pub async fn featured(db: &PgPool) -> sqlx::Result<Vec<JigId>> {
let rows = sqlx::query!(
r#"
Expand Down
28 changes: 26 additions & 2 deletions backend/api/src/http/endpoints/jig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use actix_web::{
use futures::try_join;
use ji_core::settings::RuntimeSettings;
use shared::domain::{
jig::{JigFeaturedResponse, JigTrendingResponse, ListLikedResponse},
jig::{JigFeaturedResponse, JigTrendingResponse, ListLikedResponse, ListPlayedResponse},
user::UserScope,
};
use shared::{
Expand Down Expand Up @@ -468,7 +468,7 @@ async fn unlike(
async fn list_liked(
db: Data<PgPool>,
claims: TokenUser,
query: Option<Query<<jig::Browse as ApiEndpoint>::Req>>,
query: Option<Query<<jig::ListLiked as ApiEndpoint>::Req>>,
) -> Result<Json<<jig::ListLiked as ApiEndpoint>::Res>, error::Server> {
let user_id = claims.user_id();
let query = query.map_or_else(Default::default, Query::into_inner);
Expand All @@ -484,6 +484,26 @@ async fn list_liked(
Ok(Json(ListLikedResponse { jigs }))
}

/// Get users played jigs
async fn list_played(
db: Data<PgPool>,
claims: TokenUser,
query: Option<Query<<jig::ListPlayed as ApiEndpoint>::Req>>,
) -> Result<Json<<jig::ListPlayed as ApiEndpoint>::Res>, error::Server> {
let user_id = claims.user_id();
let query = query.map_or_else(Default::default, Query::into_inner);

let page_limit = page_limit(query.page_limit).await?;

let ids = db::jig::list_played(&*db, user_id, query.page.unwrap_or(0), page_limit).await?;

let jigs = db::jig::get_by_ids(&db, &ids, DraftOrLive::Live, Some(user_id))
.await
.into_anyhow()?;

Ok(Json(ListPlayedResponse { jigs }))
}

/// Add a play to a jig
async fn play(
db: Data<PgPool>,
Expand Down Expand Up @@ -635,6 +655,10 @@ pub fn configure(cfg: &mut ServiceConfig) {
<jig::ListLiked as ApiEndpoint>::Path::PATH,
jig::ListLiked::METHOD.route().to(list_liked),
)
.route(
<jig::ListPlayed as ApiEndpoint>::Path::PATH,
jig::ListPlayed::METHOD.route().to(list_played),
)
.route(
<jig::Featured as ApiEndpoint>::Path::PATH,
jig::Featured::METHOD.route().to(featured),
Expand Down
12 changes: 11 additions & 1 deletion shared/rust/src/api/endpoints/jig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
JigSearchPath, JigSearchQuery, JigSearchResponse, JigTransferAdminPath,
JigTrendingPath, JigTrendingResponse, JigUnlikePath, JigUpdateAdminDataRequest,
JigUpdateDraftDataPath, JigUpdateDraftDataRequest, ListLikedPath, ListLikedRequest,
ListLikedResponse,
ListLikedResponse, ListPlayedPath, ListPlayedRequest, ListPlayedResponse,
},
CreateResponse,
},
Expand Down Expand Up @@ -165,6 +165,16 @@ impl ApiEndpoint for ListLiked {
const METHOD: Method = Method::Get;
}

/// List user's played JIGs.
pub struct ListPlayed;
impl ApiEndpoint for ListPlayed {
type Req = ListPlayedRequest;
type Res = ListPlayedResponse;
type Path = ListPlayedPath;
type Err = EmptyError;
const METHOD: Method = Method::Get;
}

/// Featured JIGs.
pub struct Featured;
impl ApiEndpoint for Featured {
Expand Down
27 changes: 26 additions & 1 deletion shared/rust/src/domain/jig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,7 +764,7 @@ pub struct JigTrendingResponse {
make_path_parts!(ListLikedPath => "/v1/jig/likes");

/// Response for request for list of liked jigs.
#[derive(Serialize, Deserialize, Clone, Debug)]
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct ListLikedRequest {
/// The page number of the jigs to get.
Expand All @@ -778,6 +778,31 @@ pub struct ListLikedRequest {
pub page_limit: Option<u32>,
}

make_path_parts!(ListPlayedPath => "/v1/jig/likes");

/// Response for request for list of played jigs.
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
#[serde(rename_all = "camelCase")]
pub struct ListPlayedRequest {
/// The page number of the jigs to get.
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub page: Option<u32>,

/// The hits per page to be returned
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
pub page_limit: Option<u32>,
}

/// Response for request for list of played jigs.
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ListPlayedResponse {
/// the jigs returned.
pub jigs: Vec<JigResponse>,
}

make_path_parts!(JigFeaturedPath => "/v1/jig/featured");

/// Response for request for featured.
Expand Down

0 comments on commit 4325591

Please sign in to comment.