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. public class OrganisationData_List { public String Id { get; set; } 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; } public String StatusCode { get; set; } public String StatusDesc { get; set; } } public class OrganisationData_Full : OrganisationData_List { } private void PopulateOrganisationResponseData(SaxOrganisation organisation, OrganisationData_List data) { data.Id = SysId.ToString(organisation.Id); data.Name = organisation.Name; data.TypeCode = OrganisationTypes.GetCodeForType(organisation.OrganisationType); data.TypeDesc = OrganisationTypes.GetDescForType(organisation.OrganisationType); data.CategoryCode = OrganisationCategories.GetCodeForType(organisation.OrganisationCategory); data.CategoryDesc = OrganisationCategories.GetDescForType(organisation.OrganisationCategory); data.MemberCount = AccountMgr.GetOrganisationMemberCount(organisation.Id); data.StatusCode = organisation.StatusCode; data.StatusDesc = organisation.StatusDesc; } private void PopulateOrganisationResponseData(SaxOrganisation organisation, OrganisationData_Full data) { PopulateOrganisationResponseData(organisation, data as OrganisationData_List); } //------------------------------------------------------------------- // Get Organisation List ajax request. //------------------------------------------------------------------- public class AjaxResponse_OrganisationList : AjaxResponse_Data { public List Organisations { get; } = new List(); } //------------------------------------------------------------------- [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(); // Validate session. if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData); // Item filter. SaxOrganisation.Params prmOrganisation = new SaxOrganisation.Params(); prmOrganisation.OrganisationFilter = model.Filter.Organisation; prmOrganisation.StatusCat = SaxOrganisation.StatusCat.NotDeleted; model.DataItemTotal = (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 colStatus = AccountMgr.GetOrganisationColumnDfn(SaxOrganisation.Column.Status); List columns = new List() { colId, colName, colOrganisationType, colOrganisationCategory, colPhoneMain, colPhoneAlt, colUserCount, colMemberCount, colStatus }; // Query items. model.DataItemCount = 0; using (DbResultSet resultSet = AccountMgr.ExecuteOrganisationQuery(columns, prmOrganisation)) { while (resultSet != null && resultSet.HasRows && resultSet.NextRecord()) { model.DataItemCount++; NamedValue.Map values = resultSet.GetRecordValues(); OrganisationData_List organisationData = new OrganisationData_List() { Id = values.GetString(colId.DataName), Name = values.GetString(colName.DataName), TypeCode = values.GetString(colOrganisationType.DataName), TypeDesc = OrganisationTypes.GetDescForCode(values.GetString(colOrganisationType.DataName)), CategoryCode = values.GetString(colOrganisationCategory.DataName), CategoryDesc = OrganisationCategories.GetDescForCode(values.GetString(colOrganisationCategory.DataName)), MemberCount = StringUtil.ToLong(values.GetString(colMemberCount.DataName)), StatusCode = values.GetString(colStatus.DataName), StatusDesc = SacsSystem.GetStatusDescForCode(values.GetString(colStatus.DataName)) }; responseData.Organisations.Add(organisationData); } } // Complete response. responseData.SetPaging(model); responseData.Success = true; return Json(responseData); } //------------------------------------------------------------------- // Get Organisation ajax request. //------------------------------------------------------------------- public class AjaxResponse_Organisation : AjaxResponse_Data { 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(); // Validate session. if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData); // Identify item. SysId organisationId = new SysId(StringUtil.ToLong(model.OrganisationId)); 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(); // 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 = new SysId(StringUtil.ToLong(model.OrganisationId)); 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.OrganisationStatusCode != null) organisation.Status = model.OrganisationStatus; // 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); } //------------------------------------------------------------------- } //======================================================================= }