public async Task <IHttpActionResult> PutMymoMessage(int id, MymoMessage mymoMessage) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (id != mymoMessage.MessageID) { return(BadRequest()); } db.Entry(mymoMessage).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!MymoMessageExists(id)) { return(NotFound()); } else { throw; } } return(StatusCode(HttpStatusCode.NoContent)); }
static async Task <Uri> CreateProductAsync(MymoMessage mymoMessage) { HttpResponseMessage response = await client.PostAsJsonAsync( "api/MymoMessage", mymoMessage); response.EnsureSuccessStatusCode(); // return URI of the created resource. return(response.Headers.Location); }
public async Task <IHttpActionResult> GetMymoMessage(int id) { MymoMessage mymoMessage = await db.MymoMessage.FindAsync(id); if (mymoMessage == null) { return(NotFound()); } return(Ok(mymoMessage)); }
static async Task <MymoMessage> GetProductAsync(string path) { MymoMessage mymoMessage = null; HttpResponseMessage response = await client.GetAsync(path); if (response.IsSuccessStatusCode) { mymoMessage = await response.Content.ReadAsAsync <MymoMessage>(); } return(mymoMessage); }
public async Task <IHttpActionResult> PostMymoMessage(MymoMessage mymoMessage) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } db.MymoMessage.Add(mymoMessage); await db.SaveChangesAsync(); return(CreatedAtRoute("DefaultApi", new { id = mymoMessage.MessageID }, mymoMessage)); }
static async Task <MymoMessage> UpdateProductAsync(MymoMessage mymoMessage) { HttpResponseMessage response = await client.PutAsJsonAsync( $"api/MymoMessages/{mymoMessage.MessageID}", mymoMessage); response.EnsureSuccessStatusCode(); // Deserialize the updated product from the response body. mymoMessage = await response.Content.ReadAsAsync <MymoMessage>(); return(mymoMessage); }
public async Task <IHttpActionResult> DeleteMymoMessage(int id) { MymoMessage mymoMessage = await db.MymoMessage.FindAsync(id); if (mymoMessage == null) { return(NotFound()); } db.MymoMessage.Remove(mymoMessage); await db.SaveChangesAsync(); return(Ok(mymoMessage)); }
static async Task RunAsync() { // Update port # in the following line. client.BaseAddress = new Uri("http://localhost:60528/"); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add( new MediaTypeWithQualityHeaderValue("application/json")); try { // Create a new product MymoMessage mymoMessage = new MymoMessage { ReceivedMessage = "Hello Mymo", }; var url = await CreateProductAsync(mymoMessage); Console.WriteLine($"Created at {url}"); // Get the product mymoMessage = await GetProductAsync(url.PathAndQuery); ShowProduct(mymoMessage); // Update the product Console.WriteLine("Updating price..."); mymoMessage.ReceivedMessage = Console.ReadLine(); await UpdateProductAsync(mymoMessage); // Get the updated product mymoMessage = await GetProductAsync(url.PathAndQuery); ShowProduct(mymoMessage); //// Delete the product //var statusCode = await DeleteProductAsync(mymoMessage.MessageID); //Console.WriteLine($"Deleted (HTTP Status = {(int)statusCode})"); } catch (Exception e) { Console.WriteLine(e.Message); } Console.ReadLine(); }
static void ShowProduct(MymoMessage mymoMessage) { Console.WriteLine($"ID: {mymoMessage.MessageID}\tTime: " + $"{mymoMessage.ArrivalTime}\tMessage: {mymoMessage.ReceivedMessage}"); }