예제 #1
0
        /// <summary>
        /// Froms the category edit model.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <returns>New Category</returns>
        public static Category CategoryEditModelToCategory(CategoryEditModel model)
        {
            var category = new Category
            {
                CategoryId = model.CategoryId,
                Title = model.Title,
                Username = model.Username
            };

            return category;
        }
예제 #2
0
        /// <summary>
        /// Toes the category edit model.
        /// </summary>
        /// <param name="category">The category.</param>
        /// <returns>New Category Edit Model</returns>
        public static CategoryEditModel CategoryToCategoryEditModel(Category category)
        {
            CategoryEditModel model = new CategoryEditModel
            {
                CategoryId = category.CategoryId,
                Title = category.Title,
                Username = category.Username
            };

            return model;
        }
예제 #3
0
        public async Task<ActionResult> Edit([Bind(Include = "GameId,Name,ExternalLink,OwnerId,Created,Modified")] Game game)
        {
            if (ModelState.IsValid)
            {
                var doesForumCatAlreadyExist = await db.Categories.AnyAsync(c => c.Title == game.Name);
                if (!game.GameId.HasValue)
                {
                    db.Games.Add(game);
                    if (!doesForumCatAlreadyExist)
                    {
                        var category = new Category()
                        {
                            Title = game.Name,
                            Username = User.Identity.Name
                        };

                        db.Categories.Add(category);
                        await db.SaveChangesAsync();

                        category = await db.Categories.FirstOrDefaultAsync(c => c.Title == game.Name);
                        
                        var thread = new Thread()
                        {
                            Subject = "General",
                            Username = User.Identity.Name,
                            CategoryId = category.CategoryId
                        };

                        db.Threads.Add(thread);
                        await db.SaveChangesAsync();
                    }
                }
                else
                {
                    var existingGame = await db.Games.FirstOrDefaultAsync(g => g.GameId == game.GameId);

                    existingGame.Modified = DateTime.Now;
                    existingGame.Name = game.Name;
                    existingGame.ExternalLink = game.ExternalLink;

                    db.Entry(existingGame).State = EntityState.Modified;
                    var originalName = db.Entry(existingGame).OriginalValues["Name"].ToString();

                    if (originalName != game.Name)
                    {
                        var category = await db.Categories.FirstOrDefaultAsync(c => c.Title == originalName);
                        if (category != null)
                        {
                            category.Title = existingGame.Name;
                            db.Entry(category).State = EntityState.Modified;    
                        }

                    }

                    await db.SaveChangesAsync();
                }
                return RedirectToAction("Index");
            }
            return View(game);
        }
예제 #4
0
        /// <summary>
        /// Updates the specified new category.
        /// </summary>
        /// <param name="newCategory">The new category.</param>
        public void Update(Category newCategory)
        {
            this.Modified = DateTime.Now;

            if (!string.IsNullOrEmpty(newCategory.Title))
            {
                this.Title = newCategory.Title;
            }
            if (!string.IsNullOrEmpty(newCategory.Username))
            {
                this.Username = newCategory.Username;
            }
        }
예제 #5
0
        /// <summary>
        /// Threadses to threads view model.
        /// </summary>
        /// <param name="threads">The threads.</param>
        /// <param name="category">The category.</param>
        /// <param name="sort">The sort.</param>
        /// <param name="page">The page.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <returns>New ThreadsViewModel</returns>
        public static ThreadsViewModel ThreadsToThreadsViewModel(IQueryable<Thread> threads, Category category, ViewSortOrder sort, int page = 1, int pageSize = 20)
        {
            var threadList = new List<ThreadViewModel>();

            foreach (Thread thread in threads)
            {
                threadList.Add(
                    new ThreadViewModel
                    {
                        ThreadId = thread.ThreadId,
                        Subject = thread.Subject,
                        Date = (thread.Modified > thread.Created ? thread.Modified : thread.Created).ToString(),
                        Username = thread.Username,
                        CategoryId = category.CategoryId
                    });
            }

            var model = new ThreadsViewModel
            {
                Threads = threadList.Where(t => t != null),
                PagingInfo =
                    new PagingInfo
                    {
                        CurrentPage = page,
                        ItemsPerPage = pageSize,
                        TotalItems = threadList.Count()
                    },
                Category = 
                    new CategoryViewModel
                    {
                        CategoryId = category.CategoryId,
                        Title = category.Title,
                        Username = category.Username
                    },
                SortOrder = sort
            };

            return model;
        }