using Oqtane.Models;
using System.Threading.Tasks;
using System.Net.Http;
using Oqtane.Shared;
using System.Collections.Generic;
using System.Linq;
using Oqtane.Documentation;
namespace Oqtane.Services
{
///
/// Service to store and retrieve notifications ()
///
public interface INotificationService
{
///
/// Return a list of notifications
///
///
///
///
///
Task> GetNotificationsAsync(int siteId, string direction, int userId);
///
///
///
///
///
///
///
///
///
Task> GetNotificationsAsync(int siteId, string direction, int userId, int count, bool isRead);
///
///
///
///
///
///
///
///
Task GetNotificationCountAsync(int siteId, string direction, int userId, bool isRead);
///
/// Returns a specific notifications
///
///
///
Task GetNotificationAsync(int notificationId);
///
/// Creates a new notification
///
///
///
Task AddNotificationAsync(Notification notification);
///
/// Updates a existing notification
///
///
///
Task UpdateNotificationAsync(Notification notification);
///
/// Deletes a notification
///
///
///
Task DeleteNotificationAsync(int notificationId);
}
[PrivateApi("Don't show in the documentation, as everything should use the Interface")]
public class NotificationService : ServiceBase, INotificationService
{
public NotificationService(HttpClient http, SiteState siteState) : base(http, siteState) { }
private string Apiurl => CreateApiUrl("Notification");
public async Task> GetNotificationsAsync(int siteId, string direction, int userId)
{
var notifications = await GetJsonAsync>($"{Apiurl}?siteid={siteId}&direction={direction.ToLower()}&userid={userId}");
return notifications.OrderByDescending(item => item.CreatedOn).ToList();
}
public async Task> GetNotificationsAsync(int siteId, string direction, int userId, int count, bool isRead)
{
var notifications = await GetJsonAsync>($"{Apiurl}/read?siteid={siteId}&direction={direction.ToLower()}&userid={userId}&count={count}&isread={isRead}");
return notifications.OrderByDescending(item => item.CreatedOn).ToList();
}
public async Task GetNotificationCountAsync(int siteId, string direction, int userId, bool isRead)
{
var notificationCount = await GetJsonAsync($"{Apiurl}/read-count?siteid={siteId}&direction={direction.ToLower()}&userid={userId}&isread={isRead}");
return notificationCount;
}
public async Task GetNotificationAsync(int notificationId)
{
return await GetJsonAsync($"{Apiurl}/{notificationId}");
}
public async Task AddNotificationAsync(Notification notification)
{
return await PostJsonAsync(Apiurl, notification);
}
public async Task UpdateNotificationAsync(Notification notification)
{
return await PutJsonAsync($"{Apiurl}/{notification.NotificationId}", notification);
}
public async Task DeleteNotificationAsync(int notificationId)
{
await DeleteAsync($"{Apiurl}/{notificationId}");
}
}
}