Пример #1
0
        public async Task HandleDeleteAction(LinksDeleteAction action, IDispatcher dispatcher)
        {
            var returnCode = HttpStatusCode.OK;

            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", action.Token);

            var returnObject = new DeletedObjectResponse
            {
                Deleted = false,
                Detail  = "Null response"
            };

            try
            {
                _httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Token", action.Token);
                var response = await _httpClient.DeleteAsync(
                    $"{Const.Links}{action.LinkId}/");

                returnObject = await response.Content.ReadFromJsonAsync <DeletedObjectResponse>();

                returnCode = response.StatusCode;
                if (response.StatusCode == HttpStatusCode.Accepted ||
                    response.StatusCode == HttpStatusCode.NoContent ||
                    response.StatusCode == HttpStatusCode.OK)
                {
                    if (returnObject is not null)
                    {
                        returnObject.Deleted = true;
                    }

                    dispatcher.Dispatch(new NotificationAction($"Link: {action.LinkId} - deleted", SnackbarColor.Info));
                }
                else
                {
                    if (returnObject is not null)
                    {
                        returnObject.Deleted = false;
                        dispatcher.Dispatch(new NotificationAction($"Error: {response.StatusCode}",
                                                                   SnackbarColor.Danger));
                    }
                }
            }
            catch (Exception e)
            {
                if (returnObject is not null)
                {
                    returnObject.Detail = $"Error {e}";
                }

                dispatcher.Dispatch(new NotificationAction($"Error: {e.Message}", SnackbarColor.Danger));
                returnCode = HttpStatusCode.BadRequest;
            }


            var userResult = new RootObject <OriinLink>();

            _httpClient.DefaultRequestHeaders.Authorization =
                new AuthenticationHeaderValue(scheme: "Token", action.Token);



            try
            {
                userResult = await _httpClient.GetFromJsonAsync <RootObject <OriinLink> >(
                    requestUri : Const.Links, Const.HttpClientOptions);
            }
            catch (Exception e)
            {
                dispatcher.Dispatch(new NotificationAction($"Error: {e.Message}", SnackbarColor.Danger));
                returnCode = HttpStatusCode.BadRequest;
            }

            dispatcher.Dispatch(
                new LinksDeleteResultAction(
                    delteResponse: returnObject ?? new DeletedObjectResponse(),
                    rootObject: userResult ?? new RootObject <OriinLink>(),
                    httpStatusCode: returnCode));

            if (returnCode != HttpStatusCode.BadRequest)
            {
                dispatcher.Dispatch(
                    new NotificationAction(action.DeleteLinkMessage, SnackbarColor.Success));
            }
        }
Пример #2
0
        public async Task HandleDeleteDataAction(UsersDeleteAction action, IDispatcher dispatcher)
        {
            var returnCode = HttpStatusCode.OK;
            HttpResponseMessage?response = null;

            _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Token", action.Token);

            var returnObject = new DeletedObjectResponse
            {
                Deleted = false,
                Detail  = "Null response"
            };

            try
            {
                _httpClient.DefaultRequestHeaders.Authorization =
                    new AuthenticationHeaderValue("Token", action.Token);
                response = await _httpClient.DeleteAsync(
                    $"{Const.Users}{action.UserId}/");
            }
            catch (Exception e)
            {
                dispatcher.Dispatch(new NotificationAction(e.Message, SnackbarColor.Danger));
                returnCode = HttpStatusCode.BadRequest;
            }


            if (response != null)
            {
                try
                {
                    returnObject = await response.Content.ReadFromJsonAsync <DeletedObjectResponse>();

                    if (response.StatusCode == HttpStatusCode.Accepted ||
                        response.StatusCode == HttpStatusCode.NoContent ||
                        response.StatusCode == HttpStatusCode.OK)
                    {
                        if (returnObject != null)
                        {
                            returnObject.Deleted = true;
                            dispatcher.Dispatch(
                                new NotificationAction(action.UserDeleteMessage, SnackbarColor.Success));
                        }
                    }
                    else
                    {
                        if (returnObject != null)
                        {
                            returnObject.Deleted = false;
                            returnObject.Detail  = $"Error: {response.StatusCode}";
                        }
                    }
                }
                catch (Exception e)
                {
                    dispatcher.Dispatch(new NotificationAction(e.Message, SnackbarColor.Danger));
                    returnCode = HttpStatusCode.BadRequest;
                }
            }

            dispatcher.Dispatch(
                new UsersDeleteResultAction(
                    deleteResponse: returnObject ?? new DeletedObjectResponse(),
                    resultCode: returnCode));
        }