majlis / Controllers /EventsController.cs
almakhmri253-stack
security hardening: rate limiting, account lockout, security headers, password strength
8a2f194
Raw History Blame Contribute Delete
1.64 kB
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MajlisManagement.DTOs;
using MajlisManagement.Services.Interfaces;
namespace MajlisManagement.Controllers;
[ApiController]
[Route("api/[controller]")]
public class EventsController : ControllerBase
{
private readonly IEventService _events;
public EventsController(IEventService events) => _events = events;
[HttpGet]
[Authorize]
public async Task<IActionResult> GetAll()
{
var list = await _events.GetAllAsync();
return Ok(list);
}
[HttpPost]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Create([FromBody] CreateEventDto dto)
{
if (string.IsNullOrWhiteSpace(dto.Title))
return BadRequest(new { message = "العنوان مطلوب" });
var userId = int.Parse(User.FindFirstValue(ClaimTypes.NameIdentifier)!);
var result = await _events.CreateAsync(userId, dto);
return CreatedAtAction(nameof(GetAll), new { id = result.Id }, result);
}
[HttpPut("{id:int}")]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Update(int id, [FromBody] UpdateEventDto dto)
{
if (string.IsNullOrWhiteSpace(dto.Title))
return BadRequest(new { message = "العنوان مطلوب" });
var result = await _events.UpdateAsync(id, dto);
return Ok(result);
}
[HttpDelete("{id:int}")]
[Authorize(Roles = "Admin")]
public async Task<IActionResult> Delete(int id)
{
await _events.DeleteAsync(id);
return NoContent();
}
}