headless_lms_server/controllers/cms/migration.rs
1//! Controllers for requests starting with `/api/v0/cms/migration`.
2
3use crate::domain::models_requests;
4use crate::domain::models_requests::JwtKey;
5use models::pages::CmsPageUpdate;
6
7use crate::{domain::request_id::RequestId, prelude::*};
8
9/**
10POST `/api/v0/cms/migration/new_page/{course_id}` - Create a new page from Gutenberg blocks.
11
12Creates a new page in the CMS. Accepts a `CmsPageUpdate` object; if `title` or `url_path` are empty
13they are derived from the first hero-section or heading block in `content`. The order number is
14automatically determined.
15
16# Example
17
18Request:
19
20```http
21POST /api/v0/cms/migration/new_page/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa HTTP/1.1
22Content-Type: application/json
23
24{
25 "content": [
26 {
27 "clientId": "...",
28 "isValid": true,
29 "name": "moocfi/hero-section",
30 "attributes": {
31 "title": "My Page Title"
32 },
33 "innerBlocks": []
34 },
35 {
36 "clientId": "...",
37 "isValid": true,
38 "name": "core/paragraph",
39 "attributes": {
40 "content": "Page content here"
41 },
42 "innerBlocks": []
43 }
44 ],
45 "exercises": [],
46 "exercise_slides": [],
47 "exercise_tasks": [],
48 "url_path": "",
49 "title": "",
50 "chapter_id": null,
51 "hidden": false
52}
53```
54*/
55
56#[instrument(skip(pool, jwt_key, app_conf, user, cms_update_json, course_id, request_id))]
57async fn create_page(
58 request_id: RequestId,
59 cms_update_json: web::Json<CmsPageUpdate>,
60 course_id: web::Path<Uuid>,
61 pool: web::Data<PgPool>,
62 jwt_key: web::Data<JwtKey>,
63 app_conf: web::Data<ApplicationConfiguration>,
64 user: AuthUser,
65) -> ControllerResult<web::Json<Uuid>> {
66 let mut conn = pool.acquire().await?;
67 let token = authorize(&mut conn, Act::Edit, Some(user.id), Res::Course(*course_id)).await?;
68
69 let result = models::library::migration::create_page(
70 &mut conn,
71 *course_id,
72 cms_update_json.into_inner(),
73 user.id,
74 models_requests::make_spec_fetcher(
75 app_conf.base_url.clone(),
76 request_id.0,
77 jwt_key.into_inner(),
78 ),
79 models_requests::fetch_service_info,
80 )
81 .await?;
82
83 token.authorized_ok(web::Json(result))
84}
85
86/**
87Add a route for each controller in this module.
88
89The name starts with an underline in order to appear before other functions in the module documentation.
90
91We add the routes by calling the route method instead of using the route annotations because this method preserves the function signatures for documentation.
92*/
93pub fn _add_routes(cfg: &mut ServiceConfig) {
94 cfg.route("/new_page/{course_id}", web::post().to(create_page));
95}