lacoctelera/domain/
author.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
// Copyright 2024 Felipe Torres González
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

//! Data objects related to Authors.

use crate::{domain::DataDomainError, validate_id};
use serde::{Deserialize, Serialize};
use std::cmp::PartialEq;
use utoipa::{IntoParams, ToSchema};
use uuid::Uuid;
use validator::Validate;

/// Object that represents an Author of the `Cocktail` data base.
///
/// # Description
///
/// Authors' main role is defining recipes in the data base, and own them until they delete them or transfer the
/// ownership. This object is a simple descriptor that includes some personal information of the authors.
///
/// All attributes are optional to allow using this `struct` as query object for the GET methods of the `/author`
/// endpoint. Mandatory fields are defined in the description of each method of the endpoint.
///
/// Some restrictions over the `struct`'s members:
/// - [Author::id] must contain a valid [Uuid]. Strings are parsed to [Uuid]. The expected format is a 128-bit value,
///   formatted as a hex string in five groups. The first 4 groups are randomly generated, and the fifth comes from
///   a timestamp. However, clients can freely generate this ID using other combinations as long as the length and basic
///   format rules are honored.
/// - [Author::name] and [Author::surname] shall have a minimum length of 2 and a maximum of 40 characters. These
///   fields are allowed to repeat in the DB. Authors are identified in the DB by [Author::id]. Usernames are not
///   required.
/// - [Author::email] is validated against the HTML5 regex.
/// - [Author::description] can't exceed 255 characters length.
/// - [Author::website] must contain an url format (`http://...` or `https://...`).
///
/// Authors are given the choice to share or keep private their profiles. Activate [Author::shareable] to allow
/// sharing the author's profile to the main public. Private profiles are protected from non privileged clients of the
/// API (with no API access token): only the [Author::id] and [Author::name] is given when a unprivileged client
/// requests the data of an author to the API.
///
/// The constructor [Author::default] is given to generate a new author entry using a random funny name.
///
/// Prefer [AuthorBuilder] rather than [Author::new] to build a new [Author] instance.
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema, IntoParams, Validate)]
pub struct Author {
    #[validate(custom(function = "validate_id"))]
    #[schema(value_type = String, example = "0191e13b-5ab7-78f1-bc06-be503a6c111b")]
    id: Option<Uuid>,
    #[validate(length(min = 2), length(max = 40))]
    name: Option<String>,
    #[validate(length(min = 2), length(max = 40))]
    surname: Option<String>,
    #[validate(email)]
    email: Option<String>,
    /// Decide whether an author profile can be shared to the public or not.
    pub shareable: Option<bool>,
    #[validate(length(max = 255))]
    description: Option<String>,
    #[validate(url)]
    website: Option<String>,
    social_profiles: Option<Vec<SocialProfile>>,
}

/// Simple Data object to describe a social network profile.
///
/// # Description
///
/// [SocialProfile] has no associated table in the DB. This is simply contained in the [Author]'s entry in the DB.
/// A social profile is described by the social network name and the author's user name.
#[derive(Clone, Debug, Serialize, Deserialize, ToSchema, Validate, PartialEq)]
pub struct SocialProfile {
    /// Name of the social network, i.e. Instagram, X, TikTok... 40 chars max.
    #[validate(length(max = 40))]
    pub provider_name: String,
    /// URL of the social network. 80 chars max.
    #[validate(length(max = 80))]
    pub website: String,
}

/// Implementation of the builder pattern for the [Author] `struct`.
///
/// # Description
///
/// Use this object to partially build an [Author] object. For example:
///
/// ```rust
/// use lacoctelera::domain::{Author, AuthorBuilder};
///
/// let author = AuthorBuilder::default()
///     .set_name("Jane")
///     .set_surname("Doe")
///     .set_shareable(true)
///     .build().unwrap();
/// ```
///
/// [AuthorBuilder::build] shall be called after all the member initializations. This method returns a [Result] if
/// some of the values given to the constructor don't comply with the restrictions defined to [Author]'s members.
#[derive(Default)]
pub struct AuthorBuilder {
    id: Option<String>,
    name: Option<String>,
    surname: Option<String>,
    email: Option<String>,
    shareable: bool,
    description: Option<String>,
    website: Option<String>,
    social_profiles: Option<Vec<SocialProfile>>,
}

impl std::default::Default for Author {
    /// Default constructor for [Author].
    ///
    /// # Description
    ///
    /// This constructor builds a new [Author] whose name is randomly generated using funny names and adjectives.
    fn default() -> Self {
        Author {
            id: Some(Uuid::now_v7()),
            name: None,
            surname: None,
            email: None,
            shareable: Some(false),
            description: None,
            website: None,
            social_profiles: None,
        }
    }
}

impl Author {
    /// Constructor of the [Author] struct.
    ///
    /// # Descriptor
    ///
    /// All fields are optional
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        id: Option<String>,
        name: Option<String>,
        surname: Option<String>,
        email: Option<String>,
        shareable: Option<bool>,
        description: Option<String>,
        website: Option<String>,
        social_profiles: Option<&[SocialProfile]>,
    ) -> Result<Self, DataDomainError> {
        let id = if id.is_some() {
            match Uuid::parse_str(&id.unwrap()) {
                Ok(id) => Some(id),
                Err(_) => return Err(DataDomainError::InvalidId),
            }
        } else {
            None
        };

        let author = Author {
            id,
            name,
            surname,
            email,
            shareable,
            description,
            website,
            social_profiles: social_profiles.map(Vec::from),
        };

        match author.validate() {
            Ok(_) => std::result::Result::Ok(author),
            Err(e) => Err(DataDomainError::InvalidParams { source: e }),
        }
    }

    pub fn id(&self) -> Option<String> {
        self.id.map(|id| id.to_string())
    }

    pub fn name(&self) -> Option<&str> {
        self.name.as_deref()
    }

    pub fn surname(&self) -> Option<&str> {
        self.surname.as_deref()
    }

    pub fn email(&self) -> Option<&str> {
        self.email.as_deref()
    }

    pub fn shareable(&self) -> bool {
        self.shareable.unwrap_or_default()
    }

    pub fn description(&self) -> Option<&str> {
        self.description.as_deref()
    }

    pub fn website(&self) -> Option<&str> {
        self.website.as_deref()
    }

    pub fn social_profiles(&self) -> Option<&[SocialProfile]> {
        self.social_profiles.as_deref()
    }

    pub fn mute_private_data(&mut self) {
        if !self.shareable() {
            self.email = None;
            self.description = None;
        }
    }

    pub fn enable_sharing(&mut self) {
        self.shareable = Some(true);
    }

    pub fn disable_sharing(&mut self) {
        self.shareable = Some(false);
    }

    /// Update the internal attributes using another [Author] object.
    ///
    /// # Description
    ///
    /// This method takes as reference another [Author] object, and replaces the internal values, which are also
    /// present in the given reference, using the values from the reference. This method is meant to implement a
    /// PATCH logic.
    pub fn update_from(&mut self, update: &Author) {
        if update.id().is_some() {
            self.id = Some(Uuid::parse_str(&update.id().unwrap()).unwrap());
        }
        if update.name().is_some() {
            self.name = Some(update.name().unwrap().into());
        }
        if update.surname().is_some() {
            self.surname = Some(update.surname().unwrap().into());
        }
        if update.email().is_some() {
            self.email = Some(update.email().unwrap().into());
        }
        if update.description().is_some() {
            self.description = Some(update.description().unwrap().into());
        }
        if update.website().is_some() {
            self.website = Some(update.website().unwrap().into());
        }
        if update.social_profiles().is_some() {
            self.social_profiles = Some(Vec::from(update.social_profiles().unwrap()));
        }
    }
}

impl PartialEq for Author {
    fn eq(&self, other: &Self) -> bool {
        self.name == other.name
            && self.surname == other.surname
            && self.email == other.email
            && self.shareable == other.shareable
            && self.description == other.description
            && self.social_profiles == other.social_profiles
    }
}

impl AuthorBuilder {
    pub fn set_id(mut self, id: &str) -> Self {
        self.id = Some(id.into());

        self
    }

    pub fn set_name(mut self, name: &str) -> Self {
        self.name = Some(name.into());

        self
    }

    pub fn set_surname(mut self, surname: &str) -> Self {
        self.surname = Some(surname.into());

        self
    }

    pub fn set_email(mut self, email: &str) -> Self {
        self.email = Some(email.into());

        self
    }

    pub fn set_shareable(mut self, shareable: bool) -> Self {
        self.shareable = shareable;

        self
    }

    pub fn set_description(mut self, description: &str) -> Self {
        self.description = Some(description.into());

        self
    }

    pub fn set_website(mut self, website: &str) -> Self {
        self.website = Some(website.into());

        self
    }

    pub fn set_social_profiles(mut self, profiles: &[SocialProfile]) -> Self {
        self.social_profiles = Some(Vec::from(profiles));

        self
    }

    pub fn build(self) -> Result<Author, DataDomainError> {
        Author::new(
            self.id,
            self.name,
            self.surname,
            self.email,
            Some(self.shareable),
            self.description,
            self.website,
            self.social_profiles.as_deref(),
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use pretty_assertions::assert_eq;
    use uuid::Uuid;

    #[test]
    fn build_author_using_builder() {
        let author = AuthorBuilder::default().build().unwrap();
        assert_eq!(author.id, None);
        assert_eq!(author.name, None);
        assert_eq!(author.surname, None);
        assert_eq!(author.email, None);
        assert_eq!(author.shareable, Some(false));
        assert_eq!(author.description, None);
        assert_eq!(author.website, None);
        assert!(author.social_profiles.is_none());

        let id = Uuid::now_v7().to_string();

        let social_profiles = [
            SocialProfile {
                provider_name: "Facebook".into(),
                website: "a web site".into(),
            },
            SocialProfile {
                provider_name: "Instragram".into(),
                website: "a web site".into(),
            },
        ];

        let author = AuthorBuilder::default()
            .set_id(&id)
            .set_name("Jane")
            .set_surname("Doe")
            .set_email("jane_doe@mail.com")
            .set_description("An unknown person.")
            .set_website("http://janedoe.com")
            .set_shareable(true)
            .set_social_profiles(&social_profiles)
            .build()
            .expect("Failed to build author");

        assert_eq!(author.id().unwrap(), id);
        assert_eq!(author.name().unwrap(), "Jane");
        assert_eq!(author.surname().unwrap(), "Doe");
        assert_eq!(author.email().unwrap(), "jane_doe@mail.com");
        assert_eq!(author.shareable(), true);
        assert_eq!(author.description().unwrap(), "An unknown person.");
        assert_eq!(author.website().unwrap(), "http://janedoe.com");
        assert_eq!(author.social_profiles().unwrap(), social_profiles);
    }

    #[test]
    fn build_author() {
        let mut author = Author::default();
        assert!(Uuid::parse_str(&author.id().unwrap()).is_ok());
        assert_eq!(author.name(), None);

        assert!(!author.shareable());
        author.enable_sharing();
        assert!(author.shareable());
        author.disable_sharing();
        assert!(!author.shareable());

        let id = Uuid::now_v7().to_string();
        let social_profiles = [
            SocialProfile {
                provider_name: "Facebook".into(),
                website: "a web site".into(),
            },
            SocialProfile {
                provider_name: "Instragram".into(),
                website: "a web site".into(),
            },
        ];

        let author = Author::new(
            Some(id.clone()),
            Some("Jane".to_string()),
            Some("Doe".to_string()),
            Some("jane_doe@mail.com".to_string()),
            Some(true),
            Some("An unknown person.".to_string()),
            Some("http://janedoe.com".to_string()),
            Some(&social_profiles),
        )
        .expect("Failed to create new instance of Author using new.");

        assert_eq!(author.id().unwrap(), id);
        assert_eq!(author.name().unwrap(), "Jane");
        assert_eq!(author.surname().unwrap(), "Doe");
        assert_eq!(author.email().unwrap(), "jane_doe@mail.com");
        assert_eq!(author.shareable(), true);
        assert_eq!(author.description().unwrap(), "An unknown person.");
        assert_eq!(author.website().unwrap(), "http://janedoe.com");
        assert_eq!(author.social_profiles().unwrap(), social_profiles);
    }

    #[test]
    fn build_author_using_wrong_id() {
        let author = AuthorBuilder::default().set_id("Wrong_ID").build();
        assert!(author.is_err());
        let author = AuthorBuilder::default().set_id("191919-010010-022").build();
        assert!(author.is_err());
    }

    #[test]
    fn build_author_using_wrong_text_length() {
        let author = AuthorBuilder::default().set_name("J").build();
        assert!(author.is_err());

        let author = AuthorBuilder::default().set_surname("D").build();
        assert!(author.is_err());

        let author = AuthorBuilder::default().set_website("janedoe.com").build();
        assert!(author.is_err());

        let author = AuthorBuilder::default()
            .set_email("janedoe<at>mail.com")
            .build();
        assert!(author.is_err());

        let author = AuthorBuilder::default()
            .set_description(&"dummy string".repeat(300))
            .build();
        assert!(author.is_err());
    }

    #[test]
    fn mute_fields() {
        let id = Uuid::now_v7().to_string();
        let social_profiles = [
            SocialProfile {
                provider_name: "Facebook".into(),
                website: "a web site".into(),
            },
            SocialProfile {
                provider_name: "Instragram".into(),
                website: "a web site".into(),
            },
        ];
        let mut author = Author::new(
            Some(id.clone()),
            Some("Jane".to_string()),
            Some("Doe".to_string()),
            Some("jane_doe@mail.com".to_string()),
            Some(false),
            Some("An unknown person.".to_string()),
            Some("http://janedoe.com".to_string()),
            Some(&social_profiles),
        )
        .expect("Failed to create new instance of Author using new.");

        author.mute_private_data();

        assert_eq!(author.id().unwrap(), id);
        assert_eq!(author.name().unwrap(), "Jane");
        assert_eq!(author.surname().unwrap(), "Doe");
        assert_eq!(author.email(), None);
        assert_eq!(author.shareable(), false);
        assert_eq!(author.description(), None);
        assert_eq!(author.website().unwrap(), "http://janedoe.com");
        assert_eq!(author.social_profiles().unwrap(), social_profiles);
    }

    #[test]
    fn modify_from() {
        // Let's build the author under test
        let id = Uuid::now_v7().to_string();
        let name = "Jane";
        let surname = "Doe";
        let email = "jane@mail.com";
        let website = "https://jane.com";
        let description = "A dummy description";
        let profiles = &[SocialProfile {
            provider_name: "None".into(),
            website: "https://none.com/jane".into(),
        }];

        let mut author = AuthorBuilder::default()
            .set_id(&id)
            .set_name(name)
            .set_surname(surname)
            .set_email(email)
            .set_description(description)
            .set_shareable(true)
            .set_website(website)
            .set_social_profiles(profiles)
            .build()
            .expect("Failed to build an author");

        // First test case: modify only some of the attributes.
        let author_dummy = AuthorBuilder::default()
            .set_name("Stripped")
            .set_surname("Zebra")
            .build()
            .expect("Failed to build an author");
        author.update_from(&author_dummy);

        assert_eq!(author.name, author_dummy.name);
        assert_eq!(author.surname, author_dummy.surname);
        assert_ne!(author.id, author_dummy.id);
        assert_ne!(author.email, author_dummy.email);
        assert_ne!(author.website, author_dummy.website);
        assert_ne!(author.description, author_dummy.description);
        assert_ne!(author.social_profiles, author_dummy.social_profiles);

        // Second test case: modify all the attributes but the ID.
        let name = "Juana";
        let surname = "Cierva";
        let email = "juana@mail.com";
        let website = "https://juana.com";
        let description = "Una descripción vana";
        let profiles = &[SocialProfile {
            provider_name: "None".into(),
            website: "https://none.com/juana".into(),
        }];

        let author_spa = AuthorBuilder::default()
            .set_id(&id)
            .set_name(name)
            .set_surname(surname)
            .set_email(email)
            .set_description(description)
            .set_shareable(true)
            .set_website(website)
            .set_social_profiles(profiles)
            .build()
            .expect("Failed to build an author");

        author.update_from(&author_spa);

        assert_eq!(author, author_spa);
    }
}