Skip to content

Commit

Permalink
feat(core): add post sorting functionality in repository
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael-Liendo committed Sep 24, 2024
1 parent 11f3761 commit 3410842
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
23 changes: 21 additions & 2 deletions crates/core/src/post/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use std::str::FromStr;
use chrono::{DateTime, Utc};
use pxid::Pxid;
use sea_orm::{
ActiveModelTrait, ColumnTrait, EntityTrait, PaginatorTrait, QueryFilter, QuerySelect, Set,
TransactionTrait,
ActiveModelTrait, ColumnTrait, EntityTrait, PaginatorTrait, QueryFilter, QueryOrder,
QuerySelect, Set, TransactionTrait,
};
use serde::{Deserialize, Serialize};

Expand All @@ -13,6 +13,12 @@ use crate::shared::database::Database;
use crate::shared::pagination::Pagination;
use crate::shared::query_set::QuerySet;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PostSortBy {
CreatedAtAsc,
CreatedAtDesc,
}

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
pub struct PostRecord {
pub id: String,
Expand Down Expand Up @@ -43,6 +49,7 @@ pub struct PostRepository {
#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq, Eq)]
pub struct PostFilter {
pub author_id: Option<Pxid>,
pub sort_by: Option<PostSortBy>,
}

impl PostRepository {
Expand Down Expand Up @@ -77,6 +84,18 @@ impl PostRepository {
if let Some(id) = filter.author_id {
query = query.filter(entity::post::Column::AuthorId.eq(id.to_string()));
}

match filter.sort_by {
Some(PostSortBy::CreatedAtAsc) => {
query = query.order_by_asc(entity::post::Column::CreatedAt);
}
Some(PostSortBy::CreatedAtDesc) => {
query = query.order_by_desc(entity::post::Column::CreatedAt);
}
None => {
query = query.order_by_desc(entity::post::Column::CreatedAt);
}
}
}

let count = query
Expand Down
1 change: 1 addition & 0 deletions crates/server/src/graphql/modules/user/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ impl User {
Some(pagination),
Some(PostFilter {
author_id: Some(self.id),
sort_by: None,
}),
)
.await?;
Expand Down

0 comments on commit 3410842

Please sign in to comment.