Files
MotoresArgentinosV2/Backend/MotoresArgentinosV2.API/Controllers/ProfileController.cs

119 lines
3.8 KiB
C#

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using MotoresArgentinosV2.Core.DTOs;
using MotoresArgentinosV2.Core.Entities;
using MotoresArgentinosV2.Core.Interfaces;
using MotoresArgentinosV2.Infrastructure.Data;
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
namespace MotoresArgentinosV2.API.Controllers;
[Authorize]
[ApiController]
[Route("api/[controller]")]
public class ProfileController : ControllerBase
{
private readonly MotoresV2DbContext _context;
private readonly INotificationPreferenceService _notifPrefService;
public ProfileController(
MotoresV2DbContext context,
INotificationPreferenceService notifPrefService)
{
_context = context;
_notifPrefService = notifPrefService;
}
[HttpGet]
public async Task<IActionResult> GetProfile()
{
var userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "0");
var user = await _context.Users
.Where(u => u.UserID == userId)
.Select(u => new
{
u.UserID,
u.UserName,
u.Email,
u.FirstName,
u.LastName,
u.PhoneNumber,
u.UserType,
u.CreatedAt,
u.IsEmailVerified
})
.FirstOrDefaultAsync();
if (user == null) return NotFound();
return Ok(user);
}
[HttpPut]
public async Task<IActionResult> UpdateProfile([FromBody] ProfileUpdateDto dto)
{
var userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "0");
var user = await _context.Users.FindAsync(userId);
if (user == null) return NotFound();
user.FirstName = dto.FirstName;
user.LastName = dto.LastName;
user.PhoneNumber = dto.PhoneNumber;
await _context.SaveChangesAsync();
// Audit Log
_context.AuditLogs.Add(new AuditLog
{
Action = "PROFILE_UPDATED",
Entity = "User",
EntityID = userId,
UserID = userId,
Details = "Usuario actualizó su perfil personal."
});
await _context.SaveChangesAsync();
return Ok(new { message = "Perfil actualizado con éxito." });
}
// ─── Preferencias de Notificación ────────────────────────────────────────
/// <summary>
/// Obtiene las preferencias de notificación del usuario autenticado.
/// GET api/profile/notification-preferences
/// </summary>
[HttpGet("notification-preferences")]
public async Task<IActionResult> GetNotificationPreferences()
{
var userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "0");
var prefs = await _notifPrefService.GetPreferencesAsync(userId);
return Ok(prefs);
}
/// <summary>
/// Actualiza las preferencias de notificación del usuario autenticado.
/// PUT api/profile/notification-preferences
/// </summary>
[HttpPut("notification-preferences")]
public async Task<IActionResult> UpdateNotificationPreferences(
[FromBody] UpdateNotificationPreferencesDto dto)
{
var userId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? "0");
await _notifPrefService.UpdatePreferencesAsync(userId, dto);
// Registramos en auditoría
_context.AuditLogs.Add(new AuditLog
{
Action = "NOTIFICATION_PREFS_UPDATED",
Entity = "User",
EntityID = userId,
UserID = userId,
Details = "Usuario actualizó sus preferencias de notificación."
});
await _context.SaveChangesAsync();
return Ok(new { message = "Preferencias actualizadas con éxito." });
}
}