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 09dfac9104
commit d46a165a1a
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 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))

View file

@ -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,
}
}
}