summaryrefslogtreecommitdiffstats
path: root/src/user/settings.rs
blob: 55e658ca3365c1d5efb95b74e9415ead1430c8dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use actix::{Handler, Message};
use diesel::MysqlConnection;
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};

use crate::error::ServiceError;
use crate::model::DbExecutor;
use crate::schema::passwords::{columns as password_columns, dsl::passwords};

pub struct UpdatePasswordRequest {
    pub user: u64,
    pub hash: String,
}

impl UpdatePasswordRequest {
    pub fn new(user: u64, hash: String) -> Self {
        Self { user, hash }
    }
}

impl Message for UpdatePasswordRequest {
    type Result = Result<(), ServiceError>;
}

impl Handler<UpdatePasswordRequest> for DbExecutor {
    type Result = Result<(), ServiceError>;

    fn handle(&mut self, req: UpdatePasswordRequest, _: &mut Self::Context) -> Self::Result {
        let conn: &MysqlConnection = &self.0.get().unwrap();

        diesel::update(passwords.filter(password_columns::id.eq(req.user)))
            .set(password_columns::hash.eq(req.hash))
            .execute(conn)?;

        Ok(())
    }
}