Exemple #1
0
        public IHttpActionResult Delete(PostDeleteModel postToDelete)
        {
            var service = CreatePostService();

            service.RemovePost(postToDelete);
            return(Ok());
        }
Exemple #2
0
        public void RemovePost(PostDeleteModel postToDelete)
        {
            var entity = _ctx.Posts.Single(e => e.PostId == postToDelete.PostId);

            _ctx.Posts.Remove(entity);
            _ctx.SaveChanges();
        }
Exemple #3
0
        public async Task <IActionResult> Delete(PostDeleteModel model)
        {
            await _postService.Delete(model.Id);

            TempData["PostDeletedMessage"] = "Post deleted!";
            TempData.Keep("PostDeletedMessage");

            return(RedirectToAction("Index", "Topic", new { id = model.TopicId }));
        }
Exemple #4
0
        public async Task <IActionResult> Delete(PostDeleteModel input)
        {
            var post = AutoMapperConfig.MapperInstance.Map <Post>(input);

            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            await this.postsService.Delete(post);

            return(this.RedirectToAction("All"));
        }
Exemple #5
0
        public async Task <IActionResult> Delete([FromForm] PostDeleteModel model)
        {
            try
            {
                if (this.ValidRoleForAction(_context, _auth, new string[] { "Student", "Teacher", "Editor" }))
                {
                    AppIdentityUser currentuser = this.GetLoggedUser(_auth, _context);
                    if (ModelState.IsValid)
                    {
                        if (model.PostUserId == currentuser.Id || await _auth.CheckUserRole(currentuser, "Editor"))
                        {
                            Book item = await _context.GetByIdAsync <Book>(x => x.Id == model.PostId);

                            if (item != null)
                            {
                                _context.Delete(item);
                                bool result = await _context.SaveAll();

                                if (result == true)
                                {
                                    return(Ok("Success"));
                                }
                                else
                                {
                                    return(BadRequest("Model cannot be  deleted"));
                                }
                            }
                            else
                            {
                                return(NotFound("Model not found"));
                            }
                        }
                        return(BadRequest($"{currentuser.Name}, you don't have a permission"));
                    }
                    return(BadRequest("Model is not valid"));
                }
                return(Forbid());
            }
            catch (Exception ex)
            {
                var arguments = this.GetBaseData(_context, _auth);
                _logger.LogException(ex, arguments.Email, arguments.Path);
                return(BadRequest($"{ex.GetType().Name} was thrown."));
            }
        }
Exemple #6
0
        public IActionResult Delete(int id)
        {
            var post = _postService.GetById(id);

            if (post != null)
            {
                var model = new PostDeleteModel
                {
                    TopicId    = post.Topic.Id,
                    AuthorName = post.User.UserName,
                    Created    = post.Created
                };

                return(View(model));
            }

            return(RedirectToAction("Index", "Forum"));
        }