Ejemplo n.º 1
0
        public async Task <IActionResult> DeletePost(int id)
        {
            PostInfo post = await _appDbContext.Posts.FirstOrDefaultAsync(p => p.Id == id);

            if (post != null)
            {
                try
                {
                    _appDbContext.Posts.Remove(post);
                    await _appDbContext.SaveChangesAsync();

                    string imgPath = _hostEnvironment.FromVirtualPath(post.ImgUrl);
                    if (imgPath != null && System.IO.File.Exists(imgPath))
                    {
                        System.IO.File.Delete(imgPath);
                    }
                    return(StatusCode((int)HttpStatusCode.OK));
                }
                catch (Exception)
                {
                    return(StatusCode((int)HttpStatusCode.InternalServerError));
                }
            }
            return(StatusCode((int)HttpStatusCode.BadRequest));
        }