markdown.rs
2.2 KB
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
use crate::{models::markdown, AppData};
use actix_web::{Error, HttpResponse, web};
use anyhow::Result;
use artshop_common::types::MarkdownJson;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
pub struct Patchset {
patch: Option<i32>,
}
pub async fn get_markdowns(app_data: web::Data<AppData>)
-> Result<HttpResponse, Error>
{
let pool = app_data.database_pool.clone();
Ok( web::block(move || markdown::get_markdowns(pool))
. await
. map(|markdowns| HttpResponse::Ok().json(markdowns))
. map_err(|_| HttpResponse::InternalServerError())?
)
}
pub async fn get_markdown( app_data: web::Data<AppData>
, name: web::Path<String>
, patch: web::Query<Patchset>
) -> Result<HttpResponse, Error>
{
let pool = app_data.database_pool.clone();
let name = name.into_inner();
let patch = patch.into_inner();
Ok( web::block(move || markdown::get_markdown( pool
, name.as_str()
, patch.patch) )
. await
. map(|markdowns| HttpResponse::Ok().json(markdowns))
. map_err(|_| HttpResponse::InternalServerError())?
)
}
pub async fn get_patches( app_data: web::Data<AppData>
, name: web::Path<String>
) -> Result<HttpResponse, Error> {
let pool = app_data.database_pool.clone();
let name = name.into_inner();
Ok( web::block(move || markdown::get_patches(pool, name.as_str()))
. await
. map(|patches| HttpResponse::Ok().json(patches))
. map_err(|_| HttpResponse::InternalServerError())?
)
}
pub async fn update_markdown( app_data: web::Data<AppData>
, name: web::Path<String>
, item: web::Json<MarkdownJson> )
-> Result<HttpResponse, Error>
{
let pool = app_data.database_pool.clone();
let name = name.into_inner();
let item = item.into_inner();
Ok(web::block(move || markdown::update_markdown(pool, name, item))
. await
. map(|markdown| HttpResponse::Ok().json(markdown))
. map_err(|_| HttpResponse::InternalServerError())?)
}