You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
fast-insiders/server/src/repo/user.rs

52 lines
1.2 KiB

use crate::model::user::ActiveModel;
use sea_orm::{
ActiveModelTrait, ActiveValue, ConnectionTrait, DbErr, DeriveIntoActiveModel, EntityTrait,
IntoActiveModel,
};
use serde::{Deserialize, Serialize};
use crate::model;
#[derive(Debug, PartialEq, Clone, DeriveIntoActiveModel, Serialize, Deserialize)]
pub struct NewUser {
pub email: Option<String>,
pub name: String,
pub password: String,
}
impl NewUser {
pub async fn create<C>(&self, db: &C) -> Result<model::user::Model, DbErr>
where
C: ConnectionTrait,
{
let res = self.clone().into_active_model().insert(db).await?;
Ok(res)
}
}
pub async fn follow_company<C>(db: &C, user_id: i32, company_id: i32) -> Result<(), DbErr>
where
C: ConnectionTrait,
{
let relation = model::user_company::ActiveModel {
user_id: ActiveValue::Set(user_id),
company_id: ActiveValue::Set(company_id),
};
relation.insert(db).await?;
Ok(())
}
pub async fn unfollow_company<C>(db: &C, user_id: i32, company_id: i32) -> Result<(), DbErr>
where
C: ConnectionTrait,
{
model::user_company::Entity::delete_by_id((user_id, company_id))
.exec(db)
.await?;
Ok(())
}