yavsc/Yavsc/ApiControllers/ChatApiController.cs

49 lines
1.5 KiB
C#
Raw Normal View History

2016-11-03 14:52:17 +01:00
using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Mvc;
using Microsoft.Data.Entity;
namespace Yavsc.Controllers
{
2016-12-14 11:23:58 +01:00
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;
2016-11-03 14:52:17 +01:00
using Models;
using ViewModels.Chat;
[Route("api/chat")]
public class ChatApiController : Controller
{
2016-12-14 11:23:58 +01:00
ApplicationDbContext dbContext;
UserManager<ApplicationUser> userManager;
public ChatApiController(ApplicationDbContext dbContext,
UserManager<ApplicationUser> userManager)
{
this.dbContext = dbContext;
this.userManager = userManager;
}
2016-11-03 14:52:17 +01:00
[HttpGet("users")]
2016-12-15 01:27:40 +01:00
public IEnumerable<ChatUserInfo> GetUserList()
2016-11-03 14:52:17 +01:00
{
2016-12-14 11:23:58 +01:00
2016-12-15 01:27:40 +01:00
var cxsQuery = dbContext.Connections?.Include(c=>c.Owner).GroupBy( c => c.ApplicationUserId );
2016-11-03 14:52:17 +01:00
2016-12-15 01:27:40 +01:00
// List<ChatUserInfo> result = new List<ChatUserInfo>();
if (cxsQuery!=null)
2016-12-14 11:23:58 +01:00
foreach (var g in cxsQuery) {
2016-11-03 14:52:17 +01:00
2016-12-14 11:23:58 +01:00
var uid = g.Key;
var cxs = g.ToList();
2016-12-14 11:33:43 +01:00
if (cxs !=null)
2016-12-14 11:23:58 +01:00
if (cxs.Count>0) {
2016-11-03 14:52:17 +01:00
var user = cxs.First().Owner;
2016-12-14 11:23:58 +01:00
2016-12-15 01:27:40 +01:00
yield return new ChatUserInfo { UserName = user.UserName,
2016-12-14 11:23:58 +01:00
UserId = user.Id, Avatar = user.Avatar, Connections = cxs,
2016-12-15 01:27:40 +01:00
Roles = ( userManager.GetRolesAsync(user) ).Result.ToArray() };
2016-11-03 14:52:17 +01:00
}
}
2016-12-15 01:27:40 +01:00
2016-11-03 14:52:17 +01:00
}
}
}