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

130 lines
4.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Data;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.ModelBinding;
using System.Web.Http.Results;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using Kreta.BusinessLogic.Classes.ComboBox;
using Kreta.BusinessLogic.HelperClasses;
using Kreta.BusinessLogic.Helpers;
using Kreta.BusinessLogic.Security;
using Kreta.BusinessLogic.Utils;
using Kreta.Core.FileService;
using Kreta.Enums;
using Kreta.Framework;
using Kreta.Framework.Util;
using Kreta.Web.Areas.Adatszolgaltatasok.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.Adatszolgaltatasok.ApiControllers
{
[ApiRoleClaimsAuthorize(true)]
[ApiRolePackageAuthorize(KretaClaimPackages.Adminisztrator.ClaimValue)]
public class MerohelyApiController : ApiController
{
private readonly IFileService FileService;
public MerohelyApiController(IFileService fileService)
{
FileService = fileService;
}
public DataSourceResult GetMerohelyGrid(string data, [ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request)
{
MerohelySearchModel model = JsonConvert.DeserializeObject<MerohelySearchModel>(data);
var merohelyHelper = new MerohelyHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType());
merohelyHelper.GridParameters = Converter.GridParameter(request);
var merohelyList = merohelyHelper.MerohelyKereses(model.MerohelyNevSearch, model.MerohelyTipusIdSearch, model.MerohelyMukodesiHelyIdSearch, model.IsMeroallasFrissiteseSzuksegesSearch);
return merohelyList.ToDataSourceResult();
}
public JsonResult<List<ComboBoxListItem>> GetMerohelyTipusList([DataSourceRequest] DataSourceRequest request)
{
return Json(((int)GeneratedAdatszotarTipusEnum.MerohelyTipus).GetItemsByType(ClaimData.SelectedTanevID.Value, true).ToComboBoxItemList());
}
public DataSourceResult GetMeroallasDetailGrid(int merohelyId)
{
DataSet meroallasList = new MerohelyHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()).GetMeroallasListByMerohelyId(merohelyId);
return meroallasList.ToDataSourceResult();
}
#region Meroallas
[HttpPost]
public HttpResponseMessage SaveMeroallas(MeroallasModel model)
{
ModelState.Merge(model.Validate());
if (ModelState.IsValid)
{
try
{
new MerohelyHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()).SaveMeroallas(model.ConvertToCo(), FileService);
}
catch (KretaError kretaError)
{
SDAServer.Instance.Logger.ExceptionThrown(kretaError);
ModelState.AddModelError("Error", kretaError.Message);
}
if (ModelState.IsValid)
{
return new HttpResponseMessage(HttpStatusCode.OK);
}
}
var httpResponseMessage = Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
return httpResponseMessage;
}
[HttpPost]
public HttpResponseMessage DeleteMeroallas([FromBody] int id)
{
try
{
var merohelyHelper = new MerohelyHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType());
MeroallasCO co = merohelyHelper.GetMeroallasById(id);
if (co.KepId.HasValue)
{
merohelyHelper.DeleteFileAndFileData(FileService, co.KepId.Value);
}
merohelyHelper.DeleteMeroallasById(id);
return new HttpResponseMessage(HttpStatusCode.OK);
}
catch (Exception exception)
{
throw new StatusError(HttpStatusCode.InternalServerError,
StringResourcesUtil.GetString(4358) /*Hiba történt törlés közben!*/)
{
UnHandledException = exception
};
}
}
[HttpGet]
[HttpPost]
public IHttpActionResult GetPicture(int meroallasId, int pictureId, [ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request)
{
if (meroallasId > 0 && pictureId > 0)
{
return Json(new MerohelyHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()).GetPictureList(FileService, meroallasId).ToDataSourceResult(request));
}
else
{
return Json(new { });
}
}
#endregion Meroallas
}
}