mirror of
https://github.com/QuotifyTeam/QuotifyBE.git
synced 2025-12-16 22:10:05 +01:00
kontroler - dodawanie cytatow
This commit is contained in:
92
Controllers/QuoteAddController.cs
Normal file
92
Controllers/QuoteAddController.cs
Normal file
@@ -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<IActionResult> 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<QuoteCategory>()
|
||||
};
|
||||
|
||||
// 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<IActionResult> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace QuotifyBE.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class WeatherForecastController : ControllerBase
|
||||
{
|
||||
private static readonly string[] Summaries = new[]
|
||||
{
|
||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||||
};
|
||||
|
||||
private readonly ILogger<WeatherForecastController> _logger;
|
||||
|
||||
public WeatherForecastController(ILogger<WeatherForecastController> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet(Name = "GetWeatherForecast")]
|
||||
public IEnumerable<WeatherForecast> Get()
|
||||
{
|
||||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
{
|
||||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||
TemperatureC = Random.Shared.Next(-20, 55),
|
||||
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
||||
7
DTOs/CreateQuote.cs
Normal file
7
DTOs/CreateQuote.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
public record class CreateQuoteRequest
|
||||
{
|
||||
public string Text { get; set; }
|
||||
public string Author { get; set; }
|
||||
public List<int> CategoryIds { get; set; }
|
||||
public string? ImageUrl { get; set; }
|
||||
};
|
||||
Reference in New Issue
Block a user