mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 01:31:19 +00:00
207 lines
6.4 KiB
C#
207 lines
6.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using MQTTnet;
|
|
using MQTTnet.Client;
|
|
using MQTTnet.Server;
|
|
using MyCore.Interfaces.Models;
|
|
using MyCore.Service.Services;
|
|
using MyCore.Services;
|
|
using MyCore.Interfaces.DTO;
|
|
|
|
|
|
namespace MyCore.Controllers
|
|
{
|
|
[Authorize] // TODO Add ROLES (Roles = "Admin")
|
|
[Route("api/user")]
|
|
[ApiController]
|
|
public class UserController : ControllerBase
|
|
{
|
|
private UserDatabaseService _userService;
|
|
private TokensService _tokenService;
|
|
|
|
public UserController(UserDatabaseService userService, TokensService tokenService)
|
|
{
|
|
_userService = userService;
|
|
_tokenService = tokenService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a list of user
|
|
/// </summary>
|
|
[ProducesResponseType(typeof(List<UserInfo>), 200)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet]
|
|
public ObjectResult Get()
|
|
{
|
|
try
|
|
{
|
|
List<UserInfo> users = _userService.GetAll();
|
|
|
|
return new OkObjectResult(users);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get a specific user
|
|
/// </summary>
|
|
/// <param name="id">id user</param>
|
|
[ProducesResponseType(typeof(UserInfoDetailDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("{id}")]
|
|
public ObjectResult Get(string id)
|
|
{
|
|
try
|
|
{
|
|
UserInfo user = _userService.GetById(id);
|
|
|
|
if (user == null)
|
|
throw new KeyNotFoundException("This user was not found");
|
|
|
|
return new OkObjectResult(user.ToDTO());
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new NotFoundObjectResult(ex.Message) {};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create an user
|
|
/// </summary>
|
|
/// <param name="newUser">New user info</param>
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(UserInfoDetailDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 409)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPost]
|
|
public ObjectResult CreateUser([FromBody] UserInfo newUser)
|
|
{
|
|
try
|
|
{
|
|
if (newUser == null)
|
|
throw new ArgumentNullException("User param is null");
|
|
|
|
newUser.Token = _tokenService.GenerateToken(newUser.Email).ToString();
|
|
newUser.DateCreation = DateTime.Now;
|
|
|
|
List<UserInfo> users = _userService.GetAll();
|
|
|
|
if (users.Select(u => u.Email).Contains(newUser.Email))
|
|
throw new InvalidOperationException("This Email is already used");
|
|
|
|
UserInfo userCreated = _userService.Create(newUser);
|
|
|
|
return new OkObjectResult(userCreated.ToDTO());
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) {};
|
|
}
|
|
catch (InvalidOperationException ex)
|
|
{
|
|
return new ConflictObjectResult(ex.Message) {};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Update an user
|
|
/// </summary>
|
|
/// <param name="updatedUser">User to update</param>
|
|
[ProducesResponseType(typeof(UserInfoDetailDTO), 200)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpPut]
|
|
public ObjectResult UpdateUser([FromBody] UserInfo updatedUser)
|
|
{
|
|
try
|
|
{
|
|
if (updatedUser == null)
|
|
throw new ArgumentNullException("User param is null");
|
|
|
|
UserInfo user = _userService.GetById(updatedUser.Id);
|
|
|
|
if (user == null)
|
|
throw new KeyNotFoundException("User does not exist");
|
|
|
|
UserInfo userModified = _userService.Update(updatedUser.Id, updatedUser);
|
|
|
|
return new OkObjectResult(userModified.ToDTO());
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) {};
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new NotFoundObjectResult(ex.Message) {};
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Delete an user
|
|
/// </summary>
|
|
/// <param name="id">Id of user to delete</param>
|
|
[ProducesResponseType(typeof(string), 202)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpDelete("{id}")]
|
|
public ObjectResult DeleteUser(string id)
|
|
{
|
|
try
|
|
{
|
|
if (id == null)
|
|
throw new ArgumentNullException("User param is null");
|
|
|
|
UserInfo user = _userService.GetById(id);
|
|
|
|
if (user == null)
|
|
throw new KeyNotFoundException("User does not exist");
|
|
|
|
_userService.Remove(id);
|
|
|
|
return new ObjectResult("The user has been deleted") { StatusCode = 202 };
|
|
|
|
}
|
|
catch (ArgumentNullException ex)
|
|
{
|
|
return new BadRequestObjectResult(ex.Message) { };
|
|
}
|
|
catch (KeyNotFoundException ex)
|
|
{
|
|
return new NotFoundObjectResult(ex.Message) { };
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
}
|
|
}
|