From b080467c36defc24d42cf944f8ef5c89fe810797 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tommy=20=C3=96man?= Date: Sat, 26 Jan 2019 16:52:47 +0100 Subject: [PATCH] Camelcase on answer from restservice --- Vidly/App_Start/MappingProfile.cs | 19 ++++++++++++++ Vidly/App_Start/WebApiConfig.cs | 8 +++++- Vidly/Controllers/Api/CustomersController.cs | 27 +++++++++++--------- Vidly/Dtos/CustomerDto.cs | 25 ++++++++++++++++++ Vidly/Global.asax.cs | 3 +++ Vidly/Vidly.csproj | 5 ++++ Vidly/packages.config | 1 + 7 files changed, 75 insertions(+), 13 deletions(-) create mode 100644 Vidly/App_Start/MappingProfile.cs create mode 100644 Vidly/Dtos/CustomerDto.cs diff --git a/Vidly/App_Start/MappingProfile.cs b/Vidly/App_Start/MappingProfile.cs new file mode 100644 index 0000000..a71f8f7 --- /dev/null +++ b/Vidly/App_Start/MappingProfile.cs @@ -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(); + Mapper.CreateMap(); + } + } +} \ No newline at end of file diff --git a/Vidly/App_Start/WebApiConfig.cs b/Vidly/App_Start/WebApiConfig.cs index 137a257..289ef54 100644 --- a/Vidly/App_Start/WebApiConfig.cs +++ b/Vidly/App_Start/WebApiConfig.cs @@ -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( diff --git a/Vidly/Controllers/Api/CustomersController.cs b/Vidly/Controllers/Api/CustomersController.cs index dbb4863..283fb1c 100644 --- a/Vidly/Controllers/Api/CustomersController.cs +++ b/Vidly/Controllers/Api/CustomersController.cs @@ -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 GetCustomers() + public IEnumerable GetCustomers() { - return _context.Customers.ToList(); + return _context.Customers.ToList().Select(Mapper.Map); } // 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); } // 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); _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, customerInDb); + Mapper.Map(customerDto, customerInDb); _context.SaveChanges(); diff --git a/Vidly/Dtos/CustomerDto.cs b/Vidly/Dtos/CustomerDto.cs new file mode 100644 index 0000000..c22e94a --- /dev/null +++ b/Vidly/Dtos/CustomerDto.cs @@ -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; } + } +} \ No newline at end of file diff --git a/Vidly/Global.asax.cs b/Vidly/Global.asax.cs index 79465d6..f518713 100644 --- a/Vidly/Global.asax.cs +++ b/Vidly/Global.asax.cs @@ -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()); GlobalConfiguration.Configure(WebApiConfig.Register); AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); diff --git a/Vidly/Vidly.csproj b/Vidly/Vidly.csproj index 02633ce..b9592f1 100644 --- a/Vidly/Vidly.csproj +++ b/Vidly/Vidly.csproj @@ -45,6 +45,9 @@ 4 + + ..\packages\AutoMapper.4.1.0\lib\net45\AutoMapper.dll + @@ -180,6 +183,7 @@ + @@ -189,6 +193,7 @@ + Global.asax diff --git a/Vidly/packages.config b/Vidly/packages.config index 60aff95..e5e9b48 100644 --- a/Vidly/packages.config +++ b/Vidly/packages.config @@ -1,6 +1,7 @@  +