Camelcase on answer from restservice

This commit is contained in:
2019-01-26 16:52:47 +01:00
parent 55405798e6
commit b080467c36
7 changed files with 75 additions and 13 deletions

View 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>();
}
}
}

View File

@ -1,4 +1,6 @@
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
@ -9,6 +11,10 @@ namespace Vidly
{
public static void Register(HttpConfiguration config)
{
var settings = config.Formatters.JsonFormatter.SerializerSettings;
settings.ContractResolver = new CamelCasePropertyNamesContractResolver();
settings.Formatting = Formatting.Indented;
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(

View File

@ -1,9 +1,11 @@
using System;
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Vidly.Dtos;
using Vidly.Models;
namespace Vidly.Controllers.Api
@ -17,37 +19,40 @@ namespace Vidly.Controllers.Api
}
// 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
public Customer GetCustomer(int Id)
public CustomerDto GetCustomer(int Id)
{
var customer = _context.Customers.SingleOrDefault(c => c.Id == Id);
if (customer == null)
throw new HttpResponseException(HttpStatusCode.NotFound);
return customer;
return Mapper.Map<Customer,CustomerDto>(customer);
}
// POST /Api/Customers
[HttpPost]
public Customer CreateCustomer(Customer customer)
public CustomerDto CreateCustomer(CustomerDto customerDto)
{
if (!ModelState.IsValid)
throw new HttpResponseException(HttpStatusCode.BadRequest);
var customer = Mapper.Map<CustomerDto, Customer>(customerDto);
_context.Customers.Add(customer);
_context.SaveChanges();
return customer;
customerDto.Id = customer.Id;
return customerDto;
}
// PUT /Api/Customers/1
[HttpPut]
public void UpdateCustomer(int Id,Customer customer)
public void UpdateCustomer(int Id,CustomerDto customerDto)
{
if (!ModelState.IsValid)
throw new HttpResponseException(HttpStatusCode.BadRequest);
@ -56,10 +61,8 @@ namespace Vidly.Controllers.Api
if (customerInDb == null)
throw new HttpResponseException(HttpStatusCode.NotFound);
customerInDb.Name = customer.Name;
customerInDb.BirthDate = customer.BirthDate;
customerInDb.IsSubscribedToNewsLetter = customer.IsSubscribedToNewsLetter;
customerInDb.MembershipTypeId = customer.MembershipTypeId;
//Mapper.Map<CustomerDto, Customer>(customerDto, customerInDb);
Mapper.Map(customerDto, customerInDb);
_context.SaveChanges();

25
Vidly/Dtos/CustomerDto.cs Normal file
View 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; }
}
}

View File

@ -1,3 +1,4 @@
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
@ -6,6 +7,7 @@ using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
using Vidly.App_Start;
namespace Vidly
{
@ -13,6 +15,7 @@ namespace Vidly
{
protected void Application_Start()
{
Mapper.Initialize(c => c.AddProfile<MappingProfile>());
GlobalConfiguration.Configure(WebApiConfig.Register);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);

View File

@ -45,6 +45,9 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<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="System" />
<Reference Include="System.Data" />
@ -180,6 +183,7 @@
<Compile Include="App_Start\BundleConfig.cs" />
<Compile Include="App_Start\FilterConfig.cs" />
<Compile Include="App_Start\IdentityConfig.cs" />
<Compile Include="App_Start\MappingProfile.cs" />
<Compile Include="App_Start\RouteConfig.cs" />
<Compile Include="App_Start\Startup.Auth.cs" />
<Compile Include="App_Start\WebApiConfig.cs" />
@ -189,6 +193,7 @@
<Compile Include="Controllers\ManageController.cs" />
<Compile Include="Controllers\MoviesController.cs" />
<Compile Include="Controllers\CustomersController.cs" />
<Compile Include="Dtos\CustomerDto.cs" />
<Compile Include="Global.asax.cs">
<DependentUpon>Global.asax</DependentUpon>
</Compile>

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<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="EntityFramework" version="6.2.0" targetFramework="net461" />
<package id="jQuery" version="3.3.1" targetFramework="net461" />