【问题标题】:Rust Diesel one to one relationshipRust Diesel 一对一关系
【发布时间】:2022-07-14 04:08:32
【问题描述】:

嘿,我正在创建一个 api 来返回用户及其个人资料

我有两个来自两个独立数据库、用户和个人资料的表

fn handle(
        &mut self,
        query_strings: SearchUsersQueryStrings,
        _: &mut SyncContext<Self>,
    ) -> Self::Result {
        let gateway_conn: &PgConnection = &self.1.get().unwrap();
        let own_conn: &PgConnection = &self.0.get().unwrap();

        let pattern = format!("%{}%", query_strings.username);

        let found_users = users
            .filter(username.like(pattern))
            .get_results::<User>(gateway_conn)?;

        let profile = Profile::belonging_to(&found_users)
            .load::<Profile>(own_conn)?
            .grouped_by(&found_users);
 

        let data = found_users.into_iter().zip(profile).collect();

        Ok(data)
    }

这里不好的是数据类型结果是,解析起来太难看了

[
    [
        {
            "id": 22,
            "username": "412212512",
            "email": "1231q1222122@gmail.com",
            "avatar": null,
            "created_at": "2022-02-21T09:31:29.855851"
        },
        [
            {
                "id": 3,
                "user_id": 22,
                "status": "qqq",
                "description": "xxx",
                "created_at": "2022-03-07T22:53:17.491532",
                "updated_at": null,
                "deleted_at": null
            }
        ]
    ],
    [
        {
            "id": 25,
            "username": "1412drew212512",
            "email": "1231q11srew222122@gmail.com",
            "avatar": null,
            "created_at": "2022-02-21T10:37:04.588795"
        },
        []
    ],
]

但我想要这样的东西:

[
            {
                "id": 22,
                "username": "1412212512",
                "email": "1231q1222122@gmail.com",
                "avatar": null,
                "created_at": "2022-02-21T09:31:29.855851",
                "profile": {
                    "id": 3,
                    "user_id": 22,
                    "status": "qqq",
                    "description": "xxx",
                    "created_at": "2022-03-07T22:53:17.491532",
                    "updated_at": null,
                    "deleted_at": null
                
                },
           ....
           
]

如果我在配置文件查询中使用load func,它将返回Vec&lt;Profile&gt;,但每个用户都有一条配置文件记录,如果我不使用load 而使用first,那么我就不能使用grouped_by在它上面

【问题讨论】:

    标签: rust relationship actix-web rust-diesel


    【解决方案1】:

    像这样创建一个名为 UserAPIstruct

    pub struct UserAPI {
        #[serde(flatten)]
        pub user: User,
    
        pub profile: Profile,
    }
    

    然后在压缩数据后这样做:

    fn handle(
            &mut self,
            query_strings: SearchUsersQueryStrings,
            _: &mut SyncContext<Self>,
        ) -> Self::Result {
            let gateway_conn: &PgConnection = &self.1.get().unwrap();
            let own_conn: &PgConnection = &self.0.get().unwrap();
    
            let pattern = format!("%{}%", query_strings.username);
    
            let found_users = users
                .filter(username.like(pattern))
                .get_results::<User>(gateway_conn)?;
    
            let profile = Profile::belonging_to(&found_users)
                .load::<Profile>(own_conn)?
                .grouped_by(&found_users);
     
    
            let data = found_users.into_iter().zip(profile).collect();
    
            let users_with_profile: Vec<UserAPI> = data
                    .into_iter()
                    .map(|(user, profile)| {
        
                        UserAPI {
                            user,
                            profile,
                        }
                    })
                    .collect();
    
            Ok(users_with_profile)
        }
    

    【讨论】:

      猜你喜欢
      • 2020-09-26
      • 2021-12-24
      • 2020-09-06
      • 2018-09-27
      • 2011-03-30
      • 2018-10-03
      • 2021-11-24
      • 2013-02-12
      • 2015-08-30
      相关资源
      最近更新 更多