summaryrefslogtreecommitdiffstats
path: root/src/util.rs
blob: 944feb60a268a7dd6860ccde02a20f9d6e2716d2 (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
37
use actix::{Handler, Message};
use diesel::{ExpressionMethods, MysqlConnection, QueryDsl, RunQueryDsl};

use crate::error::ServiceError;
use crate::model::{DbExecutor, User};
use crate::schema::users::{columns as user_columns, dsl::users};

pub struct UserRequest(pub String);

impl Message for UserRequest {
    type Result = Result<User, ServiceError>;
}

impl Handler<UserRequest> for DbExecutor {
    type Result = Result<User, ServiceError>;

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

        users
            .filter(user_columns::name.eq(req.0))
            .first::<User>(conn)
            .map_err(|_| ServiceError::NotFound)
    }
}

pub mod filters {
    use num_format::{CustomFormat, ToFormattedString};

    pub fn fmt_points(amount: &u64) -> Result<String, askama::Error> {
        let format = CustomFormat::builder()
            .separator(" ")
            .build()
            .expect("Format is fine");
        Ok(format!("{} JKP", (*amount).to_formatted_string(&format)))
    }
}