Skip to content

Commit

Permalink
feat(be): Added liked calls for playlist and resource
Browse files Browse the repository at this point in the history
  • Loading branch information
MendyBerger committed Jun 26, 2024
1 parent aae1c32 commit 80c3247
Show file tree
Hide file tree
Showing 13 changed files with 367 additions and 18 deletions.
104 changes: 104 additions & 0 deletions backend/api/sqlx-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -3266,6 +3266,26 @@
},
"query": "\n update resource_curation_data\n set affiliations = $2\n where resource_id = $1 and $2 is distinct from affiliations\n "
},
"354551f41670236438723df78615a1dc227c51567c58da594d5c5b1d9d479d35": {
"describe": {
"columns": [
{
"name": "count!: i64",
"ordinal": 0,
"type_info": "Int8"
}
],
"nullable": [
null
],
"parameters": {
"Left": [
"Uuid"
]
}
},
"query": "\n select count(playlist_id) as \"count!: i64\"\n from playlist_like\n where user_id = $1\n "
},
"354efe9ea705cda595830f09d4640485ba06d16430389192a75f3b153f39d7f5": {
"describe": {
"columns": [],
Expand Down Expand Up @@ -3637,6 +3657,26 @@
},
"query": "\nupdate course_data_module\nset\n index = case when index = $2 then $3 else index + 1 end,\n updated_at = now()\nwhere course_data_id = $1 and index between $3 and $2\n"
},
"3a007a80e7e68375ba5214665427f07766a63456a087416a1ea3d31df9ee3f4e": {
"describe": {
"columns": [
{
"name": "count!: i64",
"ordinal": 0,
"type_info": "Int8"
}
],
"nullable": [
null
],
"parameters": {
"Left": [
"Uuid"
]
}
},
"query": "\n select count(resource_id) as \"count!: i64\"\n from resource_like\n where user_id = $1\n "
},
"3a4bccd79e5f29fff7f31c82be3d3cb1f6f7a068503bd0b3104182f8f47ef0a9": {
"describe": {
"columns": [
Expand Down Expand Up @@ -6707,6 +6747,28 @@
},
"query": "\n insert into jig_report(jig_id, report_type)\n values ($1, $2)\n returning id as \"id!: ReportId\"\n "
},
"686590f948fdbdb9c46f9bfb96684d7473fff17d578d42227c4ec372b51a017e": {
"describe": {
"columns": [
{
"name": "resource_id",
"ordinal": 0,
"type_info": "Uuid"
}
],
"nullable": [
false
],
"parameters": {
"Left": [
"Uuid",
"Int8",
"Int8"
]
}
},
"query": "\n select resource_id\n from resource_like\n where user_id = $1\n order by created_at desc\n offset $2\n limit $3\n \n "
},
"688ca554263ad9bee35f1fe5d4c99682f7d47c847249d304422858b265bce880": {
"describe": {
"columns": [
Expand Down Expand Up @@ -8166,6 +8228,28 @@
},
"query": "delete from user_account where account_id = $1"
},
"783a8e17a4d48a5920ebc70ecd59004eeba69f873654561edb6c9721d1a70dac": {
"describe": {
"columns": [
{
"name": "playlist_id",
"ordinal": 0,
"type_info": "Uuid"
}
],
"nullable": [
false
],
"parameters": {
"Left": [
"Uuid",
"Int8",
"Int8"
]
}
},
"query": "\n select playlist_id\n from playlist_like\n where user_id = $1\n order by created_at desc\n offset $2\n limit $3\n \n "
},
"78b48e52d416cf94b8daaf7ce4b6dcbb7bae4ba6c5f85bc4121c626f37c4df4e": {
"describe": {
"columns": [],
Expand Down Expand Up @@ -14448,6 +14532,26 @@
},
"query": "\nselect published_at as \"published_at?\"\nfrom course\nwhere id = $1\n "
},
"d07feb752f409708df595d2507f3fbcd52d8e96649fa0dc0174f57300d2fac08": {
"describe": {
"columns": [
{
"name": "count!: i64",
"ordinal": 0,
"type_info": "Int8"
}
],
"nullable": [
null
],
"parameters": {
"Left": [
"Uuid"
]
}
},
"query": "\n select count(jig_id) as \"count!: i64\"\n from jig_like\n where user_id = $1\n "
},
"d1095b2f79187d6c7b251d3f39a7470e5e97b69c4424b638aec44fcb0f1d3deb": {
"describe": {
"columns": [
Expand Down
4 changes: 2 additions & 2 deletions backend/api/src/algolia.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2191,7 +2191,7 @@ impl Client {
page_limit: u32,
blocked: Option<bool>,
is_rated: Option<bool>,
) -> anyhow::Result<Option<(Vec<Uuid>, u32, u64)>> {
) -> anyhow::Result<Option<(Vec<ResourceId>, u32, u64)>> {
let mut and_filters = algolia::filter::AndFilter { filters: vec![] };

if let Some(author_id) = author_id {
Expand Down Expand Up @@ -2327,7 +2327,7 @@ impl Client {
translated_keywords: Option<String>,
privacy_level: &[PrivacyLevel],
page_limit: u32,
) -> anyhow::Result<Option<(Vec<Uuid>, u32, u64)>> {
) -> anyhow::Result<Option<(Vec<PlaylistId>, u32, u64)>> {
let mut and_filters = algolia::filter::AndFilter { filters: vec![] };

if let Some(author_id) = author_id {
Expand Down
15 changes: 15 additions & 0 deletions backend/api/src/db/jig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1724,6 +1724,21 @@ pub async fn list_liked(
Ok(rows.into_iter().map(|row| JigId(row.jig_id)).collect())
}

pub async fn liked_count(db: &PgPool, user_id: UserId) -> sqlx::Result<u64> {
let res = sqlx::query!(
r#"
select count(jig_id) as "count!: i64"
from jig_like
where user_id = $1
"#,
user_id.0,
)
.fetch_one(db)
.await?;

Ok(res.count as u64)
}

pub async fn list_played(
db: &PgPool,
user_id: UserId,
Expand Down
48 changes: 46 additions & 2 deletions backend/api/src/db/playlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ from playlist_data

pub async fn get_by_ids(
db: &PgPool,
ids: &[Uuid],
ids: &[PlaylistId],
draft_or_live: DraftOrLive,
user_id: Option<UserId>,
) -> sqlx::Result<Vec<PlaylistResponse>> {
Expand Down Expand Up @@ -308,7 +308,7 @@ inner join unnest($1::uuid[])
inner join playlist_admin_data "admin" on admin.playlist_id = playlist.id
order by ord asc
"#,
ids,
&ids.iter().map(|i| i.0).collect::<Vec<Uuid>>(),
user_id.map(|x| x.0)
)
.fetch_all(&mut txn)
Expand Down Expand Up @@ -1170,6 +1170,50 @@ select exists (
Ok(exists)
}

pub async fn list_liked(
db: &PgPool,
user_id: UserId,
page: u32,
page_limit: u32,
) -> sqlx::Result<Vec<PlaylistId>> {
let rows = sqlx::query!(
r#"
select playlist_id
from playlist_like
where user_id = $1
order by created_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| PlaylistId(row.playlist_id))
.collect())
}

pub async fn liked_count(db: &PgPool, user_id: UserId) -> sqlx::Result<u64> {
let res = sqlx::query!(
r#"
select count(playlist_id) as "count!: i64"
from playlist_like
where user_id = $1
"#,
user_id.0,
)
.fetch_one(db)
.await?;

Ok(res.count as u64)
}

pub async fn playlist_like(db: &PgPool, user_id: UserId, id: PlaylistId) -> anyhow::Result<()> {
let mut txn = db.begin().await?;

Expand Down
48 changes: 46 additions & 2 deletions backend/api/src/db/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ from resource_data
#[instrument(skip(db))]
pub async fn get_by_ids(
db: &PgPool,
ids: &[Uuid],
ids: &[ResourceId],
draft_or_live: DraftOrLive,
user_id: Option<UserId>,
) -> sqlx::Result<Vec<ResourceResponse>> {
Expand Down Expand Up @@ -310,7 +310,7 @@ inner join unnest($1::uuid[])
inner join resource_admin_data "admin" on admin.resource_id = resource.id
order by ord asc
"#,
ids,
&ids.iter().map(|i| i.0).collect::<Vec<Uuid>>(),
user_id.map(|x| x.0)
)
.fetch_all(&mut txn)
Expand Down Expand Up @@ -1240,6 +1240,50 @@ select exists (
Ok(exists)
}

pub async fn list_liked(
db: &PgPool,
user_id: UserId,
page: u32,
page_limit: u32,
) -> sqlx::Result<Vec<ResourceId>> {
let rows = sqlx::query!(
r#"
select resource_id
from resource_like
where user_id = $1
order by created_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| ResourceId(row.resource_id))
.collect())
}

pub async fn liked_count(db: &PgPool, user_id: UserId) -> sqlx::Result<u64> {
let res = sqlx::query!(
r#"
select count(resource_id) as "count!: i64"
from resource_like
where user_id = $1
"#,
user_id.0,
)
.fetch_one(db)
.await?;

Ok(res.count as u64)
}

pub async fn authz(
db: &PgPool,
user_id: UserId,
Expand Down
7 changes: 6 additions & 1 deletion backend/api/src/http/endpoints/jig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,12 @@ async fn list_liked(
.await
.into_anyhow()?;

Ok(Json(ListLikedResponse { jigs }))
let total_jig_count = db::jig::liked_count(&*db, user_id).await?;

Ok(Json(ListLikedResponse {
jigs,
total_jig_count,
}))
}

/// Get users played jigs
Expand Down
31 changes: 30 additions & 1 deletion backend/api/src/http/endpoints/playlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use actix_web::{
};
use futures::try_join;
use ji_core::settings::RuntimeSettings;
use shared::domain::user::UserScope;
use shared::domain::{playlist::ListLikedResponse, user::UserScope};
use shared::{
api::{endpoints::playlist, ApiEndpoint, PathParts},
domain::{
Expand Down Expand Up @@ -441,6 +441,31 @@ async fn unlike(
Ok(HttpResponse::NoContent().finish())
}

/// Get users liked playlist
async fn list_liked(
db: Data<PgPool>,
claims: TokenUser,
query: Option<Query<<playlist::ListLiked as ApiEndpoint>::Req>>,
) -> Result<Json<<playlist::ListLiked 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::playlist::list_liked(&*db, user_id, query.page.unwrap_or(0), page_limit).await?;

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

let total_playlist_count = db::playlist::liked_count(&*db, user_id).await?;

Ok(Json(ListLikedResponse {
playlists,
total_playlist_count,
}))
}

/// Add a play to a playlist
async fn view(
db: Data<PgPool>,
Expand Down Expand Up @@ -522,6 +547,10 @@ pub fn configure(cfg: &mut ServiceConfig) {
<playlist::Unlike as ApiEndpoint>::Path::PATH,
playlist::Unlike::METHOD.route().to(unlike),
)
.route(
<playlist::ListLiked as ApiEndpoint>::Path::PATH,
playlist::ListLiked::METHOD.route().to(list_liked),
)
.route(
<playlist::PlaylistAdminDataUpdate as ApiEndpoint>::Path::PATH,
playlist::PlaylistAdminDataUpdate::METHOD
Expand Down
Loading

0 comments on commit 80c3247

Please sign in to comment.