Files
Saxonite/SaxoniteCoreWebOnline/Online/Controllers/AccountController-Ajax-Organisations.cs
T
2021-08-09 14:18:08 +01:00

127 lines
5.9 KiB
C#

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;
namespace Saxonite.Net.Core.Web.Online.Controllers
{
//=======================================================================
// Online Account Controller - Organisations - Ajax actions.
//=======================================================================
partial class AccountController
{
//-------------------------------------------------------------------
// Account - Get Organisations.
//-------------------------------------------------------------------
private class ProfileOrganisationData
{
public long Id { get; set; }
public String Name { get; set; }
public long SportId { get; set; }
public String SportName { get; set; }
public long RegionId { get; set; }
public String RegionName { get; set; }
public long MemberCount { get; set; }
public long OpportunityCount { get; set; }
}
private class AjaxResponse_ProfileOrganisationsData : AjaxResponse_Data
{
// Construction.
public AjaxResponse_ProfileOrganisationsData(SaxModel_Online model) : base(model) { }
// Organisation list.
public List<ProfileOrganisationData> Organisations { get; } = new List<ProfileOrganisationData>();
}
// Ajax request.
[HttpPost]
[Route("Account/Ajax/GetProfileOrganisations")]
public IActionResult GetProfileOrganisations(Model_Account model)
{
String fn = GetType().Name + ".GetProfileOrganisations";
AjaxResponse_ProfileOrganisationsData responseData = new AjaxResponse_ProfileOrganisationsData(model);
String messageTitle = Localiser["account.caption.profile.organisations"];
// Validate session and referrer.
if (!ValidReferer) return Forbid();
if (!ValidUserSession) return GetAjaxResult_InvalidSession(responseData);
RefreshModel(model);
// Query organisations that user is a member of.
SaxUserOrganisation.Params prmUserOrganisation = new SaxUserOrganisation.Params();
prmUserOrganisation.UserAccountIdsIncluded.Add(SessionData.User.UserAccountId);
prmUserOrganisation.OrganisationStatusCat = SaxOrganisation.StatusCat.ActiveOnly;
prmUserOrganisation.OrganisationMembersOnly = true;
prmUserOrganisation.StatusCat = SaxUserOrganisation.StatusCat.ActiveOnly;
List<SaxUserOrganisation> userOrganisations = AccountMgr.GetUserOrganisationList(prmUserOrganisation);
List<SysId> organisationIds = userOrganisations.GetOrganisationIds();
// Item columns.
ColumnDfn colId = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.Id);
ColumnDfn colOrganisationId = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.OrganisationId);
ColumnDfn colOrganisationName = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.Organisation_Name);
ColumnDfn colOrganisationSportId = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.Organisation_SportId);
ColumnDfn colOrganisationSportName = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.Organisation_Sport_Name);
ColumnDfn colOrganisationRegionId = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.Organisation_RegionId);
ColumnDfn colOrganisationRegionName = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.Organisation_Region_Name);
ColumnDfn colMemberCount = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.Organisation_Member_Count);
ColumnDfn colOpportunityCount = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.Organisation_Opportunity_Count);
ColumnDfn colStatus = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.Status);
ColumnDfn colCreatedOn = AccountMgr.GetUserOrganisationColumnDfn(SaxUserOrganisation.Column.CreateOn_Date);
List<ColumnDfn> columns = new List<ColumnDfn>()
{
colId, colOrganisationId , colOrganisationName,
colOrganisationSportId, colOrganisationSportName,
colOrganisationRegionId, colOrganisationRegionName,
colMemberCount, colOpportunityCount,
colStatus, colStatus, colCreatedOn
};
// Sort columns.
colCreatedOn.SortAscending = false;
prmUserOrganisation.SortColumns.Add(colOrganisationName);
// Query items.
using (DbResultSet resultSet = AccountMgr.ExecuteUserOrganisationQuery(columns, prmUserOrganisation))
{
while (resultSet != null && resultSet.HasRows && resultSet.NextRecord())
{
NamedValue.Map values = resultSet.GetRecordValues();
ProfileOrganisationData organisationData = new ProfileOrganisationData()
{
Id = values.GetLong(colId.DataName),
Name = values.GetString(colOrganisationName.DataName),
SportId = values.GetLong(colOrganisationSportId.DataName),
SportName = values.GetString(colOrganisationSportName.DataName),
RegionId = values.GetLong(colOrganisationRegionId.DataName),
RegionName = values.GetString(colOrganisationRegionName.DataName),
MemberCount = values.GetLong(colMemberCount.DataName),
OpportunityCount = values.GetLong(colOpportunityCount.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);
}
//-------------------------------------------------------------------
}
//=======================================================================
}