Rest service implemented
This commit is contained in:
21
Vidly/App_Start/WebApiConfig.cs
Normal file
21
Vidly/App_Start/WebApiConfig.cs
Normal file
@ -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 }
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
83
Vidly/Controllers/Api/CustomersController.cs
Normal file
83
Vidly/Controllers/Api/CustomersController.cs
Normal file
@ -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<Customer> 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();
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
|
||||
@ -49,12 +49,21 @@
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.Data" />
|
||||
<Reference Include="System.Drawing" />
|
||||
<Reference Include="System.Net.Http.Formatting, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib\net45\System.Net.Http.Formatting.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.DynamicData" />
|
||||
<Reference Include="System.Web.Entity" />
|
||||
<Reference Include="System.Web.ApplicationServices" />
|
||||
<Reference Include="System.ComponentModel.DataAnnotations" />
|
||||
<Reference Include="System.Core" />
|
||||
<Reference Include="System.Data.DataSetExtensions" />
|
||||
<Reference Include="System.Web.Http, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Web.Http.WebHost, Version=5.2.3.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
|
||||
<HintPath>..\packages\Microsoft.AspNet.WebApi.WebHost.5.2.3\lib\net45\System.Web.Http.WebHost.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
<Reference Include="System.Web" />
|
||||
<Reference Include="System.Web.Extensions" />
|
||||
@ -173,7 +182,9 @@
|
||||
<Compile Include="App_Start\IdentityConfig.cs" />
|
||||
<Compile Include="App_Start\RouteConfig.cs" />
|
||||
<Compile Include="App_Start\Startup.Auth.cs" />
|
||||
<Compile Include="App_Start\WebApiConfig.cs" />
|
||||
<Compile Include="Controllers\AccountController.cs" />
|
||||
<Compile Include="Controllers\Api\CustomersController.cs" />
|
||||
<Compile Include="Controllers\HomeController.cs" />
|
||||
<Compile Include="Controllers\ManageController.cs" />
|
||||
<Compile Include="Controllers\MoviesController.cs" />
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
For more information on how to configure your ASP.NET application, please visit
|
||||
https://go.microsoft.com/fwlink/?LinkId=301880
|
||||
@ -6,13 +6,10 @@
|
||||
<configuration>
|
||||
<configSections>
|
||||
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
|
||||
<section name="entityFramework"
|
||||
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
|
||||
requirePermission="false"/>
|
||||
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
|
||||
</configSections>
|
||||
<connectionStrings>
|
||||
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-Vidly-20190115084450.mdf;Initial Catalog=aspnet-Vidly-20190115084450;Integrated Security=True"
|
||||
providerName="System.Data.SqlClient" />
|
||||
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-Vidly-20190115084450.mdf;Initial Catalog=aspnet-Vidly-20190115084450;Integrated Security=True" providerName="System.Data.SqlClient" />
|
||||
<add name="Sqlsvr" connectionString="Data Source=TOMMYASUS\SQLEXPR2017;Initial Catalog=aspVidly;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" providerName="System.Data.SqlClient" />
|
||||
</connectionStrings>
|
||||
<appSettings>
|
||||
@ -30,11 +27,14 @@
|
||||
<modules>
|
||||
<remove name="FormsAuthentication" />
|
||||
<remove name="TelemetryCorrelationHttpModule" />
|
||||
<add name="TelemetryCorrelationHttpModule"
|
||||
type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation"
|
||||
preCondition="integratedMode,managedHandler"/>
|
||||
<add name="TelemetryCorrelationHttpModule" type="Microsoft.AspNet.TelemetryCorrelation.TelemetryCorrelationHttpModule, Microsoft.AspNet.TelemetryCorrelation" preCondition="integratedMode,managedHandler" />
|
||||
</modules>
|
||||
</system.webServer>
|
||||
<handlers>
|
||||
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
|
||||
<remove name="OPTIONSVerbHandler" />
|
||||
<remove name="TRACEVerbHandler" />
|
||||
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
|
||||
</handlers></system.webServer>
|
||||
<runtime>
|
||||
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
|
||||
<dependentAssembly>
|
||||
@ -95,12 +95,8 @@
|
||||
</entityFramework>
|
||||
<system.codedom>
|
||||
<compilers>
|
||||
<compiler language="c#;cs;csharp" extension=".cs"
|
||||
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
|
||||
warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701"/>
|
||||
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
|
||||
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
|
||||
warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"/>
|
||||
<compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:1659;1699;1701" />
|
||||
<compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:default /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" />
|
||||
</compilers>
|
||||
</system.codedom>
|
||||
</configuration>
|
||||
@ -12,6 +12,10 @@
|
||||
<package id="Microsoft.AspNet.Razor" version="3.2.4" targetFramework="net461" />
|
||||
<package id="Microsoft.AspNet.TelemetryCorrelation" version="1.0.0" targetFramework="net461" />
|
||||
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net461" />
|
||||
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net461" />
|
||||
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net461" />
|
||||
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net461" />
|
||||
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net461" />
|
||||
<package id="Microsoft.AspNet.WebPages" version="3.2.4" targetFramework="net461" />
|
||||
<package id="Microsoft.CodeDom.Providers.DotNetCompilerPlatform" version="2.0.0" targetFramework="net461" />
|
||||
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.4" targetFramework="net461" />
|
||||
|
||||
Reference in New Issue
Block a user