Exemplo n.º 1
0
 public IActionResult Create(Post model, IFormFile photo)
 {
     if (ModelState.IsValid)
     {
         _Post.Add(model, photo);
         return(RedirectToAction("Index"));
     }
     return(View(model));
 }
        public async Task <IActionResult> Post([FromBody] NewPost post)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var topic = _topicService.GetById(post.TopicId);
                    var user  = _userManager.FindByIdAsync(post.User.Id).Result;

                    post.User  = user;
                    post.Topic = topic;

                    if (_context.Posts.Any(p => p.Content == post.Content))
                    {
                        return(BadRequest("Post with this content already exist"));
                    }

                    await _postService.Add(post);

                    await _context.SaveChangesAsync();
                }
            }
            catch (DataException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.
                return(BadRequest("Unable to save changes. Try again..."));
            }
            return(CreatedAtAction(nameof(Get), new { id = post.Id }, post));
        }
Exemplo n.º 3
0
        public async Task <IActionResult> Create([Bind("Content,Created,UserId")] NewPost post, int id)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var topic  = _topicService.GetById(id);
                    var userId = _userManager.GetUserId(User);
                    var user   = _userManager.FindByIdAsync(userId).Result;

                    post.User    = user;
                    post.Topic   = topic;
                    post.TopicId = id;


                    if (_context.Posts.Any(p => p.Content == post.Content))
                    {
                        ModelState.AddModelError("Content", "Post with this content already exist");
                        return(View(post));
                    }

                    await _postService.Add(post);

                    return(RedirectToAction("PostsByTopic", "Posts", new { id = post.Topic.Id }));
                }
            }
            catch (DataException /* dex */)
            {
                //Log the error (uncomment dex variable name and add a line here to write a log.
                ModelState.AddModelError("", "Unable to save changes. Try again...");
            }
            return(View(post));
        }
Exemplo n.º 4
0
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            var userId = _userManager.GetUserId(User);
            var user   = _userManager.FindByIdAsync(userId).Result;
            var post   = BuildPost(model, user);

            _postService.Add(post).Wait();
            return(RedirectToAction("Index", "Post", new { id = post.Id }));
        }
Exemplo n.º 5
0
        public IActionResult Post([FromBody] Post post)
        {
            if (post == null)
            {
                return(BadRequest());
            }
            service.Add(post);

            return(CreatedAtRoute("GetPost", new { id = post.Id }, post));
        }
Exemplo n.º 6
0
        public async Task <IActionResult> AddPost(CreatePostModel model)
        {
            var userId = _userManager.GetUserId(User);
            var user   = await _userManager.FindByIdAsync(userId);

            var post = BuildPostEntity(model, user);

            _postService.Add(post).Wait();                                               // .Wait() blocks current thread until the Task is completed.
            return(RedirectToAction("Detail", "Student", new { id = post.Student.Id })); // need to new up the route-id object to avoid "Sequence contains no value" exception.
        }
Exemplo n.º 7
0
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            var userId = _userManager.GetUserId(User);
            var user   = _userManager.FindByIdAsync(userId).Result;
            var post   = BuildPost(model, user);

            await _servicePost.Add(post);

            return(RedirectToAction(nameof(Index), new { id = post.Id }));
        }
Exemplo n.º 8
0
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            var userId = _userManager.GetUserId(User);
            var user   = _userManager.FindByIdAsync(userId).Result;
            var post   = BuildPost(model, user);

            await _postService.Add(post);

            return(RedirectToAction("Forum", "Post"));
        }
Exemplo n.º 9
0
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            var userId = _userManager.GetUserId(User);
            var user   = _userManager.FindByIdAsync(userId).Result;
            var post   = BuildPostReplies(model, user);

            await _postService.Add(post);

            //TODO : Implement user rating system
            return(RedirectToAction("Index", "Post", new { id = post.Id }));
        }
Exemplo n.º 10
0
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            string          userId = _userManager.GetUserId(User);
            ApplicationUser user   = _userManager.FindByIdAsync(userId).Result; //Async task requires await

            Post post = BuildPost(model, user);

            await _postService.Add(post); //Adds to db Wait blocks the current thread until the task is complete

            return(RedirectToAction("Index", "Post", new { id = post.Id }));
        }
Exemplo n.º 11
0
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            var userId = _userManager.GetUserId(User);
            var user   = await _userManager.FindByIdAsync(userId);

            var post = BuildPost(model, user);
            await _postService.Add(post);

            await _userService.UpdateUserRating(userId, typeof(Post));

            return(RedirectToAction("Index", "Post", new { id = post.Id }));
        }
Exemplo n.º 12
0
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            var userId = _userManager.GetUserId(User);
            var user   = _userManager.FindByIdAsync(userId).Result;
            var post   = BuildPost(model, user);

            _postService.Add(post).Wait(); // Block the current thread until the task is complete

            // TO DO: Implement user rating management

            return(RedirectToAction("Index", "Post", new { id = post.Id }));
        }
Exemplo n.º 13
0
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            var userId = _userManager.GetUserId(User);
            var user   = _userManager.FindByIdAsync(userId).Result;

            var post = BuildPost(model, user);

            _postService.Add(post).Wait(); // Block the current thread untill the task is complete
            await _userService.UpdateUserRating(userId, typeof(Post));

            return(RedirectToAction("Index", "Post", new { id = post.Id }));
        }
Exemplo n.º 14
0
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            var userId = _userManager.GetUserId(User);
            var user   = _userManager.GetUserAsync(User).Result;
            var post   = BuildPost(model, user);

            _post.Add(post).Wait();
            await _user.UpdateUserRating(userId, typeof(Post));

            //TODO Implement user rating
            return(RedirectToAction("Index", "Post", new { id = post.Id }));
        }
Exemplo n.º 15
0
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            var userId = _userManager.GetUserId(User);
            var user   = await _userManager.FindByIdAsync(userId);

            var post = BuildPostReplies(model, user);
            await _postService.Add(post);

            // TODO: User Rating management here...

            return(RedirectToAction("Index", "Post", post.Id));
        }
Exemplo n.º 16
0
        public async Task <ActionResult> AddPost(NewPostModel model)
        {
            var userId = User.Identity.GetUserId();
            var user   = _userManager.FindByIdAsync(userId).Result;
            var post   = BuildPost(model, user);

            await _postRepositories.Add(post);

            await _applicationUserRepositories.IncrementUserRating(user.Id, typeof(Post));

            return(RedirectToAction("Index", "Post", new { id = post.Id }));
        }
Exemplo n.º 17
0
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            var m_userId = m_userManager.GetUserId(User);
            var m_user   = m_userManager.FindByIdAsync(m_userId).Result;

            var m_post = BuildPost(model, m_user);

            // m_postService.Add(m_post).Wait(); // Block the current thread until the tast is complete
            await m_postService.Add(m_post);

            return(RedirectToAction("Index", "Post", new { id = m_post.Id }));
        }
Exemplo n.º 18
0
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            var userId = _userManager.GetUserId(User);
            var user   = _userManager.FindByIdAsync(userId).Result; //result extension ensues user is not null by waiting for a result
            var post   = BuildPost(model, user);

            _postService.Add(post).Wait();  //Blocks the current thread and waits until the task is complete

            //TO DO: User rating management

            return(RedirectToAction("Index", "Post", new { id = post.Id }));
        }
Exemplo n.º 19
0
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            var userId = _userMaanger.GetUserId(User);
            var user   = _userMaanger.FindByIdAsync(userId).Result;
            var post   = BuildPost(model, user);

            _postService.Add(post).Wait();  // block the current thread unitll the task completed.

            // TODO Implement User Raiting Management System


            return(RedirectToAction("Index", "Post", new{ id = post.Id }));
        }
Exemplo n.º 20
0
        public async Task <IActionResult> AddPost(NewPostModel model) //the NewPostModel is passed from the view based on empty NewPostModel passed there by get method and fulfilled by user
        {
            var userId = _userManager.GetUserId(User);                //take the user id by UserManager service
            var user   = _userManager.FindByIdAsync(userId).Result;   //to find the instance of that application user object from our data store
            var post   = BuildPost(model, user);

            await _postService.Add(post); //use our post service to invoke EF to make that insert of our post into data store  | should block the current thread until the task is complete

            await _userService.UpdateUserRating(userId, typeof(Post));


            return(RedirectToAction("Index", "Post", new { id = post.Id }));  //to redirect to action we are gonna specify (action, controller, id)
        }
Exemplo n.º 21
0
        public async Task <IActionResult> Create(NewPostModel model)
        {
            var userId = _userManager.GetUserId(User);
            var user   = await _userManager.FindByIdAsync(userId);

            var post = BuildPost(model, user);

            await _postService.Add(post);

            //TODO: Implement User Rating Management

            return(RedirectToAction("Index", "Post", new { id = post.Id }));
        }
Exemplo n.º 22
0
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            var userId = _userManager.GetUserId(User);
            var user   = await _userManager.FindByIdAsync(userId);

            var post = BuildPostReplies(model, user);

            await _postService.Add(post); //Block Current Thread until task is complete

            //TODO: Implement User Rating Management

            return(RedirectToAction("Index", "Post", new { id = post.Id }));
        }
Exemplo n.º 23
0
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            var userId = _userManager.GetUserId(User);
            var user   = await _userManager.FindByIdAsync(userId);

            var post = BuildPost(model, user);

            await _postService.Add(post);

            await _userService.BumpRating(userId, typeof(Post));

            return(RedirectToAction("Index", "Forum", model.ForumId));
        }
Exemplo n.º 24
0
        public async Task <IActionResult> AddPost(NewPostViewModel model)
        {
            var userId = _userManager.GetUserId(User);
            //var user = await _userManager.FindByIdAsync(userId); //There were issues when trying it with await infront.
            var user = _userManager.FindByIdAsync(userId).Result;

            var post = BuildPost(model, user);

            await _postService.Add(post);

            await _userService.UpdateUserRating(userId, typeof(Post));

            return(RedirectToAction("Index", "Post", new { id = post.Id }));
        }
Exemplo n.º 25
0
        public async Task <IActionResult> AddPost(NewPostViewModel model)
        {
            var userId = _userManager.GetUserId(User);
            //var user = await _userManager.FindByIdAsync(userId); //There were issues when trying it with await infront.
            var user = _userManager.FindByIdAsync(userId).Result;

            var post = BuildPost(model, user);

            await _postService.Add(post);

            //_postService.Add(post).Wait(); //Block the current thread until the task is complete.
            //TODO: Implement user rating management

            return(RedirectToAction("Index", "Post", new { id = post.Id }));
        }
Exemplo n.º 26
0
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            var userId = _userManager.GetUserId(User);
            //var user = await _userManager.FindByIdAsync(userId);
            var user = _userManager.FindByIdAsync(userId).Result;
            var post = BuildPost(model, user);

            await _postService.Add(post);

            // _postService.Add(post).Wait();//block current thread until task completed
            //todo: user rating management

            //return RedirectToAction("Index", "Post", post.Id);
            return(RedirectToAction("Index", "Post", new { id = post.Id }));
        }
Exemplo n.º 27
0
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            ViewBag.dropListForums = _forumService.listForumKeyValue().ToList();
            var userId = _userManager.GetUserId(User);
            var user   = _userManager.FindByIdAsync(userId).Result;

            if (ModelState.IsValid)
            {
                string urlImage = "";
                string roorPath = _hostingEnviroment.WebRootPath;
                var    files    = HttpContext.Request.Form.Files;

                var supportedTypes = new[] { ".txt", ".doc", ".docx", ".pdf", ".xls", ".xlsx" };
                if (files.Count != 0)
                {
                    var uploads   = Path.Combine(roorPath, SD.FolderDefault + "/" + SD.pathFiles);
                    var extension = Path.GetExtension(files[0].FileName).ToLower();

                    using (var filestream = new FileStream(Path.Combine(uploads, model.Title + extension), FileMode.Create))
                    {
                        files[0].CopyTo(filestream);
                    }
                    if (!supportedTypes.Contains(extension))
                    {
                        TempData["Message"] = "Định dạng file không hợp lệ !";
                        return(RedirectToAction(nameof(Create)));
                    }
                    else
                    {
                        urlImage = model.Title + extension;
                    }
                }
                var post = new Post
                {
                    Title   = model.Title,
                    Content = model.Content,
                    Created = DateTime.Now,
                    Status  = false,
                    FileUrl = urlImage,
                    User    = user,
                    ForumId = model.ForumId
                };
                await _postService.Add(post);

                return(RedirectToAction(nameof(Index)));
            }
            return(View(nameof(Create)));
        }
Exemplo n.º 28
0
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            if (model == null)
            {
                return(NotFound());
            }
            var userId = _userManager.GetUserId(User);
            var user   = _userManager.FindByIdAsync(userId).Result;
            var post   = BuildPost(model, user);

            await _postService.Add(post).ConfigureAwait(false);

            await _userService.UpdateUserRating(userId, typeof(Post)).ConfigureAwait(false);

            return(RedirectToAction("Index", "Posts", new { id = post.Id }));
        }
Exemplo n.º 29
0
        public IActionResult Create(NewPostViewModel model)
        {
            var user  = _userManager.FindByNameAsync(model.AuthorName).Result;
            var forum = _forumService.GetById(model.ForumId);
            var post  = new Post
            {
                User    = user,
                Title   = model.Title,
                Content = model.Content,
                Created = DateTime.Now,
                Forum   = forum
            };

            _postService.Add(post).Wait();
            _userService.IncrementRating(user.Id, typeof(Post));
            return(RedirectToAction("Index", "Post", new { postId = post.Id }));
        }
        public async Task <IActionResult> AddPost(NewPostModel model)
        {
            var userId = _userManager.GetUserId(User);
            var user   = await _userManager.FindByIdAsync(userId);

            var post = BuildPost(model, user);

            if (model.Content != null && model.Title != null)
            {
                await _postService.Add(post);

                await _userService.BumpRating(userId, typeof(Post));
            }


            return(RedirectToAction("Topic", "Forum", new { id = model.ForumId }));
        }