public async Task <IActionResult> PutDrug(long id, Drug drug) { if (id != drug.Id) { return(BadRequest()); } var existingdrug = await _context.Drugs.FindAsync(id); if (existingdrug == null) { return(NotFound()); } //trying to restore a drug if (existingdrug.Deleted && !drug.Deleted) { return(BadRequest("Cannot restore a drug")); } _context.Entry(existingdrug).State = EntityState.Detached; _context.Entry(drug).State = EntityState.Modified; await _context.SaveChangesAsync(); return(NoContent()); }
public async Task <IActionResult> Create(DrugViewModel drugView, IFormFile file) { if (file != null) { // путь к папке Files string path = "/Files/" + file.FileName; // сохраняем файл в папку Files в каталоге wwwroot using (var fileStream = new FileStream(_appEnvironment.WebRootPath + path, FileMode.Create)) { await file.CopyToAsync(fileStream); } Drug drug = _mapper.Map <DrugViewModel, Drug>(drugView); drug.Path = path; db.Drugs.Add(drug); await db.SaveChangesAsync(); } return(RedirectToAction("Index")); }
public async Task <ActionResult <Drug> > DeleteDrug(long id) { var drug = await _drugContext.Drugs.FindAsync(id); drug.Deleted = true; _drugContext.Entry(drug).State = EntityState.Modified; await _drugContext.SaveChangesAsync(); return(drug); }
public async Task SaveAsync() { await _context.SaveChangesAsync(); }