public async Task PutPerson(int id, PersonViewModel personViewModel) { if (id != personViewModel.Id) { throw new BadRequestException("Данные запроса не совпадают"); } var person = MapToModel(personViewModel); _context.Entry(person).State = EntityState.Modified; try { await _context.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!PersonExists(id)) { throw new NotFoundException("Человека с такими параметрами не найдено"); } else { throw; } } }
public async Task PutFace(int id, FaceViewModel faceViewModel, int personId) { if (id != faceViewModel.Id) { throw new BadRequestException("Данные запроса не совпадают"); } var face = await _context.Faces.Where(f => f.Id == id).SingleOrDefaultAsync(); if (face == null) { throw new NotFoundException("Лица с такими параметрами не найдено"); } if (personId != face.PersonId) { throw new BadRequestException("Данные запроса не совпадают"); } face.PersonId = faceViewModel.PersonId; int photoHash = Hasher.CreateHash(faceViewModel.Photo); string oldName = null; // Если фото поменялось if (photoHash != face.PhotoHash) { oldName = face.PhotoName; face.PhotoName = DateTime.UtcNow.Ticks.ToString(); face.PhotoHash = photoHash; } _context.Entry(face).State = EntityState.Modified; try { await _context.SaveChangesAsync(); if (oldName != null) { await _googleStorageService.Delete(oldName); await _googleStorageService.Write(faceViewModel.Photo, face.PhotoName); } } catch (DbUpdateConcurrencyException) { if (!FaceExists(id)) { throw new NotFoundException("Лица с такими параметрами не найдено"); } else { throw; } } }
public async Task <IActionResult> Upload(IFormCollection formCollection) { try { var file = formCollection.Files.FirstOrDefault(); // Store photo to Azure Storage string imageIdentifier = await _photoImageStorageService.SaveImage(file?.OpenReadStream(), DateTime.UtcNow.Millisecond.ToString() + file?.FileName); // Update database _dbContext.PhotoImages.Add(new PhotoImage { Url = imageIdentifier, Caption = "None" }); await _dbContext.SaveChangesAsync(); return(RedirectToAction("Index")); } catch (Exception) { return(RedirectToAction("Error")); } }
public async Task <ActionResult> Create([Bind(Include = "PhotoId,BeachId,Description,Content")] Photo photo, HttpPostedFileBase upload) { if (ModelState.IsValid) { if (upload != null && upload.ContentLength > 0) { var fileData = new MemoryStream(); upload.InputStream.CopyTo(fileData); photo.Content = fileData.ToArray(); } db.Photos.Add(photo); await db.SaveChangesAsync(); return(RedirectToAction("Index")); } ViewBag.BeachId = new SelectList(db.Beaches, "BeachId", "Description", photo.BeachId); return(View(photo)); }
public async Task <IActionResult> OnPostAsync() { var newPhoto = new Photo(); if (await TryUpdateModelAsync( newPhoto, nameof(Photo), // Prefix for form value. s => s.Title, s => s.Description, s => s.EncodedData)) { Context.Photos.Add(newPhoto); await Context.SaveChangesAsync(); return(RedirectToPage("/Index")); } return(Page()); }