mirror of
https://github.com/QuotifyTeam/QuotifyBE.git
synced 2025-12-16 15:20:06 +01:00
560 lines
21 KiB
C#
560 lines
21 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Cors;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Newtonsoft.Json.Linq;
|
|
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;
|
|
private readonly IConfiguration _appsettings;
|
|
|
|
public QuotesController(ApplicationDbContext db, GeneralUseHelpers GUHF, IConfiguration appsettings)
|
|
{
|
|
_db = db;
|
|
guhf = GUHF;
|
|
_appsettings = appsettings;
|
|
}
|
|
|
|
// 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, an empty list is returned.
|
|
/// <br/><br/>
|
|
/// <b>Important!</b>
|
|
/// 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>
|
|
/// <param name="sort">How to sort the results (desc/asc)</param>
|
|
/// <param name="category_id">(Optional) Standalone category id or comma separated ids (e.g. "1" or "1,2,3")</param>
|
|
/// <returns>A page (<= 10 quotes)</returns>
|
|
/// <response code="200">Returned on valid request</response>
|
|
/// <response code="404">Returned when requested page is invalid (page_no <= 0)</response>
|
|
[HttpGet("page/{page_no}")]
|
|
[EnableCors]
|
|
[ProducesResponseType(typeof(List<QuoteCompleteDTO>), 200)]
|
|
[ProducesResponseType(typeof(ErrorDTO), 404)]
|
|
public async Task<IActionResult> GetQuotePage(int page_no = 1, string? sort = "desc", [FromQuery] string? category_id = null)
|
|
{
|
|
var totalQuotes = await _db.Quotes.CountAsync();
|
|
const int PageSize = 10;
|
|
|
|
List<int>? categories;
|
|
try
|
|
{
|
|
categories = category_id?
|
|
.Split(",")
|
|
.Select(Int32.Parse)
|
|
.ToList();
|
|
} catch
|
|
{
|
|
// Try to catch badly formatted requests
|
|
return BadRequest(new ErrorDTO {
|
|
Status = "error",
|
|
Error_msg = "Category_id can be either an integer, or comma separated integers"
|
|
});
|
|
}
|
|
|
|
if (page_no <= 0)
|
|
{
|
|
return NotFound(new ErrorDTO { Status = "error", Error_msg = "Numer strony musi być większy niż 0" });
|
|
}
|
|
|
|
// Paginacja bez filtra
|
|
var baseQuery = _db.Quotes
|
|
.Include(q => q.QuoteCategories!)
|
|
.ThenInclude(qc => qc.Category)
|
|
.Include(q => q.User)
|
|
.Include(q => q.Image);
|
|
|
|
// Sort the results in ascending/descending order by id
|
|
IOrderedQueryable<Quote>? orderedQuery;
|
|
if (sort != null && sort.Equals("asc"))
|
|
orderedQuery = baseQuery.OrderBy(q => q.Id);
|
|
else
|
|
// Sort in descending order by default
|
|
orderedQuery = baseQuery.OrderByDescending(q => q.Id);
|
|
|
|
// Botched solution
|
|
List<Quote> pageQuotes;
|
|
|
|
// Filtrowanie przed pobraniem strony
|
|
if (categories != null)
|
|
{
|
|
pageQuotes = await orderedQuery
|
|
.Where(q => q.QuoteCategories!
|
|
.Any(qc => categories.Contains(qc.CategoryId))
|
|
//.Any(qc => qc.CategoryId == category_id.Value)
|
|
)
|
|
.Skip((page_no - 1) * PageSize)
|
|
.Take(PageSize)
|
|
.ToListAsync();
|
|
}
|
|
else
|
|
{
|
|
pageQuotes = await orderedQuery
|
|
.Skip((page_no - 1) * PageSize)
|
|
.Take(PageSize)
|
|
.ToListAsync();
|
|
}
|
|
|
|
var result = pageQuotes
|
|
.Select(q => q.ToQuoteCompleteDTO())
|
|
.ToList();
|
|
|
|
return Ok(result);
|
|
|
|
}
|
|
|
|
// GET /api/v1/quotes/{id}
|
|
/// <summary>
|
|
/// [AUTHED] Get specified quote summary
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// <s>As per project's guidelines, requires a JWT.</s> We need this endpoint to check previous draws for draw history.
|
|
/// </remarks>
|
|
/// <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(QuoteCompleteDTO), 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.ToQuoteCompleteDTO());
|
|
}
|
|
|
|
// POST /api/v1/quotes/new
|
|
/// <summary>
|
|
/// [AUTHED] Add a new quote
|
|
/// </summary>
|
|
/// <returns>Newly created quote's id</returns>
|
|
/// <remarks>
|
|
/// <b>Note</b>:
|
|
/// User-provided image URLs are validated by checking
|
|
/// if they start with "https://", "http://" or "/".
|
|
/// This is rather a naive solution.
|
|
/// </remarks>
|
|
/// <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>
|
|
/// <response code="406">Returned when image url is invalid (does not start with "https://", "http://", or "/")</response>
|
|
[HttpPost("new")]
|
|
[Authorize]
|
|
[EnableCors]
|
|
[ProducesResponseType(201)]
|
|
[ProducesResponseType(typeof(ErrorDTO), 400)]
|
|
[ProducesResponseType(typeof(ErrorDTO), 403)]
|
|
[ProducesResponseType(typeof(ErrorDTO), 406)]
|
|
public async Task<IActionResult> CreateQuote([FromBody] CreateQuoteDTO request)
|
|
{
|
|
// Get user ID from claims
|
|
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" });
|
|
|
|
// Try to find the image inside the DB
|
|
Image? image = null;
|
|
if (!string.IsNullOrEmpty(request.ImageUrl))
|
|
{
|
|
image = await _db.Images.FirstOrDefaultAsync(i => i.Url == request.ImageUrl);
|
|
|
|
// Failed? Just insert it yourself
|
|
if (image == null)
|
|
{
|
|
// Simple (naive) sanity check for image URLs
|
|
if ( !request.ImageUrl.StartsWith("http://")
|
|
&& !request.ImageUrl.StartsWith("https://")
|
|
&& !request.ImageUrl.StartsWith("/"))
|
|
return StatusCode(406, new ErrorDTO {
|
|
Status = "error",
|
|
Error_msg = "Image URLs should point to http/https url or a local resource"
|
|
});
|
|
|
|
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 ?? [])
|
|
{
|
|
Category? category = await _db.Categories.FirstOrDefaultAsync(c => c.Id == categoryId);
|
|
if (category == null)
|
|
return BadRequest(new ErrorDTO { Status = "error", Error_msg = $"Category ID {categoryId} not found" });
|
|
|
|
quote.QuoteCategories.Add(new QuoteCategory
|
|
{
|
|
Category = category,
|
|
Quote = quote
|
|
});
|
|
}
|
|
|
|
_db.Quotes.Add(quote);
|
|
await _db.SaveChangesAsync();
|
|
|
|
return CreatedAtAction(nameof(GetQuoteById), new { id = quote.Id }, quote.ToQuoteShortDTO());
|
|
}
|
|
|
|
// GET /api/v1/quotes/random
|
|
/// <summary>
|
|
/// Draw a random quote
|
|
/// </summary>
|
|
/// <returns>A quote: id, quote content and author, imageUrl and categories if successful, otherwise: error message</returns>
|
|
/// <param name="category_id">(Optional) category id to draw from</param>
|
|
/// <response code="200">Returned on valid request</response>
|
|
/// <response code="204">Returned when no quotes exist matching provided criteria</response>
|
|
/// <response code="404">Returned when no quotes exist (in the DB)</response>
|
|
[HttpGet("random")]
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(QuoteShortDTO), 200)]
|
|
[ProducesResponseType(204)]
|
|
[ProducesResponseType(typeof(ErrorDTO), 404)]
|
|
public async Task<IActionResult> GetRandomQuote([FromQuery] int? category_id = null)
|
|
{
|
|
IQueryable<Quote> query = _db.Quotes
|
|
.Include(q => q.QuoteCategories!)
|
|
.ThenInclude(qc => qc.Category)
|
|
.Include(q => q.Image);
|
|
|
|
if (category_id.HasValue)
|
|
{
|
|
query = query
|
|
.Where(q => q.QuoteCategories!
|
|
.Any(qc => qc.CategoryId == category_id.Value)
|
|
);
|
|
}
|
|
|
|
var totalQuotes = await query.CountAsync();
|
|
if (totalQuotes == 0)
|
|
{
|
|
if (category_id.HasValue)
|
|
return NoContent(); // Brak cytatów w wybranej kategorii
|
|
else
|
|
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 query
|
|
.Skip(skip)
|
|
.Take(1)
|
|
.FirstOrDefaultAsync();
|
|
|
|
if (quote == null)
|
|
return NotFound(new ErrorDTO { Status = "error", Error_msg = "Unknown error - couldn't get quote" });
|
|
|
|
// After getting and checking the quote, update the number of draws
|
|
Statistic s = await _db.Statistics
|
|
.FirstAsync(s => s.Label == "number_of_draws");
|
|
s.IValue += 1;
|
|
await _db.SaveChangesAsync();
|
|
|
|
return Ok(quote.ToQuoteShortDTO());
|
|
|
|
}
|
|
|
|
// DELETE /api/v1/quotes/{id}
|
|
/// <summary>
|
|
/// [AUTHED] Delete a quote
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Deletes a quote, granted it exists. <br/>
|
|
/// <br/>
|
|
/// <i>
|
|
/// Is this the best practice? Marking the quote as hidden is also an option.
|
|
/// </i> ~eee4
|
|
/// </remarks>
|
|
/// <returns>Json with status</returns>
|
|
/// <param name="id">Quote id which will be deleted</param>
|
|
/// <response code="200">Returned on valid request</response>
|
|
/// <response code="404">Returned when no such quote exists</response>
|
|
[HttpDelete("{id}")]
|
|
[Authorize]
|
|
[EnableCors]
|
|
[ProducesResponseType(200)]
|
|
[ProducesResponseType(typeof(ErrorDTO), 404)]
|
|
public async Task<IActionResult> DeleteQuote(int id)
|
|
{
|
|
// (Attempt to) find the quote
|
|
Quote? quote = await _db.Quotes
|
|
.FirstOrDefaultAsync(q => q.Id == id);
|
|
// Failed?
|
|
if (quote == null)
|
|
return NotFound(new { status = "error", error_msg = "Quote not found" });
|
|
|
|
// If succeded, remove the quote
|
|
_db.Quotes.Remove(quote);
|
|
await _db.SaveChangesAsync();
|
|
|
|
// ====================================================================== //
|
|
// Important! //
|
|
// Is this the best we can do? Won't marking the quote as "hidden" //
|
|
// be better than explicitly deleting it? ~eee4 //
|
|
// ====================================================================== //
|
|
|
|
// Return ok
|
|
return Ok(new { Status = "ok" });
|
|
}
|
|
|
|
// PATCH /api/v1/quotes/{id}
|
|
/// <summary>
|
|
/// [AUTHED] Modify an existing quote
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Modifies an existing quote.
|
|
/// <br/><br/>
|
|
/// <b>Warning!</b>
|
|
/// We don't check the user id which created the quote.
|
|
/// In case of single-user instances, this should not be a problem.
|
|
/// This might become one, if we want users with non-admin roles;
|
|
/// that would need some proper ACL checks here (with the help of GUHF).
|
|
/// <br/><br/>
|
|
/// <b>Important!</b>
|
|
/// Image handling works the same as with creating new quote.
|
|
/// This means that images not present in the DB will be added automatically.
|
|
/// <br/><br/>
|
|
/// <b>Important!</b>
|
|
/// "categories = null" is not the same as "categories = []"!
|
|
/// While "categories = null" will not alter the quote's categories,
|
|
/// "categories = []" will (and in turn, empty each and every present category)!<br/>
|
|
/// Be careful when handling user-provided categories!
|
|
/// <br/><br/>
|
|
/// <b>Note</b>:
|
|
/// User-provided image URLs are validated by checking
|
|
/// if they start with "https://", "http://" or "/".
|
|
/// This is rather a naive solution.
|
|
/// </remarks>
|
|
/// <returns>Newly modified quote as a DTO</returns>
|
|
/// <param name="id">Quote to be modified</param>
|
|
/// <param name="updatedQuote">Updated quote form data. Id is ignored.</param>
|
|
/// <response code="204">Returned on valid request</response>
|
|
/// <response code="400">Returned when request text or author is empty (or whitespace)</response>
|
|
/// <response code="404">Returned when no such quote exists</response>
|
|
/// <response code="406">Returned when image url is invalid (does not start with "https://", "http://", or "/")</response>
|
|
[HttpPatch("{id}")]
|
|
[Authorize]
|
|
[EnableCors]
|
|
[ProducesResponseType(typeof(QuoteShortDTO), 200)]
|
|
[ProducesResponseType(typeof(ErrorDTO), 400)]
|
|
[ProducesResponseType(typeof(ErrorDTO), 404)]
|
|
public async Task<IActionResult> EditQuote(int id, [FromBody] QuoteShortDTO updatedQuote)
|
|
{
|
|
// Try to find the quote in question
|
|
Quote? quote = await _db.Quotes
|
|
.Include(q => q.QuoteCategories)
|
|
.FirstOrDefaultAsync(q => q.Id == id);
|
|
|
|
// Failed?
|
|
if (quote == null)
|
|
return NotFound(new { status = "error", error_msg = "Quote not found" });
|
|
|
|
// Is quote contents or author empty?
|
|
if (string.IsNullOrWhiteSpace(updatedQuote.Text) || string.IsNullOrWhiteSpace(updatedQuote.Author))
|
|
return BadRequest(new ErrorDTO { Status = "error", Error_msg = "Text and author are required." });
|
|
|
|
// Alter the quote's content
|
|
quote.Text = updatedQuote.Text;
|
|
quote.Author = updatedQuote.Author;
|
|
quote.LastUpdatedAt = DateTime.UtcNow;
|
|
|
|
// Try to find the image inside the DB
|
|
Image? image = null;
|
|
if (!string.IsNullOrEmpty(updatedQuote.ImageUrl))
|
|
{
|
|
image = await _db.Images.FirstOrDefaultAsync(i => i.Url == updatedQuote.ImageUrl);
|
|
|
|
// Failed? Just insert it yourself
|
|
if (image == null)
|
|
{
|
|
// Simple (naive) sanity check for image URLs
|
|
if ( !updatedQuote.ImageUrl.StartsWith("http://")
|
|
&& !updatedQuote.ImageUrl.StartsWith("https://")
|
|
&& !updatedQuote.ImageUrl.StartsWith("/"))
|
|
return StatusCode(406, new ErrorDTO {
|
|
Status = "error",
|
|
Error_msg = "Image URLs should point to http/https url or a local resource"
|
|
});
|
|
|
|
image = new Image { Url = updatedQuote.ImageUrl };
|
|
_db.Images.Add(image);
|
|
await _db.SaveChangesAsync();
|
|
}
|
|
}
|
|
quote.Image = image;
|
|
|
|
// Don't touch categories if they are explicitly null
|
|
if (updatedQuote.Categories == null) { }
|
|
// If they aren't
|
|
else if (updatedQuote.Categories.Any())
|
|
{
|
|
// Get all the categories associated with a quote from DB
|
|
List<Category> categoriesFromDb = await _db.Categories
|
|
.Where(c => updatedQuote.Categories.Contains(c.Name))
|
|
.ToListAsync();
|
|
|
|
// Determine which ones are already present, and which to add
|
|
IEnumerable<string> existingNames = categoriesFromDb
|
|
.Select(c => c.Name);
|
|
List<string> newNames = updatedQuote.Categories
|
|
.Except(existingNames)
|
|
.ToList();
|
|
|
|
// For all the categories not present
|
|
foreach (var name in newNames)
|
|
{
|
|
// Add them to the DB
|
|
var newCat = new Category
|
|
{
|
|
Name = name,
|
|
Description = string.Empty,
|
|
CreatedAt = DateTime.UtcNow
|
|
};
|
|
_db.Categories.Add(newCat);
|
|
categoriesFromDb.Add(newCat);
|
|
}
|
|
|
|
// If any categories were added, save changes
|
|
if (newNames.Any())
|
|
await _db.SaveChangesAsync();
|
|
|
|
// Assign all the new categories to the quote
|
|
quote.QuoteCategories = categoriesFromDb
|
|
.Select(cat => new QuoteCategory
|
|
{
|
|
CategoryId = cat.Id,
|
|
QuoteId = quote.Id
|
|
})
|
|
.ToList();
|
|
}
|
|
else
|
|
{
|
|
// No categories (empty list) inside DTO?
|
|
// Clear them all!
|
|
quote.QuoteCategories.Clear();
|
|
}
|
|
|
|
// Save changes, return new quote as a DTO
|
|
await _db.SaveChangesAsync();
|
|
return Ok(quote.ToQuoteShortDTO());
|
|
}
|
|
|
|
// POST /api/v1/quotes/ai
|
|
/// <summary>
|
|
/// [AUTHED] Request a LLM-generated quote
|
|
/// </summary>
|
|
/// <returns>Generated quote's text</returns>
|
|
/// <remarks>
|
|
/// <b>Notes</b>:<br/>
|
|
///
|
|
/// <ul>
|
|
/// If <i>customPrompt</i> is passed:
|
|
/// <li>The default prompt is overriden by whatever has been passed by the user.</li>
|
|
/// </ul><br/>
|
|
///
|
|
/// <ul>
|
|
/// If <i>model</i> is passed:
|
|
/// <li>The default large language model is overriden by whatever has been passed by the user.</li>
|
|
/// </ul><br/>
|
|
///
|
|
/// <ul>
|
|
/// If <i>temperature</i> is passed:
|
|
/// <li>The default temperature (= 0.8) is overriden by whatever has been passed by the user.</li>
|
|
/// </ul><br/>
|
|
///
|
|
/// <ul>
|
|
/// If <i>categoryId</i> is passed:
|
|
/// <li>The prompt is appended with an instruction in Polish to generate quotes based on the provided category
|
|
/// (both name and description get passed to the model).</li>
|
|
/// <li><b>Heads up!</b> The text is appended even if <i>customPrompt</i> has been provided.</li>
|
|
/// </ul><br/>
|
|
///
|
|
/// <ul>
|
|
/// If <i>useSampleQuote</i> is passed:
|
|
/// <li>The prompt will be appended with a randomly chosen quote from the categoryId (if any exist),
|
|
/// thus passing categoryId becomes a prerequisite.</li>
|
|
/// <li><b>Heads up!</b> The request will fail returning status code 400 if categoryId isn't provided!</li>
|
|
/// </ul>
|
|
/// </remarks>
|
|
/// <param name="request">Form data containing required quote information</param>
|
|
/// <response code="200">Returned on valid request</response>
|
|
/// <response code="400">Returned when generation failed due to remote server error (likely because of a bad request)</response>
|
|
/// <response code="500">Returned when response has been generated, but couldn't be parsed (likely because of incompatible server or bad URL)</response>
|
|
[HttpPost("ai")]
|
|
[Authorize]
|
|
[EnableCors]
|
|
[ProducesResponseType(200)]
|
|
[ProducesResponseType(typeof(ErrorDTO), 400)]
|
|
[ProducesResponseType(typeof(ErrorDTO), 500)]
|
|
public async Task<IActionResult> CreateLLMQuote([FromBody] AskLLMInDTO request)
|
|
{
|
|
|
|
JObject? generatedResponse = await guhf.GenerateLLMResponse(
|
|
request.CustomPrompt, request.Model, request.Temperature, request.CategoryId, request.UseSampleQuote
|
|
);
|
|
|
|
string llmUsed = request.Model ?? _appsettings.GetSection("LlmIntegration")["DefaultModel"] ?? "deepclaude";
|
|
|
|
// Check if any errors occurred
|
|
if (generatedResponse == null)
|
|
{
|
|
return StatusCode(400, new ErrorDTO { Status = "error", Error_msg = "Generation failed most likely due to bad request" });
|
|
}
|
|
|
|
// Parse JSON to get the bot reply
|
|
string? llmResponse = generatedResponse["choices"]?[0]?["message"]?["content"]?.ToString().Trim('"');
|
|
|
|
// If response string is not where we expect it, return 500
|
|
if (llmResponse == null)
|
|
return StatusCode(500, new ErrorDTO { Status = "error", Error_msg = "Unexpected API response" });
|
|
|
|
// Otherwise, return the response
|
|
return Ok(new { Status = "ok", BotResponse = llmResponse, Model = llmUsed });
|
|
}
|
|
|
|
}
|