diff --git a/Controllers/CategoryController.cs b/Controllers/CategoryController.cs index b9ce66d..1d91680 100644 --- a/Controllers/CategoryController.cs +++ b/Controllers/CategoryController.cs @@ -94,7 +94,7 @@ public class CategoryController : ControllerBase [HttpGet] [EnableCors] [ProducesResponseType(typeof(List), 200)] - public async Task GetQuotePage() + public async Task GetEveryCategory() { // The following seems to be a bad idea, so I leave it as is. ~eee4 // diff --git a/Controllers/QuoteController.cs b/Controllers/QuoteController.cs index 6daa5fd..9137ee6 100644 --- a/Controllers/QuoteController.cs +++ b/Controllers/QuoteController.cs @@ -46,7 +46,7 @@ public class QuotesController : ControllerBase /// Returned when requested page is invalid (page_no <= 0) [HttpGet("page/{page_no}")] [EnableCors] - [ProducesResponseType(typeof(List), 200)] + [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(ErrorDTO), 404)] public async Task GetQuotePage(int page_no = 1, string? sort = "desc", [FromQuery] string? category_id = null) { @@ -113,7 +113,7 @@ public class QuotesController : ControllerBase } var result = pageQuotes - .Select(q => q.ToQuoteShortDTO()) + .Select(q => q.ToQuoteCompleteDTO()) .ToList(); return Ok(result); @@ -132,7 +132,7 @@ public class QuotesController : ControllerBase /// Returned on valid request /// Returned when quote id is invalid or simply doesn't exist [HttpGet("{id}")] - [ProducesResponseType(typeof(QuoteShortDTO), 200)] + [ProducesResponseType(typeof(QuoteCompleteDTO), 200)] [ProducesResponseType(typeof(ErrorDTO), 404)] public async Task GetQuoteById(int id) { @@ -147,7 +147,7 @@ public class QuotesController : ControllerBase if (quote == null) return NotFound(new { status = "error", error_msg = "Quote not found" }); - return Ok(quote.ToQuoteShortDTO()); + return Ok(quote.ToQuoteCompleteDTO()); } // POST /api/v1/quotes/new diff --git a/DTOs/QuoteCompleteDTO.cs b/DTOs/QuoteCompleteDTO.cs new file mode 100644 index 0000000..12d9b25 --- /dev/null +++ b/DTOs/QuoteCompleteDTO.cs @@ -0,0 +1,14 @@ +namespace QuotifyBE.DTOs; + +public record class QuoteCompleteDTO +{ + public int Id { get; set; } + public string Text { get; set; } = string.Empty; + public string Author { get; set; } = string.Empty; + public string? ImageUrl { get; set; } + public List? Categories { get; set; } = new(); + public DateTime? createDate { get; set; } + public DateTime? updateDate { get; set; } + +}; + diff --git a/Mapping/QuoteMapping.cs b/Mapping/QuoteMapping.cs index 278ed9d..679fbb2 100644 --- a/Mapping/QuoteMapping.cs +++ b/Mapping/QuoteMapping.cs @@ -29,4 +29,28 @@ public static class QuoteMapping Categories = categoryNames }; } + + public static QuoteCompleteDTO ToQuoteCompleteDTO(this Quote quote) + { + + List categoryNames = []; + if (quote.QuoteCategories != null) + { + foreach (QuoteCategory quoteCategory in quote.QuoteCategories) + { + categoryNames.Add(quoteCategory.Category!.Name ?? $"Unnamed category {quoteCategory.CategoryId}"); + } + } + + return new QuoteCompleteDTO + { + Id = quote.Id, + Text = quote.Text, + Author = quote.Author, + ImageUrl = quote.Image?.Url, + Categories = categoryNames, + createDate = quote.CreatedAt, + updateDate = quote.LastUpdatedAt + }; + } }