dogy_backend_api/service/pets/
store.rs

1use sqlx::{query_as, Executor, Postgres};
2use uuid::Uuid;
3
4use super::models::{FullPet, JoinedFullPet, PetAttributes, PetBase};
5
6pub async fn retrieve_full_pet<'e, E>(conn: E, pet_id: Uuid) -> FullPet
7where
8    E: Executor<'e, Database = Postgres>,
9{
10    let pet = query_as::<_, JoinedFullPet>(
11        r#"SELECT p.*, attr.is_sterilized, attr_aggr.aggression_levels,
12          attr_all.allergies, attr_be.behaviors, attr_br.breeds,
13          attr_int.interactions, attr_pe.personalities, attr_re.reactivities
14        FROM pets p
15        LEFT JOIN pet_attrs attr ON p.id = attr.pet_id
16        LEFT JOIN pet_attr_aggression_levels attr_aggr ON attr.id = attr_aggr.pet_attr_id
17        LEFT JOIN pet_attr_allergies attr_all ON attr.id = attr_all.pet_attr_id
18        LEFT JOIN pet_attr_behaviors attr_be ON attr.id = attr_be.pet_attr_id
19        LEFT JOIN pet_attr_breeds attr_br ON attr.id = attr_br.pet_attr_id
20        LEFT JOIN pet_attr_interactions attr_int ON attr.id = attr_int.pet_attr_id
21        LEFT JOIN pet_attr_personalities attr_pe ON attr.id = attr_pe.pet_attr_id
22        LEFT JOIN pet_attr_reactivities attr_re ON attr.id = attr_re.pet_attr_id
23        WHERE p.id = $1;
24        "#,
25    )
26    .bind(pet_id)
27    .fetch_one(conn)
28    .await
29    .unwrap();
30
31    FullPet {
32        base: PetBase {
33            pet_id: pet.id,
34            name: pet.name,
35            age: pet.age,
36            gender: pet.gender,
37            size: pet.size,
38            photo_url: pet.photo_url,
39            weight: pet.weight,
40            weight_unit: pet.weight_unit,
41        },
42        attributes: PetAttributes {
43            aggression_levels: pet.aggression_levels,
44            allergies: pet.allergies,
45            breeds: pet.breeds,
46            behaviors: pet.behaviors,
47            interactions: pet.interactions,
48            personalities: pet.personalities,
49            reactivities: pet.reactivities,
50            sterilized: pet.is_sterilized,
51        },
52    }
53}