mirror of
https://github.com/QuotifyTeam/QuotifyBE.git
synced 2025-12-16 22:10:05 +01:00
114 lines
3.5 KiB
C#
114 lines
3.5 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using QuotifyBE.Data;
|
|
using QuotifyBE.Entities;
|
|
using QuotifyBE.DTOs;
|
|
using System.Threading.Tasks;
|
|
using QuotifyBE.Mapping;
|
|
using Microsoft.AspNetCore.Cors;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace QuotifyBE.Controllers;
|
|
|
|
|
|
[ApiController]
|
|
[EnableCors]
|
|
[Route("api/v1/categories")]
|
|
[Produces("application/json")]
|
|
public class CategoryController : ControllerBase
|
|
{
|
|
|
|
private readonly ApplicationDbContext _db;
|
|
private readonly GeneralUseHelpers guhf;
|
|
|
|
public CategoryController(ApplicationDbContext db, GeneralUseHelpers GUHF)
|
|
{
|
|
_db = db;
|
|
guhf = GUHF;
|
|
}
|
|
|
|
// GET /api/v1/categories
|
|
/// <summary>
|
|
/// Get every category
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Can (and will) return an empty list if no categories are found in DB. <br/>
|
|
/// Has CORS set.
|
|
/// </remarks>
|
|
/// <response code="200">Returned on valid request</response>
|
|
// /// <response code="404">Returned when there are no categories to list</response>
|
|
[HttpGet]
|
|
[EnableCors]
|
|
[ProducesResponseType(typeof(CategoryShortDTO), 200)]
|
|
// [ProducesResponseType(typeof(ErrorDTO), 404)]
|
|
public async Task<IActionResult> GetQuotePage()
|
|
{
|
|
// The following seems to be a bad idea, so I leave it as is. ~eee4
|
|
//
|
|
// int totalCategories = await _db.Categories.CountAsync();
|
|
//
|
|
// if (totalCategories <= 0)
|
|
// {
|
|
// return NotFound(new ErrorDTO { Status = "error", Error_msg = "No categories to list" });
|
|
// }
|
|
|
|
// Get all the categories
|
|
List<Category> categories = await _db.Categories
|
|
.ToListAsync();
|
|
|
|
// Convert them to a list of DTO
|
|
List<CategoryShortDTO> result = categories
|
|
.Select(c => c.ToCategoryShortDTO())
|
|
.ToList();
|
|
|
|
// Return to user
|
|
return Ok(result);
|
|
|
|
}
|
|
|
|
// POST /api/v1/categories
|
|
/// <summary>
|
|
/// [AUTHED] Create a new category
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Allows authorized users to create categories. <br/>
|
|
/// <b>Important!</b>
|
|
/// <br/><br/>
|
|
/// Category names are case insensitive. <br/>
|
|
/// Has CORS set.
|
|
/// </remarks>
|
|
/// <response code="200">Returned on valid request</response>
|
|
/// <response code="406">Returned when such category already exists (case insensitive)</response>
|
|
[HttpPost]
|
|
[Authorize]
|
|
[EnableCors]
|
|
[ProducesResponseType(typeof(CategoryShortDTO), 200)]
|
|
[ProducesResponseType(typeof(ErrorDTO), 406)]
|
|
public async Task<IActionResult> PostNewCategory([FromBody] NewCategoryDTO formCategory)
|
|
{
|
|
// Check if such category doesn't already exist
|
|
Category? cat = await _db.Categories.FirstOrDefaultAsync(c => c.Name.ToLower() == formCategory.Name.ToLower());
|
|
if (cat != null)
|
|
{
|
|
return StatusCode(406, new ErrorDTO { Status = "error", Error_msg = "This category already exists" });
|
|
}
|
|
|
|
// Create new category
|
|
cat = new Category
|
|
{
|
|
Name = formCategory.Name,
|
|
Description = formCategory.Description,
|
|
CreatedAt = DateTime.UtcNow
|
|
};
|
|
|
|
// Add to DB
|
|
await _db.Categories.AddAsync(cat);
|
|
await _db.SaveChangesAsync();
|
|
|
|
// And send back to the user as DTO
|
|
return Ok(cat.ToCategoryShortDTO());
|
|
|
|
}
|
|
|
|
}
|