195 lines
7.4 KiB
C#
195 lines
7.4 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;
|
|
using Saxonite.Net.Standard.Base.App;
|
|
|
|
namespace Saxonite.Net.Core.Web.Online.Controllers
|
|
{
|
|
//=======================================================================
|
|
// Online Search Controller - Users - Ajax actions.
|
|
//=======================================================================
|
|
partial class SearchController
|
|
{
|
|
//-------------------------------------------------------------------
|
|
// Account - Get Users.
|
|
//-------------------------------------------------------------------
|
|
private class UserListData
|
|
{
|
|
public String Id { get; set; }
|
|
public String Name { get; set; }
|
|
public String Location { get; set; }
|
|
}
|
|
private class AjaxResponse_Search_Users : AjaxResponse_Data
|
|
{
|
|
// Construction.
|
|
public AjaxResponse_Search_Users(SaxModel_Online model) : base(model) { }
|
|
// User list.
|
|
public List<UserListData> Users { get; } = new List<UserListData>();
|
|
}
|
|
// Ajax request.
|
|
[HttpPost]
|
|
[Route("Search/Ajax/Users")]
|
|
[Route("Search/Ajax/GetUsers")]
|
|
public IActionResult Ajax_GetUsers(Model_Search_Users model)
|
|
{
|
|
String fn = GetType().Name + ".Ajax_GetUsers";
|
|
AjaxResponse_Search_Users responseData = new AjaxResponse_Search_Users(model);
|
|
String messageTitle = Localiser["label.users"];
|
|
|
|
// 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.Users.PageLengthMax, model.Paging.PageLength), (ushort)1);
|
|
|
|
// Ensure filter has something to restrict the list.
|
|
// ToDo May be able to show some results, such as friends when no filter provided.
|
|
if (StringUtil.IsEmpty(StringUtil.TrimAll(model.Filter.User)))
|
|
{
|
|
responseData.SetPaging(model);
|
|
responseData.Success = true;
|
|
return Json(responseData);
|
|
}
|
|
|
|
// Query users that user is a member of.
|
|
SaxUserAccount.Params prmUserAccount = new SaxUserAccount.Params();
|
|
prmUserAccount.UserFilter = model.Filter.User;
|
|
if (SysId.IsValid(model.FilterSportId))
|
|
prmUserAccount.SportIdsIncluded.Add(model.FilterSportId);
|
|
if (SysId.IsValid(model.FilterRegionId))
|
|
prmUserAccount.RegionIdsIncluded.Add(model.FilterRegionId);
|
|
prmUserAccount.StatusCat = SaxUserAccount.StatusCat.ActiveOnly;
|
|
model.Paging.ItemTotal = (ulong)AccountMgr.GetUserAccountCount(prmUserAccount);
|
|
model.PopulatePagingTo(prmUserAccount.ResultsPaging);
|
|
|
|
List<SaxUserAccount> users = AccountMgr.GetUserAccountList(prmUserAccount);
|
|
List<SysId> userIds = users.GetIds();
|
|
|
|
// Item columns.
|
|
ColumnDfn colId = AccountMgr.GetUserAccountColumnDfn(SaxUserAccount.Column.Id);
|
|
ColumnDfn colTitle = AccountMgr.GetUserAccountColumnDfn(SaxUserAccount.Column.Title);
|
|
ColumnDfn colFirstName = AccountMgr.GetUserAccountColumnDfn(SaxUserAccount.Column.FirstName);
|
|
ColumnDfn colLastName = AccountMgr.GetUserAccountColumnDfn(SaxUserAccount.Column.LastName);
|
|
ColumnDfn colRegionCountryName = AccountMgr.GetUserAccountColumnDfn(SaxUserAccount.Column.RegionCountry_Name);
|
|
ColumnDfn colRegionProvinceName = AccountMgr.GetUserAccountColumnDfn(SaxUserAccount.Column.RegionProvince_Name);
|
|
List<ColumnDfn> columns = new List<ColumnDfn>()
|
|
{
|
|
colId, colTitle, colFirstName, colLastName,
|
|
colRegionCountryName,colRegionProvinceName,
|
|
};
|
|
|
|
// Sort columns.
|
|
colLastName.SortAscending = true;
|
|
prmUserAccount.SortColumns.Add(colLastName);
|
|
|
|
// Query items.
|
|
using (DbResultSet resultSet = AccountMgr.ExecuteUserAccountQuery(columns, prmUserAccount))
|
|
{
|
|
while (resultSet != null && resultSet.HasRows && resultSet.NextRecord())
|
|
{
|
|
NamedValue.Map values = resultSet.GetRecordValues();
|
|
String displayName = SaxUserAccount.GetDisplayName(values.GetString(colTitle.DataName), values.GetString(colFirstName.DataName), values.GetString(colLastName.DataName));
|
|
List<String> location = new List<string>() { values.GetString(colRegionProvinceName.DataName), values.GetString(colRegionCountryName.DataName) };
|
|
UserListData userData = new UserListData()
|
|
{
|
|
Id = model.EncryptId(new SysId(values.GetLong(colId.DataName))),
|
|
Name = displayName,
|
|
Location = location.Concat(", ", "", "", true, true),
|
|
};
|
|
responseData.Users.Add(userData);
|
|
}
|
|
}
|
|
if (model.Paging.IsPaged)
|
|
model.Paging.ItemCount = (ushort)responseData.Users.Count;
|
|
|
|
// Complete response.
|
|
responseData.SetPaging(model);
|
|
responseData.Success = true;
|
|
return Json(responseData);
|
|
}
|
|
//-------------------------------------------------------------------
|
|
// Account - Get Users.
|
|
//-------------------------------------------------------------------
|
|
private class UserData
|
|
{
|
|
public String Id { get; set; }
|
|
public String Name { get; set; }
|
|
public String Location { get; set; }
|
|
}
|
|
private class AjaxResponse_Search_User : AjaxResponse_Data
|
|
{
|
|
// Construction.
|
|
public AjaxResponse_Search_User(SaxModel_Online model) : base(model) { }
|
|
// User list.
|
|
public UserData User { get; set; }
|
|
}
|
|
// Ajax request.
|
|
[HttpPost]
|
|
[Route("Search/Ajax/User")]
|
|
[Route("Search/Ajax/GetUser")]
|
|
public IActionResult Ajax_GetUser(Model_Search_Users model)
|
|
{
|
|
String fn = GetType().Name + ".Ajax_GetUser";
|
|
AjaxResponse_Search_User responseData = new AjaxResponse_Search_User(model);
|
|
String messageTitle = Localiser["label.user"];
|
|
|
|
// 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<SacsHierarchyItem> disposableItems = new DisposableCollection<SacsHierarchyItem>())
|
|
{
|
|
// Retrieve user.
|
|
SysId userAccountId = model.ExtractSysId(model.UserId);
|
|
if (!SysId.IsValid(userAccountId))
|
|
{
|
|
SaLogger.Warn(fn + " User Id not supplied.");
|
|
return Json(responseData);
|
|
}
|
|
SaxUserAccount userAccount = AccountMgr.GetUserAccountById(userAccountId);
|
|
if (userAccount == null)
|
|
{
|
|
SaLogger.Warn(fn + " Failed to retrieve user. Id: " + model.UserId);
|
|
return Json(responseData);
|
|
}
|
|
disposableItems.Add(userAccount);
|
|
|
|
// Populate response.
|
|
List<String> location = new List<string>() { model.GetRegionCaption(userAccount.RegionIdProvince), model.GetRegionCaption(userAccount.RegionIdCountry) };
|
|
UserData userData = new UserData()
|
|
{
|
|
Id = model.EncryptId(userAccount.Id),
|
|
Name = userAccount.GetDisplayName(),
|
|
Location = location.Concat(", ", "", "", true, true),
|
|
};
|
|
responseData.User = userData;
|
|
}
|
|
|
|
// Complete response.
|
|
responseData.Success = true;
|
|
return Json(responseData);
|
|
}
|
|
|
|
//-------------------------------------------------------------------
|
|
}
|
|
//=======================================================================
|
|
}
|