Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): add post sorting functionality in repository #232

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading