public async Task <ActionResult <Sweet> > PostSweet(SweetWithImage m) { m.Sweet.ImageFileName = StoreFile(m.ImageFile); _context.Sweets.Add(m.Sweet); await _context.SaveChangesAsync(); return(CreatedAtAction(nameof(GetSweet), new { id = m.Sweet.Id }, m.Sweet)); }
public async Task <IActionResult> PutSweet(SweetWithImage m) { _context.Entry(m.Sweet).State = EntityState.Modified; if (m.ImageFile is not null) { string oldImage = m.Sweet.ImageFileName; m.Sweet.ImageFileName = StoreFile(m.ImageFile); System.IO.File.Delete(Path.Combine("wwwroot", "sweet", oldImage)); } await _context.SaveChangesAsync(); return(NoContent()); }
private async Task SaveSweet() { try { bool success; SweetWithImage si = new SweetWithImage(s); if (fileToUpload is not null) { using Stream stream = fileToUpload.OpenReadStream(); using MemoryStream ms = new MemoryStream(); await stream.CopyToAsync(ms); UploadedFile uploadedFile = new UploadedFile(fileToUpload.Name, ms.ToArray()); si.ImageFile = uploadedFile; } if (si.Sweet.Id == default(int)) { var resp = await http.PostAsJsonAsync("sweet", si); success = resp.IsSuccessStatusCode; } else { var resp = await http.PutAsJsonAsync("sweet/withimage", si); success = resp.IsSuccessStatusCode; } if (success) { s.ImageFileName = fileToUpload.Name; await sw.FireAsync("Success", "Sweet save success.", SweetAlertIcon.Success); TitlePage = "Modify sweet"; } else { await sw.FireAsync("Error", "Something is wrong.", SweetAlertIcon.Error); } } catch (AccessTokenNotAvailableException exception) { exception.Redirect(); } }