Beispiel #1
0
        public static async Task PostUpdate(ListViewModel vm, ApplicationUser owner, IdeasContext context, IHostingEnvironment hostingEnvironment)
        {
            var idea = await context.Ideas.Include(r => r.Updates).FirstOrDefaultAsync(r => r.Id == vm.IdeaId && r.Owner == owner);

            if (idea != null)
            {
                var AddThis = Image.GetNewAndRemoveOld(vm.EditUpdateViewModel.JsonImagesNew, vm.EditUpdateViewModel.JsonImagesRemove, vm.IdeaId, vm.EditId, owner, hostingEnvironment, context).Result;
                try {
                    var update = new Update();
                    update.Text     = vm.EditUpdateViewModel.Text.Trim();
                    update.DateTime = DateTime.Now;
                    if (AddThis != null)
                    {
                        AddThis       = UnsavedImage.GetList(AddThis.Select(r => r.FileName).ToList(), context, owner);
                        update.Images = AddThis;
                        UnsavedImage.RemoveList(AddThis.Select(r => r.FileName).ToList(), context, owner);
                    }
                    idea.Updates.Add(update);
                    idea.LatestUpdate = DateTime.Now;
                    Stats.AddUpdate(idea);
                    await context.SaveChangesAsync();
                } catch (Exception ex) {
                    ex = ex;
                }
            }
        }
Beispiel #2
0
        public static async Task UpdateUpdate(ListViewModel vm, ApplicationUser owner, IdeasContext context, IHostingEnvironment hostingEnvironment)
        {
            var update = GetByUpdateFromIdea(vm.IdeaId, vm.EditId, owner, context).Result;

            if (update != null)
            {
                var jsonRemove = ((vm.EditUpdateViewModel.JsonImagesRemove != null) ? JsonConvert.DeserializeObject <List <Image> >(vm.EditUpdateViewModel.JsonImagesRemove) : null);
                var jsonNew    = ((vm.EditUpdateViewModel.JsonImagesNew != null) ? JsonConvert.DeserializeObject <List <Image> >(vm.EditUpdateViewModel.JsonImagesNew) : null);
                var AddThis    = cleanUnSavedImage(jsonNew, jsonRemove, owner, context, hostingEnvironment).Result;
                finishRemoveList(vm.IdeaId, vm.EditId, jsonRemove, context, hostingEnvironment);
                try {
                    update      = context.Updates.First(r => r.Id == vm.EditId);
                    update.Text = vm.EditUpdateViewModel.Text.Trim();
                    if (AddThis != null)
                    {
                        AddThis = UnsavedImage.GetList(AddThis.Select(r => r.FileName).ToList(), context, owner);
                        if (update.Images != null)
                        {
                            foreach (Image image in AddThis)
                            {
                                update.Images.Add(image);
                            }
                        }
                        else
                        {
                            update.Images = AddThis;
                        }
                        UnsavedImage.RemoveList(AddThis.Select(r => r.FileName).ToList(), context, owner);
                    }
                    await context.SaveChangesAsync();
                } catch (Exception ex) {
                    ex = ex;
                }
            }
        }
        public static async Task <Idea> NewIdea(NewIdeaViewModel vm, ApplicationUser owner, IdeasContext context, IHostingEnvironment hostingEnvironment)
        {
            var  AddThis = Image.GetNewAndRemoveOld(vm.JsonImagesNew, vm.JsonImagesRemove, 0, 0, owner, hostingEnvironment, context).Result;
            Idea idea    = new Idea {
                Owner        = owner,
                Titel        = vm.Titel.Trim(),
                Text         = vm.Text.Trim(),
                DateTime     = DateTime.Now,
                LatestUpdate = DateTime.Now,
                Tags         = ((vm.JsonTags != null) ? JsonConvert.DeserializeObject <List <Tag> >(vm.JsonTags) : null)
            };

            if (AddThis != null)
            {
                AddThis = UnsavedImage.GetList(AddThis.Select(r => r.FileName).ToList(), context, owner);
                if (idea.Images != null)
                {
                    foreach (Image image in AddThis)
                    {
                        idea.Images.Add(image);
                    }
                }
                else
                {
                    idea.Images = AddThis;
                }
                UnsavedImage.RemoveList(AddThis.Select(r => r.FileName).ToList(), context, owner);
            }
            UserStats.AddProject(owner, context);
            context.Ideas.Add(idea);
            return(idea);
        }
        public static async Task UpdateIdea(ListViewModel vm, ApplicationUser owner, IdeasContext context, IHostingEnvironment hostingEnvironment)
        {
            var idea = context.Ideas.Include(r => r.Tags).FirstOrDefault(r => r.Id == vm.IdeaId && r.Owner == owner);

            if (idea != null)
            {
                var jsonRemove = ((vm.EditIdeaViewModel.JsonImagesRemove != null) ? JsonConvert.DeserializeObject <List <Image> >(vm.EditIdeaViewModel.JsonImagesRemove) : null);
                var jsonNew    = ((vm.EditIdeaViewModel.JsonImagesNew != null) ? JsonConvert.DeserializeObject <List <Image> >(vm.EditIdeaViewModel.JsonImagesNew) : null);
                var AddThis    = cleanUnSavedImage(jsonNew, jsonRemove, owner, context, hostingEnvironment).Result;
                finishRemoveList(vm.IdeaId, jsonRemove, context, hostingEnvironment);

                try {
                    var jsonTagsRemove = ((vm.EditIdeaViewModel.JsonTagsRemove != null) ? JsonConvert.DeserializeObject <List <Tag> >(vm.EditIdeaViewModel.JsonTagsRemove) : null);
                    var jsonTagsNew    = ((vm.EditIdeaViewModel.JsonTags != null) ? JsonConvert.DeserializeObject <List <Tag> >(vm.EditIdeaViewModel.JsonTags) : null);

                    idea.Text  = vm.EditIdeaViewModel.Text.Trim();
                    idea.Titel = vm.EditIdeaViewModel.Titel.Trim();
                    if (jsonTagsRemove != null)
                    {
                        var remove = context.Tags.Where(r => jsonTagsRemove.Any(x => x.Value == r.Value)).ToList();
                        idea.Tags.RemoveAll(r => remove.Any(s => s.Value == r.Value));
                    }
                    if (jsonTagsNew != null)
                    {
                        if (idea.Tags != null && idea.Tags.Count > 0)
                        {
                            jsonTagsNew = jsonTagsNew.Where(r => !idea.Tags.Any(a => r.Value == a.Value)).ToList();
                            idea.Tags.AddRange(jsonTagsNew);
                        }
                        else
                        {
                            idea.Tags = jsonTagsNew;
                        }
                    }

                    if (AddThis != null)
                    {
                        AddThis = UnsavedImage.GetList(AddThis.Select(r => r.FileName).ToList(), context, owner);
                        if (idea.Images != null)
                        {
                            foreach (Image image in AddThis)
                            {
                                idea.Images.Add(image);
                            }
                        }
                        else
                        {
                            idea.Images = AddThis;
                        }
                        UnsavedImage.RemoveList(AddThis.Select(r => r.FileName).ToList(), context, owner);
                    }
                    await context.SaveChangesAsync();
                } catch (Exception ex) {
                    ex = ex;
                }
            }
        }