-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationController.cs
More file actions
88 lines (78 loc) · 3.05 KB
/
NotificationController.cs
File metadata and controls
88 lines (78 loc) · 3.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using DevDash.Attributes;
using DevDash.model;
using DevDash.Repository.IRepository;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Net;
using System.Security.Claims;
namespace DevDash.Controllers
{
[Route("api/[controller]")]
[ApiController]
[Authorize]
public class NotificationController : ControllerBase
{
private readonly INotificationRepository _notificationRepository;
private APIResponse _response;
private readonly ITenantRepository _tenantRepository;
public NotificationController(INotificationRepository notificationRepository,ITenantRepository tenantRepository)
{
_notificationRepository = notificationRepository;
_tenantRepository = tenantRepository;
_response = new APIResponse();
}
[HttpGet]
[Cache(2000)]
public async Task<ActionResult<APIResponse>> GetNotifications(string? search = null)
{
try
{
var userId = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
if (string.IsNullOrEmpty(userId))
{
_response.IsSuccess = false;
_response.StatusCode = HttpStatusCode.Unauthorized;
_response.ErrorMessages = new List<string> { "User ID not found" };
return Unauthorized(_response);
}
// Get notifications
var notifications = await _notificationRepository.GetUserNotificationsAsync(int.Parse(userId));
// Return both tenants and notifications
_response.Result = new
{
Notifications = notifications,
userId = int.Parse(userId)
};
_response.StatusCode = HttpStatusCode.OK;
_response.IsSuccess = true;
return Ok(_response);
}
catch (Exception ex)
{
_response.IsSuccess = false;
_response.StatusCode = HttpStatusCode.InternalServerError;
_response.ErrorMessages = new List<string> { ex.Message };
return StatusCode((int)HttpStatusCode.InternalServerError, _response);
}
}
[HttpPost("{notificationId}/mark-as-read")]
public async Task<ActionResult<APIResponse>> MarkAsRead(int notificationId)
{
try
{
await _notificationRepository.MarkAsReadAsync(notificationId);
_response.IsSuccess = true;
_response.StatusCode = HttpStatusCode.NoContent;
return Ok(_response);
}
catch (Exception ex)
{
_response.IsSuccess = false;
_response.StatusCode = HttpStatusCode.InternalServerError;
_response.ErrorMessages = new List<string> { ex.Message };
return StatusCode(500, _response);
}
}
}
}