Exemplo n.º 1
0
        public BaseResult <AuthorizationToken> Authenticate(UserLogin userLogin)
        {
            try
            {
                var user = _userSysRepository.GetByLoginAndPassword(userLogin);

                if (user == null)
                {
                    throw new UnauthorizedException("The email and/or password entered is invalid. Please try again.");
                }

                var token        = GenerateToken(user);
                var refreshToken = GenerateRefreshToken();

                var authorizationToken = new AuthorizationToken {
                    Token        = token,
                    RefreshToken = refreshToken.Token,
                    User         = user
                };

                return(BaseResult <AuthorizationToken> .OK(authorizationToken));
            }
            catch (Exception e)
            {
                return(BaseResult <AuthorizationToken> .NotOK(e.Message));
            }
        }
Exemplo n.º 2
0
        public BaseResult <T> Request <T>(RequestAPI requestAPI) where T : class
        {
            try
            {
                string url = $"{_customerAPIOptions.EndPointUrl}/{requestAPI.Route}";

                HttpResponseMessage response;

                var httpClient = RequestHeader();

                if (requestAPI.MethodType == RequestMethodTypeEnum.Get)
                {
                    if (requestAPI.Body != null)
                    {
                        var queryParams = _fieldService.GetQueryString(requestAPI.Body);

                        if (!String.IsNullOrEmpty(queryParams))
                        {
                            url = $"{url}?{queryParams}";
                        }
                    }

                    response = httpClient.GetAsync(url).Result;
                }
                else
                {
                    response = httpClient.PostAsync(
                        url,
                        new StringContent(
                            JsonConvert.SerializeObject(requestAPI.Body),
                            Encoding.UTF8,
                            requestAPI.ContentType)).Result;
                }

                string content = response.Content.ReadAsStringAsync().Result;

                if (response.StatusCode == HttpStatusCode.OK)
                {
                    ResponseAPI <T> responseAPI = JsonConvert.DeserializeObject <ResponseAPI <T> >(content);

                    if (responseAPI.Success && responseAPI.Data != null)
                    {
                        return(BaseResult <T> .OK(responseAPI.Data));
                    }
                }
                else if (response.StatusCode == HttpStatusCode.Unauthorized)
                {
                    return(BaseResult <T> .NotOK("Unauthorized. Please logg in.", (int)response.StatusCode));
                }

                return(BaseResult <T> .NotOK("An error occurred while communicating with the server. Please try again."));
            }
            catch (Exception e)
            {
                return(BaseResult <T> .NotOK(e.Message));
            }
        }
Exemplo n.º 3
0
        public BaseResult <ClassificationModel.Classification> GetById(int id)
        {
            try
            {
                var classification = _classificationRepository.GetById(id);

                return(BaseResult <ClassificationModel.Classification> .OK(classification));
            }
            catch (Exception e)
            {
                return(BaseResult <ClassificationModel.Classification> .NotOK(e.Message));
            }
        }
Exemplo n.º 4
0
        public BaseResult <List <CityModel.City> > GetWithRegion()
        {
            try
            {
                var cities = _cityRepository.GetWithRegion();

                return(BaseResult <List <CityModel.City> > .OK(cities));
            }
            catch (Exception e)
            {
                return(BaseResult <List <CityModel.City> > .NotOK(e.Message));
            }
        }
Exemplo n.º 5
0
        public BaseResult <UserRole> GetById(int id)
        {
            try
            {
                var userRole = _userRoleRepository.GetById(id);

                return(BaseResult <UserRole> .OK(userRole));
            }
            catch (Exception e)
            {
                return(BaseResult <UserRole> .NotOK(e.Message));
            }
        }
Exemplo n.º 6
0
        public BaseResult <List <UserRole> > Get()
        {
            try
            {
                var userRoles = _userRoleRepository.Get();

                return(BaseResult <List <UserRole> > .OK(userRoles));
            }
            catch (Exception e)
            {
                return(BaseResult <List <UserRole> > .NotOK(e.Message));
            }
        }
Exemplo n.º 7
0
        public BaseResult <CityModel.City> GetByIdWithRegion(int id)
        {
            try
            {
                var city = _cityRepository.GetByIdWithRegion(id);

                return(BaseResult <CityModel.City> .OK(city));
            }
            catch (Exception e)
            {
                return(BaseResult <CityModel.City> .NotOK(e.Message));
            }
        }
Exemplo n.º 8
0
        public BaseResult <List <CustomerModel.Customer> > Get()
        {
            try
            {
                var customers = _customerRepository.Get();

                return(BaseResult <List <CustomerModel.Customer> > .OK(customers));
            }
            catch (Exception e)
            {
                return(BaseResult <List <CustomerModel.Customer> > .NotOK(e.Message));
            }
        }
Exemplo n.º 9
0
        public BaseResult <List <RegionModel.Region> > Get()
        {
            try
            {
                var regions = _regionRepository.Get();

                return(BaseResult <List <RegionModel.Region> > .OK(regions));
            }
            catch (Exception e)
            {
                return(BaseResult <List <RegionModel.Region> > .NotOK(e.Message));
            }
        }
Exemplo n.º 10
0
        public BaseResult <RegionModel.Region> GetById(int id)
        {
            try
            {
                var region = _regionRepository.GetById(id);

                return(BaseResult <RegionModel.Region> .OK(region));
            }
            catch (Exception e)
            {
                return(BaseResult <RegionModel.Region> .NotOK(e.Message));
            }
        }
Exemplo n.º 11
0
        public BaseResult <CustomerModel.Customer> GetByIdWithAllRelations(int id)
        {
            try
            {
                var customer = _customerRepository.GetByIdWithAllRelations(id);

                return(BaseResult <CustomerModel.Customer> .OK(customer));
            }
            catch (Exception e)
            {
                return(BaseResult <CustomerModel.Customer> .NotOK(e.Message));
            }
        }
Exemplo n.º 12
0
        public ActionResult <BaseResult <ClassificationViewModel> > Get(int id)
        {
            var result = _classificationService.GetById(id);

            if (result.Success)
            {
                var resultMap = _mapper.Map <ClassificationViewModel>(result.Data);

                return(BaseResult <ClassificationViewModel> .OK(resultMap));
            }

            return(BaseResult <ClassificationViewModel> .NotOK(result.Messages));
        }
Exemplo n.º 13
0
        public ActionResult <BaseResult <CustomerViewModel> > Get(int id)
        {
            var result = _customerService.GetByIdWithAllRelations(id);

            if (result.Success)
            {
                var resultMap = _mapper.Map <CustomerViewModel>(result.Data);

                return(BaseResult <CustomerViewModel> .OK(resultMap));
            }

            return(BaseResult <CustomerViewModel> .NotOK(result.Messages));
        }
Exemplo n.º 14
0
        public BaseResult <List <CustomerModel.Customer> > GetWithAllRelations(CustomerModel.CustomerFilter filterModel)
        {
            try
            {
                var customers = _customerRepository.GetWithAllRelations(filterModel);

                return(BaseResult <List <CustomerModel.Customer> > .OK(customers));
            }
            catch (Exception e)
            {
                return(BaseResult <List <CustomerModel.Customer> > .NotOK(e.Message));
            }
        }
Exemplo n.º 15
0
        public BaseResult <List <ClassificationModel.Classification> > Get()
        {
            try
            {
                var classifications = _classificationRepository.Get();

                return(BaseResult <List <ClassificationModel.Classification> > .OK(classifications));
            }
            catch (Exception e)
            {
                return(BaseResult <List <ClassificationModel.Classification> > .NotOK(e.Message));
            }
        }
Exemplo n.º 16
0
        public BaseResult <UserSys> GetById(int id)
        {
            try
            {
                var user = _sellerRepository.GetById(id);

                return(BaseResult <UserSys> .OK(user));
            }
            catch (Exception e)
            {
                return(BaseResult <UserSys> .NotOK(e.Message));
            }
        }
Exemplo n.º 17
0
        public ActionResult <BaseResult <List <CityViewModel> > > Get()
        {
            var result = _cityService.Get();

            if (result.Success)
            {
                var resultMap = _mapper.Map <List <CityViewModel> >(result.Data);

                return(BaseResult <List <CityViewModel> > .OK(resultMap));
            }

            return(BaseResult <List <CityViewModel> > .NotOK(result.Messages));
        }
Exemplo n.º 18
0
        private void BadRequest(ResourceExecutedContext context)
        {
            if (context.Result is BadRequestObjectResult)
            {
                var result  = (BadRequestObjectResult)context.Result;
                var details = (ValidationProblemDetails)result.Value;

                var ex = context.Exception as BadRequestException;
                context.Exception = null;

                context.Result = new JsonResult(BaseResult.NotOK(details.Errors.First().Value.First() + ";" + details.Errors.First().Value.First()));
            }
        }
Exemplo n.º 19
0
        public BaseResult <GenderModel.Gender> GetById(int id)
        {
            try
            {
                var gender = _genderRepository.GetById(id);

                return(BaseResult <GenderModel.Gender> .OK(gender));
            }
            catch (Exception e)
            {
                return(BaseResult <GenderModel.Gender> .NotOK(e.Message));
            }
        }
Exemplo n.º 20
0
        public BaseResult <List <GenderModel.Gender> > Get()
        {
            try
            {
                var genders = _genderRepository.Get();

                return(BaseResult <List <GenderModel.Gender> > .OK(genders));
            }
            catch (Exception e)
            {
                return(BaseResult <List <GenderModel.Gender> > .NotOK(e.Message));
            }
        }
Exemplo n.º 21
0
        public BaseResult <CityModel> GetById(int?id)
        {
            try
            {
                var requestAPI = new RequestAPI
                {
                    ContentType = _contentType,
                    Route       = $"{_route}/{id}",
                    MethodType  = RequestMethodTypeEnum.Get
                };

                return(_restAPIService.Request <CityModel>(requestAPI));
            }
            catch (Exception e)
            {
                return(BaseResult <CityModel> .NotOK(e.Message));
            }
        }
Exemplo n.º 22
0
        public BaseResult <IEnumerable <CityModel> > Get()
        {
            try
            {
                var requestAPI = new RequestAPI
                {
                    ContentType = _contentType,
                    Route       = _route,
                    MethodType  = RequestMethodTypeEnum.Get
                };

                return(_restAPIService.Request <IEnumerable <CityModel> >(requestAPI));
            }
            catch (Exception e)
            {
                return(BaseResult <IEnumerable <CityModel> > .NotOK(e.Message));
            }
        }
Exemplo n.º 23
0
        public BaseResult <IEnumerable <CustomerModel.Customer> > Get(CustomerModel.CustomerFilter customerInputModel)
        {
            try
            {
                var requestAPI = new RequestAPI
                {
                    ContentType = _contentType,
                    Route       = _route,
                    MethodType  = RequestMethodTypeEnum.Get,
                    Body        = customerInputModel
                };

                return(_restAPIService.Request <IEnumerable <CustomerModel.Customer> >(requestAPI));
            }
            catch (Exception e)
            {
                return(BaseResult <IEnumerable <CustomerModel.Customer> > .NotOK(e.Message));
            }
        }
Exemplo n.º 24
0
        public ActionResult <BaseResult <List <CustomerViewModel> > > Get([FromQuery] CustomerInputModel inputModel)
        {
            CustomerFilter customer = null;

            if (inputModel != null)
            {
                customer = _mapper.Map <CustomerFilter>(inputModel);
            }

            var result = _customerService.GetWithAllRelations(customer);

            if (result.Success)
            {
                var resultMap = _mapper.Map <List <CustomerViewModel> >(result.Data);

                return(BaseResult <List <CustomerViewModel> > .OK(resultMap));
            }

            return(BaseResult <List <CustomerViewModel> > .NotOK(result.Messages));
        }
Exemplo n.º 25
0
        public override void OnException(ExceptionContext context)
        {
            if (context.Exception is NotFoundException)
            {
                // handle explicit 'known' API errors
                var ex = context.Exception as NotFoundException;
                context.Exception = null;

                context.Result = new JsonResult(BaseResult.NotOK(ex.Message));
                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
            }
            else if (context.Exception is BadRequestException)
            {
                var ex = context.Exception as BadRequestException;
                context.Exception = null;

                context.Result = new JsonResult(BaseResult.NotOK(ex.Message));
                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.BadRequest;
            }
            else if (context.Exception is UnauthorizedException)
            {
                var ex = context.Exception as UnauthorizedException;
                context.Exception = null;

                context.Result = new JsonResult(BaseResult.NotOK(context.Exception.Message));
                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized;
            }
            else if (context.Exception is ForbiddenException)
            {
                context.Result = new JsonResult(BaseResult.NotOK(context.Exception.Message));
                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.Forbidden;
            }
            else if (context.Exception is NotImplementedException)
            {
                context.Result = new JsonResult(BaseResult.NotOK(context.Exception.Message));
                context.HttpContext.Response.StatusCode = (int)HttpStatusCode.NotImplemented;
            }

            base.OnException(context);
        }
Exemplo n.º 26
0
        public BaseResult <AuthorizationResponse> RequestUserSession(AuthorizationRequest login)
        {
            var requestAPI = new RequestAPI
            {
                Body        = login,
                ContentType = _contentType,
                Route       = _route,
                MethodType  = RequestMethodTypeEnum.Post
            };

            var responseAPI = _restAPIService.Request <AuthorizationResponse>(requestAPI);

            if (responseAPI.Success && responseAPI.Data != null)
            {
                _session.HttpContext.Session.SetUserSession(responseAPI.Data);
            }
            else if (responseAPI.Code == (int)HttpStatusCode.Unauthorized)
            {
                return(BaseResult <AuthorizationResponse> .NotOK("The email and/or password entered is invalid. Please try again.", responseAPI.Code));
            }

            return(responseAPI);
        }