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

114 lines
4.7 KiB
C#

using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Kendo.Mvc.UI;
using Kreta.BusinessLogic.Exceptions;
using Kreta.BusinessLogic.HelperClasses;
using Kreta.BusinessLogic.Helpers;
using Kreta.BusinessLogic.Security;
using Kreta.BusinessLogic.Utils;
using Kreta.Enums.ManualEnums;
using Kreta.Web.Areas.Feljegyzes.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.Feljegyzes.ApiControllers
{
[ApiRoleClaimsAuthorize(true)]
[ApiRolePackageAuthorize(KretaClaimPackages.Tanar.ClaimValue, KretaClaimPackages.Osztalyfonok.ClaimValue, KretaClaimPackages.SzuperOsztalyfonok.ClaimValue, KretaClaimPackages.Naplo.ClaimValue)]
public class BaseBeirasokApiController : ApiController
{
[NonAction]
public DataSourceResult GetBeirasokGrid(string data, [System.Web.Http.ModelBinding.ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request, SzervezetTipusEnum? szervezetTipus)
{
var model = JsonConvert.DeserializeObject<FeljegyzesekSearchModel>(data);
if (model?.CsoportIdSearch == null)
{
return new DataSourceResult();
}
var helper = new TanevHelper(ConnectionTypeExtensions.GetSessionConnectionType());
var tanevCo = helper.GetTanevInfo();
var elsoTanitasiNap = tanevCo.OraFelvetelKezdete.Date;
var feljegyzesHelper = new FeljegyzesHelper(ConnectionTypeExtensions.GetSessionConnectionType())
{
GridParameters = Converter.GridParameter(request),
};
var ds = feljegyzesHelper.GetBeirasokGrid(model.CsoportIdSearch.Value, elsoTanitasiNap, (int?)szervezetTipus);
var result = ds.ToDataSourceResult();
return result;
}
[ApiRolePackageAuthorize(KretaClaimPackages.FelhasznaloMunkakoreNemTiltoListas.ClaimValue, KretaClaimPackages.Osztalyfonok.ClaimValue, KretaClaimPackages.SzuperOsztalyfonok.ClaimValue, KretaClaimPackages.Naplo.ClaimValue)]
public DataSourceResult GetBeirasokReszletekGrid(int tanuloId, [System.Web.Http.ModelBinding.ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request, SzervezetTipusEnum? szervezetTipus)
{
var helper = new FeljegyzesHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType())
{
GridParameters = Converter.GridParameter(request),
};
var ds = helper.GetBeirasokReszletekForGrid(tanuloId, (int?)szervezetTipus, true);
var result = ds.ToDataSourceResult();
return result;
}
[HttpPost]
[ApiValidateAjaxAntiForgeryToken]
public HttpResponseMessage SaveBeiras(BeirasModel model)
{
var allowedTags = new List<string> {
"span", "#text", "b", "blockquote",
"code", "del", "dd", "dl", "dt",
"em", "h1", "h2", "h3", "i", "kbd",
"li", "ol", "p", "pre", "s", "sup",
"sub", "strong", "strike", "ul", "br", "hr", "a", "div"
};
model.Tartalom = CommonUtils.RemoveNotAllowedHtmlTags(WebUtility.HtmlDecode(model.Tartalom), allowedTags);
ModelState.Merge(model.Validate());
var co = model.ToCo();
var feljegyzesHelper = new FeljegyzesHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType());
var isTanuloTagjaOsztalynakByEsemenyDatumError = feljegyzesHelper.IsTanuloTagjaOsztalyCsoportnakByEsemenyDatum(co);
if (!string.IsNullOrWhiteSpace(isTanuloTagjaOsztalynakByEsemenyDatumError))
{
ModelState.AddModelError("EsemenyDatuma", isTanuloTagjaOsztalynakByEsemenyDatumError);
}
if (ModelState.IsValid)
{
_ = feljegyzesHelper.SaveFeljegyzes(co, isBeiras: true);
return new HttpResponseMessage(HttpStatusCode.OK);
}
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
[HttpPost]
[ApiValidateAjaxAntiForgeryToken]
public HttpResponseMessage DeleteBeiras(FeljegyzesTorlesModel model)
{
try
{
new FeljegyzesHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()).DeleteBeiras(model.Id, model.TanuloId);
return new HttpResponseMessage(HttpStatusCode.OK);
}
catch (CannotBeDeletedException ex)
{
throw new StatusError(HttpStatusCode.BadRequest, ex.Message);
}
}
}
}