using AutoMapper using AutomatedLibrarySyst em Domain DTO using Automa

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using AutoMapper;
using AutomatedLibrarySystem.Domain.DTO;
using AutomatedLibrarySystem.Domain.Services;
using AutomatedLibrarySystem.WebApi.Infrastructure.Mapping;
using AutomatedLibrarySystem.WebApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace AutomatedLibrarySystem.WebApi.Controllers
{
public class UserCardController : ApiController
{
IMapper mapper;
SearchingService searchingService;
RegistrationService registrationService;
//UserCardModel[] userCardModels = new UserCardModel[] {
// new UserCardModel { Id = 1, FirstName = "FirstName1", LastName = "LastName1" },
// new UserCardModel { Id = 2, FirstName = "FirstName2", LastName = "LastName2" },
// new UserCardModel { Id = 3, FirstName = "FirstName3", LastName = "LastName3" },
// new UserCardModel { Id = 4, FirstName = "FirstName4", LastName = "LastName4" }
//};
public UserCardController() {
searchingService = new SearchingService();
registrationService = new RegistrationService();
mapper = AutoMapperWebApiConfiguration.Configure().CreateMapper();
}
[Route("api/UserCards")]
public IEnumerable<UserCardModel> GetAllUserCards() { //TODO Почему-то Влад возвращает IHttpActionResult в аналогичном контроллере в аукционе
return mapper.Map<IEnumerable<UserCardDTO>, IEnumerable<UserCardModel>>(searchingService.GetAllUserCards());
}
[Route("api/UserCard/{id}")]
public IHttpActionResult GetUserCard(int id) {
UserCardModel userCard = mapper.Map<UserCardDTO, UserCardModel>(searchingService.GetUserCard(id));
if (userCard == null) {
return NotFound();
}
return Ok(userCard);
}
[Route("api/UserCard/ByName/{firstName}")]
public IHttpActionResult GetUserCard(string firstName) {
UserCardModel userCard = mapper.Map<UserCardDTO, UserCardModel>(searchingService.GetUserCardByFirstName(firstName));
if (userCard == null) {
return NotFound();
}
return Ok(userCard);
}
}
}