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

main
Last change on this file since e882b92 was e882b92, checked in by kikisrbinoska <srbinoskakristina07@…>, 13 days ago

Added corrected entitef from the ER diagram and changes for the logic to be coresponding to the ddl

  • 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 contentType = n.ContentType,
38 storyId = n.StoryId,
39 link = n.Link,
40 });
41 return Ok(result);
42 }
43
44 [HttpPut("{id:int}/read")]
45 [Authorize]
46 public async Task<ActionResult> MarkRead(int id)
47 {
48 var notification = await _notificationRepository.GetByIdAsync(id);
49 if (notification == null) return NotFound();
50 notification.IsRead = true;
51 await _notificationRepository.UpdateAsync(notification);
52 return Ok();
53 }
54
55 [HttpGet]
56 [AllowAnonymous]
57 public async Task<ActionResult> GetAll()
58 {
59 _logger.LogInformation("Fetching all notifications");
60 var response = await _mediator.Send(new GetAllRequest());
61 return Ok(response);
62 }
63
64 [HttpGet("{id:int}")]
65 [AllowAnonymous]
66 public async Task<ActionResult> GetById(int id)
67 {
68 _logger.LogInformation("Fetching notification with ID: {NotificationId}", id);
69 var response = await _mediator.Send(new GetRequest(id));
70 return Ok(response);
71 }
72
73 [HttpPost]
74 [Authorize]
75 public async Task<ActionResult> Add([FromBody] AddRequest request)
76 {
77 _logger.LogInformation("Adding a new notification");
78 var response = await _mediator.Send(request);
79 return Ok(response);
80 }
81
82 [HttpPut("{id:int}")]
83 [Authorize]
84 public async Task<ActionResult> Update(int id, [FromBody] UpdateRequest request)
85 {
86 _logger.LogInformation("Updating notification with ID: {NotificationId}", id);
87 if (id != request.Id)
88 {
89 return BadRequest("Route ID and body ID must match.");
90 }
91
92 var response = await _mediator.Send(request);
93 return Ok(response);
94 }
95
96 [HttpDelete("{id:int}")]
97 [Authorize]
98 public async Task<ActionResult> Delete(int id)
99 {
100 _logger.LogInformation("Deleting notification with ID: {NotificationId}", id);
101 var response = await _mediator.Send(new DeleteRequest(id));
102 return Ok(response);
103 }
104 }
105}
106
Note: See TracBrowser for help on using the repository browser.