yavsc/Yavsc/ApiControllers/ChatApiController.cs

49 lines
1.5 KiB
C#

using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
namespace Yavsc.Controllers
{
8 years ago
using Microsoft.AspNet.Identity;
using Models;
using ViewModels.Chat;
[Route("api/chat")]
public class ChatApiController : Controller
{
8 years ago
ApplicationDbContext dbContext;
UserManager<ApplicationUser> userManager;
public ChatApiController(ApplicationDbContext dbContext,
UserManager<ApplicationUser> userManager)
{
this.dbContext = dbContext;
this.userManager = userManager;
}
[HttpGet("users")]
8 years ago
public IEnumerable<ChatUserInfo> GetUserList()
{
List<ChatUserInfo> result = new List<ChatUserInfo>();
8 years ago
var cxsQuery = dbContext.Connections?.Include(c=>c.Owner).GroupBy( c => c.ApplicationUserId );
8 years ago
// List<ChatUserInfo> result = new List<ChatUserInfo>();
if (cxsQuery!=null)
8 years ago
foreach (var g in cxsQuery) {
8 years ago
var uid = g.Key;
var cxs = g.ToList();
if (cxs !=null)
8 years ago
if (cxs.Count>0) {
var user = cxs.First().Owner;
8 years ago
result.Add(new ChatUserInfo { UserName = user.UserName,
8 years ago
UserId = user.Id, Avatar = user.Avatar, Connections = cxs,
Roles = ( userManager.GetRolesAsync(user) ).Result.ToArray() });
}
}
return result;
}
}
}