[ENHANCE] - Added IsRead property to Notifications

Fixed Version to Tenant.04.00.01.01 and reverted the Program.cs back to the way it was

This reverts commit 82fef82c4f.

[ENHANCE] - Added API to get Count of New Notifications based on IsRead

Fixed Typo in Notification Controller

[ENHANCE] - Added API to get Notifications by Count and IsRead
This commit is contained in:
vnetonline
2023-07-05 15:58:35 +10:00
parent 563695cdfa
commit b7de4b81a6
9 changed files with 257 additions and 22 deletions

View File

@ -9,6 +9,9 @@ using Oqtane.Repository;
using Oqtane.Security;
using System.Net;
using System.Reflection.Metadata;
using Microsoft.Extensions.Localization;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using System.Linq;
namespace Oqtane.Controllers
{
@ -30,6 +33,72 @@ namespace Oqtane.Controllers
_alias = tenantManager.GetAlias();
}
// GET: api/<controller>/read?siteid=x&direction=to&userid=1&count=5&isread=false
[HttpGet("read")]
[Authorize(Roles = RoleNames.Registered)]
public IEnumerable<Notification> Get(string siteid, string direction, string userid, string count, string isread)
{
IEnumerable<Notification> notifications = null;
int SiteId;
int UserId;
int Count;
bool IsRead;
if (int.TryParse(siteid, out SiteId) && SiteId == _alias.SiteId && int.TryParse(userid, out UserId) && int.TryParse(count, out Count) && bool.TryParse(isread, out IsRead) && IsAuthorized(UserId))
{
if (direction == "to")
{
notifications = _notifications.GetNotifications(SiteId, -1, UserId, Count, IsRead);
}
else
{
notifications = _notifications.GetNotifications(SiteId, UserId, -1, Count, IsRead);
}
}
else
{
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Notification Get Attempt {SiteId} {Direction} {UserId} {Count} {isRead}", siteid, direction, userid, count, isread);
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
notifications = null;
}
return notifications;
}
// GET: api/<controller>/read?siteid=x&direction=to&userid=1&count=5&isread=false
[HttpGet("read-count")]
[Authorize(Roles = RoleNames.Registered)]
public int Get(string siteid, string direction, string userid, string isread)
{
int notificationsCount = 0;
int SiteId;
int UserId;
bool IsRead;
if (int.TryParse(siteid, out SiteId) && SiteId == _alias.SiteId && int.TryParse(userid, out UserId) && bool.TryParse(isread, out IsRead) && IsAuthorized(UserId))
{
if (direction == "to")
{
notificationsCount = _notifications.GetNotificationCount(SiteId, -1, UserId, IsRead);
}
else
{
notificationsCount = _notifications.GetNotificationCount(SiteId, UserId, -1, IsRead);
}
}
else
{
_logger.Log(LogLevel.Error, this, LogFunction.Security, "Unauthorized Notification Get Attempt {SiteId} {Direction} {UserId} {isRead}", siteid, direction, userid, isread);
HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
notificationsCount = 0;
}
return notificationsCount;
}
// GET: api/<controller>?siteid=x&type=y&userid=z
[HttpGet]
[Authorize(Roles = RoleNames.Registered)]

View File

@ -0,0 +1,35 @@
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Oqtane.Databases.Interfaces;
using Oqtane.Migrations.EntityBuilders;
using Oqtane.Repository;
using Oqtane.Shared;
namespace Oqtane.Migrations.Tenant
{
[DbContext(typeof(TenantDBContext))]
[Migration("Tenant.04.00.01.01")]
public class AddNotificationIsRead : MultiDatabaseMigration
{
public AddNotificationIsRead(IDatabase database) : base(database)
{
}
protected override void Up(MigrationBuilder migrationBuilder)
{
var notificationEntityBuilder = new NotificationEntityBuilder(migrationBuilder, ActiveDatabase);
notificationEntityBuilder.AddBooleanColumn("IsRead", false);
notificationEntityBuilder.UpdateColumn("IsRead", "1", "bool", "");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
var notificationEntityBuilder = new NotificationEntityBuilder(migrationBuilder, ActiveDatabase);
notificationEntityBuilder.DropColumn("IsPublic");
}
}
}

View File

@ -6,6 +6,8 @@ namespace Oqtane.Repository
public interface INotificationRepository
{
IEnumerable<Notification> GetNotifications(int siteId, int fromUserId, int toUserId);
IEnumerable<Notification> GetNotifications(int siteId, int fromUserId, int toUserId, int count, bool isRead);
int GetNotificationCount(int siteId, int fromUserId, int toUserId, bool isRead);
Notification AddNotification(Notification notification);
Notification UpdateNotification(Notification notification);
Notification GetNotification(int notificationId);

View File

@ -33,6 +33,52 @@ namespace Oqtane.Repository
.ToList();
}
public IEnumerable<Notification> GetNotifications(int siteId, int fromUserId, int toUserId, int count, bool isRead)
{
if (toUserId == -1 && fromUserId == -1)
{
return _db.Notification
.Where(item => item.SiteId == siteId)
.Where(item => item.IsDelivered == false && item.IsDeleted == false)
.Where(item => item.SendOn == null || item.SendOn < System.DateTime.UtcNow)
.Where(item => item.IsRead == isRead)
.ToList()
.Take(count);
}
return _db.Notification
.Where(item => item.SiteId == siteId)
.Where(item => item.ToUserId == toUserId || toUserId == -1)
.Where(item => item.FromUserId == fromUserId || fromUserId == -1)
.Where(item => item.IsRead == isRead)
.ToList()
.Take(count);
}
public int GetNotificationCount(int siteId, int fromUserId, int toUserId, bool isRead)
{
if (toUserId == -1 && fromUserId == -1)
{
return _db.Notification
.Where(item => item.SiteId == siteId)
.Where(item => item.IsDelivered == false && item.IsDeleted == false)
.Where(item => item.SendOn == null || item.SendOn < System.DateTime.UtcNow)
.Where(item => item.IsRead == isRead)
.ToList()
.Count();
}
return _db.Notification
.Where(item => item.SiteId == siteId)
.Where(item => item.ToUserId == toUserId || toUserId == -1)
.Where(item => item.FromUserId == fromUserId || fromUserId == -1)
.Where(item => item.IsRead == isRead)
.ToList()
.Count();
}
public Notification AddNotification(Notification notification)
{
_db.Notification.Add(notification);