markdown.rs 2.2 KB
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())?)
}