Ejemplo n.º 1
0
        public async Task <IActionResult> Edit(ControllerModels.Smileys.EditSmileysInput input)
        {
            if (ModelState.IsValid)
            {
                var result = await SmileyRepository.Edit(input);

                ModelState.AddModelErrors(result.Errors);

                if (ModelState.IsValid)
                {
                    TempData[Constants.InternalKeys.StatusMessage] = $"Smileys were updated.";

                    var referrer = ForumViewResult.GetReferrer(this);
                    return(Redirect(referrer));
                }
            }

            return(RedirectToAction(nameof(Index)));
        }
Ejemplo n.º 2
0
        public async Task <ControllerModels.Smileys.EditResult> Edit(ControllerModels.Smileys.EditSmileysInput input)
        {
            var result = new ControllerModels.Smileys.EditResult();

            var smileySortOrder = new Dictionary <int, int>();

            foreach (var smileyInput in input.Smileys)
            {
                var smileyRecord = DbContext.Smileys.Find(smileyInput.Id);

                if (smileyRecord is null)
                {
                    result.Errors.Add(nameof(smileyInput.Id), $@"No smiley was found with the id '{smileyInput.Id}'");
                    break;
                }

                smileySortOrder.Add(smileyRecord.Id, smileyRecord.SortOrder);
            }

            if (!result.Errors.Any())
            {
                foreach (var smileyInput in input.Smileys)
                {
                    var newSortOrder = (smileyInput.Column * 1000) + smileyInput.Row;

                    if (smileySortOrder[smileyInput.Id] != newSortOrder)
                    {
                        foreach (var kvp in smileySortOrder.Where(kvp => smileyInput.Column == kvp.Value / 1000 && kvp.Value >= newSortOrder).ToList())
                        {
                            smileySortOrder[kvp.Key]++;
                        }

                        smileySortOrder[smileyInput.Id] = newSortOrder;
                    }
                }

                foreach (var smileyInput in input.Smileys)
                {
                    var smileyRecord = DbContext.Smileys.Find(smileyInput.Id);

                    if (smileyRecord.Code != smileyInput.Code)
                    {
                        smileyRecord.Code = smileyInput.Code;
                        DbContext.Update(smileyRecord);
                    }

                    if (smileyRecord.Thought != smileyInput.Thought)
                    {
                        smileyRecord.Thought = smileyInput.Thought;
                        DbContext.Update(smileyRecord);
                    }

                    if (smileyRecord.SortOrder != smileySortOrder[smileyRecord.Id])
                    {
                        smileyRecord.SortOrder = smileySortOrder[smileyRecord.Id];
                        DbContext.Update(smileyRecord);
                    }
                }

                await DbContext.SaveChangesAsync();
            }

            return(result);
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Edit(ControllerModels.Smileys.EditSmileysInput input)
        {
            if (ModelState.IsValid)
            {
                var smileySortOrder = new Dictionary <int, int>();

                foreach (var smileyInput in input.Smileys)
                {
                    var smileyRecord = await SmileyRepository.FindById(smileyInput.Id);

                    if (smileyRecord is null)
                    {
                        ModelState.AddModelError(nameof(smileyInput.Id), $@"No smiley was found with the id '{smileyInput.Id}'");
                        break;
                    }

                    smileySortOrder.Add(smileyRecord.Id, smileyRecord.SortOrder);
                }

                if (ModelState.IsValid)
                {
                    foreach (var smileyInput in input.Smileys)
                    {
                        var newSortOrder = (smileyInput.Column * 1000) + smileyInput.Row;

                        if (smileySortOrder[smileyInput.Id] != newSortOrder)
                        {
                            foreach (var kvp in smileySortOrder.Where(kvp => smileyInput.Column == kvp.Value / 1000 && kvp.Value >= newSortOrder).ToList())
                            {
                                smileySortOrder[kvp.Key]++;
                            }

                            smileySortOrder[smileyInput.Id] = newSortOrder;
                        }
                    }

                    foreach (var smileyInput in input.Smileys)
                    {
                        var smileyRecord = await SmileyRepository.FindById(smileyInput.Id);

                        if (smileyRecord.Code != smileyInput.Code)
                        {
                            smileyRecord.Code = smileyInput.Code;
                            DbContext.Update(smileyRecord);
                        }

                        if (smileyRecord.Thought != smileyInput.Thought)
                        {
                            smileyRecord.Thought = smileyInput.Thought;
                            DbContext.Update(smileyRecord);
                        }

                        if (smileyRecord.SortOrder != smileySortOrder[smileyRecord.Id])
                        {
                            smileyRecord.SortOrder = smileySortOrder[smileyRecord.Id];
                            DbContext.Update(smileyRecord);
                        }
                    }

                    await DbContext.SaveChangesAsync();

                    TempData[Constants.InternalKeys.StatusMessage] = $"Smileys were updated.";

                    var referrer = this.GetReferrer();
                    return(Redirect(referrer));
                }
            }

            return(RedirectToAction(nameof(Index)));
        }