mirror of
https://github.com/QuotifyTeam/QuotifyBE.git
synced 2025-12-16 15:40:07 +01:00
222 lines
7.6 KiB
C#
222 lines
7.6 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Cors;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using QuotifyBE.Data;
|
|
using QuotifyBE.DTOs;
|
|
using QuotifyBE.Entities;
|
|
using QuotifyBE.Mapping;
|
|
using System.Security.Claims;
|
|
|
|
namespace QuotifyBE.Controllers;
|
|
|
|
|
|
[ApiController]
|
|
[Route("api/v1/quotes")]
|
|
[Produces("application/json")]
|
|
public class QuotesController : ControllerBase
|
|
{
|
|
|
|
private readonly ApplicationDbContext _db;
|
|
private readonly GeneralUseHelpers guhf;
|
|
|
|
public QuotesController(ApplicationDbContext db, GeneralUseHelpers GUHF)
|
|
{
|
|
_db = db;
|
|
guhf = GUHF;
|
|
}
|
|
|
|
// GET /api/v1/quotes
|
|
/// <summary>
|
|
/// Get a page of quotes
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// A page of quotes consists of 10 quotes or less.
|
|
/// If a page does not contain any quotes, 404 is returned.
|
|
/// Important! Has CORS set, unlike e.g. GET /api/v1/quote/{id} or GET /api/v1/quote/random.
|
|
/// </remarks>
|
|
/// <param name="page_no">The page number</param>
|
|
/// <returns>A page (10 quotes)</returns>
|
|
/// <response code="200">Returned on valid request</response>
|
|
/// <response code="404">Returned when requested page is invalid</response>
|
|
[HttpGet("page/{page_no}")]
|
|
[EnableCors]
|
|
[ProducesResponseType(typeof(List<QuoteShortDTO>), 200)]
|
|
[ProducesResponseType(typeof(ErrorDTO), 404)]
|
|
public async Task<IActionResult> GetQuotePage(int page_no)
|
|
{
|
|
var totalQuotes = await _db.Quotes.CountAsync();
|
|
const int PageSize = 10;
|
|
|
|
if (page_no <= 0)
|
|
{
|
|
return NotFound(new ErrorDTO { Status = "error", Error_msg = "Numer strony musi być większy niż 0" });
|
|
}
|
|
var quotes = await _db.Quotes
|
|
.Include(q => q.QuoteCategories)
|
|
.ThenInclude(qc => qc.Category)
|
|
.Include(q => q.User)
|
|
.Include(q => q.Image)
|
|
.OrderBy(q => q.Id)
|
|
.Skip((page_no - 1) * PageSize)
|
|
.Take(PageSize)
|
|
.ToListAsync();
|
|
|
|
var result = quotes
|
|
.Select(q => q.ToQuoteShortDTO())
|
|
.ToList();
|
|
|
|
return Ok(result);
|
|
|
|
}
|
|
|
|
// GET /api/v1/quotes/{id}
|
|
/// <summary>
|
|
/// Get specified quote summary
|
|
/// </summary>
|
|
/// <param name="id">The quote id in question</param>
|
|
/// <returns>A quote: id, quote content and author, imageUrl and categories if successful, otherwise: error message</returns>
|
|
/// <response code="200">Returned on valid request</response>
|
|
/// <response code="404">Returned when quote id is invalid or simply doesn't exist</response>
|
|
[HttpGet("{id}")]
|
|
[ProducesResponseType(typeof(QuoteShortDTO), 200)]
|
|
[ProducesResponseType(typeof(ErrorDTO), 404)]
|
|
public async Task<IActionResult> GetQuoteById(int id)
|
|
{
|
|
|
|
var quote = await _db.Quotes
|
|
.Include(q => q.QuoteCategories!)
|
|
.ThenInclude(qc => qc.Category)
|
|
.Include(q => q.User)
|
|
.Include(q => q.Image)
|
|
.FirstOrDefaultAsync(q => q.Id == id);
|
|
|
|
if (quote == null)
|
|
return NotFound(new { status = "error", error_msg = "Quote not found" });
|
|
|
|
return Ok(quote.ToQuoteShortDTO());
|
|
}
|
|
|
|
// POST /api/v1/quotes/new
|
|
/// <summary>
|
|
/// [AUTHED] Add a new quote
|
|
/// </summary>
|
|
/// <returns>Newly created quote's id</returns>
|
|
/// <param name="request">Form data containing required quote information</param>
|
|
/// <response code="201">Returned on valid request</response>
|
|
/// <response code="400">Returned when any of the categories does not exist</response>
|
|
/// <response code="403">Returned when user's id does not match the creator's id</response>
|
|
[HttpPost("new")]
|
|
[Authorize]
|
|
[EnableCors]
|
|
[ProducesResponseType(201)] // ? FIXME
|
|
[ProducesResponseType(typeof(ErrorDTO), 400)]
|
|
[ProducesResponseType(typeof(ErrorDTO), 403)]
|
|
public async Task<IActionResult> CreateQuote([FromBody] CreateQuoteDTO request)
|
|
{
|
|
// Get user ID from claims
|
|
// FIXME
|
|
var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
|
if (userIdClaim == null || !int.TryParse(userIdClaim, out int userId))
|
|
// https://stackoverflow.com/a/47708867
|
|
return StatusCode(403, new ErrorDTO { Status = "error", Error_msg = "Invalid user ID" });
|
|
|
|
// Find or create image
|
|
Image? image = null;
|
|
if (!string.IsNullOrEmpty(request.ImageUrl))
|
|
{
|
|
image = await _db.Images.FirstOrDefaultAsync(i => i.Url == request.ImageUrl);
|
|
if (image == null)
|
|
{
|
|
image = new Image { Url = request.ImageUrl };
|
|
_db.Images.Add(image);
|
|
await _db.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
// Create quote
|
|
var quote = new Quote
|
|
{
|
|
Text = request.Text,
|
|
Author = request.Author,
|
|
CreatedAt = DateTime.UtcNow,
|
|
LastUpdatedAt = DateTime.UtcNow,
|
|
ImageId = image?.Id ?? null,
|
|
UserId = userId,
|
|
QuoteCategories = new List<QuoteCategory>()
|
|
};
|
|
|
|
// Attach categories
|
|
foreach (var categoryId in request.CategoryIds ?? [])
|
|
{
|
|
var categoryExists = await _db.Categories.AnyAsync(c => c.Id == categoryId);
|
|
if (!categoryExists)
|
|
return BadRequest(new ErrorDTO { Status = "error", Error_msg = $"Category ID {categoryId} not found"});
|
|
|
|
quote.QuoteCategories.Add(new QuoteCategory
|
|
{
|
|
CategoryId = categoryId,
|
|
Quote = quote
|
|
});
|
|
}
|
|
|
|
_db.Quotes.Add(quote);
|
|
await _db.SaveChangesAsync();
|
|
|
|
return CreatedAtAction(nameof(GetQuoteById), new { id = quote.Id }, quote);
|
|
}
|
|
|
|
// GET /api/v1/quotes/random
|
|
/// <summary>
|
|
/// Get a random quote summary
|
|
/// </summary>
|
|
/// <returns>A quote: id, quote content and author, imageUrl and categories if successful, otherwise: error message</returns>
|
|
/// <response code="200">Returned on valid request</response>
|
|
/// <response code="404">Returned when no quotes exist</response>
|
|
[HttpGet("random")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(QuoteShortDTO), 200)]
|
|
[ProducesResponseType(typeof(ErrorDTO), 404)]
|
|
public async Task<IActionResult> GetRandomQuote()
|
|
{
|
|
var totalQuotes = await _db.Quotes.CountAsync();
|
|
if (totalQuotes == 0)
|
|
return NotFound(new ErrorDTO { Status = "error", Error_msg = "No quotes to choose from" });
|
|
|
|
var random = new Random();
|
|
var skip = random.Next(0, totalQuotes);
|
|
|
|
var quote = await _db.Quotes
|
|
.Include(q => q.QuoteCategories!)
|
|
.ThenInclude(qc => qc.Category)
|
|
.Skip(skip)
|
|
.Take(1)
|
|
.FirstOrDefaultAsync();
|
|
|
|
if (quote == null)
|
|
return NotFound(new ErrorDTO { Status = "error", Error_msg = "Unknown error - couldn't get quote"});
|
|
|
|
Image? image = null;
|
|
if (quote.ImageId != 0)
|
|
{
|
|
image = await _db.Images.FirstOrDefaultAsync(i => i.Id == quote.ImageId);
|
|
}
|
|
|
|
var dto = new QuoteShortDTO
|
|
{
|
|
Id = quote.Id,
|
|
Text = quote.Text,
|
|
Author = quote.Author,
|
|
ImageUrl = image?.Url,
|
|
Categories = quote.QuoteCategories?
|
|
.Select(qc => qc.Category?.Name ?? "")
|
|
.Where(name => !string.IsNullOrEmpty(name))
|
|
.ToList() ?? new List<string>()
|
|
};
|
|
|
|
return Ok(dto);
|
|
|
|
}
|
|
|
|
}
|