File size: 2,119 Bytes
47e77e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1201c4d
 
 
 
 
 
 
 
 
 
 
 
 
47e77e5
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
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using TaskTrackingSystem.Shared;
using TaskTrackingSystem.WebApi.Infrastructure;

namespace TaskTrackingSystem.WebApi.Features.Notification;

[ApiController]
[Authorize]
[Route("api/[controller]")]
public class NotificationController : ControllerBase
{
    private readonly NotificationService _notificationService;

    public NotificationController(NotificationService notificationService)
    {
        _notificationService = notificationService;
    }

    [HttpGet("mine")]
    public async Task<ActionResult<IEnumerable<TaskTrackingSystem.Shared.Models.Notification.NotificationDto>>> GetMine([FromQuery] int take = 10)
    {
        var userId = User.GetUserId();
        if (userId <= 0)
        {
            return Unauthorized();
        }

        var items = await _notificationService.GetRecentAsync(userId, take);
        return Ok(items);
    }

    [HttpGet("unread-count")]
    public async Task<ActionResult<int>> GetUnreadCount()
    {
        var userId = User.GetUserId();
        if (userId <= 0)
        {
            return Unauthorized();
        }

        var count = await _notificationService.GetUnreadCountAsync(userId);
        return Ok(count);
    }

    [HttpPost("{id}/read")]
    public async Task<ActionResult<Result>> MarkRead(long id)
    {
        var userId = User.GetUserId();
        if (userId <= 0)
        {
            return Unauthorized(Result.Failure("User is not authenticated.", 401));
        }

        var result = await _notificationService.MarkReadAsync(userId, id);
        return StatusCode(result.StatusCode, result);
    }

    [HttpDelete("read")]
    public async Task<ActionResult<Result<int>>> DeleteRead([FromQuery] DateTime? before = null)
    {
        var userId = User.GetUserId();
        if (userId <= 0)
        {
            return Unauthorized(Result.Failure("User is not authenticated.", 401));
        }

        var deletedCount = await _notificationService.DeleteReadNotificationsAsync(userId, before);
        return Ok(Result<int>.Success(deletedCount));
    }
}