using AutoMapper; using GamificationService.Exceptions.Services.ProfileService; using GamificationService.Models.Database; using GamificationService.Models.DTO; using GamificationService.Services.UsersProfile; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; namespace GamificationService.Controllers; [ApiController] [Authorize(Policy = "User")] [Route("api/[controller]")] public class UserProfileController : ControllerBase { private readonly IUserProfileService _userProfilesService; private readonly UserManager _userManager; private readonly ILogger _logger; private readonly IMapper _mapper; public UserProfileController(IUserProfileService userProfilesService, UserManager userManager, ILogger logger, IMapper mapper) { _userProfilesService = userProfilesService; _userManager = userManager; _logger = logger; _mapper = mapper; } /// /// Gets a user profile by its ID. /// /// The username of the user profile's owner. /// An containing the user profile DTO if found, or a 404 Not Found if not found. /// Returns the user profile DTO /// If the user profile is not found [HttpGet("user/{username}")] public async Task GetUserProfileByUsername(string username) { try { var user = (await _userManager.FindByNameAsync(username)); if (user == null) { return NotFound(); } var userProfile = _userProfilesService.GetUserProfileByUserId(user.Id); return Ok(_mapper.Map(userProfile)); } catch (ProfileNotFoundException) { return NotFound(); } } /// /// Gets a user profile by its ID. /// /// The ID of the user profile. /// An containing the user profile DTO if found, or a 404 Not Found if not found. /// Returns the user profile DTO /// If the user profile is not found [HttpGet("{id}")] public IActionResult GetUserProfileById(long id) { try { var userProfile = _userProfilesService.GetUserProfileById(id); return Ok(_mapper.Map(userProfile)); } catch (ProfileNotFoundException) { return NotFound(); } } /// /// Adds a new user profile. /// /// The username of the user. /// The user profile model. /// A containing the created user profile if successful, or a 500 Internal Server Error if not successful. /// Returns the created user profile /// If the user is not found [HttpPost("user/{username}")] [Authorize(Policy = "Admin")] public async Task AddUserProfile(string username, [FromBody] UserProfileCreateDTO model) { var user = (await _userManager.FindByNameAsync(username)); if (user == null) { return NotFound(); } try { var userProfile = await _userProfilesService.AddUserProfile(user.Id, model); return Ok(_mapper.Map(userProfile)); } catch (ProfileNotFoundException) { return NotFound(); } } /// /// Update user profile for the logged in user. /// /// The user profile model. /// A containing the updated user profile if successful, or a 500 Internal Server Error if not successful. /// Returns the updated user profile /// If the user profile is not found [HttpPut] public async Task UpdateUserProfile([FromBody] UserProfileCreateDTO model) { string username = User.Claims.First(c => c.Type == "username").Value; long userId = (await _userManager.FindByNameAsync(username))!.Id; try { bool result = await _userProfilesService.UpdateUserProfileByUserId(userId, model); return Ok(result); } catch (ProfileNotFoundException) { return NotFound(); } } /// /// Updates an existing user profile. /// /// The username of the user. /// The user profile model. /// A containing the updated user profile if successful, or a 500 Internal Server Error if not successful. /// Returns the updated user profile /// If the user profile is not found [HttpPut] [Authorize(Policy = "Admin")] [Route("user/{userId}")] public async Task UpdateUserProfileByUsername(string username, [FromBody] UserProfileCreateDTO model) { var user = (await _userManager.FindByNameAsync(username)); if (user == null) { return NotFound(); } try { bool result = await _userProfilesService.UpdateUserProfileByUserId(user.Id, model); return Ok(result); } catch (ProfileNotFoundException) { return NotFound(); } } /// /// Deletes an existing user profile. /// /// The ID of the user profile to delete. /// A /// Returns true. /// If the user profile is not found [HttpDelete("{id}")] [Authorize(Policy = "Admin")] public IActionResult DeleteUserProfile(long id) { try { _userProfilesService.DeleteUserProfile(id); return Ok(); } catch (ProfileNotFoundException) { return NotFound(); } } }