using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.Http.ModelBinding; using Kendo.Mvc.UI; using Kreta.BusinessLogic.Classes.ComboBox; using Kreta.BusinessLogic.Helpers; using Kreta.BusinessLogic.Logic; using Kreta.BusinessLogic.Security; using Kreta.Client.KGR; using Kreta.Core; using Kreta.Resources; using Kreta.Web.Areas.GRModul.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.GRModul.ApiControllers { [ApiRoleClaimsAuthorize(true)] [ApiRolePackageAuthorize(KretaClaimPackages.Adminisztrator.ClaimValue)] public class KotvallKezelesApiController : ApiController { private readonly IKGRClient _kgrClient; public KotvallKezelesApiController(IKGRClient kgrClient) { _kgrClient = kgrClient ?? throw new ArgumentNullException(nameof(kgrClient)); } public DataSourceResult GetKotvallGrid(string data, [ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request) { var model = JsonConvert.DeserializeObject(data); var helper = new KotvallKezelesHelper(ConnectionTypeExtensions.GetSessionConnectionType()) { GridParameters = Converter.GridParameter(request), }; var result = helper.GetKotvallItemList(_kgrClient, ClaimData.IntezmenyGuid ?? throw new ApplicationException(ErrorResource.NincsAzIskolanakEgyediAzonositoja), model.ConvertToCo()); var gridModel = result.Select(x => new KotvallGridModel(x, ClaimData.FelhasznaloIdpEgyediAzonosito)).ToList(); return gridModel.ToDataSourceResult(helper.GridParameters); } public HttpResponseMessage GetKotvallExport(string data, [ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request) { try { var model = JsonConvert.DeserializeObject(data); var helper = new KotvallKezelesHelper(ConnectionTypeExtensions.GetSessionConnectionType()) { GridParameters = Converter.GridParameter(request), }; var result = helper.GetKotvallItemListExport(_kgrClient, ClaimData.IntezmenyGuid ?? throw new ApplicationException(ErrorResource.NincsAzIskolanakEgyediAzonositoja), model.ConvertToCo()); var gridModelList = result.Select(x => new KotvallGridModel(x, ClaimData.FelhasznaloIdpEgyediAzonosito)).ToList(); gridModelList = gridModelList.SortingAndPaging(helper.GridParameters.OrderDictionary); var simpleExportColumnCos = SimpleExportLogic.GetSimpleExportColumnCos(KotvallGridModel.KotvallGridExportAttributeId); var memoryStream = SimpleExportLogic.GetExport(KotvallKezelesResource.KotVallGridExportSheetName, simpleExportColumnCos, gridModelList, ClaimData.SelectedTanevID.Value); return HttpResponseExtensions.GetFileHttpResponse(memoryStream.ToArray(), $"{KotvallKezelesResource.ExportFilename}.xlsx"); } catch (Exception ex) { if (ex.Data["IntezmenyBeszerzesHiba"] != null) { throw new StatusError(HttpStatusCode.BadRequest, null) { Json = ex.Data["IntezmenyBeszerzesHiba"], }; } throw new StatusError(HttpStatusCode.BadRequest, ex.Message, ex); } } public List GetKotvallStatusList() { return new KotvallKezelesHelper(ConnectionTypeExtensions.GetSessionConnectionType()).GetKotvallStatusList(_kgrClient).Select(x => new ComboBoxListItem() { Text = x.Nev, Value = x.Id.ToString() }).ToList(); } public List GetKotvallTargyList() { return new KotvallKezelesHelper(ConnectionTypeExtensions.GetSessionConnectionType()).GetKotvallTargyList(_kgrClient).Select(x => new ComboBoxListItem() { Text = x.Nev, Value = x.Id.ToString() }).ToList(); } public HttpResponseMessage SaveBeszerzesiIgeny(KotvallModel model) { if (ModelState.IsValid) { try { model.IntezmenyEgyediAzonosito = ClaimData.IntezmenyGuid ?? throw new ApplicationException(ErrorResource.NincsAzIskolanakEgyediAzonositoja); var (oktatasiAzonosito, emailCim, elotag, vezeteknev, utonev) = new FelhasznaloHelper(ConnectionTypeExtensions.GetSessionConnectionType()).GetUserDetailsForKotVall(); model.IdpEgyediAzonosito = ClaimData.FelhasznaloIdpEgyediAzonosito; model.TanevId = ClaimData.SelectedTanevID; model.LetrehozoEmail = emailCim; model.OktatasiAzonosito = oktatasiAzonosito; model.EloTag = elotag; model.Utonev = utonev; model.CsaladiNev = vezeteknev; if (!model.VisMajorIgeny) { model.VisMajorIndok = string.Empty; } new KotvallKezelesHelper(ConnectionTypeExtensions.GetSessionConnectionType()).SaveKotvallItem(_kgrClient, KotvallModel.ConvertToCo(model)); return new HttpResponseMessage(HttpStatusCode.OK); } catch (Exception ex) { if (ex.Data["IntezmenyBeszerzesHiba"] != null) { throw new StatusError(HttpStatusCode.BadRequest, null) { Json = ex.Data["IntezmenyBeszerzesHiba"], }; } throw new StatusError(HttpStatusCode.BadRequest, ex.Message, ex); } } return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } [HttpPost] [ApiValidateAjaxAntiForgeryToken] public HttpResponseMessage Visszavonas([FromBody] int id) { var connectionType = ConnectionTypeExtensions.GetSessionConnectionType(); var helper = new KotvallKezelesHelper(connectionType); var intezmenyGuid = ClaimData.IntezmenyGuid ?? throw new ApplicationException(ErrorResource.NincsAzIskolanakEgyediAzonositoja); var visszavonando = helper.GetKotvallItem(_kgrClient, intezmenyGuid, id); if (!new KotvallGridModel(visszavonando, ClaimData.FelhasznaloIdpEgyediAzonosito).IsDeleteEnabled) { throw new StatusError(HttpStatusCode.Forbidden, ErrorResource.NincsJogaAMuveletVegrehajtasahoz); } var (oktatasiAzonosito, emailCim, elotag, vezeteknev, utonev) = new FelhasznaloHelper(connectionType).GetUserDetailsForKotVall(); var model = new KotvallVisszavonasModel { Id = id, IntezmenyEgyediAzonosito = intezmenyGuid, IdpEgyediAzonosito = ClaimData.FelhasznaloIdpEgyediAzonosito, CsaladiNev = vezeteknev, Utonev = utonev, EloTag = elotag, OktatasiAzonosito = oktatasiAzonosito, }; try { helper.KotvallVisszavonas(_kgrClient, model.ConvertToCo()); return new HttpResponseMessage(HttpStatusCode.OK); } catch (Exception ex) { if (ex.Data["IntezmenyBeszerzesHiba"] != null) { throw new StatusError(HttpStatusCode.BadRequest, null) { Json = ex.Data["IntezmenyBeszerzesHiba"], }; } throw new StatusError(HttpStatusCode.BadRequest, ex.Message, ex); } } } }