You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/UserCenter/Controllers/MessageController.cs

64 lines
2.1 KiB

using Application.Models;
using Infrastructure.Extensions;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Raven.Embedded;
using System;
using System.Linq;
using UserCenter.Services;
namespace UserCenter.Controllers
{
public class MessageController : Controller
{
private readonly IHubContext<PageHub> _pageHubContext;
public MessageController(IHubContext<PageHub> pageHubContext)
{
this._pageHubContext = pageHubContext;
}
public IActionResult SendMessage(string content = "窗前明月光,疑是地上霜")
{
var message = new Message
{
Id = Guid.NewGuid().ToString(),
Type = "通知",
FromId = "admin",
FromName = "管理员",
ToId = "iot",
ToName = "智慧教室",
Content = content + DateTime.Now.Ticks,
Timestamp = DateTimeOffset.Now.ToUnixTimeMilliseconds()
};
using (var store = EmbeddedServer.Instance.GetDocumentStore("Embedded"))
{
using (var session = store.OpenSession())
{
session.Store(message);
session.SaveChanges();
if (message.Type == "通知")
{
this._pageHubContext.Clients.All.SendAsync("receive", message);
}
}
}
return Json(message);
}
public IActionResult GetMessage(string toId, int pageIndex = 0, int pageSize = 20)
{
using (var store = EmbeddedServer.Instance.GetDocumentStore("Embedded"))
{
using (var session = store.OpenSession())
{
var list = session.Query<Message>()
.Where(o => o.Type == "通知" && o.ToId == toId)
.OrderByDescending(o => o.Timestamp)
.Paged(pageIndex, pageSize);
return Json(list);
}
}
}
}
}