Files
laatzen/LaaProductionWeb/LaaProduction.Web/Controllers/ProtocolController.cs
T

163 lines
5.1 KiB
C#

namespace LaaProduction.Web.Controllers
{
using LaaProduction.Personalization;
using LaaProduction.Web.App_Infrastructure;
using LaaProduction.Services.Interfaces;
using LaaProduction.Services.Models;
using LaaProductionDI;
using System.Web.Mvc;
using System.Collections.Generic;
using System.Linq;
[AllowedRoles(EmployeesRoles.WEB_PuneProtokoll)]
public class ProtocolController : Controller
{
public const string MessagePDF = nameof(MessagePDF);
public const string Succeeded = nameof(Succeeded);
private readonly IProtocolService protocolService;
public ProtocolController()
=> this.protocolService = LaaServiceProvider.GetService<IProtocolService>();
[HttpGet]
public ActionResult Index(int orderId = 0)
{
var model = this.protocolService.FindOrder(orderId);
return this.View(model);
}
[HttpPost]
public ActionResult Index(int orderId, int clientId/*OrderClientModel model*/)
{
if (orderId > 0 && clientId > 0)
{
var files = this.Request.Files;
if (files.Count > 0)
{
var file = files.Get(0);
if (file.ContentType == "application/pdf")
{
var savedId = this.protocolService.SaveFile(file.InputStream, clientId, orderId);
if (savedId != 0)
{
if (savedId > 0)
{
this.TempData[MessagePDF] = $"Der PDF Datei wurde erfolgreich hochgeladen.";
this.TempData[Succeeded] = true;
}
else if (savedId < 0)
{
this.TempData[MessagePDF] = $"Der PDF Datei ist bereits hochgeladen.";
}
}
else
{
this.TempData[MessagePDF] = $"Der PDF Datei könte nicht hochgeladen werden.";
}
}
else
{
this.TempData[MessagePDF] = $"Bitte, PDF Datei hochladen.";
}
}
else
{
this.TempData[MessagePDF] = "Kein PDF Datei wurde ausgewählt.";
}
}
else
{
this.TempData[MessagePDF] = $"Bitte Auftrag auswählen.";
}
return this.RedirectToAction(nameof(Index), new { orderId });
}
[HttpGet]
public ActionResult Files(int orderId, int? fileId = null)
{
var files = this.protocolService.GetFiles(orderId);
if (!files.Any())
{
return this.RedirectToAction(nameof(Index), new { orderId });
}
var selectedFile = default(KeyValuePair<int, string>);
if (fileId is null || !files.ContainsKey(fileId.Value))
{
selectedFile = files.FirstOrDefault();
fileId = selectedFile.Key;
}
else
{
selectedFile = files.FirstOrDefault(x => x.Key == fileId.Value);
}
var model = new OrderFiles
{
OrderId = orderId,
FileId = fileId.Value,
FileContent = selectedFile.Value,
Files = files.Select(x => x.Key)
};
return this.View(model);
}
[HttpGet]
public ActionResult DeleteFile(int fileId, int orderId)
{
this.protocolService.DeleteFile(fileId);
return this.RedirectToAction(nameof(this.Files), new { orderId });
}
public ActionResult Clients(string search = null)
{
var clientsOrders = this.protocolService.FindOrdersByClient(search);
return this.PartialView(clientsOrders);
}
public ActionResult Orders(int search = 0)
{
var clientsOrders = this.protocolService.FindOrdersById(search);
return this.PartialView(clientsOrders);
}
[HttpPost]
public ActionResult Print(int orderId = 0, short[] positions = null)
{
var model = this.protocolService.FindOrder(orderId);
if (positions?.Any() ?? false)
{
foreach (var position in model.Positions.Keys.ToArray())
{
model.Positions[position] = false;
}
foreach (var position in positions)
{
model.Positions[position] = true;
}
model.Results = model
.Results
.Where(x => positions.Contains(x.PosNr));
}
return this.View(model);
}
}
}