public async Task <IActionResult> Post([FromBody] Person person) { if (!ModelState.IsValid) { return(BadRequest(string.Join(Environment.NewLine, ModelState.Values.SelectMany(x => x.Errors).Select(x => x.ErrorMessage)))); } var created = _context.Persons.Add(person); if (created.Entity == null) { return(BadRequest("Error while adding user to database")); } try { _context.SaveChanges(); ICloudBlobService cloudBlobService = new CloudBlobService(); ICollection <Face> faceBlobs = await cloudBlobService.UploadFaceBlobAsync(created.Entity.Id, created.Entity.Id.ToString() + _containerNamePrefix, person.Photos); foreach (Face blob in faceBlobs) { var faceBlobCreated = _context.Faces.Add(blob); if (faceBlobCreated.Entity == null) { return(BadRequest("Error while adding face to database")); } } _context.SaveChanges(); } catch (Exception ex) { return(BadRequest("Error: " + ex.Message + Environment.NewLine + Environment.NewLine + ex.StackTrace + Environment.NewLine + Environment.NewLine + ex.Source)); } return(Ok(created.Entity)); }
public async Task <IActionResult> Put([FromBody] Person person, [FromRoute] int id) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } try { var updated = _context.Persons.Update(person); if (updated.Entity == null) { return(BadRequest("Error while updating.")); } ICloudBlobService cloudBlobService = new CloudBlobService(); ICollection <Face> faceBlobs = await cloudBlobService.UploadFaceBlobAsync(id, id.ToString() + _containerNamePrefix, person.Photos); foreach (Face blob in faceBlobs) { var faceBlobCreated = _context.Faces.Add(blob); if (faceBlobCreated.Entity == null) { return(BadRequest("Error while adding face to database")); } } _context.SaveChanges(); } catch (Exception ex) { return(BadRequest("Error: " + ex.Message + Environment.NewLine + Environment.NewLine + ex.StackTrace + Environment.NewLine + Environment.NewLine + ex.Source)); } return(Ok()); }