using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.Http.Results; using System.Web.Mvc; 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.Enums; 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.Tanar.ClaimValue)] public class BaseTanmenetApiController : ApiController { public DataSourceResult GetTanmenetGrid(string data, [System.Web.Http.ModelBinding.ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request) { var model = JsonConvert.DeserializeObject(data); model.FeltoltoIdSearch = ClaimData.FelhasznaloId; var thelper = new TanmenetHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()); thelper.GridParameters = Converter.GridParameter(request); TanmenetKereseseCO co = ConvertSearchModelToCo(model); DataSet tanmenetek = thelper.GetTanmenetek(co); return tanmenetek.ToDataSourceResult(); } public string GetTanmenetModalHeader(int tantargyId, int osztalyId) { Enum.TryParse(new OsztalyCsoportHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()).GetOsztalyCsoportFeladatKategoria(osztalyId).ToString(), out OktNevelesiKategoriaEnum kategoria); return new TanmenetHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()).GetTanmenetModalHeader(ClaimData.FelhasznaloId, tantargyId, osztalyId, kategoria); } public DataSourceResult GetTanmenetOrak(int foglalkozasId, int TantargyId, int OsztalyId, [System.Web.Http.ModelBinding.ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request) { var thelper = new TanmenetHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()); thelper.GridParameters = Converter.GridParameter(request); DataSet tanmenetek = thelper.GetTanmenetek(TantargyId, OsztalyId, ClaimData.FelhasznaloId, foglalkozasId: foglalkozasId); return tanmenetek.ToDataSourceResult(); } [System.Web.Http.HttpPost] public HttpResponseMessage SaveTanmenet(TanmenetModel model) { if (model.TanmenetBejegyzesek.All(x => string.IsNullOrWhiteSpace(x.Tema))) { throw new StatusError(HttpStatusCode.BadRequest, ErrorResource.UresTanmenetetNemLehetMenteni); } var temaLengthErrors = new List(); foreach (var bejegyzes in model.TanmenetBejegyzesek.Where(b => !string.IsNullOrWhiteSpace(b.Tema))) { // NOTE: bejegyzés név = "{oraszam}. {tema}", max 1000 karakter, amiből óraszám max 3 karakter lehet if (bejegyzes.Tema.Length > 995) { temaLengthErrors.Add(string.Format(ErrorResource.TanmenetBejegyzesTemaMaxKarakter, bejegyzes.Oraszam)); } } if (temaLengthErrors.Any()) { var errorMsg = string.Join(Core.Constants.General.Sortores, temaLengthErrors); throw new StatusError(HttpStatusCode.BadRequest, errorMsg); } model.AlkalmazottId = ClaimData.FelhasznaloId; TanmenetCO co = ConvertModelToCo(model); var tHelper = new TanmenetHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()); tHelper.SaveTanmenet(co); return new HttpResponseMessage(HttpStatusCode.OK); } [System.Web.Http.HttpPost] [ApiValidateAjaxAntiForgeryToken] public HttpResponseMessage Delete([FromBody] int foglalkozasId) { try { var helper = new TanmenetHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()); List tanmenetCoList = helper.GetTanmenetCoList(ClaimData.FelhasznaloId).Where(x => x.TantargyfelosztasId == foglalkozasId).ToList(); foreach (TanmenetItemCo tanmenetCo in tanmenetCoList) { helper.DeleteTanmenet(tanmenetCo.Id); } return new HttpResponseMessage(HttpStatusCode.OK); } catch (Exception e) { throw new StatusError(HttpStatusCode.InternalServerError, ErrorResource.HibaATorlesSoran) { UnHandledException = e }; } } public List GetTantargyList() { List list = new List(); TantargyHelper helper = new TantargyHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()); foreach (var item in helper.GetTantargyakForDDL(ClaimData.FelhasznaloId, isSzakkepzo: ClaimData.IsSzakkepzoIntezmeny)) { SelectListItem sli = new SelectListItem() { Text = item.Value, Value = item.Key }; list.Add(sli); } return list; } private TanmenetKereseseCO ConvertSearchModelToCo(TanmenetSearchModel model) { TanmenetKereseseCO co = new TanmenetKereseseCO() { AlkalmazottID = model.FeltoltoIdSearch, Megjegyzes = model.MegjegyzesSearch, Nev = model.NevSearch, Oraszam = model.OraszamSearch, OsztalyID = model.OsztalyCsoportIdSearch, RovidNev = model.RovidNevSearch, TantargyID = model.TantargyIdSearch, Tema = model.TemaSearch }; return co; } private TanmenetCO ConvertModelToCo(TanmenetModel model) { TanmenetCO co = new TanmenetCO() { AlkalmazottID = model.AlkalmazottId, FoglalkozasID = model.FoglalkozasId, Orak = new List() }; foreach (var tema in model.TanmenetBejegyzesek) { co.Orak.Add(new TanmenetOrakCO() { ID = string.IsNullOrWhiteSpace(tema.ID) ? new int?() : int.Parse(tema.ID), Tema = tema.Tema, Oraszam = tema.Oraszam } ); } return co; } public JsonResult> GetFoglalkozasList() { var helper = new TantargyFelosztasHelper(ConnectionTypeExtensions.GetSessionConnectionType()) { GridParameters = null, }; var foglalkozasok = helper.GetTantargyFelosztasForDDL(ClaimData.FelhasznaloId); return Json(foglalkozasok.ToComboBoxItemList()); } } }