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

@ -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();