-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod errors; | ||
pub mod user; | ||
pub mod video; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crate::controllers::errors::HandlerError; | ||
use crate::controllers::user::UserResponse; | ||
use crate::models::video; | ||
use crate::AppState; | ||
use axum::extract::State; | ||
use axum::Json; | ||
use chrono::{DateTime, Utc}; | ||
use entity::id::Id; | ||
use entity::video::Visibility; | ||
use serde::Serialize; | ||
|
||
#[derive(Serialize)] | ||
pub struct VideoResponse { | ||
pub id: Id, | ||
pub creator: UserResponse, | ||
pub duration_secs: u32, | ||
pub uploaded_at: DateTime<Utc>, | ||
pub name: String, | ||
pub description: String, | ||
pub visibility: Visibility, | ||
pub views: u64, | ||
pub likes: u64, | ||
pub dislikes: u64, | ||
} | ||
|
||
pub async fn get_videos( | ||
State(state): State<AppState>, | ||
) -> Result<Json<Vec<VideoResponse>>, HandlerError> { | ||
Ok(Json( | ||
video::find_all(&state.connection) | ||
.await? | ||
.into_iter() | ||
.map(|(video, user)| VideoResponse { | ||
id: video.id, | ||
creator: user.into(), | ||
duration_secs: video.duration_secs, | ||
uploaded_at: video.uploaded_at, | ||
name: video.name, | ||
description: video.description, | ||
visibility: video.visibility, | ||
views: video.views, | ||
likes: 0, | ||
dislikes: 0, | ||
}) | ||
.collect(), | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod base; | ||
pub mod user; | ||
pub mod video; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
use crate::models::user::User; | ||
use chrono::{DateTime, Utc}; | ||
use entity::id::Id; | ||
use entity::video::Visibility; | ||
use entity::{user, video, view}; | ||
use sea_orm::{ | ||
ColumnTrait, DatabaseConnection, DbErr, EntityTrait, FromQueryResult, JoinType, QuerySelect, | ||
}; | ||
|
||
#[derive(FromQueryResult)] | ||
pub struct VideoResult { | ||
pub id: Id, | ||
pub duration_secs: u32, | ||
pub uploaded_at: DateTime<Utc>, | ||
pub name: String, | ||
pub description: String, | ||
pub visibility: Visibility, | ||
pub views: u64, | ||
pub likes: u64, | ||
pub dislikes: u64, | ||
} | ||
|
||
pub async fn find_all(connection: &DatabaseConnection) -> Result<Vec<(VideoResult, User)>, DbErr> { | ||
video::Entity::find() | ||
.find_also_related(user::Entity) | ||
.join_rev( | ||
JoinType::InnerJoin, | ||
view::Entity::belongs_to(video::Entity) | ||
.from(view::Column::VideoId) | ||
.to(video::Column::Id) | ||
.into(), | ||
) | ||
.column_as(view::Column::Id.count(), "views") | ||
.group_by(video::Column::Id) | ||
.group_by(user::Column::Id) | ||
.into_model::<VideoResult, user::Model>() | ||
.all(connection) | ||
.await? | ||
.into_iter() | ||
.map(|(video, creator)| { | ||
Ok(( | ||
video, | ||
creator | ||
.map(Into::into) | ||
.ok_or(DbErr::Custom("video creator was empty".into()))?, | ||
)) | ||
}) | ||
.collect() | ||
} |