diff --git a/Controllers/QuoteAddController.cs b/Controllers/QuoteAddController.cs new file mode 100644 index 0000000..d171711 --- /dev/null +++ b/Controllers/QuoteAddController.cs @@ -0,0 +1,92 @@ +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Authorization; +using QuotifyBE.Data; +using QuotifyBE.Entities; +using System.Security.Claims; +using Microsoft.EntityFrameworkCore; + +namespace QuotifyBE.Controllers +{ + [ApiController] + [Route("controller")] + + public class QuotesController : ControllerBase + { + private readonly ApplicationDbContext _db; + + public QuotesController(ApplicationDbContext db) + { + _db = db; + } + + [HttpPost] + [Authorize(Roles = "Admin")] + public async Task CreateQuote([FromBody] CreateQuoteRequest request) + { + // Get user ID from claims + var userIdClaim = User.FindFirst(ClaimTypes.NameIdentifier)?.Value; + if (userIdClaim == null || !int.TryParse(userIdClaim, out int userId)) + return Unauthorized("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 ?? 0, + UserId = userId, + QuoteCategories = new List() + }; + + // Attach categories + foreach (var categoryId in request.CategoryIds) + { + var categoryExists = await _db.Categories.AnyAsync(c => c.Id == categoryId); + if (!categoryExists) + return BadRequest($"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); + } + + [HttpGet("{id}")] + public async Task GetQuoteById(int id) + { + var quote = await _db.Quotes + .Include(q => q.QuoteCategories) + .ThenInclude(qc => qc.Category) + .Include(q => q.User) + .Include(q => q.ImageId) + .FirstOrDefaultAsync(q => q.Id == id); + + if (quote == null) + return NotFound(); + + return Ok(quote); + } + } +} \ No newline at end of file diff --git a/Controllers/RandomQuoteController.cs b/Controllers/RandomQuoteController.cs new file mode 100644 index 0000000..b5bd70a --- /dev/null +++ b/Controllers/RandomQuoteController.cs @@ -0,0 +1,63 @@ +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using QuotifyBE.Data; +using QuotifyBE.Entities; +namespace QuotifyBE.Controllers +{ + [ApiController] + [Route("controller")] + public class QuotesController_2 : ControllerBase + { + private readonly ApplicationDbContext _db; + + public QuotesController_2(ApplicationDbContext db) + { + _db = db; + } + [HttpGet] + [AllowAnonymous] + + public async Task GetRandomQuote() + { + var totalQuotes = await _db.Quotes.CountAsync(); + if (totalQuotes == 0) + return NotFound(new { status = "error", error_msg = "brak cytatow" }); + + 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(); + + Image? image = null; + if (quote.ImageId != 0) + { + image = await _db.Images.FirstOrDefaultAsync(i => i.Id == quote.ImageId); + } + + var dto = new RandomQuote + { + 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() + }; + + return Ok(dto); + + } + + + } +} \ No newline at end of file diff --git a/DTOs/CreateQuote.cs b/DTOs/CreateQuote.cs new file mode 100644 index 0000000..ca35264 --- /dev/null +++ b/DTOs/CreateQuote.cs @@ -0,0 +1,7 @@ +public record class CreateQuoteRequest +{ + public string Text { get; set; } + public string Author { get; set; } + public List CategoryIds { get; set; } + public string? ImageUrl { get; set; } +}; \ No newline at end of file diff --git a/DTOs/RandomQuote.cs b/DTOs/RandomQuote.cs new file mode 100644 index 0000000..88f4c35 --- /dev/null +++ b/DTOs/RandomQuote.cs @@ -0,0 +1,9 @@ +public record class RandomQuote +{ + 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(); + +}; +