Пример #1
0
        public async Task <IActionResult> Delete(int id)
        {
            if (ModelState.IsValid)
            {
                var smileyRecord = await SmileyRepository.FindById(id);

                if (smileyRecord is null)
                {
                    TempData[Constants.InternalKeys.StatusMessage] = $"No smiley was found with the id '{id}'";
                    return(this.RedirectToReferrer());
                }

                DbContext.Smileys.Remove(smileyRecord);

                var thoughts = DbContext.MessageThoughts.Where(t => t.SmileyId == id).ToList();

                if (thoughts.Any())
                {
                    DbContext.MessageThoughts.RemoveRange(thoughts);
                }

                // Only delete the file if no other smileys are using the file.
                if (!DbContext.Smileys.Any(s => s.FileName == smileyRecord.FileName))
                {
                    await ImageStore.Delete(new ImageStoreDeleteOptions {
                        ContainerName = Constants.InternalKeys.SmileyImageContainer,
                        Path          = smileyRecord.Path
                    });
                }

                await DbContext.SaveChangesAsync();

                TempData[Constants.InternalKeys.StatusMessage] = "The smiley was deleted.";
            }

            return(this.RedirectToReferrer());
        }
Пример #2
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)));
        }