using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using Saxonite.Net.Core.Web.Online.Models; using Saxonite.Net.Core.Web.Base.App; using Saxonite.Net.Core.Web.Base.Controllers; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Threading.Tasks; using Saxonite.Net.Standard.Base.Bus; using Sarand.Net.Standard.Base.App; using Sarand.Net.Standard.Base.Bus; using Sarand.Net.Standard.Base.Utils; using Sarand.Net.Standard.Base.Core; using Sarand.Net.Standard.Base.Dbas; using Saxonite.Net.Standard.Base.App; namespace Saxonite.Net.Core.Web.Online.Controllers { //======================================================================= // Online Search Controller - Organisations - Ajax actions. //======================================================================= partial class SearchController { //------------------------------------------------------------------- // Account - Get Organisations. //------------------------------------------------------------------- private class OrganisationListData { public String Id { get; set; } public String Name { get; set; } public String CategoryDesc { get; set; } public String SportId { get; set; } public String SportName { get; set; } public String RegionId { get; set; } public String RegionName { get; set; } public String Location { get; set; } } private class AjaxResponse_Search_Organisations : AjaxResponse_Data { // Construction. public AjaxResponse_Search_Organisations(SaxModel_Online model) : base(model) { } // Organisation list. public List Organisations { get; } = new List(); } // Ajax request. [HttpPost] [Route("Search/Ajax/Organisations")] [Route("Search/Ajax/GetOrganisations")] public IActionResult Ajax_GetOrganisations(Model_Search_Organisations model) { String fn = GetType().Name + ".Ajax_GetOrganisations"; AjaxResponse_Search_Organisations responseData = new AjaxResponse_Search_Organisations(model); String messageTitle = Localiser["organisation.caption.organisations"]; // Validate session and referrer. if (!ValidReferer) return Forbid(); if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData); RefreshModel(model); // Ensure data is paged to avoid leaking large volumes. model.Paging.PageNo = Math.Max(1, model.Paging.PageNo); model.Paging.PageLength = Math.Max(Math.Min(Sax.Config.Online.Search.Organisations.PageLengthMax, model.Paging.PageLength), (ushort)1); // Query organisations that user is a member of. SaxOrganisation.Params prmOrganisation = new SaxOrganisation.Params(); prmOrganisation.OrganisationFilter = model.Filter.Organisation; prmOrganisation.OrganisationType = model.FilterOrganisationType; prmOrganisation.OrganisationCategory = model.FilterOrganisationCategory; prmOrganisation.OrganisationCategoriesExcluded.Add(Sax.OrganisationCategory.Partner); prmOrganisation.OrganisationCategoriesExcluded.Add(Sax.OrganisationCategory.Other); if (SysId.IsValid(model.FilterSportId)) prmOrganisation.SportIdsIncluded.Add(model.FilterSportId); if (SysId.IsValid(model.FilterRegionId)) prmOrganisation.RegionIdsIncluded.Add(model.FilterRegionId); prmOrganisation.StatusCat = SaxOrganisation.StatusCat.ActiveOnly; model.Paging.ItemTotal = (ulong)AccountMgr.GetOrganisationCount(prmOrganisation); model.PopulatePagingTo(prmOrganisation.ResultsPaging); List organisations = AccountMgr.GetOrganisationList(prmOrganisation); List organisationIds = organisations.GetIds(); // Item columns. ColumnDfn colId = AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.Id); ColumnDfn colName = AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.Name); ColumnDfn colCategory = AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.OrganisationCategory_Desc); 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); List columns = new List() { colId, colName, colCategory, colSportId, colSportName, colRegionId, colRegionName, }; // Sort columns. colName.SortAscending = true; prmOrganisation.SortColumns.Add(colName); // Query items. using (DbResultSet resultSet = AccountMgr.ExecuteOrganisationQuery(columns, prmOrganisation)) { while (resultSet != null && resultSet.HasRows && resultSet.NextRecord()) { NamedValue.Map values = resultSet.GetRecordValues(); Sax.OrganisationCategory category = (Sax.OrganisationCategory)OrganisationCategoryCodes.GetTypeForCode(values.GetString(colCategory.DataName)); OrganisationListData organisationData = new OrganisationListData() { Id = model.EncryptId(new SysId(values.GetLong(colId.DataName))), Name = values.GetString(colName.DataName), CategoryDesc = Localiser.GetOrganisationCategoryCaption(category), //SportId = model.GetEncryptedSportId(new SysId(values.GetLong(colSportId.DataName))), SportName = values.GetString(colSportName.DataName), //RegionId = model.GetEncryptedRegionId(new SysId(values.GetLong(colRegionId.DataName))), Location = values.GetString(colRegionName.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); } //------------------------------------------------------------------- // Account - Get Organisations. //------------------------------------------------------------------- private class OrganisationData { public String Id { get; set; } public String Name { get; set; } public String Category { get; set; } public String Sport { get; set; } public String Location { get; set; } public String Address { get; set; } public String PostalCode { get; set; } public String Phone { get; set; } } private class AjaxResponse_Search_Organisation : AjaxResponse_Data { // Construction. public AjaxResponse_Search_Organisation(SaxModel_Online model) : base(model) { } // Organisation list. public OrganisationData Organisation { get; set; } } // Ajax request. [HttpPost] [Route("Search/Ajax/Organisation")] [Route("Search/Ajax/GetOrganisation")] public IActionResult Ajax_GetOrganisation(Model_Search_Organisations model) { String fn = GetType().Name + ".Ajax_GetOrganisation"; AjaxResponse_Search_Organisation responseData = new AjaxResponse_Search_Organisation(model); String messageTitle = Localiser["label.organisation"]; // Validate session and referrer. if (!ValidReferer) return Forbid(); if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData); RefreshModel(model); // Record disposable items as they are created so all can be disposed when needed. using (DisposableCollection disposableItems = new DisposableCollection()) { // Retrieve organisation. SysId organisationId = model.ExtractSysId(model.OrganisationId); if (!SysId.IsValid(organisationId)) { SaLogger.Warn(fn + " Organisation Id not supplied."); return Json(responseData); } SaxOrganisation organisation = AccountMgr.GetOrganisationById(organisationId); if (organisation == null) { SaLogger.Warn(fn + " Failed to retrieve organisation. Id: " + model.OrganisationId); return Json(responseData); } disposableItems.Add(organisation); // Populate response. OrganisationData organisationData = new OrganisationData() { Id = model.EncryptId(organisation.Id), Name = organisation.Name, Category = Localiser.GetOrganisationCategoryCaption(organisation.OrganisationCategory), Sport = model.GetSportCaption(organisation.SportIdPrimary), Location = model.GetRegionCaption(organisation.RegionIdPrimary), }; responseData.Organisation = organisationData; // Organisation address. SaxItemAddress address = ItemDataMgr.GetFirstItemAddressByItemAndType(organisation.GetItemId()); if (address != null) { String addressLines = (new List() { address.Line1, address.Line2, address.Line3 }).Concat(", ", "", "", true, true); String addressTownProvince = (new List() { address.Town, address.Province }).Concat(", ", "", "", true, true); organisationData.Address = (new List() { addressLines, addressTownProvince }).Concat("\n", "", "", true, true); organisationData.PostalCode = address.PostalCode; } } // Complete response. responseData.Success = true; return Json(responseData); } //------------------------------------------------------------------- } //======================================================================= }