bioscopelib/dto/
picture_request.rs

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
38
39
40
41
42
43
44
45
46
47
48
//!
//! Request from microscope client to change or add `Picture`
//!
use actix_web::web::Bytes;
use common::model::jwt_token::JwtToken;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::jsonb::picture_data::PictureData;

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct PictureRequest {
    /// Picture ID - can be generated if picture is new
    #[serde(skip_serializing_if = "Option::is_none")]
    pub id: Option<Uuid>,

    /// ID of the microscope that took the photo
    pub microscope: Uuid,

    /// The user who requested picture (picture can be receive without user request)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub user: Option<Uuid>,

    /// The picture itself in byte representation, don't save in database, save as png on the server
    pub image: Vec<u8>,

    /// Info about picture
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content: Option<PictureData>,
}

impl PictureRequest {
    pub fn new(jwt_token: JwtToken, payload: Bytes, picture_data: String) -> PictureRequest {
        let picture_data: PictureData =
            serde_json::from_str(picture_data.as_str()).expect("Can't parse picture info");

        let picture_request = PictureRequest {
            id: Option::from(Uuid::new_v4()),
            microscope: jwt_token.user_id,
            user: None,
            image: payload.to_vec(),
            content: Option::from(picture_data),
        };

        picture_request
    }
}