using Microsoft.AspNetCore.Mvc; using Sarand.Net.Standard.Base.App; using Sarand.Net.Standard.Base.Bus; using Sarand.Net.Standard.Base.Core; using Sarand.Net.Standard.Base.Dbas; using Sarand.Net.Standard.Base.Utils; using Saxonite.Net.Core.Web.Admin.Models; using Saxonite.Net.Standard.Base.App; using Saxonite.Net.Standard.Base.Bus; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace Saxonite.Net.Core.Web.Admin.Controllers { //======================================================================= // Controller: Organisation - Ajax //======================================================================= partial class OrganisationController { //------------------------------------------------------------------- // Organisation data. private class OrganisationData_List : AjaxResponse_Data.ItemData_Base { public String Name { get; set; } public String TypeCode { get; set; } public String TypeDesc { get; set; } public String CategoryCode { get; set; } public String CategoryDesc { get; set; } public long MemberCount { get; set; } // Sport. public long SportId { get; set; } public String SportName { get; set; } // Region - country. public long RegionIdCountry { get; set; } public String RegionNameCountry { get; set; } // Address. public String AddressSummary { get; set; } } private class OrganisationData_Full : OrganisationData_List { public String About { get; set; } public String Facilities { get; set; } } private void PopulateOrganisationResponseData(SaxOrganisation organisation, OrganisationData_List data) { data.Id = SysId.ToString(organisation.Id); data.Name = organisation.Name; data.TypeCode = OrganisationTypeCodes.GetCodeForType(organisation.OrganisationType); data.TypeDesc = OrganisationTypeCodes.GetDescForType(organisation.OrganisationType); data.CategoryCode = OrganisationCategoryCodes.GetCodeForType(organisation.OrganisationCategory); data.CategoryDesc = OrganisationCategoryCodes.GetDescForType(organisation.OrganisationCategory); data.MemberCount = AccountMgr.GetOrganisationMemberCount(organisation.Id); // Sport. data.SportId = SysId.ToLong(organisation.SportIdPrimary); using (SaxSport sport = ReferenceMgr.GetSportById(organisation.SportIdPrimary)) data.SportName = sport?.Name ?? ""; // Region. data.RegionIdCountry = SysId.ToLong(organisation.RegionIdPrimary); using (SaxRegion region = ReferenceMgr.GetRegionById(organisation.RegionIdPrimary)) data.RegionNameCountry = region?.Name ?? ""; // Common properties. data.StatusCode = organisation.StatusCode; data.StatusDesc = organisation.StatusDesc; } private void PopulateOrganisationResponseData(SaxOrganisation organisation, OrganisationData_Full data) { PopulateOrganisationResponseData(organisation, data as OrganisationData_List); data.About = organisation.About; data.Facilities = organisation.Facilities; data.Notes = organisation.Notes; } //------------------------------------------------------------------- // Get Organisation List ajax request. //------------------------------------------------------------------- private class AjaxResponse_OrganisationList : AjaxResponse_Data { // Construction. public AjaxResponse_OrganisationList(SaxModel_Admin model) : base(model) { } // Organisation list. public List Organisations { get; } = new List(); // Reference data. public List Statuses { get; } = new List(); public String DefaultStatusCode { get; set; } } //------------------------------------------------------------------- [HttpPost] [Route("Organisation/Ajax/GetList")] public IActionResult Ajax_GetList(Model_Organisation model) { String fn = GetType().Name + ".Ajax_GetList"; String messageTitle = Localiser["organisation.caption.organisations"]; AjaxResponse_OrganisationList responseData = new AjaxResponse_OrganisationList(model); // Validate session. if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData); // Reference data - statuses. foreach (SaStatus status in SaxOrganisation.ValidStatuses.FindAll(item => item != SaStatus.Deleted)) responseData.Statuses.Add(SacsSystem.GetStatusCode(status), Localiser.GetStatusCaption(status)); responseData.DefaultStatusCode = SacsSystem.GetStatusCode(SaxOrganisation.DefaultStatus); // Item filter. SaxOrganisation.Params prmOrganisation = new SaxOrganisation.Params(); prmOrganisation.OrganisationFilter = model.Filter.Organisation; prmOrganisation.OrganisationType = model.FilterOrganisationType; prmOrganisation.OrganisationCategory = model.FilterOrganisationCategory; prmOrganisation.SportIdsIncluded.AddId(model.Filter.OrganisationSportId.ToSysId()); prmOrganisation.RegionIdsIncluded.AddId(model.Filter.OrganisationRegionId.ToSysId()); prmOrganisation.StatusCat = model.Filter.OrganisationStatusCategoryCode != null ? model.FilterOrganisationStatusCategory : SaxOrganisation.StatusCat.NotDeleted; prmOrganisation.SortColumns.Add(AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.Name)); model.Paging.ItemTotal = (ulong)AccountMgr.GetOrganisationCount(prmOrganisation); model.PopulatePagingTo(prmOrganisation.ResultsPaging); // Item columns. ColumnDfn colId = AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.Id); ColumnDfn colName = AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.Name); ColumnDfn colOrganisationType = AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.OrganisationType); ColumnDfn colOrganisationCategory = AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.OrganisationCategory); ColumnDfn colPhoneMain = AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.PhoneMain); ColumnDfn colPhoneAlt = AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.PhoneAlt); ColumnDfn colUserCount = AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.User_Count); ColumnDfn colMemberCount = AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.Member_Count); ColumnDfn colSportId = AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.SportId); ColumnDfn colSportName = AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.Sport_Name); ColumnDfn colRegionId = AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.RegionId); ColumnDfn colRegionName = AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.Region_Name); ColumnDfn colStatus = AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.Status); List columns = new List() { colId, colName, colOrganisationType, colOrganisationCategory, colPhoneMain, colPhoneAlt, colUserCount, colMemberCount, colSportId, colSportName, colRegionId, colRegionName, colStatus }; // Query items. using (DbResultSet resultSet = AccountMgr.ExecuteOrganisationQuery(columns, prmOrganisation)) { while (resultSet != null && resultSet.HasRows && resultSet.NextRecord()) { NamedValue.Map values = resultSet.GetRecordValues(); OrganisationData_List organisationData = new OrganisationData_List() { Id = values.GetLong(colId.DataName).ToString(), Name = values.GetString(colName.DataName), TypeCode = values.GetString(colOrganisationType.DataName), TypeDesc = OrganisationTypeCodes.GetDescForCode(values.GetString(colOrganisationType.DataName)), CategoryCode = values.GetString(colOrganisationCategory.DataName), CategoryDesc = OrganisationCategoryCodes.GetDescForCode(values.GetString(colOrganisationCategory.DataName)), MemberCount = StringUtil.ToLong(values.GetString(colMemberCount.DataName)), SportId = values.GetLong(colSportId.DataName), SportName = values.GetString(colSportName.DataName), RegionIdCountry = values.GetLong(colRegionId.DataName), RegionNameCountry = values.GetString(colRegionName.DataName), StatusCode = values.GetString(colStatus.DataName), StatusDesc = SacsSystem.GetStatusDescForCode(values.GetString(colStatus.DataName)) }; responseData.Organisations.Add(organisationData); } } if (model.Paging.IsPaged) model.Paging.ItemCount = (ushort)responseData.Organisations.Count; // Complete response. responseData.SetPaging(model); responseData.Success = true; return Json(responseData); } //------------------------------------------------------------------- // Get Organisation ajax request. //------------------------------------------------------------------- private class AjaxResponse_Organisation : AjaxResponse_Data { // Construction. public AjaxResponse_Organisation(SaxModel_Admin model) : base(model) { } // Organisation details. public OrganisationData_Full organisation { get; } = new OrganisationData_Full(); } //------------------------------------------------------------------- [HttpPost] [Route("Organisation/Ajax/GetItem")] public IActionResult Ajax_GetItem(Model_Organisation model) { String fn = GetType().Name + ".Ajax_GetItem"; String messageTitle = Localiser["organisation.caption.organisation"]; AjaxResponse_Organisation responseData = new AjaxResponse_Organisation(model); // Validate session. if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData); // Identify item. SysId organisationId = model.OrganisationId.ToSysId(); if (!SysId.IsValid(organisationId)) return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.specified"])); // Retrieve item. SaxOrganisation organisation = AccountMgr.GetOrganisationById(organisationId); if (organisation == null) { SaLogger.Warn(fn + " Failed to retrieve organisation for Id: '" + model.OrganisationId + "'"); return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.found"])); } // Construct response. PopulateOrganisationResponseData(organisation, responseData.organisation); // Complete response. responseData.Success = true; return Json(responseData); } //------------------------------------------------------------------- // Store Organisation ajax request. //------------------------------------------------------------------- [HttpPost] [Route("Organisation/Ajax/StoreItem")] public IActionResult Ajax_StoreItem(Model_Organisation model) { String fn = GetType().Name + ".Ajax_StoreItem"; String messageTitle = Localiser["organisation.caption.organisation"]; AjaxResponse_Organisation responseData = new AjaxResponse_Organisation(model); // Validate session. if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData); // Validate item properties. model.ValidateOrganisationData(responseData); if (responseData.HasControlErrors()) return Json(responseData); // Retrieve existing item. SysId organisationId = model.OrganisationId.ToSysId(); SaxOrganisation organisation = AccountMgr.GetOrganisationById(organisationId); if (organisation == null && SysId.IsValid(organisationId)) { SaLogger.Warn(fn + " Failed to retrieve organisation for Id: '" + model.OrganisationId + "'"); return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.id.not.found"])); } // Create new item if required. if (organisation == null) organisation = AccountMgr.CreateOrganisation(); // Populate item properties. if (model.OrganisationName != null) organisation.Name = model.OrganisationName; if (model.OrganisationTypeValue != null) organisation.OrganisationType = model.OrganisationType; if (model.OrganisationCategoryValue != null) organisation.OrganisationCategory = model.OrganisationCategory; if (model.OrganisationSportId != null) organisation.SportIdPrimary = model.OrganisationSportId.ToSysId(); if (model.OrganisationRegionIdCountry != null) organisation.RegionIdPrimary = model.OrganisationRegionIdCountry.ToSysId(); if (model.OrganisationAbout != null) organisation.About = model.OrganisationAbout; if (model.OrganisationFacilities != null) organisation.Facilities = model.OrganisationFacilities; // Common properties. if (model.OrganisationStatusCode != null) organisation.Status = model.OrganisationStatus; if (model.OrganisationNotes != null) organisation.Notes = model.OrganisationNotes; // Store item. if (!SysId.IsValid(organisation.Id)) { if (!AccountMgr.InsertOrganisation(organisation, DbUpdateInfo)) { SaLogger.Warn(fn + " Failed to insert organisation: " + AccountMgr.ErrorStateMessage); return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.insert.failed"] + "\n\n" + AccountMgr.ErrorStateMessage)); } } else { if (!AccountMgr.UpdateOrganisation(organisation, DbUpdateInfo)) { SaLogger.Warn(fn + " Failed to update organisation [Id:" + SysId.ToString(organisation.Id) + "]: " + AccountMgr.ErrorStateMessage); return Json(responseData.SetMessage(messageTitle, Localiser["msg.error.item.update.failed"] + "\n\n" + AccountMgr.ErrorStateMessage)); } } // Construct response. PopulateOrganisationResponseData(organisation, responseData.organisation); // Complete response. responseData.Success = true; return Json(responseData); } //------------------------------------------------------------------- } //======================================================================= }