mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
137 lines
3.6 KiB
C#
137 lines
3.6 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.Models;
|
|
using MyCore.Services;
|
|
|
|
namespace MyCore.Controllers
|
|
{
|
|
[Authorize(Roles = "Admin")]
|
|
[Route("api/user")]
|
|
[ApiController]
|
|
public class UserController : ControllerBase
|
|
{
|
|
private UserDatabaseService _userService;
|
|
private TokenService _tokenService;
|
|
|
|
public UserController(UserDatabaseService userService, TokenService tokenService)
|
|
{
|
|
_userService = userService;
|
|
_tokenService = tokenService;
|
|
}
|
|
|
|
// GET api/user
|
|
/// <summary>
|
|
/// Get a list of user
|
|
/// </summary>
|
|
[HttpGet]
|
|
public ActionResult<IEnumerable<UserInfo>> Get()
|
|
{
|
|
//return new string[] { "value1", "value2" };
|
|
//return _userService.GetUsers();
|
|
return null;
|
|
}
|
|
|
|
|
|
// GET api/user/5
|
|
/// <summary>
|
|
/// Get a specific user
|
|
/// </summary>
|
|
/// <param name="id">id user</param>
|
|
[HttpGet("{id}")]
|
|
public ActionResult<UserInfo> Get(string id)
|
|
{
|
|
if (id != null)
|
|
{
|
|
List<UserInfo> users = _userService.GetAll();
|
|
|
|
if (!users.Select(u => u.Id).Contains(id))
|
|
{
|
|
return Conflict("This user was not found");
|
|
}
|
|
|
|
return _userService.GetById(id);
|
|
}
|
|
return StatusCode(500);
|
|
}
|
|
|
|
// POST: User/Create
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[AllowAnonymous]
|
|
[HttpPost]
|
|
public ActionResult<UserInfo> CreateUser([FromBody] UserInfo newUser)
|
|
{
|
|
if (newUser != 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))
|
|
{
|
|
return Conflict("This Email is already used");
|
|
}
|
|
|
|
UserInfo userCreated = _userService.Create(newUser);
|
|
|
|
return userCreated;
|
|
}
|
|
return StatusCode(500);
|
|
}
|
|
|
|
|
|
// PUT: User/Update
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[HttpPut]
|
|
public ActionResult<UserInfo> UpdateUser([FromBody] UserInfo updatedUser)
|
|
{
|
|
if (updatedUser != null)
|
|
{
|
|
List<UserInfo> users = _userService.GetAll();
|
|
|
|
if (!users.Select(u => u.Email).Contains(updatedUser.Email))
|
|
{
|
|
return NotFound("The user was not found");
|
|
}
|
|
|
|
UserInfo userModified = _userService.Update(updatedUser.Id, updatedUser);
|
|
|
|
return userModified;
|
|
}
|
|
return StatusCode(500);
|
|
}
|
|
|
|
|
|
// DELETE: User/Delete
|
|
[HttpDelete("{id}")]
|
|
public ActionResult DeleteUser(string id)
|
|
{
|
|
if (id != null)
|
|
{
|
|
List<UserInfo> users = _userService.GetAll();
|
|
|
|
if (!users.Select(u => u.Id).Contains(id))
|
|
{
|
|
return NotFound("The user was not found");
|
|
}
|
|
|
|
_userService.Remove(id);
|
|
|
|
return Accepted("The user has been deleted");
|
|
}
|
|
return StatusCode(500);
|
|
}
|
|
}
|
|
}
|