Camelcase on answer from restservice
This commit is contained in:
19
Vidly/App_Start/MappingProfile.cs
Normal file
19
Vidly/App_Start/MappingProfile.cs
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
using AutoMapper;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Web;
|
||||||
|
using Vidly.Dtos;
|
||||||
|
using Vidly.Models;
|
||||||
|
|
||||||
|
namespace Vidly.App_Start
|
||||||
|
{
|
||||||
|
public class MappingProfile:Profile
|
||||||
|
{
|
||||||
|
public MappingProfile()
|
||||||
|
{
|
||||||
|
Mapper.CreateMap<Customer, CustomerDto>();
|
||||||
|
Mapper.CreateMap<CustomerDto, Customer>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,4 +1,6 @@
|
|||||||
using System;
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Serialization;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Web.Http;
|
using System.Web.Http;
|
||||||
@ -9,6 +11,10 @@ namespace Vidly
|
|||||||
{
|
{
|
||||||
public static void Register(HttpConfiguration config)
|
public static void Register(HttpConfiguration config)
|
||||||
{
|
{
|
||||||
|
var settings = config.Formatters.JsonFormatter.SerializerSettings;
|
||||||
|
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
|
||||||
|
settings.Formatting = Formatting.Indented;
|
||||||
|
|
||||||
config.MapHttpAttributeRoutes();
|
config.MapHttpAttributeRoutes();
|
||||||
|
|
||||||
config.Routes.MapHttpRoute(
|
config.Routes.MapHttpRoute(
|
||||||
|
|||||||
@ -1,9 +1,11 @@
|
|||||||
using System;
|
using AutoMapper;
|
||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
using System.Web.Http;
|
using System.Web.Http;
|
||||||
|
using Vidly.Dtos;
|
||||||
using Vidly.Models;
|
using Vidly.Models;
|
||||||
|
|
||||||
namespace Vidly.Controllers.Api
|
namespace Vidly.Controllers.Api
|
||||||
@ -17,37 +19,40 @@ namespace Vidly.Controllers.Api
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GET /Api/Customers
|
// GET /Api/Customers
|
||||||
public IEnumerable<Customer> GetCustomers()
|
public IEnumerable<CustomerDto> GetCustomers()
|
||||||
{
|
{
|
||||||
return _context.Customers.ToList();
|
return _context.Customers.ToList().Select(Mapper.Map<Customer,CustomerDto>);
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET /Api/Customers/1
|
// GET /Api/Customers/1
|
||||||
public Customer GetCustomer(int Id)
|
public CustomerDto GetCustomer(int Id)
|
||||||
{
|
{
|
||||||
var customer = _context.Customers.SingleOrDefault(c => c.Id == Id);
|
var customer = _context.Customers.SingleOrDefault(c => c.Id == Id);
|
||||||
if (customer == null)
|
if (customer == null)
|
||||||
throw new HttpResponseException(HttpStatusCode.NotFound);
|
throw new HttpResponseException(HttpStatusCode.NotFound);
|
||||||
return customer;
|
return Mapper.Map<Customer,CustomerDto>(customer);
|
||||||
}
|
}
|
||||||
|
|
||||||
// POST /Api/Customers
|
// POST /Api/Customers
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
public Customer CreateCustomer(Customer customer)
|
public CustomerDto CreateCustomer(CustomerDto customerDto)
|
||||||
{
|
{
|
||||||
if (!ModelState.IsValid)
|
if (!ModelState.IsValid)
|
||||||
throw new HttpResponseException(HttpStatusCode.BadRequest);
|
throw new HttpResponseException(HttpStatusCode.BadRequest);
|
||||||
|
|
||||||
|
var customer = Mapper.Map<CustomerDto, Customer>(customerDto);
|
||||||
_context.Customers.Add(customer);
|
_context.Customers.Add(customer);
|
||||||
_context.SaveChanges();
|
_context.SaveChanges();
|
||||||
|
|
||||||
return customer;
|
customerDto.Id = customer.Id;
|
||||||
|
|
||||||
|
return customerDto;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// PUT /Api/Customers/1
|
// PUT /Api/Customers/1
|
||||||
[HttpPut]
|
[HttpPut]
|
||||||
public void UpdateCustomer(int Id,Customer customer)
|
public void UpdateCustomer(int Id,CustomerDto customerDto)
|
||||||
{
|
{
|
||||||
if (!ModelState.IsValid)
|
if (!ModelState.IsValid)
|
||||||
throw new HttpResponseException(HttpStatusCode.BadRequest);
|
throw new HttpResponseException(HttpStatusCode.BadRequest);
|
||||||
@ -56,10 +61,8 @@ namespace Vidly.Controllers.Api
|
|||||||
if (customerInDb == null)
|
if (customerInDb == null)
|
||||||
throw new HttpResponseException(HttpStatusCode.NotFound);
|
throw new HttpResponseException(HttpStatusCode.NotFound);
|
||||||
|
|
||||||
customerInDb.Name = customer.Name;
|
//Mapper.Map<CustomerDto, Customer>(customerDto, customerInDb);
|
||||||
customerInDb.BirthDate = customer.BirthDate;
|
Mapper.Map(customerDto, customerInDb);
|
||||||
customerInDb.IsSubscribedToNewsLetter = customer.IsSubscribedToNewsLetter;
|
|
||||||
customerInDb.MembershipTypeId = customer.MembershipTypeId;
|
|
||||||
|
|
||||||
_context.SaveChanges();
|
_context.SaveChanges();
|
||||||
|
|
||||||
|
|||||||
25
Vidly/Dtos/CustomerDto.cs
Normal file
25
Vidly/Dtos/CustomerDto.cs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Web;
|
||||||
|
using Vidly.Models;
|
||||||
|
|
||||||
|
namespace Vidly.Dtos
|
||||||
|
{
|
||||||
|
public class CustomerDto
|
||||||
|
{
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
[Required(ErrorMessage = "Please enter customer's name.")]
|
||||||
|
[StringLength(255)]
|
||||||
|
public string Name { get; set; }
|
||||||
|
|
||||||
|
[Min18YearsIfAMember]
|
||||||
|
public DateTime? BirthDate { get; set; }
|
||||||
|
|
||||||
|
public bool IsSubscribedToNewsLetter { get; set; }
|
||||||
|
|
||||||
|
public byte MembershipTypeId { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,3 +1,4 @@
|
|||||||
|
using AutoMapper;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@ -6,6 +7,7 @@ using System.Web.Http;
|
|||||||
using System.Web.Mvc;
|
using System.Web.Mvc;
|
||||||
using System.Web.Optimization;
|
using System.Web.Optimization;
|
||||||
using System.Web.Routing;
|
using System.Web.Routing;
|
||||||
|
using Vidly.App_Start;
|
||||||
|
|
||||||
namespace Vidly
|
namespace Vidly
|
||||||
{
|
{
|
||||||
@ -13,6 +15,7 @@ namespace Vidly
|
|||||||
{
|
{
|
||||||
protected void Application_Start()
|
protected void Application_Start()
|
||||||
{
|
{
|
||||||
|
Mapper.Initialize(c => c.AddProfile<MappingProfile>());
|
||||||
GlobalConfiguration.Configure(WebApiConfig.Register);
|
GlobalConfiguration.Configure(WebApiConfig.Register);
|
||||||
AreaRegistration.RegisterAllAreas();
|
AreaRegistration.RegisterAllAreas();
|
||||||
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
|
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
|
||||||
|
|||||||
@ -45,6 +45,9 @@
|
|||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Reference Include="AutoMapper, Version=4.1.0.0, Culture=neutral, PublicKeyToken=be96cd2c38ef1005, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\packages\AutoMapper.4.1.0\lib\net45\AutoMapper.dll</HintPath>
|
||||||
|
</Reference>
|
||||||
<Reference Include="Microsoft.CSharp" />
|
<Reference Include="Microsoft.CSharp" />
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
<Reference Include="System.Data" />
|
<Reference Include="System.Data" />
|
||||||
@ -180,6 +183,7 @@
|
|||||||
<Compile Include="App_Start\BundleConfig.cs" />
|
<Compile Include="App_Start\BundleConfig.cs" />
|
||||||
<Compile Include="App_Start\FilterConfig.cs" />
|
<Compile Include="App_Start\FilterConfig.cs" />
|
||||||
<Compile Include="App_Start\IdentityConfig.cs" />
|
<Compile Include="App_Start\IdentityConfig.cs" />
|
||||||
|
<Compile Include="App_Start\MappingProfile.cs" />
|
||||||
<Compile Include="App_Start\RouteConfig.cs" />
|
<Compile Include="App_Start\RouteConfig.cs" />
|
||||||
<Compile Include="App_Start\Startup.Auth.cs" />
|
<Compile Include="App_Start\Startup.Auth.cs" />
|
||||||
<Compile Include="App_Start\WebApiConfig.cs" />
|
<Compile Include="App_Start\WebApiConfig.cs" />
|
||||||
@ -189,6 +193,7 @@
|
|||||||
<Compile Include="Controllers\ManageController.cs" />
|
<Compile Include="Controllers\ManageController.cs" />
|
||||||
<Compile Include="Controllers\MoviesController.cs" />
|
<Compile Include="Controllers\MoviesController.cs" />
|
||||||
<Compile Include="Controllers\CustomersController.cs" />
|
<Compile Include="Controllers\CustomersController.cs" />
|
||||||
|
<Compile Include="Dtos\CustomerDto.cs" />
|
||||||
<Compile Include="Global.asax.cs">
|
<Compile Include="Global.asax.cs">
|
||||||
<DependentUpon>Global.asax</DependentUpon>
|
<DependentUpon>Global.asax</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
<packages>
|
<packages>
|
||||||
<package id="Antlr" version="3.5.0.2" targetFramework="net461" />
|
<package id="Antlr" version="3.5.0.2" targetFramework="net461" />
|
||||||
|
<package id="AutoMapper" version="4.1.0" targetFramework="net461" />
|
||||||
<package id="bootstrap" version="3.3.7" targetFramework="net461" />
|
<package id="bootstrap" version="3.3.7" targetFramework="net461" />
|
||||||
<package id="EntityFramework" version="6.2.0" targetFramework="net461" />
|
<package id="EntityFramework" version="6.2.0" targetFramework="net461" />
|
||||||
<package id="jQuery" version="3.3.1" targetFramework="net461" />
|
<package id="jQuery" version="3.3.1" targetFramework="net461" />
|
||||||
|
|||||||
Reference in New Issue
Block a user