shared/domain/
module.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//! Types for jig Modules.

use super::asset::{AssetId, AssetType};
use crate::api::endpoints::PathPart;
use chrono::{DateTime, Utc};
use macros::make_path_parts;
use serde::{Deserialize, Serialize};
use std::str::FromStr;

/// Module bodies
pub mod body;

pub use body::Body as ModuleBody;

wrap_uuid! {
    /// Wrapper type around [`Uuid`](Uuid), represents the **unique ID** of a module.
    ///
    /// This uniquely identifies a module. There is no other module that shares this ID.
    #[serde(rename_all = "camelCase")]
    pub struct ModuleId
}

wrap_uuid! {
    /// Wrapper type around [`Uuid`](Uuid), represents the **unique ID** of a module.
    ///
    /// This uniquely identifies a module. There is no other module that shares this ID.
    #[serde(rename_all = "camelCase")]
    pub struct StableModuleId
}

/// Represents the various kinds of data a module can represent.
#[repr(i16)]
#[cfg_attr(feature = "backend", derive(sqlx::Type))]
#[derive(Serialize, Deserialize, Debug, Copy, Clone, PartialEq, Eq)]
pub enum ModuleKind {
    /// This is a sort of special module, every jig has one and it can't be deleted TODO: is that so?
    Cover = 0,

    /// Flashcards
    Flashcards = 1,

    /// Matching
    Matching = 2,

    /// Memory Game
    Memory = 3,

    /// Talking Poster
    Poster = 4,

    /// Listen & Learn
    TappingBoard = 5,

    /// Tracing
    Tracing = 6,

    /// Video
    Video = 7,

    /// Deprecated, next new module should use this slot
    //VisualQuiz = 8,

    /// Quiz Game
    CardQuiz = 9,

    /// Drag & Drop
    DragDrop = 10,

    /// Legacy
    Legacy = 11,

    /// ResourceCover user for resources and playlist cover
    ResourceCover = 12,

    /// Answer This (Previously "Find the Answer")
    FindAnswer = 13,

    /// Embed
    Embed = 14,
}

impl ModuleKind {
    /// casts `self` to a string
    #[must_use]
    pub const fn as_str(self) -> &'static str {
        match self {
            Self::Cover => "cover",
            Self::ResourceCover => "resource-cover",
            Self::Flashcards => "flashcards",
            Self::Matching => "matching",
            Self::Memory => "memory",
            Self::Poster => "poster",
            Self::TappingBoard => "tapping-board",
            Self::DragDrop => "drag-drop",
            Self::Tracing => "tracing",
            Self::Video => "video",
            Self::Embed => "embed",
            Self::CardQuiz => "card-quiz",
            Self::Legacy => "legacy",
            Self::FindAnswer => "find-answer",
        }
    }

    /// display name for each module kind
    pub fn display_name(self) -> &'static str {
        match self {
            Self::Cover => "Cover",
            Self::ResourceCover => "Cover",
            Self::Flashcards => "Flashcards",
            Self::Matching => "Matching",
            Self::Memory => "Memory Game",
            Self::Poster => "Talking Poster",
            Self::TappingBoard => "Listen & Learn",
            Self::Tracing => "Tracing",
            Self::Video => "Video Player",
            Self::Embed => "Embed It",
            Self::CardQuiz => "Multiple Choice",
            Self::DragDrop => "Drag & Drop",
            Self::FindAnswer => "Answer This",
            Self::Legacy => "Legacy",
        }
    }

    /// Whether this ModuleKind has auto-generated content
    pub fn autogenerated(&self) -> bool {
        match self {
            Self::Flashcards | Self::Matching | Self::Memory | Self::CardQuiz => true,
            _ => false,
        }
    }

    /// Whether this ModuleKind has scoring
    pub fn has_scoring(&self) -> bool {
        match self {
            Self::Matching | Self::CardQuiz | Self::DragDrop | Self::FindAnswer => true,
            _ => false,
        }
    }
}

impl FromStr for ModuleKind {
    type Err = anyhow::Error;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let res = match s {
            "cover" => Self::Cover,
            "resource-cover" => Self::ResourceCover,
            "flashcards" => Self::Flashcards,
            "matching" => Self::Matching,
            "memory" => Self::Memory,
            "poster" => Self::Poster,
            "tapping-board" => Self::TappingBoard,
            "drag-drop" => Self::DragDrop,
            "tracing" => Self::Tracing,
            "video" => Self::Video,
            "embed" => Self::Embed,
            "card-quiz" => Self::CardQuiz,
            "legacy" => Self::Legacy,
            "find-answer" => Self::FindAnswer,
            _ => anyhow::bail!("Invalid ModuleKind: {}", s),
        };

        Ok(res)
    }
}
impl ModuleBody {
    /// Maps module content from request body
    pub fn map_module_contents(body: &Self) -> anyhow::Result<(ModuleKind, serde_json::Value)> {
        let kind = body.kind();

        let body = match body {
            Self::CardQuiz(body) => serde_json::to_value(body)?,
            Self::Cover(body) => serde_json::to_value(body)?,
            Self::ResourceCover(body) => serde_json::to_value(body)?,
            Self::DragDrop(body) => serde_json::to_value(body)?,
            Self::Flashcards(body) => serde_json::to_value(body)?,
            Self::Matching(body) => serde_json::to_value(body)?,
            Self::MemoryGame(body) => serde_json::to_value(body)?,
            Self::Poster(body) => serde_json::to_value(body)?,
            Self::TappingBoard(body) => serde_json::to_value(body)?,
            Self::Video(body) => serde_json::to_value(body)?,
            Self::Embed(body) => serde_json::to_value(body)?,
            Self::FindAnswer(body) => serde_json::to_value(body)?,
            Self::Legacy(body) => serde_json::to_value(body)?,
        };

        Ok((kind, body))
    }

    /// Transforms module content from database
    pub fn transform_response_kind(
        contents: serde_json::Value,
        kind: ModuleKind,
    ) -> anyhow::Result<Self> {
        match kind {
            ModuleKind::CardQuiz => Ok(Self::CardQuiz(serde_json::from_value(contents)?)),
            ModuleKind::Cover => Ok(Self::Cover(serde_json::from_value(contents)?)),
            ModuleKind::ResourceCover => Ok(Self::ResourceCover(serde_json::from_value(contents)?)),
            ModuleKind::DragDrop => Ok(Self::DragDrop(serde_json::from_value(contents)?)),
            ModuleKind::Flashcards => Ok(Self::Flashcards(serde_json::from_value(contents)?)),
            ModuleKind::Matching => Ok(Self::Matching(serde_json::from_value(contents)?)),
            ModuleKind::Memory => Ok(Self::MemoryGame(serde_json::from_value(contents)?)),
            ModuleKind::Poster => Ok(Self::Poster(serde_json::from_value(contents)?)),
            ModuleKind::TappingBoard => Ok(Self::TappingBoard(serde_json::from_value(contents)?)),
            ModuleKind::Video => Ok(Self::Video(serde_json::from_value(contents)?)),
            ModuleKind::Embed => Ok(Self::Embed(serde_json::from_value(contents)?)),
            ModuleKind::FindAnswer => Ok(Self::FindAnswer(serde_json::from_value(contents)?)),
            ModuleKind::Legacy => Ok(Self::Legacy(serde_json::from_value(contents)?)),

            _ => anyhow::bail!("Unimplemented response kind"),
        }
    }
}

/// Minimal information about a module.
#[derive(Clone, Serialize, Deserialize, Debug)]
pub struct LiteModule {
    /// The module's unique ID.
    pub id: ModuleId,

    /// ID that doesn't change when publishing.
    pub stable_id: StableModuleId,

    /// Which kind of module this is.
    pub kind: ModuleKind,

    /// Whether this module is completed.
    #[serde(default)]
    pub is_complete: bool,
}

/// Over the wire representation of a module.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Module {
    /// The module's unique ID.
    pub id: ModuleId,

    /// ID that doesn't change when publishing.
    pub stable_id: StableModuleId,

    /// The module's body.
    pub body: ModuleBody,

    /// Whether the module is complete or not.
    pub is_complete: bool,

    /// Whether a jig has been updated.
    pub is_updated: bool,

    /// When the module was originally created.
    pub created_at: DateTime<Utc>,

    /// When the module was last updated.
    pub updated_at: DateTime<Utc>,
}

make_path_parts!(ModuleCreatePath => "/v1/module/draft");

/// Request to create a new `Module`.
#[derive(Serialize, Deserialize, Debug)]
pub struct ModuleCreateRequest {
    /// ID for Playlist or JIG
    #[serde(flatten)]
    pub parent_id: AssetId,

    /// The module's body.
    pub body: ModuleBody,
}

make_path_parts!(ModuleGetLivePath => "/v1/{}/module/live/{}" => AssetType, ModuleId);

make_path_parts!(ModuleGetDraftPath => "/v1/{}/module/draft/{}" => AssetType, ModuleId);

/// Response for successfully finding a module
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ModuleResponse {
    /// The module we found
    pub module: Module,
}

make_path_parts!(ModuleUploadPath => "/v1/module/draft/{}" => ModuleId);

/// Request to update a `Module`.
/// note: fields here cannot be nulled out (`None` means "don't change").
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ModuleUpdateRequest {
    /// ID for Playlist or JIG
    #[serde(flatten)]
    pub parent_id: AssetId,

    /// The module's body.
    #[serde(default)]
    pub body: Option<ModuleBody>,

    /// Where to move this module to in the parent. Relevant for the order that the modules
    /// are returned when fetching JIG data.
    ///
    /// Numbers larger than the parent JIG's module count will move it to the *end*.
    #[serde(default)]
    pub index: Option<u16>,

    /// check if module is complete
    #[serde(default)]
    pub is_complete: Option<bool>,
}

make_path_parts!(ModuleDeletePath => "/v1/module/draft/{}" => ModuleId);

/// Request to delete a `Module`.
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct ModuleDeleteRequest {
    /// ID for Playlist or JIG
    #[serde(flatten)]
    pub parent_id: AssetId,
}