diff --git a/Vidly/App_Start/WebApiConfig.cs b/Vidly/App_Start/WebApiConfig.cs new file mode 100644 index 0000000..137a257 --- /dev/null +++ b/Vidly/App_Start/WebApiConfig.cs @@ -0,0 +1,21 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web.Http; + +namespace Vidly +{ + public static class WebApiConfig + { + public static void Register(HttpConfiguration config) + { + config.MapHttpAttributeRoutes(); + + config.Routes.MapHttpRoute( + name: "DefaultApi", + routeTemplate: "api/{controller}/{id}", + defaults: new { id = RouteParameter.Optional } + ); + } + } +} diff --git a/Vidly/Controllers/Api/CustomersController.cs b/Vidly/Controllers/Api/CustomersController.cs new file mode 100644 index 0000000..dbb4863 --- /dev/null +++ b/Vidly/Controllers/Api/CustomersController.cs @@ -0,0 +1,83 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Web.Http; +using Vidly.Models; + +namespace Vidly.Controllers.Api +{ + public class CustomersController : ApiController + { + private ApplicationDbContext _context; + public CustomersController() + { + _context = new ApplicationDbContext(); + } + + // GET /Api/Customers + public IEnumerable GetCustomers() + { + return _context.Customers.ToList(); + } + + // GET /Api/Customers/1 + public Customer GetCustomer(int Id) + { + var customer = _context.Customers.SingleOrDefault(c => c.Id == Id); + if (customer == null) + throw new HttpResponseException(HttpStatusCode.NotFound); + return customer; + } + + // POST /Api/Customers + [HttpPost] + public Customer CreateCustomer(Customer customer) + { + if (!ModelState.IsValid) + throw new HttpResponseException(HttpStatusCode.BadRequest); + + _context.Customers.Add(customer); + _context.SaveChanges(); + + return customer; + + } + + // PUT /Api/Customers/1 + [HttpPut] + public void UpdateCustomer(int Id,Customer customer) + { + if (!ModelState.IsValid) + throw new HttpResponseException(HttpStatusCode.BadRequest); + + var customerInDb = _context.Customers.SingleOrDefault(c => c.Id == Id); + if (customerInDb == null) + throw new HttpResponseException(HttpStatusCode.NotFound); + + customerInDb.Name = customer.Name; + customerInDb.BirthDate = customer.BirthDate; + customerInDb.IsSubscribedToNewsLetter = customer.IsSubscribedToNewsLetter; + customerInDb.MembershipTypeId = customer.MembershipTypeId; + + _context.SaveChanges(); + + } + + // DELETE /Api/Customers/1 + [HttpDelete] + public void DeleteCustomer(int Id) + { + var customerInDb = _context.Customers.SingleOrDefault(c => c.Id == Id); + if (customerInDb == null) + throw new HttpResponseException(HttpStatusCode.NotFound); + + _context.Customers.Remove(customerInDb); + _context.SaveChanges(); + + } + + + } +} diff --git a/Vidly/Global.asax.cs b/Vidly/Global.asax.cs index 80e9206..79465d6 100644 --- a/Vidly/Global.asax.cs +++ b/Vidly/Global.asax.cs @@ -2,6 +2,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Web; +using System.Web.Http; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; @@ -12,6 +13,7 @@ namespace Vidly { protected void Application_Start() { + GlobalConfiguration.Configure(WebApiConfig.Register); AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); diff --git a/Vidly/Vidly.csproj b/Vidly/Vidly.csproj index 13b4c59..02633ce 100644 --- a/Vidly/Vidly.csproj +++ b/Vidly/Vidly.csproj @@ -49,12 +49,21 @@ + + ..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll + + + ..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll + + + ..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.3\lib\net45\System.Web.Http.WebHost.dll + @@ -173,7 +182,9 @@ + + diff --git a/Vidly/Web.config b/Vidly/Web.config index 727b35a..ff71d0b 100644 --- a/Vidly/Web.config +++ b/Vidly/Web.config @@ -1,4 +1,4 @@ - + -
+
- - + + - - - - + + + + - - - + + + - - - + + + - + + + + + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + - + - - + + \ No newline at end of file diff --git a/Vidly/packages.config b/Vidly/packages.config index f4d007a..60aff95 100644 --- a/Vidly/packages.config +++ b/Vidly/packages.config @@ -12,6 +12,10 @@ + + + +