markdown.rs 2.01 KB
use crate::models::markdown;
use crate::Pool;

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(pool: web::Data<Pool>)
    -> Result<HttpResponse, Error>
{
    Ok( web::block(move || markdown::get_markdowns(pool.into_inner()))
      . await
      . map(|markdowns| HttpResponse::Ok().json(markdowns))
      . map_err(|_| HttpResponse::InternalServerError())?
      )
}

pub async fn get_markdown( pool: web::Data<Pool>
                         , name: web::Path<String>
                         , patch: web::Query<Patchset>
                         ) -> Result<HttpResponse, Error>
{
    let pool = pool.into_inner();
    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( pool: web::Data<Pool>
                        , name: web::Path<String>
                        ) -> Result<HttpResponse, Error> {
    let pool = pool.into_inner();
    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( pool: web::Data<Pool>
                            , name: web::Path<String>
                            , item: web::Json<MarkdownJson> )
    -> Result<HttpResponse, Error>
{
    let pool = pool.into_inner();
    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())?)
}