framework_cqrs_lib/cqrs/infra/mappers/
state_view.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use std::cmp::max;
use std::collections::HashMap;

use serde::Serialize;

use crate::cqrs::core::context::Context;
use crate::cqrs::core::data::Entity;
use crate::cqrs::core::repositories::query::Paged;
use crate::cqrs::models::jsonapi::{CanBeView, CanGetTypee, ManyView, PageView, PaginationView};
use crate::cqrs::models::views::{DataWrapperView, LinkView};
use crate::cqrs::models::views::entities::{EntityView, LinksEntityView};

pub trait CanBeManyView<T: Serialize + Clone> {
    fn to_many_view(
        &self,
        ctx: &Context,
        ontology: String,
        other_link: HashMap<String, String>,
    ) -> ManyView<T>;
}

impl<T: Serialize + Clone> CanBeManyView<T> for Paged<T> {
    fn to_many_view(&self, ctx: &Context, ontology: String, other_link: HashMap<String, String>) -> ManyView<T> {
        let external_url = ctx.meta
            .get("externalUrl")
            .map(|urlref| urlref.clone())
            .unwrap_or("unknown".to_string());

        let query_without_prefix = ctx.filters
            .iter()
            .map(|(k, v)| {
                format!("{k}={v}")
            })
            .collect::<Vec<String>>()
            .join("&");

        let query = if query_without_prefix.is_empty() {
            query_without_prefix
        } else {
            format!("?{query_without_prefix}")
        };


        let query_first_without_prefix = ctx.filters
            .iter()
            .filter(|(k, _)| **k != "page[number]".to_string() && **k != "page[size]".to_string())
            .map(|(k, v)| {
                format!("{k}={v}")
            })
            .chain(vec!["page[number]=0".to_string(), format!("page[size]={}", self.meta.page.size)])
            .collect::<Vec<String>>()
            .join("&");

        let query_first = if query_first_without_prefix.is_empty() {
            query_first_without_prefix
        } else {
            format!("?{query_first_without_prefix}")
        };

        let query_prev_without_prefix = ctx.filters
            .iter()
            .filter(|(k, _)| **k != "page[number]".to_string() && **k != "page[size]".to_string())
            .map(|(k, v)| {
                format!("{k}={v}")
            })
            .chain(vec![format!("page[number]={}", max(if self.meta.page.number > 0 { self.meta.page.number - 1 } else { 0 }, 0)), format!("page[size]={}", self.meta.page.size)])
            .collect::<Vec<String>>()
            .join("&");

        let query_prev = if query_prev_without_prefix.is_empty() {
            query_prev_without_prefix
        } else {
            format!("?{query_prev_without_prefix}")
        };


        let other_link_sanitize = other_link
            .into_iter()
            .map(|(k, v)| (k, format!("{external_url}/{v}")))
            .collect::<HashMap<String, String>>();


        let links = LinkView {
            links: HashMap::from([
                ("self".to_string(), format!("{external_url}/{ontology}")),
                ("last".to_string(), format!("{external_url}/{ontology}{}", query.clone())),
                ("prev".to_string(), format!("{external_url}/{ontology}{}", query_prev.clone())),
                ("first".to_string(), format!("{external_url}/{ontology}{query_first}")),
                ("next".to_string(), format!("{external_url}/{ontology}{}", query.clone())),
            ])
                .into_iter().chain(other_link_sanitize)
                .collect()
        };

        ManyView {
            data: self.data.clone(),
            meta: Some(
                PaginationView {
                    total_pages: self.meta.total_pages.clone(),
                    total_records: self.meta.total_records.clone(),
                    page: PageView {
                        number: self.meta.page.number.clone(),
                        size: self.meta.page.size.clone(),
                    },
                }
            ),
            links: Some(links),
        }
    }
}

pub fn from_states_to_entity_view<DATA, VIEW>(
    entity: Entity<DATA, String>,
    ontology: String,
    context: &Context,
) -> EntityView<VIEW>
where
    VIEW: Serialize + Clone,
    DATA: Clone + CanBeView<VIEW> + CanGetTypee,
{
    let entity_id = entity.entity_id.as_str();

    let external_url = context.meta
        .get("externalUrl")
        .map(|urlref| urlref.clone())
        .unwrap_or("unknown".to_string());

    let link_event = (
        "events".to_string(), format!("{external_url}/{ontology}/{entity_id}/events")
    );
    let link_self = (
        "self".to_string(), format!("{external_url}/{ontology}/{entity_id}")
    );

    EntityView {
        r#type: entity.data.get_type(),
        id: entity_id.to_string(),
        attributes: entity.data.to_view(),
        links: Some(LinksEntityView {
            links: HashMap::from([link_event, link_self])
        }),
    }
}

pub fn from_states_to_view<DATA, VIEW>(
    entity: Entity<DATA, String>,
    ontology: String,
    context: &Context,
) -> DataWrapperView<EntityView<VIEW>>
where
    VIEW: Serialize + Clone,
    DATA: Clone + CanBeView<VIEW> + CanGetTypee,
{
    DataWrapperView {
        data: from_states_to_entity_view(entity, ontology, context)
    }
}