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
//! Controllers for requests starting with `/api/v0/main-frontend/global-stats`.

use models::library::global_stats::{GlobalCourseModuleStatEntry, GlobalStatEntry};

use crate::{domain::authorization::authorize, prelude::*};

/**
GET `/api/v0/main-frontend/global-stats/number-of-people-completed-a-course`
*/
#[instrument(skip(pool))]
async fn get_number_of_people_completed_a_course(
    pool: web::Data<PgPool>,
    user: AuthUser,
) -> ControllerResult<web::Json<Vec<GlobalStatEntry>>> {
    let mut conn = pool.acquire().await?;
    let token = authorize(
        &mut conn,
        Act::ViewStats,
        Some(user.id),
        Res::GlobalPermissions,
    )
    .await?;
    let res =
        models::library::global_stats::get_number_of_people_completed_a_course(&mut conn).await?;

    token.authorized_ok(web::Json(res))
}

/**
GET `/api/v0/main-frontend/global-stats/number-of-people-registered-completion-to-study-registry`
*/
#[instrument(skip(pool))]
async fn get_number_of_people_registered_completion_to_study_registry(
    pool: web::Data<PgPool>,
    user: AuthUser,
) -> ControllerResult<web::Json<Vec<GlobalStatEntry>>> {
    let mut conn = pool.acquire().await?;
    let token = authorize(
        &mut conn,
        Act::ViewStats,
        Some(user.id),
        Res::GlobalPermissions,
    )
    .await?;
    let res = models::library::global_stats::get_number_of_people_registered_completion_to_study_registry(&mut conn).await?;

    token.authorized_ok(web::Json(res))
}

/**
 * GET `/api/v0/main-frontend/global-stats/number-of-people-done-at-least-one-exercise`
 */
#[instrument(skip(pool))]
async fn get_number_of_people_done_at_least_one_exercise(
    pool: web::Data<PgPool>,
    user: AuthUser,
) -> ControllerResult<web::Json<Vec<GlobalStatEntry>>> {
    let mut conn = pool.acquire().await?;
    let token = authorize(
        &mut conn,
        Act::ViewStats,
        Some(user.id),
        Res::GlobalPermissions,
    )
    .await?;
    let res =
        models::library::global_stats::get_number_of_people_done_at_least_one_exercise(&mut conn)
            .await?;

    token.authorized_ok(web::Json(res))
}

/**
 * GET `/api/v0/main-frontend/global-stats/number-of-people-started-course`
 */
#[instrument(skip(pool))]
async fn get_number_of_people_started_course(
    pool: web::Data<PgPool>,
    user: AuthUser,
) -> ControllerResult<web::Json<Vec<GlobalStatEntry>>> {
    let mut conn = pool.acquire().await?;
    let token = authorize(
        &mut conn,
        Act::ViewStats,
        Some(user.id),
        Res::GlobalPermissions,
    )
    .await?;
    let res = models::library::global_stats::get_number_of_people_started_course(&mut conn).await?;

    token.authorized_ok(web::Json(res))
}

/**
 * GET `/api/v0/main-frontend/global-stats/course-module-stats-by-completions-registered-to-study-registry`
 */
#[instrument(skip(pool))]
async fn get_course_module_stats_by_completions_registered_to_study_registry(
    pool: web::Data<PgPool>,
    user: AuthUser,
) -> ControllerResult<web::Json<Vec<GlobalCourseModuleStatEntry>>> {
    let mut conn = pool.acquire().await?;
    let token = authorize(
        &mut conn,
        Act::ViewStats,
        Some(user.id),
        Res::GlobalPermissions,
    )
    .await?;
    let res = models::library::global_stats::get_course_module_stats_by_completions_registered_to_study_registry(&mut conn).await?;

    token.authorized_ok(web::Json(res))
}

/**
Add a route for each controller in this module.

The name starts with an underline in order to appear before other functions in the module documentation.

We add the routes by calling the route method instead of using the route annotations because this method preserves the function signatures for documentation.
*/
pub fn _add_routes(cfg: &mut ServiceConfig) {
    cfg.route(
        "/number-of-people-completed-a-course",
        web::get().to(get_number_of_people_completed_a_course),
    )
    .route(
        "/number-of-people-registered-completion-to-study-registry",
        web::get().to(get_number_of_people_registered_completion_to_study_registry),
    )
    .route(
        "/number-of-people-done-at-least-one-exercise",
        web::get().to(get_number_of_people_done_at_least_one_exercise),
    )
    .route(
        "/number-of-people-started-course",
        web::get().to(get_number_of_people_started_course),
    )
    .route(
        "/course-module-stats-by-completions-registered-to-study-registry",
        web::get().to(get_course_module_stats_by_completions_registered_to_study_registry),
    );
}