source: ChapterX.API/Controllers/ReadingListsController.cs@ a7550ca

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

Fixed reading lists,comments and likes

  • Property mode set to 100644
File size: 3.8 KB
Line 
1using ChapterX.Application.ReadingList.Commands;
2using ChapterX.Application.ReadingList.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 ReadingListsController : ControllerBase
14 {
15 private readonly IMediator _mediator;
16 private readonly IReadingListRepository _readingListRepository;
17 private readonly ILogger<ReadingListsController> _logger;
18
19 public ReadingListsController(IMediator mediator, IReadingListRepository readingListRepository, ILogger<ReadingListsController> logger)
20 {
21 _mediator = mediator;
22 _readingListRepository = readingListRepository;
23 _logger = logger;
24 }
25
26 [HttpGet("user/{userId:int}")]
27 [Authorize]
28 public async Task<ActionResult> GetByUser(int userId)
29 {
30 _logger.LogInformation("Fetching reading lists for user {UserId}", userId);
31 var lists = await _readingListRepository.GetByUserIdAsync(userId);
32 var result = lists.Select(l => MapList(l));
33 return Ok(result);
34 }
35
36 [HttpGet]
37 [AllowAnonymous]
38 public async Task<ActionResult> GetAll()
39 {
40 _logger.LogInformation("Fetching all reading lists");
41 var lists = await _readingListRepository.GetAllAsync();
42 var result = lists.Select(l => MapList(l));
43 return Ok(result);
44 }
45
46 private static object MapList(Domain.Entities.ReadingList l) => new
47 {
48 id = l.Id,
49 name = l.Name,
50 content = l.Content,
51 isPublic = l.IsPublic,
52 userId = l.UserId,
53 createdAt = l.CreatedAt,
54 username = l.User?.Username ?? "",
55 readingListItems = l.ReadingListItems.Select(i => new
56 {
57 listId = i.ListId,
58 storyId = i.StoryId,
59 addedAt = i.AddedAt,
60 storyTitle = i.Story?.ShortDescription ?? "",
61 authorUsername = i.Story?.Writer?.User?.Username ?? "",
62 genres = i.Story?.HasGenres.Select(hg => hg.Genre?.Name).Where(n => n != null).ToList() ?? []
63 }).ToList()
64 };
65
66 [HttpGet("{id:int}")]
67 [AllowAnonymous]
68 public async Task<ActionResult> GetById(int id)
69 {
70 _logger.LogInformation("Fetching reading list with ID: {ReadingListId}", id);
71 var response = await _mediator.Send(new GetRequest(id));
72 return Ok(response);
73 }
74
75 [HttpPost]
76 [Authorize]
77 public async Task<ActionResult> Add([FromBody] AddRequest request)
78 {
79 _logger.LogInformation("Adding a new reading list with Name: {ReadingListName}", request.Name);
80 var response = await _mediator.Send(request);
81 return Ok(response);
82 }
83
84 [HttpPut("{id:int}")]
85 [Authorize]
86 public async Task<ActionResult> Update(int id, [FromBody] UpdateRequest request)
87 {
88 _logger.LogInformation("Updating reading list with ID: {ReadingListId}", id);
89 if (id != request.Id)
90 {
91 return BadRequest("Route ID and body ID must match.");
92 }
93
94 var response = await _mediator.Send(request);
95 return Ok(response);
96 }
97
98 [HttpDelete("{id:int}")]
99 [Authorize]
100 public async Task<ActionResult> Delete(int id)
101 {
102 _logger.LogInformation("Deleting reading list with ID: {ReadingListId}", id);
103 var response = await _mediator.Send(new DeleteRequest(id));
104 return Ok(response);
105 }
106 }
107}
108
Note: See TracBrowser for help on using the repository browser.