using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.Http.ModelBinding; using System.Web.Http.Results; 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.Exceptions; using Kreta.Enums; using Kreta.Framework.Entities; using Kreta.Framework.Util; 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.Adminisztrator.ClaimValue)] public class OraTervApiController : ApiController { /// /// Visszaadja a Grid result-ot (1 szint) /// /// /// /// public DataSourceResult GetOraTervGrid(string data, [ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request) { OraTervSearchModel model = JsonConvert.DeserializeObject(data); OratervHelper helper = new OratervHelper(ConnectionTypeExtensions.GetSessionConnectionType()); helper.GridParameters = Converter.GridParameter(request); OratervCO srcCO = Convert_SearchModel_to_CO(model); var oraterv = helper.GetOraTerv(srcCO); return oraterv.ToDataSourceResult(); } /// /// Visszaadja a Grid result-ot (2 szint) /// /// /// /// public DataSourceResult GetOraTervDetailGrid(int oratervId, [ModelBinder(typeof(ModelBinder.DataSourceRequestModelBinder))] DataSourceRequest request) { OraTervModel model = new OraTervModel() { OratervId = oratervId }; OratervHelper helper = new OratervHelper(ConnectionTypeExtensions.GetSessionConnectionType()); helper.GridParameters = Converter.GridParameter(request); OratervCO srcCO = Convert_Model_to_CO(model); var oratervTantargy = helper.GetOratervTantargy(srcCO); return oratervTantargy.ToDataSourceResult(); } public int GetOraTervDetailCount(int oratervId) { OraTervModel model = new OraTervModel() { OratervId = oratervId }; OratervHelper helper = new OratervHelper(ConnectionTypeExtensions.GetSessionConnectionType()); helper.GridParameters = null; OratervCO srcCO = Convert_Model_to_CO(model); return helper.GetOratervTantargy(srcCO).Tables[0].Rows.Count; } /// /// Menti egy ÓraTerv adatait /// /// /// [HttpPost] [ApiValidateAjaxAntiForgeryToken] public HttpResponseMessage SaveOraTervData(OraTervModel model) { if (ModelState.IsValid) { if (model.OratervId.HasValue && model.OratervId > 0) { OratervCO co = Convert_Model_to_CO(model); OratervHelper helper = new OratervHelper(ConnectionTypeExtensions.GetSessionConnectionType()); var expression = $"OratervId <> {model.OratervId.Value}"; //Egyedi Nev validacio if (helper.GetOraTerv(new OratervCO { Nev = co.Nev, IsValidacio = true }).Tables[0].Select(expression).ToList().Any()) throw new StatusError(HttpStatusCode.BadRequest, ErrorResource.DuplikaltOraterv); helper.UpdateOratervCO(co); } else { OratervCO co = Convert_Model_to_CO(model); OratervHelper helper = new OratervHelper(ConnectionTypeExtensions.GetSessionConnectionType()); //Egyedi Nev validacio if (helper.GetOraTerv(new OratervCO { Nev = co.Nev, IsValidacio = true }).Tables[0].Rows.Count > 0) throw new StatusError(HttpStatusCode.BadRequest, ErrorResource.DuplikaltOraterv); helper.InsertOratervCO(co); } return new HttpResponseMessage(HttpStatusCode.OK); } return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } /// /// Tantárgy insert egy OraTervhez /// /// /// [HttpPost] [ApiValidateAjaxAntiForgeryToken] public HttpResponseMessage AddOraTervTargy(OraTervTargyModel model) { if (ModelState.IsValid) { try { if (model.OratervTantargyId.HasValue && model.OratervTantargyId > 0) { OratervTantargyCO co = Convert_Model_to_CO(model); OratervHelper helper = new OratervHelper(ConnectionTypeExtensions.GetSessionConnectionType()); helper.UpdateOratervTantargyCO(co, ClaimData.IsActivTanev); } else { OratervTantargyCO co = Convert_Model_to_CO(model); OratervHelper helper = new OratervHelper(ConnectionTypeExtensions.GetSessionConnectionType()); helper.InsertOratervTantargyCO(co, ClaimData.IsActivTanev); } return new HttpResponseMessage(HttpStatusCode.OK); } catch (BlException exception) { throw new StatusError(HttpStatusCode.BadRequest, exception.Message); } } return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } /// /// Töröl egy óratervet /// /// /// [HttpPost] [ApiValidateAjaxAntiForgeryToken] public HttpResponseMessage DeleteOraTerv([FromBody] int oratervID) { if (ClaimData.SelectedTanevID == ClaimData.KovTanevID) { throw new StatusError(HttpStatusCode.BadRequest, ErrorResource.OratervTorleseCsakAzAktualisTanevbenLehetseges); } try { OratervHelper helper = new OratervHelper(ConnectionTypeExtensions.GetSessionConnectionType()); if (helper.IfModifyOrDeleteOraTerv(oratervID)) { helper.DeleteOratervCO(oratervID); return new HttpResponseMessage(HttpStatusCode.OK); } throw new StatusError(HttpStatusCode.BadRequest, ErrorResource.TorlesCsakAkkorLehetsegesHaAzOratervhezNincsRogzitveTantargy); } catch (EntityDeleteFailedException ex) { var errorMessage = string.Format(ErrorResource.NemTorolhetoKapcsolatMiatt, TantargyResource.Oraterv, ex.ConnectionErrorMessage); throw new StatusError(HttpStatusCode.BadRequest, errorMessage); } } /// /// Töröl egy óraterv tantárgyat /// /// /// [HttpPost] [ApiValidateAjaxAntiForgeryToken] public HttpResponseMessage DeleteOraTervTantargy([FromBody] int tantargyID) { OratervHelper helper = new OratervHelper(ConnectionTypeExtensions.GetSessionConnectionType()); helper.DeleteOratervTantargy(tantargyID); return new HttpResponseMessage(HttpStatusCode.OK); } /// /// Törli az összes tantárgyat egy óratervhez /// /// /// [HttpPost] [ApiValidateAjaxAntiForgeryToken] public HttpResponseMessage DeleteAllOraTervTantargy([FromBody] int id) { OratervHelper helper = new OratervHelper(ConnectionTypeExtensions.GetSessionConnectionType()); helper.DeleteAllOratervTantargy(id); return new HttpResponseMessage(HttpStatusCode.OK); } #region Helpers public class ItemModel { public int Id { set; get; } } private OratervCO Convert_Model_to_CO(OraTervModel model) { OratervCO co = new OratervCO() { OratervId = model.OratervId, TantervId = model.TantervId, Evfolyam = model.EvfolyamId ?? 0, Nev = model.Nev }; return co; } private OratervCO Convert_SearchModel_to_CO(OraTervSearchModel model) { OratervCO co = new OratervCO() { TantervId = model.TantervIdSearch, Evfolyam = model.EvfolyamIdSearch ?? 0, Nev = model.NevSearch }; return co; } private OratervTantargyCO Convert_Model_to_CO(OraTervTargyModel model) { OratervTantargyCO co = new OratervTantargyCO() { OratervId = model.OratervId, EvesOraszam = model.EvesOraszam.Value, Tantargy = model.TantargyId.Value, OratervTantargyId = model.OratervTantargyId }; return co; } private OraTervModel Convert_CO_to_Model(OratervCO co) { OraTervModel model = new OraTervModel() { OratervId = co.OratervId, TantervId = co.TantervId, EvfolyamId = co.Evfolyam, Nev = co.Nev }; return model; } private OraTervTargyModel Convert_TantargyCO_to_Model(OratervTantargyCO co) { OraTervTargyModel model = new OraTervTargyModel() { OratervId = co.OratervId, EvesOraszam = co.EvesOraszam, TantargyId = co.Tantargy }; return model; } public OraTervModel GetOraTervElem(int id) { OratervCO co; var helper = new OratervHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()); co = helper.GetOratervCO(id); OraTervModel model = Convert_CO_to_Model(co); return model; } public OraTervTargyModel GetOraTervTantargyElem(int id) { OratervTantargyCO co; var helper = new OratervHelper(ConnectionTypeExtensions.GetActiveSessionConnectionType()); co = helper.GetOratervTantargyCO(id); OraTervTargyModel model = Convert_TantargyCO_to_Model(co); return model; } #endregion // NOTE: Kiszervezni -> GetTantervList ComboBoxHelperApi public JsonResult> GetTantervList([DataSourceRequest] DataSourceRequest request) { var helper = new TantervHelper(ConnectionTypeExtensions.GetSessionConnectionType()); var dictionary = helper.GetTanterv(""); List dropdownListItems = new List(); foreach (var item in dictionary) { ComboBoxListItem sli = new ComboBoxListItem() { Text = item.Value, Value = item.Key }; dropdownListItems.Add(sli); } return Json(dropdownListItems); } public JsonResult> GetEvfolyamList([DataSourceRequest] DataSourceRequest request) { List items = ((int)GeneratedAdatszotarTipusEnum.EvfolyamTipus).GetItemsByType(ClaimData.SelectedTanevID.Value, true).ToComboBoxItemList(); return Json(items); } } }