source: ChapterX.Application/Chapter/Commands/UpdateHandler.cs@ b373fea

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

Fixes for authentication and auhtorization\

  • Property mode set to 100644
File size: 1.4 KB
Line 
1using ChapterX.Domain.Repositories;
2using MediatR;
3using Microsoft.Extensions.Logging;
4
5namespace ChapterX.Application.Chapter.Commands
6{
7 public class UpdateHandler : IRequestHandler<UpdateRequest, UpdateResponse>
8 {
9 private readonly IChapterRepository _chapterRepository;
10 private readonly ILogger<UpdateHandler> _logger;
11
12 public UpdateHandler(IChapterRepository chapterRepository, ILogger<UpdateHandler> logger)
13 {
14 _chapterRepository = chapterRepository;
15 _logger = logger;
16 }
17
18 public async Task<UpdateResponse> Handle(UpdateRequest request, CancellationToken cancellationToken)
19 {
20 var chapter = await _chapterRepository.GetByIdWithStoryAsync(request.Id, cancellationToken);
21 if (chapter is null)
22 return new UpdateResponse(false);
23
24 if (chapter.Story.UserId != request.CallerId)
25 throw new UnauthorizedAccessException("You do not own this chapter.");
26
27 chapter.Number = request.Number;
28 chapter.Name = request.Name;
29 chapter.Title = request.Title;
30 chapter.Content = request.Content;
31 chapter.WordCount = request.WordCount;
32 chapter.UpdatedAt = DateTime.UtcNow;
33
34 await _chapterRepository.UpdateAsync(chapter, cancellationToken);
35
36 return new UpdateResponse(true);
37 }
38 }
39}
Note: See TracBrowser for help on using the repository browser.