zero4rs 2.0.0

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
use async_graphql::Context;
use async_graphql::Error;
use async_graphql::FieldResult;
use async_graphql::Object;

use crate::services::feeds_service::comments::models::FeedCommentVo;
use crate::services::feeds_service::comments::models::UserFeedsCommentsCountVo;

use crate::services::feeds_service::models::FetchFeeds;
use crate::services::feeds_service::models::UserFeedsThumbUpCountVo;
use crate::services::feeds_service::models::UserFeedsVo;

use crate::services::feeds_service::feeds_thumb_up_exists;
use crate::services::feeds_service::feeds_thumb_ups_count;
use crate::services::feeds_service::query_feeds_count;
use crate::services::feeds_service::select_feeds;

use crate::services::feeds_service::feed_comment_list;
use crate::services::feeds_service::feeds_comment_count;

use super::get_user_id;
use super::required_auth;

#[derive(Default)]
pub struct QueryFeeds;

#[Object(extends)]
impl QueryFeeds {
    /// user_feeds_count
    async fn user_feeds_count(&self, ctx: &Context<'_>) -> FieldResult<u64> {
        let web_ctx = ctx.data_unchecked::<actix_web::web::Data<crate::server::AppContext>>();

        match query_feeds_count(web_ctx.mongo()).await {
            Ok(count) => Ok(count),
            Err(e) => Err(Error::new(e.to_string())),
        }
    }

    /// user feeds
    async fn user_feeds_list(
        &self,
        ctx: &Context<'_>,
        input: FetchFeeds,
    ) -> FieldResult<Vec<UserFeedsVo>> {
        // let _user_details = required_auth(ctx)?;
        // log::info!("_user_details={:?}", _user_details);

        let web_ctx = ctx.data_unchecked::<actix_web::web::Data<crate::server::AppContext>>();

        match select_feeds(web_ctx.mongo(), input).await {
            Ok(result) => Ok(result),
            Err(e) => Err(Error::new(e.to_string())),
        }
    }

    /// 查询点赞数量
    async fn feeds_thumb_ups_count(
        &self,
        ctx: &Context<'_>,
        feed_id: String,
    ) -> FieldResult<UserFeedsThumbUpCountVo> {
        let web_ctx = ctx.data_unchecked::<actix_web::web::Data<crate::server::AppContext>>();

        match feeds_thumb_ups_count(web_ctx.mongo(), &feed_id).await {
            Ok(count) => Ok(UserFeedsThumbUpCountVo { count }),
            Err(e) => Err(Error::new(e.to_string())),
        }
    }

    /// 查询当前用户是否点赞过
    async fn feeds_thumb_up_exists(&self, ctx: &Context<'_>, feed_id: String) -> FieldResult<bool> {
        let user_details = required_auth(ctx)?;
        let web_ctx = ctx.data_unchecked::<actix_web::web::Data<crate::server::AppContext>>();

        match feeds_thumb_up_exists(web_ctx.mongo(), &feed_id, &user_details.user_id).await {
            Ok(result) => Ok(result),
            Err(e) => Err(Error::new(e.to_string())),
        }
    }

    /// feed comments
    async fn feed_comment_list(
        &self,
        ctx: &Context<'_>,
        feed_id: String,
    ) -> FieldResult<Vec<FeedCommentVo>> {
        let web_ctx = ctx.data_unchecked::<actix_web::web::Data<crate::server::AppContext>>();

        match feed_comment_list(web_ctx.mongo(), get_user_id(ctx), &feed_id).await {
            Ok(result) => Ok(result),
            Err(e) => Err(Error::new(e.to_string())),
        }
    }

    /// feed comments
    async fn feeds_comment_count(
        &self,
        ctx: &Context<'_>,
        feed_id: String,
    ) -> FieldResult<UserFeedsCommentsCountVo> {
        let web_ctx = ctx.data_unchecked::<actix_web::web::Data<crate::server::AppContext>>();

        match feeds_comment_count(web_ctx.mongo(), &feed_id).await {
            Ok(result) => Ok(UserFeedsCommentsCountVo { count: result }),
            Err(e) => Err(Error::new(e.to_string())),
        }
    }
}