public IHttpActionResult PutSample(int id, Sample sample) { //Check if supplied model is valid. if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != sample.SampleID) { return(BadRequest()); } //Check if there was a previous var oldSample = db.Samples.Find(id); if (oldSample.SampleMP3Blob != null) { if (GetSoundsContainer().GetBlockBlobReference(oldSample.SampleMP3Blob).Exists()) { //Delete the old blob CloudBlockBlob blob = GetSoundsContainer().GetBlockBlobReference(oldSample.SampleMP3Blob); blob.Delete(); } } db.Entry(oldSample).State = EntityState.Detached; //Update the record. db.Entry(sample).State = EntityState.Modified; try { db.SaveChanges(); } catch (DbUpdateConcurrencyException) { if (!SampleExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
public async Task <T> Handle(DeleteEntityCommand <T> request, CancellationToken cancellationToken) { var entity = await _context.Set <T>().SingleOrDefaultAsync(x => x.Id == request.EntityId, cancellationToken); if (entity is null) { return(null); } _context.Attach(entity); _context.Entry(entity).State = EntityState.Deleted; await _context.SaveChangesAsync(cancellationToken); return(entity); }
public async Task <T> Handle(UpdateEntityCommand <T> request, CancellationToken cancellationToken) { var exists = await _context.Set <T>().AnyAsync(x => x.Id == request.EntityId, cancellationToken); if (!exists) { return(null); } var entity = request.Entity; entity.Id = request.EntityId; _context.Attach(entity); _context.Entry(entity).State = EntityState.Modified; await _context.SaveChangesAsync(cancellationToken); return(entity); }