public IHttpActionResult PostSample(Sample sample) { //Create a new record if the model is valid. if (!ModelState.IsValid) { return BadRequest(ModelState); } //Save the record to the DB. db.Samples.Add(sample); db.SaveChanges(); return CreatedAtRoute("DefaultApi", new { id = sample.SampleID }, sample); }
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); }
private static async Task Post() { //try to authenticate the user using ad AuthenticationResult result = await getAuthResult(); if (result == null) { await ReturnToMenu(); } else { //Send token in request header. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken); } //Post a new sample Random rand = new Random(); var sample = new Sample() { Title = "Song " + rand.Next(50), Artist = "Craig", DateOfSampleCreation = DateTime.Now }; HttpResponseMessage response = await client.PostAsJsonAsync("api/samples", sample); if (response.IsSuccessStatusCode) { Console.ForegroundColor = ConsoleColor.Green; Console.WriteLine("Successfully Posted new Sample to the api"); } await ReturnToMenu(); }