public IActionResult GetAllUsuarios() { try { Console.WriteLine("[GetAllUsuarios] -> listar todos los usuarios"); List <Usuario> result = usuariosService.ListAllUsuarios(); if (result == null || result.Count() == 0) { Console.WriteLine("[GetAllUsuarios] -> no hay resultados"); RestResponse r = RestUtils.GenerateResponseOkEmpty(); r.Header.Message = RestUtils.RESPONSE_NOTFOUND_MSG; return(NotFound(r)); } Console.WriteLine("[GetAllUsuarios] -> request exitosa"); RestResponse response = RestUtils.GenerateResponseOkWithData(result); return(Ok(response)); } catch (Exception exception) { Console.WriteLine("[GetAllUsuarios] -> " + RestUtils.RESPONSE_INTERNAL_ERROR_MSG); RestResponse response = RestUtils.GenerateResponseErrorWith( new ResponseError( exception.Message, exception.GetType().ToString() ) ); response.Header.Message = RestUtils.RESPONSE_INTERNAL_ERROR_MSG; return(StatusCode( StatusCodes.Status500InternalServerError, response )); } }
public IActionResult GetUsuarioBy(string user) { try { Console.WriteLine("[GetUsuarioBy] -> buscar usuario: " + user); Usuario result = usuariosService.FindUsuarioBy(user); if (result == null) { Console.WriteLine("[GetUsuarioBy] -> no hay resultados"); RestResponse r = RestUtils.GenerateResponseOkEmpty(); r.Header.Message = RestUtils.RESPONSE_NOTFOUND_MSG; return(NotFound(r)); } Console.WriteLine("[GetUsuarioBy] -> request exitosa"); RestResponse response = RestUtils.GenerateResponseOkWithData(result); return(Ok(response)); } catch (Exception exception) { Console.WriteLine("[GetUsuarioBy] -> " + RestUtils.RESPONSE_INTERNAL_ERROR_MSG); RestResponse response = RestUtils.GenerateResponseErrorWith( new ResponseError( exception.Message, exception.GetType().ToString() ) ); response.Header.Message = RestUtils.RESPONSE_INTERNAL_ERROR_MSG; return(StatusCode( StatusCodes.Status500InternalServerError, response )); } }
public IActionResult GetEmpresaBy(int id) { try{ Console.WriteLine("[GetEmpresaBy] -> buscar empresa con id: " + id); Empresa result = empresasService.FindEmpresaBy(id); RestResponse response = RestUtils.GenerateResponseOkEmpty(); if (result == null) { Console.WriteLine("[GetEmpresaBy] -> no hay resultados"); response.Header.Message = RestUtils.RESPONSE_NOTFOUND_MSG; return(NotFound(response)); } Console.WriteLine("[GetEmpresaBy] -> request exitosa"); response.Header.Message = RestUtils.RESPONSE_OK_MSG; response.AddObjectToData(result); return(Ok(response)); }catch (Exception exception) { Console.WriteLine("[GetEmpresaBy] -> " + RestUtils.RESPONSE_INTERNAL_ERROR_MSG); RestResponse response = RestUtils.GenerateResponseErrorWith( new ResponseError( exception.Message, exception.GetType().ToString() ) ); response.Header.Message = RestUtils.RESPONSE_INTERNAL_ERROR_MSG; return(StatusCode( StatusCodes.Status500InternalServerError, response )); } }
public IActionResult GetSectoresByEmpresa(int id) { try{ Console.WriteLine("[GetSectoresByEmpresa] -> listar sectores de la empresa id: " + id); List <Sector> result = sectoresService.ListarSectoresPorEmpresa(id); RestResponse response = RestUtils.GenerateResponseOkEmpty(); if (result == null || result.Count() == 0) { Console.WriteLine("[GetSectoresByEmpresa] -> no hay resultados"); response.Header.Message = RestUtils.RESPONSE_NOTFOUND_MSG; return(NotFound(response)); } Console.WriteLine("[GetSectoresByEmpresa] -> request exitosa"); response.Header.Message = RestUtils.RESPONSE_OK_MSG; response.AddObjectToData(result); return(Ok(response)); }catch (Exception exception) { Console.WriteLine("[GetSectoresByEmpresa] -> " + RestUtils.RESPONSE_INTERNAL_ERROR_MSG); RestResponse response = RestUtils.GenerateResponseErrorWith( new ResponseError( exception.Message, exception.GetType().ToString() ) ); response.Header.Message = RestUtils.RESPONSE_INTERNAL_ERROR_MSG; return(StatusCode( StatusCodes.Status500InternalServerError, response )); } }
public IActionResult InsertEmpresa([FromBody] EmpresaDTO body) { try{ Console.WriteLine("[InsertEmpresa] -> request: " + body.ToString()); // se valida body de request if (body == null || body.Nombre == null) { Console.WriteLine("[InsertEmpresa] -> empresa sin nombre o body nulo"); RestResponse responseErr = RestUtils.GenerateResponseErrorWith( new ResponseError( RestUtils.RESPONSE_BADREQUEST_CODE, "Empresa sin nombre o request body nulo" ) ); responseErr.Header.Message = RestUtils.RESPONSE_BADREQUEST_MSG; return(BadRequest(responseErr)); } // se realiza insersion int result = empresasService.AddNewEmpresa( ModelMapper.Map(body) ); // se valida resultado de operacion if (result == 0) { Console.WriteLine("[InsertEmpresa] -> operacion fallida"); RestResponse responseErr = RestUtils.GenerateResponseErrorWith( new ResponseError( RestUtils.RESPONSE_INTERNAL_ERROR_MSG, "Operacion fallida, no se completo proceso" ) ); responseErr.Header.Message = RestUtils.RESPONSE_ERROR_CODE; return(StatusCode( StatusCodes.Status500InternalServerError, responseErr )); } body.Id = result; Console.WriteLine("[InsertEmpresa] -> operacion exitosa"); return(StatusCode( StatusCodes.Status201Created, RestUtils.GenerateResponseOkWithData(body) )); } catch (Exception exception) { Console.WriteLine("[InsertEmpresa] -> " + RestUtils.RESPONSE_INTERNAL_ERROR_MSG); RestResponse response = RestUtils.GenerateResponseErrorWith( new ResponseError( exception.Message, exception.GetType().ToString() ) ); response.Header.Message = RestUtils.RESPONSE_INTERNAL_ERROR_MSG; return(StatusCode( StatusCodes.Status500InternalServerError, response )); } }
public IActionResult UpdateEmpresa([FromBody] EmpresaDTO body) { try{ Console.WriteLine("[UpdateEmpresa] -> request body: " + body.ToString()); // se validan datos de entrada if (body == null || body.Id == 0) { Console.WriteLine("[UpdateEmpresa] -> falta id de empresa en request body"); RestResponse responseErr = RestUtils.GenerateResponseErrorWith( new ResponseError( RestUtils.RESPONSE_BADREQUEST_CODE, "Falta id de empresa en request body" ) ); responseErr.Header.Message = RestUtils.RESPONSE_BADREQUEST_MSG; return(BadRequest(responseErr)); } // se envia info a modificar EmpresaDTO result = empresasService.ModifyEmpresa( body.Id, ModelMapper.Map(body) ); // se valida resultado if (result == null) { Console.WriteLine("[UpdateEmpresa] -> operacion fallida"); RestResponse responseErr = RestUtils.GenerateResponseErrorWith( new ResponseError( RestUtils.RESPONSE_NOTFOUND_MSG, "Operacion fallida, no se puede modifica empresa , ya que no hay resultados asociados al id" + body.Id ) ); responseErr.Header.Message = RestUtils.RESPONSE_ERROR_CODE; return(NotFound(responseErr)); } Console.WriteLine("[UpdateEmpresa] -> operacion exitosa"); return(Ok(RestUtils.GenerateResponseOkWithData(result))); } catch (Exception exception) { Console.WriteLine("[UpdateEmpresa] -> " + RestUtils.RESPONSE_INTERNAL_ERROR_MSG); RestResponse response = RestUtils.GenerateResponseErrorWith( new ResponseError( exception.Message, exception.GetType().ToString() ) ); response.Header.Message = RestUtils.RESPONSE_INTERNAL_ERROR_MSG; return(StatusCode( StatusCodes.Status500InternalServerError, response )); } }
public IActionResult Login([FromBody] RequestLogin req) { try { // Request validation if (req == null || req.User == null || req.Password == null) { Console.WriteLine("[Login] -> Faltan datos requeridos para login"); RestResponse responseErr = RestUtils.GenerateResponseErrorWith( new ResponseError( RestUtils.RESPONSE_BADREQUEST_CODE, "Faltan datos requeridos para login" ) ); responseErr.Header.Message = RestUtils.RESPONSE_BADREQUEST_MSG; return(BadRequest(responseErr)); } Console.WriteLine("[Login] -> Iniciando login para user: "******"Fallo autenticacion de usuario"; Console.WriteLine("[Login] -> " + response.Header.Message); return(Unauthorized(response)); } else { response = RestUtils.GenerateResponseOkWithData(result); Console.WriteLine("[Login] -> " + response.Header.Message); string tokenString = this.generateToken(result); Console.WriteLine("[Login] -> token generated"); response.Data.Add(new { token = tokenString }); return(Ok(response)); } } catch (Exception exception) { RestResponse response = RestUtils.GenerateResponseErrorWith( new ResponseError( exception.Message, exception.GetType().ToString() ) ); // errores generales Console.WriteLine("[Login] ->" + RestUtils.RESPONSE_INTERNAL_ERROR_MSG); response.Header.Message = RestUtils.RESPONSE_INTERNAL_ERROR_MSG; return(StatusCode( StatusCodes.Status500InternalServerError, response )); } }
public IActionResult GetDocumentsBySector(int?idEmpresa, int?idSector) { try{ // se valida header if (idEmpresa == null || idEmpresa.Value == 0 || idSector == null || idSector.Value == 0) { Console.WriteLine("[GetDocumentsBySector] -> falta 'idEmpresa' o 'idSector' en la request"); RestResponse responseErr = RestUtils.GenerateResponseErrorWith( new ResponseError( RestUtils.RESPONSE_BADREQUEST_CODE, "Falta header 'usuario' en la request" ) ); responseErr.Header.Message = RestUtils.RESPONSE_BADREQUEST_MSG; return(BadRequest(responseErr)); } RestResponse response = RestUtils.GenerateResponseOkEmpty(); // busqueda de documentos Console.WriteLine("[GetDocumentsBySector] -> se van a buscar los documentos de la empresa con id: " + idEmpresa + " y sector " + idSector); List <DocumentoDTO> result = documentosService.FindDocumentosBySector(idEmpresa.Value, idSector.Value); // validacion de resultados if (result == null || result.Count() == 0) { Console.WriteLine("[GetDocumentsBySector] -> no se encontraron resultados"); } else { Console.WriteLine("[GetDocumentsBySector] -> imprimiendo resultados"); foreach (DocumentoDTO d in result) { response.AddObjectToData(d); } } return(Ok(response)); } catch (Exception exception) { RestResponse response = RestUtils.GenerateResponseErrorWith( new ResponseError( exception.Message, exception.GetType().ToString() ) ); // errores generales Console.WriteLine("[GetDocumentsBySector] ->" + RestUtils.RESPONSE_INTERNAL_ERROR_MSG); response.Header.Message = RestUtils.RESPONSE_INTERNAL_ERROR_MSG; return(StatusCode( StatusCodes.Status500InternalServerError, response )); } }
public IActionResult GetDocumento(string docNumber) { try{ // se validan headers if (docNumber == null || docNumber == "") { Console.WriteLine("[GetDocumento] -> Faltan parametros en la request"); RestResponse responseErr = RestUtils.GenerateResponseErrorWith( new ResponseError( RestUtils.RESPONSE_BADREQUEST_CODE, "Faltan parametros en la request" ) ); responseErr.Header.Message = RestUtils.RESPONSE_BADREQUEST_MSG; return(BadRequest(responseErr)); } // se ejecuta busqueda Console.WriteLine("[GetDocumento] -> Buscar documento numero: " + docNumber); List <DocumentoDTO> result = documentosService.FindDocumentoWith(docNumber); RestResponse response = RestUtils.GenerateResponseOkEmpty(); // se validan resultados if (result == null || result.Count() == 0) { Console.WriteLine("[GetDocumento] -> no hay resultados"); response.Header.Message = RestUtils.RESPONSE_NOTFOUND_MSG; return(NotFound(response)); } Console.WriteLine("[GetDocumento] -> request exitosa"); foreach (DocumentoDTO d in result) { response.AddObjectToData(d); } return(Ok(response)); } catch (Exception exception) { RestResponse response = RestUtils.GenerateResponseErrorWith( new ResponseError( exception.Message, exception.GetType().ToString() ) ); // errores generales Console.WriteLine("[GetDocumento] ->" + RestUtils.RESPONSE_INTERNAL_ERROR_MSG); response.Header.Message = RestUtils.RESPONSE_INTERNAL_ERROR_MSG; return(StatusCode( StatusCodes.Status500InternalServerError, response )); } }
public IActionResult DeleteDocumentBy(string docNumber) { try { // se validan headers if (docNumber == null || docNumber == "") { Console.WriteLine("[DeleteDocumentBy] -> Faltan parametros en la request"); RestResponse responseErr = RestUtils.GenerateResponseErrorWith( new ResponseError( RestUtils.RESPONSE_BADREQUEST_CODE, "Faltan parametros en la request" ) ); responseErr.Header.Message = RestUtils.RESPONSE_BADREQUEST_MSG; return(BadRequest(responseErr)); } Console.WriteLine("[DeleteDocumentBy] -> Eliminando documento con numero: " + docNumber); int cantReg = documentosService.DeleteDocumento(docNumber); RestResponse response = RestUtils.GenerateResponseOkEmpty(); response.Header.Message += ". Registros eliminados = " + cantReg; return(Ok(response)); } catch (Exception exception) { RestResponse response = RestUtils.GenerateResponseErrorWith( new ResponseError( exception.Message, exception.GetType().ToString() ) ); // respuesta usuario sin permisos if (typeof(AccessDeniedException).IsInstanceOfType(exception)) { Console.WriteLine("[InsertDocumento] ->" + exception.Message); response.Header.Message = exception.Message; return(StatusCode( StatusCodes.Status403Forbidden, response )); } // errores generales Console.WriteLine("[DeleteDocumentBy] ->" + RestUtils.RESPONSE_INTERNAL_ERROR_MSG); response.Header.Message = RestUtils.RESPONSE_INTERNAL_ERROR_MSG; return(StatusCode( StatusCodes.Status500InternalServerError, response )); } }
public IActionResult DeleteEmpresaWith(int idEmpresa) { try{ // validacion de parametro if (idEmpresa < 1) { Console.WriteLine("[DeleteEmpresaWith] -> idEmpresa invalido"); return(BadRequest( RestUtils.GenerateResponseErrorWith( new ResponseError(RestUtils.RESPONSE_BADREQUEST_CODE, "idEmpresa invalido") ) )); } Console.WriteLine("[DeleteEmpresaWith] -> se va a eliminar empresa con id: " + idEmpresa); EmpresaDTO result = empresasService.DeleteEmpresa(idEmpresa); if (result == null) { Console.WriteLine("[DeleteEmpresaWith] -> no se encontro empresa a eliminar"); return(NotFound( RestUtils.GenerateResponseErrorWith( new ResponseError(RestUtils.RESPONSE_NOTFOUND_MSG, "No se encontro empresa con id " + idEmpresa) ) )); } Console.WriteLine("[DeleteEmpresaWith] -> operacion exitosa"); return(Ok(RestUtils.GenerateResponseOkWithData(result))); }catch (Exception exception) { Console.WriteLine("[UpdateEmpresa] -> " + RestUtils.RESPONSE_INTERNAL_ERROR_MSG); RestResponse response = RestUtils.GenerateResponseErrorWith( new ResponseError( exception.Message, exception.GetType().ToString() ) ); response.Header.Message = RestUtils.RESPONSE_INTERNAL_ERROR_MSG; return(StatusCode( StatusCodes.Status500InternalServerError, response )); } }
public IActionResult InsertNewSectorEmpresa([FromBody] SectorDTO body, int idEmpresa) { try{ Console.WriteLine("[InsertNewSectorEmpresa] -> request: " + body.ToString()); // se valida param y body de request if (body == null || body.Nombre == null || idEmpresa == 0) { Console.WriteLine("[InsertNewSectorEmpresa] -> falta sector o idEmpresa en request"); RestResponse responseErr = RestUtils.GenerateResponseErrorWith( new ResponseError( RestUtils.RESPONSE_BADREQUEST_CODE, "Falta sector o idEmpresa en request" ) ); responseErr.Header.Message = RestUtils.RESPONSE_BADREQUEST_MSG; return(BadRequest(responseErr)); } // se realiza insersion int result = empresasService.AddNewSectorInEmpresa( idEmpresa, ModelMapper.Map(body) ); // se valida resultado de operacion if (result == 0) { Console.WriteLine("[InsertNewSectorEmpresa] -> operacion fallida"); RestResponse responseErr = RestUtils.GenerateResponseErrorWith( new ResponseError( RestUtils.RESPONSE_INTERNAL_ERROR_MSG, "Operacion fallida, no se completo proceso" ) ); responseErr.Header.Message = RestUtils.RESPONSE_ERROR_CODE; return(StatusCode( StatusCodes.Status500InternalServerError, responseErr )); } else if (result == -99) { Console.WriteLine("[InsertNewSectorEmpresa] -> operacion fallida"); RestResponse responseErr = RestUtils.GenerateResponseErrorWith( new ResponseError( RestUtils.RESPONSE_NOTFOUND_MSG, "Operacion fallida, no se pudo insertar sector porque no hay empresa asociada al id" + idEmpresa ) ); responseErr.Header.Message = RestUtils.RESPONSE_ERROR_CODE; return(NotFound(responseErr)); } body.EmpresaId = idEmpresa; body.SectorId = result; Console.WriteLine("[InsertNewSectorEmpresa] -> operacion exitosa"); return(StatusCode( StatusCodes.Status201Created, RestUtils.GenerateResponseOkWithData(body) )); } catch (Exception exception) { Console.WriteLine("[InsertNewSectorEmpresa] -> " + RestUtils.RESPONSE_INTERNAL_ERROR_MSG); RestResponse response = RestUtils.GenerateResponseErrorWith( new ResponseError( exception.Message, exception.GetType().ToString() ) ); response.Header.Message = RestUtils.RESPONSE_INTERNAL_ERROR_MSG; return(StatusCode( StatusCodes.Status500InternalServerError, response )); } }
public IActionResult InsertDocumento( int?idEmpresa, int?idSector, [FromBody] DocumentoDTO body ) { try{ // se validan headers if (idEmpresa == null || idEmpresa.Value == 0 || idSector == null || idSector.Value == 0) { Console.WriteLine("[InsertDocumento] -> Falta 'idEmpresa' o 'idSector' en la request"); RestResponse responseErr = RestUtils.GenerateResponseErrorWith( new ResponseError( RestUtils.RESPONSE_BADREQUEST_CODE, "Falta 'idEmpresa' o 'idSector' en la request" ) ); responseErr.Header.Message = RestUtils.RESPONSE_BADREQUEST_MSG; return(BadRequest(responseErr)); } Console.WriteLine("[InsertDocumento] -> request: " + body.ToString()); // se valida body InputValidation(body); body = documentosService.AddNewDocument(body, idEmpresa.Value, idSector.Value); // se validan resultados if (body == null) { RestResponse responseErr = RestUtils.GenerateResponseErrorWith( new ResponseError( RestUtils.RESPONSE_INTERNAL_ERROR_MSG, "Operacion fallida, no se completo proceso" ) ); responseErr.Header.Message = RestUtils.RESPONSE_ERROR_CODE; return(StatusCode( StatusCodes.Status500InternalServerError, responseErr )); } RestResponse response = RestUtils.GenerateResponseOkEmpty(); response.AddObjectToData(body); return(Ok(response)); } catch (Exception exception) { RestResponse response = RestUtils.GenerateResponseErrorWith( new ResponseError( exception.Message, exception.GetType().ToString() ) ); if (typeof(WrongInputException).IsInstanceOfType(exception)) { Console.WriteLine("[InsertDocumento] ->" + exception.Message); response.Header.Message = exception.Message; return(StatusCode( StatusCodes.Status400BadRequest, response )); } // errores generales Console.WriteLine("[InsertDocumento] ->" + RestUtils.RESPONSE_INTERNAL_ERROR_MSG); response.Header.Message = RestUtils.RESPONSE_INTERNAL_ERROR_MSG; return(StatusCode( StatusCodes.Status500InternalServerError, response )); } }