Move inner label logic from api to label module

This commit is contained in:
Simon Bruder 2024-07-12 17:28:46 +02:00
parent 37c7def8d1
commit ce171225be
Signed by: simon
GPG key ID: 347FF8699CDA0776
2 changed files with 17 additions and 14 deletions

View file

@ -8,7 +8,7 @@ use serde::Deserialize;
use sqlx::PgPool; use sqlx::PgPool;
use uuid::Uuid; use uuid::Uuid;
use crate::label::{Label, LabelPage, LabelPreset}; use crate::label::{Label, LabelPreset};
use crate::manage; use crate::manage;
pub fn config(cfg: &mut web::ServiceConfig) { pub fn config(cfg: &mut web::ServiceConfig) {
@ -39,18 +39,7 @@ async fn items(
.await .await
.map_err(error::ErrorInternalServerError)?; .map_err(error::ErrorInternalServerError)?;
let label_config = params.preset.clone().into(); let label = Label::for_items(&items, params.preset.clone().into());
let label = Label {
pages: items
.into_iter()
.map(|item| LabelPage {
id: Some(item.id),
short_id: Some(format!("{:06}", item.short_id)),
})
.collect(),
config: label_config,
};
Ok(HttpResponse::Ok() Ok(HttpResponse::Ok()
.insert_header(ContentType(mime::APPLICATION_PDF)) .insert_header(ContentType(mime::APPLICATION_PDF))

View file

@ -8,6 +8,7 @@ mod preset;
use std::fs::File; use std::fs::File;
use std::sync::OnceLock; use std::sync::OnceLock;
use barcode::{encode_code128, encode_data_matrix};
use pdf::{IndirectFontRef, PdfLayerReference}; use pdf::{IndirectFontRef, PdfLayerReference};
use printpdf as pdf; use printpdf as pdf;
use printpdf::{ImageTransform, Mm, PdfDocument, PdfDocumentReference, Pt, Px}; use printpdf::{ImageTransform, Mm, PdfDocument, PdfDocumentReference, Pt, Px};
@ -15,7 +16,7 @@ use rust_fontconfig::{FcFontCache, FcPattern};
use thiserror::Error; use thiserror::Error;
use uuid::Uuid; use uuid::Uuid;
use barcode::{encode_code128, encode_data_matrix}; use crate::models::Item;
pub use preset::LabelPreset; pub use preset::LabelPreset;
#[derive(Error, Debug)] #[derive(Error, Debug)]
@ -236,4 +237,17 @@ impl Label {
Ok(doc.ok_or(Error::NoPages)?.save_to_bytes()?) Ok(doc.ok_or(Error::NoPages)?.save_to_bytes()?)
} }
pub fn for_items(items: &[Item], config: LabelConfig) -> Self {
Label {
pages: items
.iter()
.map(|item| LabelPage {
id: Some(item.id),
short_id: Some(format!("{:06}", item.short_id)),
})
.collect(),
config,
}
}
} }