source: ChapterX.API/Controllers/NotificationsController.cs@ a8f4a2d

main
Last change on this file since a8f4a2d was 73b69b2, checked in by kikisrbinoska <srbinoskakristina07@…>, 3 months ago

Fixed reading lists,comments and likes

  • Property mode set to 100644
File size: 3.5 KB
Line 
1using ChapterX.Application.Notification.Commands;
2using ChapterX.Application.Notification.Queries;
3using ChapterX.Domain.Repositories;
4using MediatR;
5using Microsoft.AspNetCore.Authorization;
6using Microsoft.AspNetCore.Mvc;
7using Microsoft.Extensions.Logging;
8
9namespace ChapterX.API.Controllers
10{
11 [Route("api/[controller]")]
12 [ApiController]
13 public class NotificationsController : ControllerBase
14 {
15 private readonly IMediator _mediator;
16 private readonly INotificationRepository _notificationRepository;
17 private readonly ILogger<NotificationsController> _logger;
18
19 public NotificationsController(IMediator mediator, INotificationRepository notificationRepository, ILogger<NotificationsController> logger)
20 {
21 _mediator = mediator;
22 _notificationRepository = notificationRepository;
23 _logger = logger;
24 }
25
26 [HttpGet("user/{userId:int}")]
27 [Authorize]
28 public async Task<ActionResult> GetByUser(int userId)
29 {
30 var notifications = await _notificationRepository.GetByUserIdAsync(userId);
31 var result = notifications.Select(n => new
32 {
33 id = n.Id,
34 content = n.Content,
35 isRead = n.IsRead,
36 createdAt = n.CreatedAt,
37 type = n.Type ?? "info",
38 link = n.Link,
39 });
40 return Ok(result);
41 }
42
43 [HttpPut("{id:int}/read")]
44 [Authorize]
45 public async Task<ActionResult> MarkRead(int id)
46 {
47 var notification = await _notificationRepository.GetByIdAsync(id);
48 if (notification == null) return NotFound();
49 notification.IsRead = true;
50 await _notificationRepository.UpdateAsync(notification);
51 return Ok();
52 }
53
54 [HttpGet]
55 [AllowAnonymous]
56 public async Task<ActionResult> GetAll()
57 {
58 _logger.LogInformation("Fetching all notifications");
59 var response = await _mediator.Send(new GetAllRequest());
60 return Ok(response);
61 }
62
63 [HttpGet("{id:int}")]
64 [AllowAnonymous]
65 public async Task<ActionResult> GetById(int id)
66 {
67 _logger.LogInformation("Fetching notification with ID: {NotificationId}", id);
68 var response = await _mediator.Send(new GetRequest(id));
69 return Ok(response);
70 }
71
72 [HttpPost]
73 [Authorize]
74 public async Task<ActionResult> Add([FromBody] AddRequest request)
75 {
76 _logger.LogInformation("Adding a new notification");
77 var response = await _mediator.Send(request);
78 return Ok(response);
79 }
80
81 [HttpPut("{id:int}")]
82 [Authorize]
83 public async Task<ActionResult> Update(int id, [FromBody] UpdateRequest request)
84 {
85 _logger.LogInformation("Updating notification with ID: {NotificationId}", id);
86 if (id != request.Id)
87 {
88 return BadRequest("Route ID and body ID must match.");
89 }
90
91 var response = await _mediator.Send(request);
92 return Ok(response);
93 }
94
95 [HttpDelete("{id:int}")]
96 [Authorize]
97 public async Task<ActionResult> Delete(int id)
98 {
99 _logger.LogInformation("Deleting notification with ID: {NotificationId}", id);
100 var response = await _mediator.Send(new DeleteRequest(id));
101 return Ok(response);
102 }
103 }
104}
105
Note: See TracBrowser for help on using the repository browser.