using System; using System.Threading.Tasks; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using DevHive.Services.Interfaces; using DevHive.Services.Models.ProfilePicture; using DevHive.Common.Jwt.Interfaces; using AutoMapper; using DevHive.Web.Models.ProfilePicture; namespace DevHive.Web.Controllers { /// /// All endpoints for interacting with the profile picture layer /// [ApiController] [Route("api/[controller]")] public class ProfilePictureController { private readonly IProfilePictureService _profilePictureService; private readonly IJwtService _jwtService; private readonly IMapper _profilePictureMapper; public ProfilePictureController(IProfilePictureService profilePictureService, IJwtService jwtService, IMapper profilePictureMapper) { this._profilePictureService = profilePictureService; this._jwtService = jwtService; this._profilePictureMapper = profilePictureMapper; } /// /// Get the URL of user's profile picture /// /// The profile picture's Id /// JWT Bearer Token /// The URL of the profile picture [HttpGet] [AllowAnonymous] public async Task ReadProfilePicture(Guid profilePictureId) { string profilePicURL = await this._profilePictureService.GetProfilePictureById(profilePictureId); return new OkObjectResult(new { ProfilePictureURL = profilePicURL} ); } /// /// Alter the profile picture of a user /// /// The user's Id /// The new profile picture /// JWT Bearer Token /// The URL of the new profile picture [HttpPut] [Authorize(Roles = "User,Admin")] public async Task UpdateProfilePicture(Guid userId, [FromForm] ProfilePictureWebModel profilePictureWebModel, [FromHeader] string authorization) { if (!this._jwtService.ValidateToken(userId, authorization)) return new UnauthorizedResult(); ProfilePictureServiceModel profilePictureServiceModel = this._profilePictureMapper.Map(profilePictureWebModel); profilePictureServiceModel.UserId = userId; string url = await this._profilePictureService.UpdateProfilePicture(profilePictureServiceModel); return new OkObjectResult(new { URL = url }); } } }