Files
WorkFlowCheck/src/WorkFlowCheck.API/Controllers/UserController.cs
T
2025-03-27 11:13:42 +01:00

319 lines
9.3 KiB
C#

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WorkFlowCheck.Common.DTO;
using WorkFlowCheck.BL.Services.Interfaces;
using Serilog;
using WorkFlowCheck.BL.Services;
namespace WorkFlowCheck.API.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class UserController : ControllerBase
{
private IUserService _userService;
public UserController(IUserService userService)
{
_userService = userService;
}
[HttpGet("GetUser/{id}")]
public async Task<ApiResponseDTO<UserDTO>> GetUser(int id)
{
var retVal = new ApiResponseDTO<UserDTO>()
{
IsSuccess = true,
};
var userDTO = await _userService.GetUserAsync(id);
if (userDTO != null)
{
if (userDTO.Id == 0)
{
retVal.IsSuccess = false;
retVal.Errors.Add("No match!");
retVal.Data = userDTO;
}
else
{
retVal.IsSuccess = true;
retVal.Data = userDTO;
}
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpGet("GetAllUsers")]
public async Task<ApiResponseDTO<List<UserDTO>>> GetAllUsers()
{
var retVal = new ApiResponseDTO<List<UserDTO>>()
{
IsSuccess = true,
};
var userDTO = await _userService.GetAllUserAsync();
if (userDTO != null)
{
retVal.IsSuccess = true;
retVal.Data = userDTO;
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpPost("UpdateUser")]
public async Task<ApiResponseDTO<UserDTO>> UpdateUser([FromBody] UserDTO userDTO)
{
var retVal = new ApiResponseDTO<UserDTO>()
{
IsSuccess = true
};
try
{
var result = await _userService.UpdateUserAsync(userDTO);
retVal.Data = result;
}
catch (Exception ex)
{
retVal.IsSuccess = false;
Log.Error(ex.Message);
}
return retVal;
}
[HttpPost("Authenticate")]
public async Task<ApiResponseDTO<UserDTO>> Authenticate([FromBody] UserSimpleDTO userSimpleDTO)
{
var retVal = new ApiResponseDTO<UserDTO>()
{
IsSuccess = true
};
if (string.IsNullOrEmpty(userSimpleDTO.UserName) || string.IsNullOrEmpty(userSimpleDTO.Password))
{
retVal.IsSuccess = false;
retVal.Errors.Add("Nem megfelelo felhasználói név vagy jelszó");
return retVal;
}
try
{
var userDTO_Response = await _userService.Authenticate(userSimpleDTO.UserName, userSimpleDTO.Password);
if (userDTO_Response != null)
{
if (string.IsNullOrEmpty(userDTO_Response.UserName) == false && userDTO_Response.UserName == userSimpleDTO.UserName)
{
retVal.IsSuccess = true;
retVal.Data = userDTO_Response;
Log.Information($"Sikeres azonosítás! {userSimpleDTO.UserName}");
}
else
{
var error = $"Nem megfelelo felhasználó vagy jelszó! {userSimpleDTO.UserName}";
retVal.IsSuccess = false;
retVal.Errors.Add(error);
Log.Information(error);
}
}
else
{
var error = $"Nem megfelelo felhasználó vagy jelszó! {userSimpleDTO.UserName}";
retVal.IsSuccess = false;
retVal.Errors.Add(error);
Log.Information(error);
}
}
catch (Exception ex)
{
Log.Error(ex.Message);
}
return retVal;
}
[HttpGet("GetRole/{id}")]
public async Task<ApiResponseDTO<RoleDTO>> GetRole(int id)
{
var retVal = new ApiResponseDTO<RoleDTO>()
{
IsSuccess = true,
};
var RoleDTO = await _userService.GetRoleAsync(id);
if (RoleDTO != null)
{
if (RoleDTO.Id == 0)
{
retVal.IsSuccess = false;
retVal.Errors.Add("No match!");
retVal.Data = RoleDTO;
}
else
{
retVal.IsSuccess = true;
retVal.Data = RoleDTO;
}
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpGet("GetAllRoles")]
public async Task<ApiResponseDTO<List<RoleDTO>>> GetAllRoles()
{
var retVal = new ApiResponseDTO<List<RoleDTO>>()
{
IsSuccess = true,
};
var RoleDTO = await _userService.GetAllRoleAsync();
if (RoleDTO != null)
{
retVal.IsSuccess = true;
retVal.Data = RoleDTO;
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpPost("UpdateRole")]
public async Task<ApiResponseDTO<RoleDTO>> UpdateRole([FromBody] RoleDTO roleDTO)
{
var retVal = new ApiResponseDTO<RoleDTO>()
{
IsSuccess = true
};
try
{
var result = await _userService.UpdateRoleAsync(roleDTO);
retVal.Data = result;
}
catch (Exception ex)
{
retVal.IsSuccess = false;
Log.Error(ex.Message);
}
return retVal;
}
[HttpGet("GetUserRole/{id}")]
public async Task<ApiResponseDTO<UserRoleDTO>> GetUserRole(int id)
{
var retVal = new ApiResponseDTO<UserRoleDTO>()
{
IsSuccess = true,
};
var RoleDTO = await _userService.GetUserRoleAsync(id);
if (RoleDTO != null)
{
if (RoleDTO.Id == 0)
{
retVal.IsSuccess = false;
retVal.Errors.Add("No match!");
retVal.Data = RoleDTO;
}
else
{
retVal.IsSuccess = true;
retVal.Data = RoleDTO;
}
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpGet("GetAllUserRoles")]
public async Task<ApiResponseDTO<List<UserRoleDTO>>> GetAllUserRoles()
{
var retVal = new ApiResponseDTO<List<UserRoleDTO>>()
{
IsSuccess = true,
};
var RoleDTO = await _userService.GetAllUserRoleAsync();
if (RoleDTO != null)
{
retVal.IsSuccess = true;
retVal.Data = RoleDTO;
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
[HttpPost("UpdateUserRole")]
public async Task<ApiResponseDTO<UserRoleDTO>> UpdateUserRole([FromBody] UserRoleDTO userRoleDTO)
{
var retVal = new ApiResponseDTO<UserRoleDTO>()
{
IsSuccess = true
};
try
{
var result = await _userService.UpdateUserRoleAsync(userRoleDTO);
retVal.Data = result;
}
catch (Exception ex)
{
retVal.IsSuccess = false;
Log.Error(ex.Message);
}
return retVal;
}
[HttpGet("GetAllRoleCheckListTemplateHeaders")]
public async Task<ApiResponseDTO<List<RoleCheckListTemplateHeaderDTO>>> GetAllRoleCheckListTemplateHeaders()
{
var retVal = new ApiResponseDTO<List<RoleCheckListTemplateHeaderDTO>>()
{
IsSuccess = true,
};
var results = await _userService.GetAllRoleCheckListTemplateHeadersAsync();
if (results != null)
{
retVal.IsSuccess = true;
retVal.Data = results;
}
else
{
retVal.IsSuccess = false;
retVal.Errors.Add("No data!");
}
return retVal;
}
}
}