public async Task <ActionResult> ToggleFixedNewsFlag([FromBody] NewsResolveDTO newsDTO) { var username = User.Claims.FirstOrDefault(x => x.Type == "FullName").Value; newsDTO.AuthorAcceptedFix = username; // Getting warehouse news by id var news = await NewsService.toggleNewsFixedProblemFlag(newsDTO); // Returning warehouse news return(Ok(news)); }
public async Task <News> toggleNewsFixedProblemFlag(NewsResolveDTO newsDTO) { // Getting warehouse news from DB var warehouseNews = await DB.NewsDB.Include(o => o.Warehouse).FirstOrDefaultAsync(x => x.Id == newsDTO.Id); // Checking if it's not null if (warehouseNews == null) { // If it's null, then we will throw new exception throw new Exception("Not found"); } // If object was found, then we return it else { // Cahnging flag value to true warehouseNews.FixedProblem = true; warehouseNews.FixedDate = DateTime.Now; warehouseNews.AuthorAcceptedFix = newsDTO.AuthorAcceptedFix; // Updating DB DB.NewsDB.Update(warehouseNews); // Saving changes in DB, because next step will also include DB operations await DB.SaveChangesAsync(); // Checking does house in that we just toggled flag, still hase some news/problems that was not resolved var hasProblems = await DB.WarehouseDB.AnyAsync(x => x.News.Any(o => o.FixedProblem != true && o.WarehouseId == warehouseNews.WarehouseId)); if (hasProblems) { // If has, then we get this house and change it flag hasProblems to true var house = await DB.WarehouseDB.FirstOrDefaultAsync(x => x.Id == warehouseNews.WarehouseId); house.HasProblems = true; DB.WarehouseDB.Update(house); } else { // If that was the last news that has not been resolved, then we change has problems flag to false var house = await DB.WarehouseDB.FirstOrDefaultAsync(x => x.Id == warehouseNews.WarehouseId); house.HasProblems = false; DB.WarehouseDB.Update(house); } // Saving changes in DB await DB.SaveChangesAsync(); return(warehouseNews); } }