using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; namespace ChatRoomServer.Www.Areas.Api.Controllers { using System.Data.Entity; using System.Threading.Tasks; using System.Web.Mvc; using ChatRoomServer.Db; using Newtonsoft.Json; [System.Web.Http.RoutePrefix("api/room")] public class RoomController : ApiController { [AllowAnonymous] [System.Web.Http.Route("list"), System.Web.Http.HttpGet, OutputCache(Duration = 600)] public async Task GetAnnouncements() { var db = new ChatDb(); var items = await db.Rooms.AsNoTracking().ToArrayAsync(); var sgroup = await db.ServerGroups.AsNoTracking().ToDictionaryAsync(s => s.ID); var result = items.Select(s => new { id = s.ID, onlinecount = s.OnlineCount, room_people_num = 1000, category = s.Category, url = (sgroup.GetValue(s.ServerGroup)?.Url.EnsureEndWith("/") ?? "") + "room/" + s.ID + "/#uid#", status = s.Status, name = s.Name }).ToArray(); return result; } [Route("updateOnlineCount")] [HttpPost] public async Task UpdateOnlineCount(string data) { //limit to local if (!RequestContext.IsLocal) return "Invalid operation."; var db = new ChatDb(); var rooms = await db.Rooms.ToDictionaryAsync(s => s.ID); var idata = JsonConvert.DeserializeAnonymousType(data, System.FishLib.CollectionUtility.CreateAnymousTypeList(new { id = "", count = 0 })); foreach (var x1 in idata) { var room = rooms.GetValue(x1.id); if (room != null) room.OnlineCount = x1.count; } await db.SaveChangesAsync(); return "Operation succeed"; } } }