199 lines
7.9 KiB
C#
199 lines
7.9 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Net;
|
|
using System.Net.Http;
|
|
using System.Web.Http;
|
|
using ICSharpCode.SharpZipLib.Zip;
|
|
using Kreta.BusinessLogic.Helpers;
|
|
using Kreta.BusinessLogic.Security;
|
|
using Kreta.Enums.ManualEnums;
|
|
using Kreta.Resources;
|
|
using Kreta.Web.Areas.Orarend.Models;
|
|
using Kreta.Web.Helpers;
|
|
using Kreta.Web.Helpers.Error;
|
|
using Kreta.Web.Security;
|
|
|
|
namespace Kreta.Web.Areas.Orarend.ApiControllers
|
|
{
|
|
[ApiRoleClaimsAuthorize(true)]
|
|
[ApiRolePackageAuthorize(KretaClaimPackages.Adminisztrator.ClaimValue)]
|
|
public class OrarendekTorleseApiController : ApiController
|
|
{
|
|
[HttpPost]
|
|
[ApiValidateAjaxAntiForgeryToken]
|
|
public HttpResponseMessage FelhelyOrarendekTorlese(FelhelyOrarendTorleseParameterModel model)
|
|
{
|
|
var orarendTorleseHelper = new OrarendTorleseHelper(ConnectionTypeExtensions.GetSessionConnectionType());
|
|
(model.MinDatum, model.MaxDatum) = orarendTorleseHelper.GetMinMaxDatumValaszto();
|
|
|
|
if (!ValidateBaseModel(model, out var errorDictionary))
|
|
{
|
|
ModelState.AddRange(errorDictionary);
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(model.KivalasztottFelhelyekIds))
|
|
{
|
|
ModelState.AddModelError(nameof(model.KivalasztottFelhelyekIds), OrarendekTorleseResource.LegalabbEgyFeladatellatasiHelyKivalasztasaKotelezo);
|
|
}
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
try
|
|
{
|
|
var orarendekTorleseCo = model.ConvertToCo();
|
|
var excelFiles = orarendTorleseHelper.OrarendekTorlese(orarendekTorleseCo);
|
|
var base64ZipResult = CreateBase64ZipResult(excelFiles);
|
|
|
|
return Request.CreateResponse(HttpStatusCode.OK, base64ZipResult);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new StatusError(HttpStatusCode.BadRequest, ex.Message);
|
|
}
|
|
}
|
|
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ApiValidateAjaxAntiForgeryToken]
|
|
public HttpResponseMessage CsoportOrarendekTorlese(CsoportOrarendTorleseModel model)
|
|
{
|
|
var orarendTorleseHelper = new OrarendTorleseHelper(ConnectionTypeExtensions.GetSessionConnectionType());
|
|
(model.MinDatum, model.MaxDatum) = orarendTorleseHelper.GetMinMaxDatumValaszto();
|
|
|
|
if (!ValidateBaseModel(model, out var errorDictionary))
|
|
{
|
|
ModelState.AddRange(errorDictionary);
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(model.SelectedCsoportok))
|
|
{
|
|
ModelState.AddModelError(nameof(model.SelectedCsoportok), OrarendekTorleseResource.LegalabbEgyCsoportKivalasztasaKotelezo);
|
|
}
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
try
|
|
{
|
|
var orarendekTorleseCo = model.ConvertToCo();
|
|
var excelFiles = orarendTorleseHelper.OrarendekTorlese(orarendekTorleseCo);
|
|
var base64ZipResult = CreateBase64ZipResult(excelFiles);
|
|
|
|
return Request.CreateResponse(HttpStatusCode.OK, base64ZipResult);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new StatusError(HttpStatusCode.BadRequest, ex.Message);
|
|
}
|
|
}
|
|
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ApiValidateAjaxAntiForgeryToken]
|
|
public HttpResponseMessage OsztalyOrarendekTorlese(OsztalyOrarendTorleseModel model)
|
|
{
|
|
var orarendTorleseHelper = new OrarendTorleseHelper(ConnectionTypeExtensions.GetSessionConnectionType());
|
|
(model.MinDatum, model.MaxDatum) = orarendTorleseHelper.GetMinMaxDatumValaszto();
|
|
|
|
if (!ValidateBaseModel(model, out var errorDictionary))
|
|
{
|
|
ModelState.AddRange(errorDictionary);
|
|
}
|
|
|
|
if (string.IsNullOrWhiteSpace(model.SelectedOsztalyok))
|
|
{
|
|
ModelState.AddModelError(nameof(model.SelectedOsztalyok), OrarendekTorleseResource.LegalabbEgyOsztalyKivalasztasaKotelezo);
|
|
}
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
try
|
|
{
|
|
var orarendekTorleseCo = model.ConvertToCo();
|
|
var excelFiles = orarendTorleseHelper.OrarendekTorlese(orarendekTorleseCo);
|
|
var base64ZipResult = CreateBase64ZipResult(excelFiles);
|
|
|
|
return Request.CreateResponse(HttpStatusCode.OK, base64ZipResult);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new StatusError(HttpStatusCode.BadRequest, ex.Message);
|
|
}
|
|
}
|
|
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
|
|
}
|
|
|
|
private bool ValidateBaseModel(BaseOrarendTorleseModel model, out Dictionary<string, List<string>> errorDictionary)
|
|
{
|
|
errorDictionary = new Dictionary<string, List<string>>();
|
|
|
|
if (model.KezdeteDatum.HasValue && model.VegeDatum.HasValue && model.KezdeteDatum > model.VegeDatum)
|
|
{
|
|
AddErrorMessage(errorDictionary, nameof(model.KezdeteDatum), OrarendekTorleseResource.IntervallumKezdeteNemLehetNagyobbMintVege);
|
|
}
|
|
|
|
if (model.KezdeteDatum.HasValue && model.KezdeteDatum < model.MinDatum)
|
|
{
|
|
AddErrorMessage(errorDictionary, nameof(model.KezdeteDatum), OrarendekTorleseResource.IntervallumKezdeteNemLehetKisebbMintTanevKezdete);
|
|
}
|
|
|
|
if (model.VegeDatum.HasValue && model.VegeDatum > model.MaxDatum)
|
|
{
|
|
AddErrorMessage(errorDictionary, nameof(model.VegeDatum), OrarendekTorleseResource.IntervallumVegeNemLehetNagyobbMintTanevVege);
|
|
}
|
|
|
|
if (!Enum.TryParse(model.OraTipus, out OrarendekTorleseNezetTipusEnum _))
|
|
{
|
|
AddErrorMessage(errorDictionary, nameof(model.OraTipus), OrarendekTorleseResource.OraTipusNemMegfelelo);
|
|
}
|
|
|
|
return !errorDictionary.Any();
|
|
}
|
|
|
|
private void AddErrorMessage(Dictionary<string, List<string>> errorDictionary, string key, string message)
|
|
{
|
|
if (!errorDictionary.TryGetValue(key, out var messages))
|
|
{
|
|
messages = new List<string>();
|
|
errorDictionary.Add(key, messages);
|
|
}
|
|
messages.Add(message);
|
|
}
|
|
|
|
private string CreateBase64ZipResult(List<(string FileName, MemoryStream FileStream)> excelFiles)
|
|
{
|
|
string result = null;
|
|
if (excelFiles.Any())
|
|
{
|
|
using (var outputStream = new MemoryStream())
|
|
{
|
|
using (var zipOutputStream = new ZipOutputStream(outputStream))
|
|
{
|
|
var buffer = new byte[4096];
|
|
foreach (var (FileName, FileStream) in excelFiles)
|
|
{
|
|
var entry = new ZipEntry(FileName);
|
|
zipOutputStream.PutNextEntry(entry);
|
|
|
|
int sourceBytes;
|
|
do
|
|
{
|
|
sourceBytes = FileStream.Read(buffer, 0, buffer.Length);
|
|
zipOutputStream.Write(buffer, 0, sourceBytes);
|
|
} while (sourceBytes > 0);
|
|
}
|
|
|
|
zipOutputStream.Finish();
|
|
outputStream.Position = 0;
|
|
|
|
result = Convert.ToBase64String(outputStream.ToArray());
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
}
|
|
}
|