ENEI2019-Public/api/Controllers/UsersController.cs

158 lines
4.0 KiB
C#
Raw Normal View History

2019-03-17 16:12:39 +00:00
using System;
2018-12-12 15:17:08 +00:00
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using api.Data;
using api.Dtos;
using api.Models;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
namespace api.Controllers
{
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class UsersController : ControllerBase
{
private readonly IUsersRepository _repo;
private readonly IMapper _mapper;
private readonly RoleManager<Role> _roleManager;
private readonly UserManager<User> _userManager;
2019-03-17 16:12:39 +00:00
public UsersController(IUsersRepository repo, IMapper mapper, RoleManager<Role> roleManager, UserManager<User> UserManager)
2018-12-12 15:17:08 +00:00
{
_mapper = mapper;
_roleManager = roleManager;
_userManager = UserManager;
_repo = repo;
}
2019-03-24 18:57:09 +00:00
[AllowAnonymous]
[HttpGet("getProfileImage/{QRcode}")]
public async Task<IActionResult> getProfileImage(string QRcode)
{
var user = _repo.getProfileImageAsync(QRcode);
profileImageToReturn a = new profileImageToReturn();
if (user.Result != null)
{
a.profileBase64 = user.Result;
return Ok(a);
}
return NotFound();
}
2019-03-24 13:41:40 +00:00
[HttpPost("changeProfileImage")]
public async Task<IActionResult> changeProfileImage(profileImage i)
{
User a = new User();
2019-03-24 18:57:09 +00:00
var cenas = await _repo.changeProfileImage(i);
2019-03-17 16:12:39 +00:00
2019-03-24 13:41:40 +00:00
return Ok(cenas);
}
2018-12-12 15:17:08 +00:00
//
[HttpGet("{id}")]
public async Task<IActionResult> GetUser(int id)
{
2019-03-17 16:12:39 +00:00
2018-12-12 15:17:08 +00:00
var user = await _repo.GetUser(id);
2019-03-17 16:12:39 +00:00
2018-12-12 15:17:08 +00:00
var userToReturn = _mapper.Map<UserForDetailedDto>(user);
2019-03-17 16:12:39 +00:00
2018-12-12 15:17:08 +00:00
return Ok(userToReturn);
2019-03-17 16:12:39 +00:00
2018-12-12 15:17:08 +00:00
}
2019-03-17 16:12:39 +00:00
[Authorize(Policy = "RequireAdminRole")]
2018-12-12 15:17:08 +00:00
[HttpGet]
public async Task<IActionResult> GetUsers()
{
var users = await _repo.GetUsers();
var usersToReturn = _mapper.Map<IEnumerable<UserForListDto>>(users);
2019-03-17 16:12:39 +00:00
2018-12-12 15:17:08 +00:00
return Ok(usersToReturn);
}
2019-03-17 16:12:39 +00:00
2019-03-24 13:41:40 +00:00
2019-03-17 16:12:39 +00:00
[Authorize(Policy = "RequireAdminRole")]
[HttpPost("updateAll")]
public async Task<IActionResult> UpdateUsers(updateUsersDTO[] req)
{
try
{
foreach (var user in req)
{
2019-03-24 13:41:40 +00:00
2019-03-17 16:12:39 +00:00
}
return Ok(req);
}
catch (Exception e)
{
}
return Ok();
}
2018-12-12 15:17:08 +00:00
[HttpPut("{id}")]
2019-03-17 16:12:39 +00:00
public async Task<IActionResult> UpdateUser(int id, UserForUpdateDto userForUpdate)
{
2018-12-12 15:17:08 +00:00
if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
return Unauthorized(); //garante que é o próprio pode aceder à sua informação
var userFromRepo = await _repo.GetUser(id);
_mapper.Map(userForUpdate, userFromRepo);
2019-03-17 16:12:39 +00:00
if (await _repo.SaveAll())
2018-12-12 15:17:08 +00:00
return NoContent();
2019-03-17 16:12:39 +00:00
2018-12-12 15:17:08 +00:00
throw new System.Exception($"updating user {id} failed on save");
}
2019-03-17 16:12:39 +00:00
2018-12-12 15:17:08 +00:00
[HttpPut("update/{id}")]
2019-03-17 16:12:39 +00:00
[Authorize(Policy = "RequireAdminRole")]
public async Task<IActionResult> UpdateUserById(int id, UserForUpdateDto userForUpdate)
{
2018-12-12 15:17:08 +00:00
var userFromRepo = await _repo.GetUser(id);
_mapper.Map(userForUpdate, userFromRepo);
2019-03-17 16:12:39 +00:00
if (await _repo.SaveAll())
2018-12-12 15:17:08 +00:00
return NoContent();
throw new System.Exception($"updating user {id} failed on save");
}
2019-03-17 16:12:39 +00:00
2018-12-12 15:17:08 +00:00
//adiciona uma nova role à base de dados
2019-03-17 16:12:39 +00:00
[Authorize(Policy = "RequireAdminRole")]
2018-12-12 15:17:08 +00:00
[HttpPost("addRole")]
2019-03-17 16:12:39 +00:00
public IActionResult addRole(Role role)
{
2018-12-12 15:17:08 +00:00
_roleManager.CreateAsync(role).Wait();
2019-03-17 16:12:39 +00:00
2018-12-12 15:17:08 +00:00
return Ok();
}
}
}