using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using RMuseum.Models.Notification; using RMuseum.Models.Notification.ViewModels; using RMuseum.Services; using RSecurityBackend.Models.Generic; using System; using System.Linq; using System.Net; using System.Threading.Tasks; namespace RMuseum.Controllers { [Produces("application/json")] [Route("api/notifications")] public class NoificationController : Controller { /// /// Get User Notifications /// /// [HttpGet] [Authorize] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(RUserNotificationViewModel[]))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task GetUserNotifications() { Guid loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); RServiceResult res = await _notificationService.GetUserNotifications(loggedOnUserId); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } [HttpGet] [Route("unread/count")] [Authorize] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(int))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task GetUnreadUserNotificationsCount() { Guid loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); RServiceResult res = await _notificationService.GetUnreadUserNotificationsCount(loggedOnUserId); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// Switch Notification Status /// /// /// [HttpPut] [Route("{notificationId}")] [Authorize] [ProducesResponseType((int)HttpStatusCode.OK, Type = typeof(RUserNotificationViewModel))] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task SwitchNotificationStatus(Guid notificationId) { Guid loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); RServiceResult res = await _notificationService.SwitchNotificationStatus(notificationId, loggedOnUserId); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(res.Result); } /// /// Set All User Notifications Status Read /// /// [HttpPut] [Route("allread")] [Authorize] [ProducesResponseType((int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task SetAllNotificationsStatusRead() { Guid loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); RServiceResult res = await _notificationService.SetAllNotificationsStatus(loggedOnUserId, NotificationStatus.Read); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(); } /// /// Set All User Notifications Status Unread /// [HttpPut] [Route("allunread")] [Authorize] [ProducesResponseType((int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task SetAllNotificationsStatusUnread() { Guid loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); RServiceResult res = await _notificationService.SetAllNotificationsStatus(loggedOnUserId, NotificationStatus.Unread); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(); } /// /// Delete Notification /// /// /// [HttpDelete] [Route("{notificationId}")] [Authorize] [ProducesResponseType((int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task DeleteNotification(Guid notificationId) { Guid loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); RServiceResult res = await _notificationService.DeleteNotification(notificationId, loggedOnUserId); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(); } /// /// Delete All Read Notifications /// /// [HttpDelete] [Authorize] [ProducesResponseType((int)HttpStatusCode.OK)] [ProducesResponseType((int)HttpStatusCode.BadRequest, Type = typeof(string))] public async Task DeleteAllReadNotification() { Guid loggedOnUserId = new Guid(User.Claims.FirstOrDefault(c => c.Type == "UserId").Value); RServiceResult res = await _notificationService.DeleteNotification(Guid.Empty, loggedOnUserId); if (!string.IsNullOrEmpty(res.ExceptionString)) return BadRequest(res.ExceptionString); return Ok(); } /// /// Notification Service /// protected readonly IRNotificationService _notificationService; /// /// constructor /// /// public NoificationController(IRNotificationService notificationService) { _notificationService = notificationService; } } }