55 lines
1.8 KiB
C#
55 lines
1.8 KiB
C#
|
|
using CloudPrint.Client.Models;
|
||
|
|
|
||
|
|
namespace CloudPrint.Client.Services.Grpc;
|
||
|
|
|
||
|
|
public static class CloudPrintGrpcMapper
|
||
|
|
{
|
||
|
|
public static PrintJob ToPrintJob(Cloudprint.PrintJob backendJob)
|
||
|
|
{
|
||
|
|
var orderItem = backendJob.OrderItem;
|
||
|
|
var file = orderItem?.File;
|
||
|
|
|
||
|
|
return new PrintJob
|
||
|
|
{
|
||
|
|
Id = backendJob.Id,
|
||
|
|
JobNo = backendJob.JobNo,
|
||
|
|
PrinterId = backendJob.PrinterId,
|
||
|
|
OrderItemId = backendJob.OrderItemId,
|
||
|
|
FileName = file?.FileName ?? backendJob.JobNo,
|
||
|
|
FilePath = file?.FilePath ?? string.Empty,
|
||
|
|
FileId = orderItem?.FileId ?? 0,
|
||
|
|
Copies = Math.Max(1, orderItem?.Copies ?? 1),
|
||
|
|
Color = orderItem?.Color ?? false,
|
||
|
|
Duplex = orderItem?.Duplex ?? false,
|
||
|
|
PageRange = string.IsNullOrWhiteSpace(orderItem?.PageRange) ? "all" : orderItem.PageRange,
|
||
|
|
Status = FromBackendStatus(backendJob.Status),
|
||
|
|
Progress = backendJob.Progress,
|
||
|
|
ErrorMessage = backendJob.ErrorMsg
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
public static string ToBackendStatus(PrintJobStatus status)
|
||
|
|
{
|
||
|
|
return status switch
|
||
|
|
{
|
||
|
|
PrintJobStatus.Pending => "pending",
|
||
|
|
PrintJobStatus.Printing => "printing",
|
||
|
|
PrintJobStatus.Completed => "completed",
|
||
|
|
PrintJobStatus.Failed => "failed",
|
||
|
|
PrintJobStatus.Cancelled => "cancelled",
|
||
|
|
_ => "pending"
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
private static PrintJobStatus FromBackendStatus(string status)
|
||
|
|
{
|
||
|
|
return status?.ToLowerInvariant() switch
|
||
|
|
{
|
||
|
|
"printing" => PrintJobStatus.Printing,
|
||
|
|
"completed" => PrintJobStatus.Completed,
|
||
|
|
"failed" => PrintJobStatus.Failed,
|
||
|
|
"cancelled" => PrintJobStatus.Cancelled,
|
||
|
|
_ => PrintJobStatus.Pending
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|