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

211 lines
8.9 KiB
C#

using System.Collections.Generic;
using System.Data;
using System.Linq;
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.Logic;
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)]
public class BaseElektronikusUzenetekApiController : ApiController
{
[NonAction]
public DataSourceResult GetElektronikusUzenetekNaploNezetGrid(string data, [System.Web.Http.ModelBinding.ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request, SzervezetTipusEnum? szervezetTipus)
{
var model = JsonConvert.DeserializeObject<ElektronikusUzenetSearchModel>(data);
if (model?.CsoportIdSearch == null)
{
return new DataSourceResult();
}
TanevCO tanevCo;
var connectionType = ConnectionTypeExtensions.GetSessionConnectionType();
var helper = new TanevHelper(connectionType);
tanevCo = helper.GetTanevInfo();
var elsoTanitasiNap = tanevCo.OraFelvetelKezdete.Date;
var feljegyzesHelper = new FeljegyzesHelper(connectionType)
{
GridParameters = Converter.GridParameter(request),
};
var ds = feljegyzesHelper.GetElektronikusUzenetekNaploNezetGrid(model.CsoportIdSearch.Value, elsoTanitasiNap, (int?)szervezetTipus);
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
foreach (var row in ds.Tables[0].AsEnumerable())
{
row[nameof(FaliujsagBejegyzesekGridModel.Tartalom)] = CommonUtils.RemoveAllHtmlTags(row.Field<string>(nameof(FaliujsagBejegyzesekGridModel.Tartalom)));
}
}
var result = ds.ToDataSourceResult();
return result;
}
[NonAction]
public DataSourceResult GetElektronikusUzenetekNaploNezetReszletekGrid(int tanuloId, [System.Web.Http.ModelBinding.ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request, SzervezetTipusEnum? szervezetTipus)
{
var helper = new FeljegyzesHelper(ConnectionTypeExtensions.GetSessionConnectionType())
{
GridParameters = Converter.GridParameter(request),
};
var ds = helper.GetElektronikusUzenetekNaploNezetReszletekGrid(tanuloId, (int?)szervezetTipus, true);
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
foreach (var row in ds.Tables[0].AsEnumerable())
{
row[nameof(FaliujsagBejegyzesekGridModel.Tartalom)] = WebUtility.HtmlDecode(CommonUtils.RemoveAllHtmlTags(row.Field<string>(nameof(FaliujsagBejegyzesekGridModel.Tartalom))));
}
}
var result = ds.ToDataSourceResult();
return result;
}
public DataSourceResult GetElektronikusUzenetekListaNezetGrid(string data, [System.Web.Http.ModelBinding.ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request, SzervezetTipusEnum? szervezetTipus)
{
var model = JsonConvert.DeserializeObject<FeljegyzesekModel>(data);
if (model?.OsztalyCsoportId == null)
{
return new DataSourceResult();
}
var helper = new FeljegyzesHelper(ConnectionTypeExtensions.GetSessionConnectionType())
{
GridParameters = Converter.GridParameter(request),
};
var ds = helper.GetElektronikusUzenetekListaNezetGrid(model.OsztalyCsoportId.Value, (int?)szervezetTipus);
if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
foreach (var row in ds.Tables[0].AsEnumerable())
{
row[nameof(FaliujsagBejegyzesekGridModel.Tartalom)] = WebUtility.HtmlDecode(CommonUtils.RemoveAllHtmlTags(row.Field<string>(nameof(FaliujsagBejegyzesekGridModel.Tartalom))));
}
}
DataSourceResult result = ds.ToDataSourceResult();
return result;
}
public DataSourceResult GetElektronikusUzenetekModifyPopupGrid(int osztalycsoportId)
{
var helper = new FeljegyzesHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType());
var ds = helper.GetElektronikusUzenetekModifyPopupGrid(osztalycsoportId);
DataSourceResult result = ds.ToDataSourceResult();
return result;
}
[HttpPost]
[ApiValidateAjaxAntiForgeryToken]
public HttpResponseMessage SaveElektronikusUzenetPopup(ElektronikusUzenetPopupModel popupModel)
{
var model = popupModel.ToSaveModel();
var result = SaveElektronikusUzenet(model);
return result;
}
[HttpPost]
[ApiValidateAjaxAntiForgeryToken]
public HttpResponseMessage SaveElektronikusUzenet(ElektronikusUzenetModel 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 = RichTextLogic.GetLegalHtmlString(model.Tartalom, allowedTags);
ModelState.Merge(model.Validate());
if (ModelState.IsValid)
{
Dictionary<int, List<string>> validationMessageDictionary = new FeljegyzesHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()).ValidateElektronikusUzenetTanuloListOsztalybaTartozikDatumAlapjan(model.TanuloIdList, model.OsztalyCsoportId.Value, model.EsemenyDatuma.Value, model.Vege, model.IsFaliujsagMegjelenes);
var responseMessageDictionary = new Dictionary<string, List<string>>();
if (validationMessageDictionary.Any())
{
model.IsMindenkinek = false;
foreach (var validationMessageDictionaryKeyValuePair in validationMessageDictionary)
{
var validationMessage = string.Join(Core.Constants.General.Sortores, validationMessageDictionaryKeyValuePair.Value);
if (!responseMessageDictionary.ContainsKey("ValidationMessage"))
{
responseMessageDictionary.Add("ValidationMessage", new List<string> { validationMessage });
}
else
{
responseMessageDictionary["ValidationMessage"].Add(validationMessage);
}
model.TanuloIdList.Remove(validationMessageDictionaryKeyValuePair.Key);
}
}
_ = new FeljegyzesHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()).SaveFeljegyzes(model.ToCo());
return Request.CreateResponse(HttpStatusCode.OK, responseMessageDictionary);
}
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
}
[HttpPost]
[ApiValidateAjaxAntiForgeryToken]
public HttpResponseMessage DeleteElektronikusUzenet(FeljegyzesTorlesModel model)
{
try
{
new FeljegyzesHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()).DeleteElektronikusUzenetOrFaliujsagBejegyzes(model.Id);
return new HttpResponseMessage(HttpStatusCode.OK);
}
catch (CannotBeDeletedException ex)
{
throw new StatusError(HttpStatusCode.BadRequest, ex.Message);
}
}
[HttpPost]
[ApiValidateAjaxAntiForgeryToken]
public HttpResponseMessage DeleteElektronikusUzenetForTanulo(FeljegyzesTorlesModel model)
{
try
{
new FeljegyzesHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()).DeleteElektronikusUzenetForTanulo(model.Id, model.TanuloId);
return new HttpResponseMessage(HttpStatusCode.OK);
}
catch (CannotBeDeletedException ex)
{
throw new StatusError(HttpStatusCode.BadRequest, ex.Message);
}
}
}
}