aboutsummaryrefslogtreecommitdiff
path: root/src/Web/DevHive.Web/Controllers/ProfilePictureController.cs
blob: 9a76e2c8f0d97cfbe2db8e5b4125ca520b78e405 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
{
	/// <summary>
	/// All endpoints for interacting with the profile picture layer
	/// </summary>
	[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;
		}

		/// <summary>
		/// Get the URL of user's profile picture
		/// </summary>
		/// <param name="profilePictureId">The profile picture's Id</param>
		/// <param name="authorization">JWT Bearer Token</param>
		/// <returns>The URL of the profile picture</returns>
		[HttpGet]
		[AllowAnonymous]
		public async Task<IActionResult> ReadProfilePicture(Guid profilePictureId, [FromHeader] string authorization)
		{
			string profilePicURL = await this._profilePictureService.GetProfilePictureById(profilePictureId);
			return new OkObjectResult(new { ProfilePictureURL = profilePicURL} );
		}

		/// <summary>
		/// Alter the profile picture of a user
		/// </summary>
		/// <param name="userId">The user's Id</param>
		/// <param name="profilePictureWebModel">The new profile picture</param>
		/// <param name="authorization">JWT Bearer Token</param>
		/// <returns>The URL of the new profile picture</returns>
		[HttpPut]
		[Authorize(Roles = "User,Admin")]
		public async Task<IActionResult> UpdateProfilePicture(Guid userId, [FromForm] ProfilePictureWebModel profilePictureWebModel, [FromHeader] string authorization)
		{
			if (!this._jwtService.ValidateToken(userId, authorization))
				return new UnauthorizedResult();

			ProfilePictureServiceModel profilePictureServiceModel = this._profilePictureMapper.Map<ProfilePictureServiceModel>(profilePictureWebModel);
			profilePictureServiceModel.UserId = userId;

			string url = await this._profilePictureService.UpdateProfilePicture(profilePictureServiceModel);
			return new OkObjectResult(new { URL = url });
		}
	}
}