Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using App.Api;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using TravelPlanner;
- namespace App.Api.Repository
- {
- public interface ICustomerRepository
- {
- IList<Customer> Get();
- Customer Get(Guid id);
- Customer GetByEmail(String email);
- void Insert(Customer model, Guid id);
- void Update(Customer model);
- void Delete(Customer model);
- }
- public class CustomerRepository : ICustomerRepository
- {
- private readonly newCustomerFactory Factory;
- private readonly UserEntity User;
- public CustomerRepository(DataContext context)
- {
- if (context == null)
- {
- throw new ArgumentNullException("context");
- }
- Factory = new newCustomerFactory(context);
- User = context.User;
- }
- public void Delete(Customer model)
- {
- newCustomerEntity entity = Factory.LoadByPrimaryKey(model.id.Value);
- if (entity != null)
- {
- Factory.Delete(entity);
- }
- }
- public IList<Customer> Get()
- {
- return Factory
- .LoadAll()
- .Select(customer => Convert(customer))
- .OrderBy(customer => customer.name)
- .ToList();
- }
- public Customer Get(Guid id)
- {
- return Convert(Factory.LoadByPrimaryKey(id));
- }
- public Customer GetByEmail(String email)
- {
- return Convert(Factory.Load(newCustomer.Email, new[] { email }).First);
- }
- public void Insert(Customer model, Guid id)
- {
- newCustomerEntity entity = new newCustomerEntity();
- entity.Id = id;
- entity.Name = model.name;
- entity.Firstname = model.firstName;
- entity.Lastname = model.lastName;
- entity.Address = model.address;
- entity.Email = model.email;
- entity.Password = model.password;
- entity.Phonenumber = model.phoneNumber;
- entity.New();
- Factory.Save(entity, User);
- }
- // Id, Name, FirstName, LastName, Address, Email
- public void Update(Customer model)
- {
- if (model.id.HasValue)
- {
- newCustomerEntity entity = Factory.LoadByPrimaryKey(model.id.Value);
- if (entity != null)
- {
- entity.Name = model.name;
- entity.Firstname = model.firstName;
- entity.Lastname = model.lastName;
- entity.Address = model.address;
- entity.Email = model.email;
- entity.Password = model.password;
- entity.Phonenumber = model.phoneNumber;
- Factory.Save(entity, User);
- }
- }
- }
- private Customer Convert(newCustomerEntity entity)
- {
- if (entity == null)
- {
- return null;
- }
- Customer model = new Customer();
- model.id = entity.Id;
- model.name = entity.Name;
- model.firstName = entity.Firstname;
- model.lastName = entity.Lastname;
- model.address = entity.Address;
- model.email = entity.Email;
- model.password = entity.Password;
- model.phoneNumber = entity.Phonenumber;
- return model;
- }
- }
- }
- ===================================================================
- using App.Api;
- using App.Api.Controllers;
- using App.Api.Repository;
- using App.Web.Api.Filters;
- using System;
- using System.Net;
- using System.Web.Http;
- using TravelPlanner;
- namespace App.Api.Controllers
- {
- public class CustomerController : BaseController
- {
- private ICustomerRepository Repository { get; set; }
- protected override void Initialize(DataContext context)
- {
- Repository = new CustomerRepository(context);
- base.Initialize(context);
- }
- public CustomerController() { }
- public CustomerController(ICustomerRepository repository)
- {
- this.Repository = repository;
- }
- [HttpPost]
- [AllowAnonymous]
- [Route("api/customers/authenticate")]
- public IHttpActionResult Authenticate(Customer model)
- {
- Customer entity = Repository.GetByEmail(model.email);
- if (entity != null)
- {
- if(entity.password.Equals(model.password))
- {
- return Ok(entity);
- }
- }
- return NotFound();
- }
- [HttpGet]
- [AllowAnonymous]
- [Route("api/customers")]
- public IHttpActionResult Get()
- {
- return Ok(Repository.Get());
- }
- [HttpGet]
- [Route("api/customers/{id}", Name = "SingleCustomer")]
- public IHttpActionResult Get(Guid id)
- {
- Customer entity = Repository.Get(id);
- if (entity != null)
- {
- return Ok(entity);
- }
- return NotFound();
- }
- [HttpPost]
- [AllowAnonymous]
- [Route("api/customers")]
- public IHttpActionResult Post(Customer model)
- {
- Guid id = Guid.NewGuid();
- Repository.Insert(model, id);
- return CreatedAtRoute("SingleCustomer", new { id = id }, model);
- }
- [HttpPut]
- [ValidateModelState]
- [Route("api/customers/{id}")]
- public IHttpActionResult Put(Customer model)
- {
- Repository.Update(model);
- return Content(HttpStatusCode.Accepted, model);
- }
- [HttpDelete]
- [ValidateModelState]
- public IHttpActionResult Delete(Customer model)
- {
- Repository.Delete(model);
- return Ok(model);
- }
- }
- }
- ==================================================================================================
- using App.Api.Controllers;
- using App.Api.Repository;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- using Moq;
- using System;
- using System.Net;
- using System.Web.Http;
- using System.Web.Http.Results;
- namespace App.Api.Tests
- {
- [TestClass]
- public class CustomerControllerTest
- {
- Guid guid;
- Customer customer;
- [TestInitialize]
- public void before()
- {
- guid = new Guid();
- customer = new Customer { id = guid, name = "customer_name" };
- }
- [TestMethod]
- public void GetReturnsCustomerWithSameId()
- {
- var mockRepository = new Mock<ICustomerRepository>();
- mockRepository.Setup(x => x.Get(guid))
- .Returns(new Customer { id = guid });
- var controller = new CustomerController(mockRepository.Object);
- IHttpActionResult actionResult = controller.Get(guid);
- var contentResult = actionResult as OkNegotiatedContentResult<Customer>;
- Assert.IsNotNull(contentResult);
- Assert.IsNotNull(contentResult.Content);
- Assert.AreEqual(guid, contentResult.Content.id);
- }
- [TestMethod]
- public void GetReturnsNotFound()
- {
- var mockRepository = new Mock<ICustomerRepository>();
- var controller = new CustomerController(mockRepository.Object);
- IHttpActionResult actionResult = controller.Get(guid);
- Assert.IsInstanceOfType(actionResult, typeof(NotFoundResult));
- }
- [TestMethod]
- public void DeleteReturnsOk()
- {
- var mockRepository = new Mock<ICustomerRepository>();
- var controller = new CustomerController(mockRepository.Object);
- IHttpActionResult actionResult = controller.Delete(customer);
- Assert.IsInstanceOfType(actionResult, typeof(OkNegotiatedContentResult<Customer>));
- }
- [TestMethod]
- public void PostMethodSetsLocationHeader()
- {
- var mockRepository = new Mock<ICustomerRepository>();
- var controller = new CustomerController(mockRepository.Object);
- IHttpActionResult actionResult = controller.Post(customer);
- var contentResult = actionResult as CreatedAtRouteNegotiatedContentResult<Customer>;
- /*
- Assert.IsNotNull(contentResult);
- Assert.AreEqual("SingleCustomer", contentResult.RouteName);
- Assert.AreEqual(guid, contentResult.RouteValues["id"]);
- */
- }
- [TestMethod]
- public void PutReturnsContentResult()
- {
- var mockRepository = new Mock<ICustomerRepository>();
- var controller = new CustomerController(mockRepository.Object);
- IHttpActionResult actionResult = controller.Put(customer);
- var contentResult = actionResult as NegotiatedContentResult<Customer>;
- Assert.IsNotNull(contentResult);
- Assert.AreEqual(HttpStatusCode.Accepted, contentResult.StatusCode);
- Assert.IsNotNull(contentResult.Content);
- Assert.AreEqual(guid, contentResult.Content.id);
- }
- [TestCleanup]
- public void after()
- {
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement