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