Example #1
0
        public async Task <ICommandResult <IEnumerable <Moderator> > > CreateAsync(Moderator moderator, IEnumerable <int> categoryIds)
        {
            var result = new CommandResult <IEnumerable <Moderator> >();

            // Get all moderators
            var moderators = await _moderatorStore
                             .QueryAsync()
                             .ToList();

            var output = new List <Moderator>();

            foreach (var categoryId in categoryIds)
            {
                // Does the moderator already exists for the category?
                var existingModerator =
                    moderators.Data.FirstOrDefault(m => m.UserId == moderator.UserId && m.CategoryId == categoryId);

                Moderator newOrUpdatedModerator = null;

                // IF so update existing moderator
                if (existingModerator != null)
                {
                    moderator.Id          = existingModerator.Id;
                    newOrUpdatedModerator = await _moderatorStore.UpdateAsync(moderator);
                }
                else
                {
                    moderator.CategoryId  = categoryId;
                    newOrUpdatedModerator = await _moderatorStore.CreateAsync(moderator);
                }

                if (newOrUpdatedModerator != null)
                {
                    output.Add(newOrUpdatedModerator);
                }
            }


            return(result.Success(output));
        }
Example #2
0
        public override async Task <IViewProviderResult> BuildUpdateAsync(Moderator model, IViewProviderContext context)
        {
            var moderator = await _moderatorStore.GetByIdAsync(model.Id);

            if (moderator == null)
            {
                return(await BuildIndexAsync(model, context));
            }

            // Validate model
            if (await ValidateModelAsync(model, context.Updater))
            {
                // Get all moderators
                var moderators = await _moderatorStore
                                 .QueryAsync()
                                 .ToList();

                // Get selected channels
                var selectedCategories = GetChannelsToAdd();

                var output = new List <Moderator>();
                foreach (var categoryId in selectedCategories)
                {
                    // Does the moderator already exists for the category?
                    var existingModerator =
                        moderators.Data.FirstOrDefault(m =>
                                                       m.UserId == moderator.UserId && m.CategoryId == categoryId);

                    Moderator newOrUpdatedModerator = null;
                    moderator.CategoryId = categoryId;
                    moderator.Claims     = GetPostedClaims();

                    // If so update existing moderator
                    if (existingModerator != null)
                    {
                        moderator.Id          = existingModerator.Id;
                        newOrUpdatedModerator = await _moderatorStore.UpdateAsync(moderator);
                    }
                    else
                    {
                        moderator.Id          = 0;
                        newOrUpdatedModerator = await _moderatorStore.CreateAsync(moderator);
                    }

                    if (newOrUpdatedModerator != null)
                    {
                        output.Add(newOrUpdatedModerator);
                    }
                }

                // If we have channels, lets delete the "All Channels" entry
                if (output.Count > 0)
                {
                    var allChannels = moderators?.Data.FirstOrDefault(m =>
                                                                      m.UserId == moderator.UserId && m.CategoryId == 0);
                    if (allChannels != null)
                    {
                        await _moderatorStore.DeleteAsync(allChannels);
                    }
                }
            }

            return(await BuildEditAsync(moderator, context));
        }
Example #3
0
        public async Task <IActionResult> CreatePost(EditModeratorViewModel viewModel)
        {
            // Build users to effect
            var users = new List <User>();

            if (!String.IsNullOrEmpty(viewModel.Users))
            {
                var items = JsonConvert.DeserializeObject <IEnumerable <UserApiResult> >(viewModel.Users);
                foreach (var item in items)
                {
                    if (item.Id > 0)
                    {
                        var user = await _userStore.GetByIdAsync(item.Id);

                        if (user != null)
                        {
                            users.Add(user);
                        }
                    }
                }
            }

            var userId = 0;

            foreach (var user in users)
            {
                userId = user.Id;
            }

            var moderator = new Moderator()
            {
                UserId = userId
            };

            // Validate model state within all involved view providers
            if (await _viewProvider.IsModelStateValidAsync(moderator, this))
            {
                // Get composed type from all involved view providers
                var model = await _viewProvider.ComposeModelAsync(moderator, this);

                // Create moderator
                var result = await _moderatorStore.CreateAsync(model);

                if (result != null)
                {
                    // Update moderator within various view providers
                    await _viewProvider.ProvideUpdateAsync(result, this);

                    // Everything was OK
                    _alerter.Success(T["Moderator Created Successfully!"]);

                    // Redirect to topic
                    return(RedirectToAction(nameof(Index), new { Id = 0 }));
                }
            }
            else
            {
                _alerter.Danger(T["You must specify at least 1 user!"]);
            }

            return(await Create());


            //if (users.Count > 0)
            //{

            //    // Compose moderator from all involved view providers
            //    // This ensures the claims are always populated
            //    var composedModerator = await _viewProvider.GetComposedType(this);
            //    var isValid = false;

            //    //// Update each user
            //    //foreach (var user in users)
            //    //{

            //    //    composedModerator.UserId = user.Id;

            //    //    // Validate model state within all view providers
            //    //    if (await _viewProvider.IsModelStateValid(composedModerator, this))
            //    //    {
            //    //        // Create moderator
            //    //        var result = await _moderatorStore.CreateAsync(composedModerator);
            //    //        if (result != null)
            //    //        {
            //    //            // Update moderator within various view providers
            //    //            await _viewProvider.ProvideUpdateAsync(result, this);
            //    //            isValid = true;
            //    //        }
            //    //    }
            //    //}

            //    //if (isValid)
            //    //{

            //    //    // Everything was OK
            //    //    _alerter.Success(T["Moderator Created Successfully!"]);

            //    //    // Redirect to topic
            //    //    return RedirectToAction(nameof(Index), new {Id = 0});

            //    //}

            //    // if we reach this point some view model validation
            //    // failed within a view provider, display model state errors
            //    foreach (var modelState in ViewData.ModelState.Values)
            //    {
            //        foreach (var error in modelState.Errors)
            //        {
            //            _alerter.Danger(T[error.ErrorMessage]);
            //        }
            //    }

            //    return await Create();

            //}
            //else
            //{
            //    _alerter.Danger(T["You must specify at least 1 user!"]);
            //}

            //return await Create();
        }