Example #1
0
 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
                    ));
     }
 }
Example #2
0
 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
                    ));
     }
 }
Example #3
0
 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
                    ));
     }
 }
Example #4
0
 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 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 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
                    ));
     }
 }