public async Task <List <Ita> > AddOrUpdateCategories(IEnumerable <Category> categories)
        {
            var changedIta = new List <Ita>();

            using (var db = new ThreadContext())
            {
                var dbCategories = db.Categories;
                foreach (var category in categories)
                {
                    var dbCategory = (from dbcate in dbCategories
                                      where dbcate.Name == category.Name
                                      select dbcate).FirstOrDefault();
                    if (dbCategory == null)
                    {
                        var addedcate = db.Add(category);
                        foreach (var ita in category.Itas)
                        {
                            ita.CategoryId = addedcate.Entity.CategoryId;
                            db.Add(ita);
                        }
                    }
                    else
                    {
                        foreach (var ita in category.Itas)
                        {
                            var dbexistIta = (from dbIta in db.Itas
                                              where dbIta.Name == ita.Name
                                              select dbIta).FirstOrDefault();
                            if (dbexistIta == null)
                            {
                                ita.CategoryId = dbCategory.CategoryId;
                                db.Add(ita);
                            }
                            else
                            {
                                if (ita.Url != dbexistIta.Url)
                                {
                                    dbexistIta.UpdateInformation(ita);
                                    db.Update(dbexistIta);
                                    changedIta.Add(dbexistIta);
                                }
                            }
                        }
                    }
                }
                await db.SaveChangesAsync();
            }
            return(changedIta);
        }
        public async Task <Thread> AddOrUpdateThreadInformation(Thread additionalThread)
        {
            using (var db = new ThreadContext())
            {
                var threads = db.Threads;
                var exist   = (from thread in threads
                               where (thread.Name == additionalThread.Name) && (thread.ItaId == additionalThread.ItaId) && (thread.Ita.CategoryId == additionalThread.Ita.CategoryId)
                               select thread).FirstOrDefault();
                EntityEntry <Thread> returnThreadEntry;
                if (exist != null)
                {
                    exist.UpdateInformation(additionalThread);
                    returnThreadEntry = db.Update(exist);
                }
                else
                {
                    exist             = additionalThread;
                    returnThreadEntry = db.Add(exist);
                }

                await db.SaveChangesAsync();

                var returnThread = returnThreadEntry.Entity;
                if (returnThread.Ita == null)
                {
                    returnThread.Ita = (from ita in db.Itas
                                        where ita.ItaId == returnThread.ItaId
                                        select ita).FirstOrDefault();
                }
                return(returnThread);
            }
        }
 public async Task AddCategories(IEnumerable <Category> categories)
 {
     using (var db = new ThreadContext())
     {
         foreach (var category in categories)
         {
             var dbcate = db.Add(category);
             foreach (var ita in category.Itas)
             {
                 ita.CategoryId = dbcate.Entity.CategoryId;
                 db.Add(ita);
             }
         }
         await db.SaveChangesAsync();
     }
 }
        public async Task <ObservableCollection <Post> > AddPosts(ObservableCollection <Post> newPosts, Thread thread)
        {
            using (var db = new ThreadContext())
            {
                var posts = (from post in db.Posts
                             where post.ThreadId == thread.ThreadId
                             orderby post.DataId
                             select post).ToList();

                IEnumerable <Post> addPosts;
                if (posts.Count() > 0)
                {
                    addPosts = (from newPost in newPosts
                                where newPost.DataId > posts.Last().DataId
                                select newPost).ToList();
                }
                else
                {
                    addPosts = newPosts;
                }

                foreach (var newPost in addPosts)
                {
                    newPost.ThreadId = thread.ThreadId;
                    newPost.Thread   = thread;
                }

                if (addPosts != null)
                {
                    foreach (var addpost in addPosts)
                    {
                        db.Add(addpost);
                    }
                }

                await db.SaveChangesAsync();

                posts = (from post in db.Posts
                         where post.ThreadId == thread.ThreadId
                         orderby post.DataId
                         select post).ToList();

                var returnPosts = new ObservableCollection <Post>();
                foreach (var post in posts)
                {
                    returnPosts.Add(post);
                }
                return(returnPosts);
            }
        }
예제 #5
0
        public async Task <Thread> Create(Thread model, LoginUser user, int albumId)
        {
            var thread = new Thread
            {
                Title       = model.Title,
                CreateDate  = DateTime.Now,
                Description = model.Description,
                UserID      = user.Id,
                AlbumId     = albumId,
                Votes       = model.Votes,
                UserName    = user.UserName,
                Flagged     = false,
                NoReports   = 0
            };

            _context.Add(thread);
            await _context.SaveChangesAsync();

            return(thread);
        }