public void CreateDiscussion(CreateDiscussionViewModel model)
        {
            var discussion = Mapper.Map <Discussion>(model);

            Db.Discussions.Add(discussion);
            Db.SaveChanges();
        }
Esempio n. 2
0
        public ActionResult CreateDiscussion(CreateDiscussionViewModel model)
        {
            if (!ModelState.IsValid)
            {
                model.Categories = context.Categories.ToList();
                return(View(model));
            }

            else
            {
                Discussion newDiscussion = new Discussion();
                newDiscussion.UserId      = User.Identity.GetUserId();
                newDiscussion.Title       = model.Title;
                newDiscussion.CategoryId  = model.CategoryId;
                newDiscussion.Description = model.Description;
                newDiscussion.CreatedDate = DateTime.Now;
                context.Discussions.Add(newDiscussion);

                ApplicationUser user = context.Users.SingleOrDefault(u => u.Id == newDiscussion.UserId);
                user.DiscussionsCount = user.DiscussionsCount + 1;

                Category category = context.Categories.SingleOrDefault(c => c.CategoryId == newDiscussion.CategoryId);
                category.DiscussionsCount = category.DiscussionsCount + 1;

                context.SaveChanges();
                return(RedirectToAction("Index"));
            }
        }
Esempio n. 3
0
        public ActionResult CreateDiscussion()
        {
            CreateDiscussionViewModel model = new CreateDiscussionViewModel();

            model.Categories = context.Categories.ToList();
            return(View(model));
        }
        public ActionResult CreateDiscussion(CreateDiscussionViewModel model)
        {
            if (ModelState.IsValid)
            {
                var discussion = this.mapper.Map <Discussion>(model);

                discussion.StartDate = discussion.StartDate.AddMinutes(model.TimezoneOffset);
                discussion.EndDate   = discussion.EndDate.AddMinutes(model.TimezoneOffset);

                var currentUserUserName = System.Web.HttpContext.Current.User.Identity.GetUserName();
                var currentUser         = this.usersService.GetUserByUserName(currentUserUserName);

                discussion.Creator = currentUser;
                discussion.Users.Add(currentUser);

                var bookId = int.Parse(Request.Params["bookId"]);
                var book   = this.booksService.GetById(bookId);
                discussion.Book = book;
                this.discussionsService.AddDiscussion(discussion);

                return(this.RedirectToAction("GetById", "Discussions", new { discussionId = discussion.Id }));
            }

            return(this.View(model));
        }
        public IActionResult Create(CreateDiscussionViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            int id = Convert.ToInt32(User.Claims.Where(c => c.Type == "Id")
                                     .Select(c => c.Value).SingleOrDefault());

            bool created = logic.Add(id, model.Title, model.Content);

            if (!created)
            {
                ModelState.AddModelError("", "Creating of the discussion failed.");

                return(View(model));
            }

            return(RedirectToAction("Index"));;
        }
        public IActionResult Create(CreateDiscussionViewModel model)
        {
            DiscussionService.CreateDiscussion(model);

            return(View(nameof(Index)));
        }