public async Task <IActionResult> News([FromRoute] int id, [FromBody] News news) { //if (id != news.IDNews) //{ // return BadRequest(ModelState); //} News newsQuery = await dbContext.News.FindAsync(id); if (newsQuery == null) { return(BadRequest()); } if (news.imageNews != "") { if (newsQuery.imageNews != news.imageNews) { var imageArr = newsQuery.imageNews.Split('/'); var imageName = imageArr[imageArr.Length - 1].ToString(); FilesAndObjectOperation.DeleteFile(imageName); } } else { if (news.imageNews == "") { news.imageNews = newsQuery.imageNews; } } dbContext.News.Remove(newsQuery); await dbContext.SaveChangesAsync(); newsQuery = new News() { contentNews = news.contentNews, PublishNews = newsQuery.PublishNews, imageNews = news.imageNews, isShow = news.isShow }; dbContext.News.Add(newsQuery); await dbContext.SaveChangesAsync(); return(CreatedAtAction("GetNews", new { id = news.IDNews }, news)); }
public async Task <ActionResult <GallaryStaticImage> > DeleteGallaryStaticImages(int id) { var gallaryStaticImage = await dbContext.GallaryStaticImages.FindAsync(id); if (gallaryStaticImage == null) { return(NotFound()); } var imageArr = gallaryStaticImage.imageURL.Split('/'); var imageName = imageArr[imageArr.Length - 1].ToString(); FilesAndObjectOperation.DeleteFile(imageName); dbContext.GallaryStaticImages.Remove(gallaryStaticImage); await dbContext.SaveChangesAsync(); return(gallaryStaticImage); }