Advertisement
Guest User

Untitled

a guest
Jan 9th, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.59 KB | None | 0 0
  1. using App.Api;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using TravelPlanner;
  6.  
  7. namespace App.Api.Repository
  8. {
  9. public interface ICustomerRepository
  10. {
  11. IList<Customer> Get();
  12. Customer Get(Guid id);
  13. Customer GetByEmail(String email);
  14. void Insert(Customer model, Guid id);
  15. void Update(Customer model);
  16. void Delete(Customer model);
  17. }
  18.  
  19. public class CustomerRepository : ICustomerRepository
  20. {
  21. private readonly newCustomerFactory Factory;
  22. private readonly UserEntity User;
  23.  
  24. public CustomerRepository(DataContext context)
  25. {
  26. if (context == null)
  27. {
  28. throw new ArgumentNullException("context");
  29. }
  30. Factory = new newCustomerFactory(context);
  31. User = context.User;
  32. }
  33.  
  34.  
  35. public void Delete(Customer model)
  36. {
  37. newCustomerEntity entity = Factory.LoadByPrimaryKey(model.id.Value);
  38. if (entity != null)
  39. {
  40. Factory.Delete(entity);
  41. }
  42. }
  43.  
  44. public IList<Customer> Get()
  45. {
  46. return Factory
  47. .LoadAll()
  48. .Select(customer => Convert(customer))
  49. .OrderBy(customer => customer.name)
  50. .ToList();
  51. }
  52.  
  53. public Customer Get(Guid id)
  54. {
  55. return Convert(Factory.LoadByPrimaryKey(id));
  56. }
  57.  
  58. public Customer GetByEmail(String email)
  59. {
  60. return Convert(Factory.Load(newCustomer.Email, new[] { email }).First);
  61. }
  62.  
  63. public void Insert(Customer model, Guid id)
  64. {
  65. newCustomerEntity entity = new newCustomerEntity();
  66. entity.Id = id;
  67. entity.Name = model.name;
  68. entity.Firstname = model.firstName;
  69. entity.Lastname = model.lastName;
  70. entity.Address = model.address;
  71. entity.Email = model.email;
  72. entity.Password = model.password;
  73. entity.Phonenumber = model.phoneNumber;
  74. entity.New();
  75. Factory.Save(entity, User);
  76. }
  77.  
  78.  
  79. // Id, Name, FirstName, LastName, Address, Email
  80. public void Update(Customer model)
  81. {
  82. if (model.id.HasValue)
  83. {
  84. newCustomerEntity entity = Factory.LoadByPrimaryKey(model.id.Value);
  85. if (entity != null)
  86. {
  87. entity.Name = model.name;
  88. entity.Firstname = model.firstName;
  89. entity.Lastname = model.lastName;
  90. entity.Address = model.address;
  91. entity.Email = model.email;
  92. entity.Password = model.password;
  93. entity.Phonenumber = model.phoneNumber;
  94. Factory.Save(entity, User);
  95. }
  96. }
  97. }
  98.  
  99. private Customer Convert(newCustomerEntity entity)
  100. {
  101. if (entity == null)
  102. {
  103. return null;
  104. }
  105. Customer model = new Customer();
  106. model.id = entity.Id;
  107. model.name = entity.Name;
  108. model.firstName = entity.Firstname;
  109. model.lastName = entity.Lastname;
  110. model.address = entity.Address;
  111. model.email = entity.Email;
  112. model.password = entity.Password;
  113. model.phoneNumber = entity.Phonenumber;
  114. return model;
  115. }
  116. }
  117. }
  118.  
  119. ===================================================================
  120.  
  121. using App.Api;
  122. using App.Api.Controllers;
  123. using App.Api.Repository;
  124. using App.Web.Api.Filters;
  125. using System;
  126. using System.Net;
  127. using System.Web.Http;
  128. using TravelPlanner;
  129.  
  130. namespace App.Api.Controllers
  131. {
  132. public class CustomerController : BaseController
  133. {
  134. private ICustomerRepository Repository { get; set; }
  135. protected override void Initialize(DataContext context)
  136. {
  137. Repository = new CustomerRepository(context);
  138. base.Initialize(context);
  139. }
  140.  
  141. public CustomerController() { }
  142.  
  143. public CustomerController(ICustomerRepository repository)
  144. {
  145. this.Repository = repository;
  146. }
  147.  
  148. [HttpPost]
  149. [AllowAnonymous]
  150. [Route("api/customers/authenticate")]
  151. public IHttpActionResult Authenticate(Customer model)
  152. {
  153. Customer entity = Repository.GetByEmail(model.email);
  154. if (entity != null)
  155. {
  156. if(entity.password.Equals(model.password))
  157. {
  158. return Ok(entity);
  159. }
  160. }
  161. return NotFound();
  162. }
  163.  
  164. [HttpGet]
  165. [AllowAnonymous]
  166. [Route("api/customers")]
  167. public IHttpActionResult Get()
  168. {
  169. return Ok(Repository.Get());
  170. }
  171.  
  172. [HttpGet]
  173. [Route("api/customers/{id}", Name = "SingleCustomer")]
  174. public IHttpActionResult Get(Guid id)
  175. {
  176. Customer entity = Repository.Get(id);
  177. if (entity != null)
  178. {
  179. return Ok(entity);
  180. }
  181. return NotFound();
  182. }
  183.  
  184. [HttpPost]
  185. [AllowAnonymous]
  186. [Route("api/customers")]
  187. public IHttpActionResult Post(Customer model)
  188. {
  189. Guid id = Guid.NewGuid();
  190. Repository.Insert(model, id);
  191. return CreatedAtRoute("SingleCustomer", new { id = id }, model);
  192. }
  193.  
  194.  
  195. [HttpPut]
  196. [ValidateModelState]
  197. [Route("api/customers/{id}")]
  198. public IHttpActionResult Put(Customer model)
  199. {
  200. Repository.Update(model);
  201. return Content(HttpStatusCode.Accepted, model);
  202. }
  203.  
  204. [HttpDelete]
  205. [ValidateModelState]
  206. public IHttpActionResult Delete(Customer model)
  207. {
  208. Repository.Delete(model);
  209. return Ok(model);
  210. }
  211. }
  212. }
  213.  
  214. ==================================================================================================
  215.  
  216. using App.Api.Controllers;
  217. using App.Api.Repository;
  218. using Microsoft.VisualStudio.TestTools.UnitTesting;
  219. using Moq;
  220. using System;
  221. using System.Net;
  222. using System.Web.Http;
  223. using System.Web.Http.Results;
  224.  
  225. namespace App.Api.Tests
  226. {
  227. [TestClass]
  228. public class CustomerControllerTest
  229. {
  230.  
  231. Guid guid;
  232. Customer customer;
  233.  
  234. [TestInitialize]
  235. public void before()
  236. {
  237. guid = new Guid();
  238. customer = new Customer { id = guid, name = "customer_name" };
  239. }
  240.  
  241. [TestMethod]
  242. public void GetReturnsCustomerWithSameId()
  243. {
  244. var mockRepository = new Mock<ICustomerRepository>();
  245. mockRepository.Setup(x => x.Get(guid))
  246. .Returns(new Customer { id = guid });
  247.  
  248. var controller = new CustomerController(mockRepository.Object);
  249.  
  250. IHttpActionResult actionResult = controller.Get(guid);
  251. var contentResult = actionResult as OkNegotiatedContentResult<Customer>;
  252.  
  253. Assert.IsNotNull(contentResult);
  254. Assert.IsNotNull(contentResult.Content);
  255. Assert.AreEqual(guid, contentResult.Content.id);
  256. }
  257.  
  258. [TestMethod]
  259. public void GetReturnsNotFound()
  260. {
  261. var mockRepository = new Mock<ICustomerRepository>();
  262. var controller = new CustomerController(mockRepository.Object);
  263.  
  264. IHttpActionResult actionResult = controller.Get(guid);
  265.  
  266. Assert.IsInstanceOfType(actionResult, typeof(NotFoundResult));
  267. }
  268.  
  269. [TestMethod]
  270. public void DeleteReturnsOk()
  271. {
  272. var mockRepository = new Mock<ICustomerRepository>();
  273.  
  274. var controller = new CustomerController(mockRepository.Object);
  275.  
  276. IHttpActionResult actionResult = controller.Delete(customer);
  277.  
  278. Assert.IsInstanceOfType(actionResult, typeof(OkNegotiatedContentResult<Customer>));
  279. }
  280.  
  281. [TestMethod]
  282. public void PostMethodSetsLocationHeader()
  283. {
  284. var mockRepository = new Mock<ICustomerRepository>();
  285. var controller = new CustomerController(mockRepository.Object);
  286.  
  287. IHttpActionResult actionResult = controller.Post(customer);
  288. var contentResult = actionResult as CreatedAtRouteNegotiatedContentResult<Customer>;
  289. /*
  290. Assert.IsNotNull(contentResult);
  291. Assert.AreEqual("SingleCustomer", contentResult.RouteName);
  292. Assert.AreEqual(guid, contentResult.RouteValues["id"]);
  293. */
  294. }
  295.  
  296. [TestMethod]
  297. public void PutReturnsContentResult()
  298. {
  299. var mockRepository = new Mock<ICustomerRepository>();
  300. var controller = new CustomerController(mockRepository.Object);
  301.  
  302. IHttpActionResult actionResult = controller.Put(customer);
  303. var contentResult = actionResult as NegotiatedContentResult<Customer>;
  304.  
  305. Assert.IsNotNull(contentResult);
  306. Assert.AreEqual(HttpStatusCode.Accepted, contentResult.StatusCode);
  307. Assert.IsNotNull(contentResult.Content);
  308. Assert.AreEqual(guid, contentResult.Content.id);
  309. }
  310.  
  311. [TestCleanup]
  312. public void after()
  313. {
  314.  
  315. }
  316.  
  317.  
  318. }
  319. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement