From ce171225be526a9a301185fdd84b22b63239cdc7 Mon Sep 17 00:00:00 2001 From: Simon Bruder Date: Fri, 12 Jul 2024 17:28:46 +0200 Subject: [PATCH] Move inner label logic from api to label module --- src/api/v1/label.rs | 15 ++------------- src/label/mod.rs | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/src/api/v1/label.rs b/src/api/v1/label.rs index 4a36ee2..f7c6f55 100644 --- a/src/api/v1/label.rs +++ b/src/api/v1/label.rs @@ -8,7 +8,7 @@ use serde::Deserialize; use sqlx::PgPool; use uuid::Uuid; -use crate::label::{Label, LabelPage, LabelPreset}; +use crate::label::{Label, LabelPreset}; use crate::manage; pub fn config(cfg: &mut web::ServiceConfig) { @@ -39,18 +39,7 @@ async fn items( .await .map_err(error::ErrorInternalServerError)?; - let label_config = 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, - }; + let label = Label::for_items(&items, params.preset.clone().into()); Ok(HttpResponse::Ok() .insert_header(ContentType(mime::APPLICATION_PDF)) diff --git a/src/label/mod.rs b/src/label/mod.rs index 71d2cfc..97874f1 100644 --- a/src/label/mod.rs +++ b/src/label/mod.rs @@ -8,6 +8,7 @@ mod preset; use std::fs::File; use std::sync::OnceLock; +use barcode::{encode_code128, encode_data_matrix}; use pdf::{IndirectFontRef, PdfLayerReference}; use printpdf as pdf; use printpdf::{ImageTransform, Mm, PdfDocument, PdfDocumentReference, Pt, Px}; @@ -15,7 +16,7 @@ use rust_fontconfig::{FcFontCache, FcPattern}; use thiserror::Error; use uuid::Uuid; -use barcode::{encode_code128, encode_data_matrix}; +use crate::models::Item; pub use preset::LabelPreset; #[derive(Error, Debug)] @@ -236,4 +237,17 @@ impl Label { 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, + } + } }