示例#1
0
        public async Task <ObjectResult> ChangeUserName(string username, string newUsername)
        {
            var accountsResult = await accountsService.ChangeUserName(username, newUsername);

            var newsResult = await newsService.ChangeUserName(username, newUsername);

            var subscriptionsResult = await subscriptionsService.ChangeUserName(username, newUsername);

            if (accountsResult == null || newsResult == null || subscriptionsResult == null)
            {
                if (accountsResult != null)
                {
                    await accountsService.ChangeUserName(newUsername, username);
                }
                if (newsResult != null)
                {
                    await newsService.ChangeUserName(newUsername, username);
                }
                if (subscriptionsResult != null)
                {
                    await subscriptionsService.ChangeUserName(newUsername, username);
                }

                Scheduler.ScheduleRetryUntilSuccess(async() =>
                {
                    using (var client = new HttpClient())
                    {
                        try
                        {
                            accountsResult      = await accountsService.ChangeUserName(username, newUsername);
                            newsResult          = await newsService.ChangeUserName(username, newUsername);
                            subscriptionsResult = await subscriptionsService.ChangeUserName(username, newUsername);

                            if (accountsResult == null || newsResult == null || subscriptionsResult == null)
                            {
                                if (accountsResult != null)
                                {
                                    await accountsService.ChangeUserName(newUsername, username);
                                }
                                if (newsResult != null)
                                {
                                    await newsService.ChangeUserName(newUsername, username);
                                }
                                if (subscriptionsResult != null)
                                {
                                    await subscriptionsService.ChangeUserName(newUsername, username);
                                }
                                return(false);
                            }
                            eventBus.Publish(new ChangeUsernameEvent
                            {
                                OldUsername = username,
                                NewUsername = newUsername
                            });
                            return(true);
                        }
                        catch { }
                        return(false);
                    }
                });
                return(StatusCode(200, "Services status: " +
                                  $"AS: {(accountsResult != null ? "online" : "offline")};" +
                                  $"NS: {(newsResult != null ? "online" : "offline")};" +
                                  $"SS: {(subscriptionsResult != null ? "online" : "offline")};"));
            }

            var message = string.Empty;

            if (!accountsResult.IsSuccessStatusCode)
            {
                message += $"AS operation status: {accountsResult.StatusCode}, {accountsResult.Content.ReadAsStringAsync().Result};";
            }
            if (!newsResult.IsSuccessStatusCode)
            {
                message += $"NS operation status: {newsResult.StatusCode}, {newsResult.Content.ReadAsStringAsync().Result};";
            }
            if (!subscriptionsResult.IsSuccessStatusCode)
            {
                message += $"SS operation status: {subscriptionsResult.StatusCode}, {subscriptionsResult.Content.ReadAsStringAsync().Result};";
            }

            if (string.IsNullOrWhiteSpace(message))
            {
                eventBus.Publish(new ChangeUsernameEvent
                {
                    OldUsername = username,
                    NewUsername = newUsername
                });
                return(Ok(""));
            }
            return(StatusCode(500, message));
        }