kreta/KretaWeb/Areas/Tantargy/ApiControllers/TanorakApiController.cs
2024-03-13 00:33:46 +01:00

113 lines
4.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using Kendo.Mvc.UI;
using Kreta.BusinessLogic.HelperClasses;
using Kreta.BusinessLogic.Helpers;
using Kreta.BusinessLogic.Logic;
using Kreta.BusinessLogic.Security;
using Kreta.Core;
using Kreta.Framework.Util;
using Kreta.Resources;
using Kreta.Web.Areas.Tantargy.Models;
using Kreta.Web.Helpers;
using Kreta.Web.Helpers.Error;
using Kreta.Web.Helpers.Grid;
using Kreta.Web.Security;
using Newtonsoft.Json;
namespace Kreta.Web.Areas.Tantargy.ApiControllers
{
[ApiRoleClaimsAuthorize(true)]
[ApiRolePackageAuthorize(KretaClaimPackages.Naplo.ClaimValue)]
public class TanorakApiController : ApiController
{
public DataSourceResult GetTanorakGrid(string data, [ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request)
{
TanorakSearchModel model = JsonConvert.DeserializeObject<TanorakSearchModel>(data);
var helper = new TanoraHelper(ConnectionTypeExtensions.GetSessionConnectionType());
helper.GridParameters = Converter.GridParameter(request);
var ret = helper.GetTanorak(ConvertTanorakSearchModelToTanorakSearchCO(model));
return ret.ToDataSourceResult();
}
public DataSourceResult GetTanorakGridForNaplozas(int osztalyCsoportId, int tantargyId, DateTime oraKezdete, DataSourceRequest request)
{
var model = new TanorakSearchModel
{
OsztalyCsoportId = osztalyCsoportId,
TantargyId = tantargyId,
OraKezdete = oraKezdete
};
var (gridParameter, modelList) = GetGridData(model, request);
return modelList.ToDataSourceResult(gridParameter);
}
public HttpResponseMessage GetExport(int osztalyCsoportId, int tantargyId, DataSourceRequest request)
{
try
{
var model = new TanorakSearchModel
{
OsztalyCsoportId = osztalyCsoportId,
TantargyId = tantargyId
};
var (gridParameter, modelList) = GetGridData(model, request);
modelList = modelList.SortingAndPaging(gridParameter.OrderDictionary);
var simpleExportColumnCos = SimpleExportLogic.GetSimpleExportColumnCos<TanorakGridModel>(TanorakGridModel.TanorakExportAttributeId);
var memoryStream = SimpleExportLogic.GetExport(OrarendResource.KorabbiOrakNaplozasiAdataiExportSheetName, simpleExportColumnCos, modelList, ClaimData.SelectedTanevID.Value);
return HttpResponseExtensions.GetFileHttpResponse(memoryStream.ToArray(), $"{OrarendResource.KorabbiOrakNaplozasiAdatai_Export}_{DateTime.Now:yyyy_MM_dd}.xlsx");
}
catch (Exception ex)
{
throw new StatusError(HttpStatusCode.BadRequest, ErrorResource.HibaTortentAFajlExportalasaKozben) { UnHandledException = ex };
}
}
private (GridParameters gridParameter, List<TanorakGridModel> modelList) GetGridData(TanorakSearchModel model, DataSourceRequest request)
{
var gridParameter = Converter.GridParameter(request);
var coList = new TanoraHelper(ConnectionTypeExtensions.GetSessionConnectionType()).GetTanorakGridForNaplozasCoList(ConvertTanorakSearchModelToTanorakSearchCO(model));
var modelList = new List<TanorakGridModel>();
foreach (var co in coList)
{
var gridModel = new TanorakGridModel(co);
modelList.Add(gridModel);
}
return (gridParameter, modelList);
}
private TanorakSearchCO ConvertTanorakSearchModelToTanorakSearchCO(TanorakSearchModel model)
{
TanorakSearchCO co = new TanorakSearchCO
{
IdoszakKezdete = model.IdoszakKezdete,
IdoszakVege = model.IdoszakVege,
OsztalyCsoportId = model.OsztalyCsoportId,
Helyetesitett = model.Helyetesitett,
TantargyId = model.TantargyId,
TanarId = model.TanarId,
OraKezdete = model.OraKezdete,
};
return co;
}
}
}