-
-
Save anonymous/43ed2f29f8adfb44cef6 to your computer and use it in GitHub Desktop.
WebApi Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
namespace WebApiTest.Models | |
{ | |
public class Customer | |
{ | |
public int Id { get; set; } | |
public string Name { get; set; } | |
public virtual ICollection<Order> Orders { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data; | |
using System.Data.Entity.Infrastructure; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Web; | |
using System.Web.Http; | |
using WebApiTest.Models; | |
using WebApiTest.Models.DAL; | |
namespace WebApiTest.Controllers | |
{ | |
public class CustomersController : ApiController | |
{ | |
private ModelDbContext db = new ModelDbContext(); | |
// GET api/Customer | |
public IEnumerable<Customer> GetCustomers() | |
{ | |
return db.Customers.AsEnumerable(); | |
} | |
// GET api/Customer/5 | |
public Customer GetCustomer(int id) | |
{ | |
Customer customer = db.Customers.Find(id); | |
if (customer == null) | |
{ | |
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound)); | |
} | |
return customer; | |
} | |
// PUT api/Customer/5 | |
public HttpResponseMessage PutCustomer(int id, Customer customer) | |
{ | |
if (ModelState.IsValid && id == customer.Id) | |
{ | |
db.Entry(customer).State = EntityState.Modified; | |
try | |
{ | |
db.SaveChanges(); | |
} | |
catch (DbUpdateConcurrencyException) | |
{ | |
return Request.CreateResponse(HttpStatusCode.NotFound); | |
} | |
return Request.CreateResponse(HttpStatusCode.OK, customer); | |
} | |
else | |
{ | |
return Request.CreateResponse(HttpStatusCode.BadRequest); | |
} | |
} | |
// POST api/Customer | |
public HttpResponseMessage PostCustomer(Customer customer) | |
{ | |
if (ModelState.IsValid) | |
{ | |
db.Customers.Add(customer); | |
db.SaveChanges(); | |
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, customer); | |
response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = customer.Id })); | |
return response; | |
} | |
else | |
{ | |
return Request.CreateResponse(HttpStatusCode.BadRequest); | |
} | |
} | |
// DELETE api/Customer/5 | |
public HttpResponseMessage DeleteCustomer(int id) | |
{ | |
Customer customer = db.Customers.Find(id); | |
if (customer == null) | |
{ | |
return Request.CreateResponse(HttpStatusCode.NotFound); | |
} | |
db.Customers.Remove(customer); | |
try | |
{ | |
db.SaveChanges(); | |
} | |
catch (DbUpdateConcurrencyException) | |
{ | |
return Request.CreateResponse(HttpStatusCode.NotFound); | |
} | |
return Request.CreateResponse(HttpStatusCode.OK, customer); | |
} | |
protected override void Dispose(bool disposing) | |
{ | |
db.Dispose(); | |
base.Dispose(disposing); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Data.Entity; | |
using System.Data.Metadata.Edm; | |
using System.Linq; | |
using System.Web; | |
namespace WebApiTest.Models.DAL | |
{ | |
public class ModelDbContext : DbContext | |
{ | |
public DbSet<Customer> Customers { get; set; } | |
public DbSet<Order> Orders { get; set; } | |
public ModelDbContext() : base() | |
{ | |
// Just to get some data in there... | |
var c = new Customer() { Id = 1, Name = "Customer 1", Orders = new List<Order>() }; | |
this.Customers.Add(c); | |
c.Orders.Add(new Order() { Id = 1, CustomerId = 1, Description = "Drugs" }); | |
this.SaveChanges(); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Web; | |
namespace WebApiTest.Models | |
{ | |
public class Order | |
{ | |
public int Id { get; set; } | |
public int CustomerId { get; set; } | |
public string Description { get; set; } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment