Changeset 99c1e45 for ChapterX.API


Ignore:
Timestamp:
06/24/26 16:28:50 (12 days ago)
Author:
kikisrbinoska <srbinoskakristina07@…>
Branches:
main
Children:
a8f4a2d
Parents:
0b502c2
Message:

Fixed writer section and admin management

Location:
ChapterX.API
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • ChapterX.API/Controllers/ChaptersController.cs

    r0b502c2 r99c1e45  
    11using ChapterX.Application.Chapter.Commands;
    22using ChapterX.Application.Chapter.Queries;
     3using ChapterX.Domain.Repositories;
    34using MediatR;
    45using Microsoft.AspNetCore.Authorization;
     
    1415    {
    1516        private readonly IMediator _mediator;
     17        private readonly IChapterRepository _chapterRepository;
    1618        private readonly ILogger<ChaptersController> _logger;
    1719
    18         public ChaptersController(IMediator mediator, ILogger<ChaptersController> logger)
     20        public ChaptersController(IMediator mediator, IChapterRepository chapterRepository, ILogger<ChaptersController> logger)
    1921        {
    2022            _mediator = mediator;
     23            _chapterRepository = chapterRepository;
    2124            _logger = logger;
    2225        }
     
    3841            var response = await _mediator.Send(new GetRequest(id));
    3942            return Ok(response);
     43        }
     44
     45        [HttpPatch("{id:int}/view")]
     46        [AllowAnonymous]
     47        public async Task<ActionResult> IncrementView(int id)
     48        {
     49            await _chapterRepository.IncrementViewCountAsync(id);
     50            return NoContent();
    4051        }
    4152
  • ChapterX.API/Controllers/StoriesController.cs

    r0b502c2 r99c1e45  
    2929            _logger.LogInformation("Fetching all stories");
    3030            var response = await _mediator.Send(request);
    31             return Ok(response);
     31            var stories = response.Stories.Select(s => new
     32            {
     33                id = s.Id,
     34                userId = s.UserId,
     35                title = s.Title,
     36                shortDescription = s.ShortDescription,
     37                image = s.Image,
     38                content = s.Content,
     39                matureContent = s.MatureContent,
     40                createdAt = s.CreatedAt,
     41                updatedAt = s.UpdatedAt,
     42                writer = s.Writer == null ? null : new { user = s.Writer.User == null ? null : new { username = s.Writer.User.Username } },
     43                hasGenres = s.HasGenres.Select(hg => new { genre = new { name = hg.Genre?.Name ?? "" } }),
     44                likes = s.Likes.Select(l => new { userId = l.UserId }),
     45                comments = s.Comments.Select(c => new { id = c.Id }),
     46                chapters = s.Chapters.Select(c => new { id = c.Id, viewCount = c.ViewCount }),
     47            });
     48            return Ok(new { stories });
    3249        }
    3350
     
    3956            _logger.LogInformation("Fetching story with ID: {StoryId}", id);
    4057            var response = await _mediator.Send(new GetRequest(id));
    41             return Ok(response);
     58            if (response.Story == null) return NotFound();
     59            var s = response.Story;
     60            return Ok(new
     61            {
     62                id = s.Id,
     63                userId = s.UserId,
     64                title = s.Title,
     65                shortDescription = s.ShortDescription,
     66                image = s.Image,
     67                content = s.Content,
     68                matureContent = s.MatureContent,
     69                createdAt = s.CreatedAt,
     70                updatedAt = s.UpdatedAt,
     71                writer = s.Writer == null ? null : new { user = s.Writer.User == null ? null : new { username = s.Writer.User.Username } },
     72                hasGenres = s.HasGenres.Select(hg => new { genre = new { name = hg.Genre?.Name ?? "" } }),
     73                likes = s.Likes.Select(l => new { userId = l.UserId }),
     74                comments = s.Comments.Select(c => new { id = c.Id }),
     75                chapters = s.Chapters.Select(c => new { id = c.Id, viewCount = c.ViewCount }),
     76            });
    4277        }
    4378
     
    76111            _logger.LogInformation("Deleting story with ID: {StoryId}", id);
    77112            var callerId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
    78             var response = await _mediator.Send(new DeleteRequest(id, callerId));
     113            var isAdmin = User.IsInRole("Admin");
     114            var response = await _mediator.Send(new DeleteRequest(id, callerId, isAdmin));
    79115            return Ok(response);
    80116        }
  • ChapterX.API/Program.cs

    r0b502c2 r99c1e45  
    7777var app = builder.Build();
    7878
    79 app.UseCors("Frontend");
    80 
    81 if (app.Environment.IsDevelopment())
    82 {
    83     app.UseSwagger();
    84     app.UseSwaggerUI();
    85 }
    86 
    8779app.UseExceptionHandler(err => err.Run(async ctx =>
    8880{
     
    130122}));
    131123
     124app.UseCors("Frontend");
     125
     126if (app.Environment.IsDevelopment())
     127{
     128    app.UseSwagger();
     129    app.UseSwaggerUI();
     130}
     131
    132132app.UseAuthentication();
    133133app.UseAuthorization();
Note: See TracChangeset for help on using the changeset viewer.