public ActionResult Add(AddTopicViewModel model)
        {
            if (!ModelState.IsValid)
            {
                var errors = "";
                foreach (var modelStateVal in ViewData.ModelState.Values)
                {
                    foreach (var error in modelStateVal.Errors)
                    {
                        errors += error.ErrorMessage;
                    }
                }
                TempData["AddTErr"] = errors;
                return(RedirectToAction("Topics"));
            }
            var teacher = this.userService.GetTeacherByAppUserId(this.User.Identity.GetUserId());

            this.topicService.Add(
                new Topic
            {
                Name      = model.Name,
                TeacherId = teacher.Id
            });
            return(RedirectToAction("Topics"));
        }
        public IActionResult Add()
        {
            AddTopicViewModel addTopicViewModel = new AddTopicViewModel();

            return(View(addTopicViewModel));
            //WARNING: check if catetory
            //is correct ref, since not using category as used in CheeseCategory
        }
Beispiel #3
0
        public AddTopicViewModel GetAddTopicViewModel(AddTopicBindingModel model)
        {
            if (model == null)
            {
                return(null);
            }
            AddTopicViewModel viewModel = Mapper.Instance.Map <AddTopicViewModel>(model);

            return(viewModel);
        }
        public async Task <ActionResult> Create(AddTopicViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await topicService.Create(model.ForumId, model.Name, model.Text, User.Identity.Name);

                if (result.Succedeed)
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    ModelState.AddModelError("", result.Message);
                }
            }

            return(View(model));
        }
        public IActionResult Add(AddTopicViewModel addTopicViewModel)
        {
            if (ModelState.IsValid)
            {
                //CheeseCategory newCheeseCategory =
                //context.Categories.Single(c => c.ID == addCheeseViewModel.CategoryID);
                // Add the new cheese to my existing cheeses
                Topic newTopic = new Topic
                {
                    Name        = addTopicViewModel.Name,
                    Description = addTopicViewModel.Description,
                    //Category = newCheeseCategory //was 'addCheeseViewModel.CategoryID'
                };
                context.Topics.Add(newTopic);
                context.SaveChanges();
                return(Redirect("/Topic"));
            }

            return(View(addTopicViewModel));
        }
        public async Task <IActionResult> AddTopic(AddTopicViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            User user = await _context.User.FirstOrDefaultAsync(t => t.Id.Equals(model.UserId));

            Topic topic = new Topic()
            {
                Title = model.Topic,
                Rate  = 0,
                Date  = DateTime.Now.ToString("dd/MM/yyyy HH:mm"),
                User  = user
            };

            Comment comment = new Comment()
            {
                Content = model.Comment,
                Date    = DateTime.Now.ToString("dd/MM/yyyy HH:mm"),
                User    = user,
                Topic   = topic
            };

            user.TopicsCount++;
            user.CommentsCount++;

            _context.Update(user);
            await _context.AddAsync(topic);

            await _context.AddAsync(comment);

            await _context.SaveChangesAsync();

            return(Ok());
        }
        public async Task <IActionResult> AddTopic(AddTopicViewModel model)
        {
            if (ModelState.IsValid)
            {
                User user = await _context.User.FirstOrDefaultAsync(t => t.Id.Equals(_userManager.GetUserId(User)));

                Topic topic = new Topic()
                {
                    Title = model.Topic,
                    Rate  = 0,
                    Date  = DateTime.Now.ToString("dd/MM/yyyy HH:mm"),
                    User  = user
                };

                Comment comment = new Comment()
                {
                    Content = model.Comment,
                    Date    = DateTime.Now.ToString("dd/MM/yyyy HH:mm"),
                    User    = user,
                    Topic   = topic
                };

                user.TopicsCount++;
                user.CommentsCount++;

                _context.Update(user);
                await _context.AddAsync(topic);

                await _context.AddAsync(comment);

                await _context.SaveChangesAsync();

                return(RedirectToAction("ShowTopic", new { id = topic.Id }));
            }
            return(View(model));
        }