Changeset 99c1e45 for ChapterX.API
- Timestamp:
- 06/24/26 16:28:50 (12 days ago)
- Branches:
- main
- Children:
- a8f4a2d
- Parents:
- 0b502c2
- Location:
- ChapterX.API
- Files:
-
- 3 edited
-
Controllers/ChaptersController.cs (modified) (3 diffs)
-
Controllers/StoriesController.cs (modified) (3 diffs)
-
Program.cs (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
ChapterX.API/Controllers/ChaptersController.cs
r0b502c2 r99c1e45 1 1 using ChapterX.Application.Chapter.Commands; 2 2 using ChapterX.Application.Chapter.Queries; 3 using ChapterX.Domain.Repositories; 3 4 using MediatR; 4 5 using Microsoft.AspNetCore.Authorization; … … 14 15 { 15 16 private readonly IMediator _mediator; 17 private readonly IChapterRepository _chapterRepository; 16 18 private readonly ILogger<ChaptersController> _logger; 17 19 18 public ChaptersController(IMediator mediator, I Logger<ChaptersController> logger)20 public ChaptersController(IMediator mediator, IChapterRepository chapterRepository, ILogger<ChaptersController> logger) 19 21 { 20 22 _mediator = mediator; 23 _chapterRepository = chapterRepository; 21 24 _logger = logger; 22 25 } … … 38 41 var response = await _mediator.Send(new GetRequest(id)); 39 42 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(); 40 51 } 41 52 -
ChapterX.API/Controllers/StoriesController.cs
r0b502c2 r99c1e45 29 29 _logger.LogInformation("Fetching all stories"); 30 30 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 }); 32 49 } 33 50 … … 39 56 _logger.LogInformation("Fetching story with ID: {StoryId}", id); 40 57 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 }); 42 77 } 43 78 … … 76 111 _logger.LogInformation("Deleting story with ID: {StoryId}", id); 77 112 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)); 79 115 return Ok(response); 80 116 } -
ChapterX.API/Program.cs
r0b502c2 r99c1e45 77 77 var app = builder.Build(); 78 78 79 app.UseCors("Frontend");80 81 if (app.Environment.IsDevelopment())82 {83 app.UseSwagger();84 app.UseSwaggerUI();85 }86 87 79 app.UseExceptionHandler(err => err.Run(async ctx => 88 80 { … … 130 122 })); 131 123 124 app.UseCors("Frontend"); 125 126 if (app.Environment.IsDevelopment()) 127 { 128 app.UseSwagger(); 129 app.UseSwaggerUI(); 130 } 131 132 132 app.UseAuthentication(); 133 133 app.UseAuthorization();
Note:
See TracChangeset
for help on using the changeset viewer.
