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> GetUser(int id) { var retVal = new ApiResponseDTO() { 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>> GetAllUsers() { var retVal = new ApiResponseDTO>() { 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> UpdateUser([FromBody] UserDTO userDTO) { var retVal = new ApiResponseDTO() { 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> Authenticate([FromBody] UserSimpleDTO userSimpleDTO) { var retVal = new ApiResponseDTO() { 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> GetRole(int id) { var retVal = new ApiResponseDTO() { 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>> GetAllRoles() { var retVal = new ApiResponseDTO>() { 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> UpdateRole([FromBody] RoleDTO roleDTO) { var retVal = new ApiResponseDTO() { 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> GetUserRole(int id) { var retVal = new ApiResponseDTO() { 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>> GetAllUserRoles() { var retVal = new ApiResponseDTO>() { 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> UpdateUserRole([FromBody] UserRoleDTO userRoleDTO) { var retVal = new ApiResponseDTO() { 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("GetRoleCheckListTemplateHeader/{id}")] public async Task> GetRoleCheckListTemplateHeader(int id) { var retVal = new ApiResponseDTO() { IsSuccess = true, }; var roleCheckListTemplateHeaderDTO = await _userService.GetRoleCheckListTemplateHeaderAsync(id); if (roleCheckListTemplateHeaderDTO != null) { if (roleCheckListTemplateHeaderDTO.Id == 0) { retVal.IsSuccess = false; retVal.Errors.Add("No match!"); retVal.Data = roleCheckListTemplateHeaderDTO; } else { retVal.IsSuccess = true; retVal.Data = roleCheckListTemplateHeaderDTO; } } else { retVal.IsSuccess = false; retVal.Errors.Add("No data!"); } return retVal; } [HttpGet("GetAllRoleCheckListTemplateHeaders")] public async Task>> GetAllRoleCheckListTemplateHeaders() { var retVal = new ApiResponseDTO>() { 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; } [HttpPost("UpdateRoleCheckListTemplateHeader")] public async Task> UpdateRoleCheckListTemplateHeader([FromBody] RoleCheckListTemplateHeaderDTO roleCheckListTemplateHeaderDTO) { var retVal = new ApiResponseDTO() { IsSuccess = true }; try { var result = await _userService.UpdateRoleCheckListTemplateHeaderAsync(roleCheckListTemplateHeaderDTO); retVal.Data = result; } catch (Exception ex) { retVal.IsSuccess = false; Log.Error(ex.Message); } return retVal; } } }