Ignore:
Timestamp:
06/23/26 15:20:39 (13 days ago)
Author:
kikisrbinoska <srbinoskakristina07@…>
Branches:
main
Children:
0b502c2
Parents:
d300631
Message:

Fixes for authentication and auhtorization\

Location:
ChapterX.Application/Chapter/Commands
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • ChapterX.Application/Chapter/Commands/AddRequest.cs

    rd300631 rb373fea  
    11using MediatR;
     2using System.ComponentModel.DataAnnotations;
    23
    34namespace ChapterX.Application.Chapter.Commands
    45{
    56    public record AddRequest(
    6         int Number,
    7         string Name,
    8         string Title,
    9         string Content,
     7        [Range(1, int.MaxValue)] int Number,
     8        [Required][MaxLength(200)] string Name,
     9        [Required][MaxLength(300)] string Title,
     10        [Required] string Content,
    1011        int StoryId
    1112    ) : IRequest<AddResponse>;
  • ChapterX.Application/Chapter/Commands/DeleteHandler.cs

    rd300631 rb373fea  
    1818        public async Task<DeleteResponse> Handle(DeleteRequest request, CancellationToken cancellationToken)
    1919        {
    20             var chapter = await _chapterRepository.GetByIdAsync(request.Id, cancellationToken);
     20            var chapter = await _chapterRepository.GetByIdWithStoryAsync(request.Id, cancellationToken);
    2121            if (chapter is null)
    2222                return new DeleteResponse(false);
     23
     24            if (chapter.Story.UserId != request.CallerId)
     25                throw new UnauthorizedAccessException("You do not own this chapter.");
    2326
    2427            await _chapterRepository.DeleteAsync(chapter, cancellationToken);
  • ChapterX.Application/Chapter/Commands/DeleteRequest.cs

    rd300631 rb373fea  
    33namespace ChapterX.Application.Chapter.Commands
    44{
    5     public record DeleteRequest(int Id) : IRequest<DeleteResponse>;
     5    public record DeleteRequest(int Id, int CallerId) : IRequest<DeleteResponse>;
    66}
  • ChapterX.Application/Chapter/Commands/UpdateHandler.cs

    rd300631 rb373fea  
    1818        public async Task<UpdateResponse> Handle(UpdateRequest request, CancellationToken cancellationToken)
    1919        {
    20             var chapter = await _chapterRepository.GetByIdAsync(request.Id, cancellationToken);
     20            var chapter = await _chapterRepository.GetByIdWithStoryAsync(request.Id, cancellationToken);
    2121            if (chapter is null)
    2222                return new UpdateResponse(false);
     23
     24            if (chapter.Story.UserId != request.CallerId)
     25                throw new UnauthorizedAccessException("You do not own this chapter.");
    2326
    2427            chapter.Number = request.Number;
  • ChapterX.Application/Chapter/Commands/UpdateRequest.cs

    rd300631 rb373fea  
    99        string Title,
    1010        string Content,
    11         int? WordCount
     11        int? WordCount,
     12        int CallerId = 0
    1213    ) : IRequest<UpdateResponse>;
    1314}
Note: See TracChangeset for help on using the changeset viewer.