-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConverterBook.cs
88 lines (77 loc) · 2.89 KB
/
ConverterBook.cs
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
using AutoMapper;
using Core.Constants;
using Core.DTO;
using Core.Entities;
using Core.Interfaces;
using System.Collections.Generic;
namespace Core.Converters
{
public class ConverterBook : IConverter<Book, BookDTO>
{
private IGenreService _genreService;
public ConverterBook(IGenreService genreService)
{
_genreService = genreService;
}
public Book ConvertDTOByModel(BookDTO modelDTO)
{
Book book = new Book()
{
Id = modelDTO.Id,
Name = modelDTO.Name,
Author = modelDTO.Author,
Code = modelDTO.Code,
GenreId = modelDTO.GenreId,
Info = modelDTO.Info,
IsDisplay = modelDTO.IsDisplay,
IsFavorite = modelDTO.IsFavorite,
IsNew = modelDTO.IsNew,
Path = (modelDTO.Path).Replace(PathConstants.PAPH_BOOKS, string.Empty),
Price = modelDTO.Price,
PublishingHouse = modelDTO.PublishingHouse,
YearPublishing = modelDTO.YearPublishing,
YearOfWriting = modelDTO.YearOfWriting
};
return book;
}
public IEnumerable<Book> ConvertDTOsByModels(IEnumerable<BookDTO> modelDTOs)
{
var mapper = new MapperConfiguration(cfg => cfg.CreateMap<BookDTO, Book>()).CreateMapper();
var books = mapper.Map<IEnumerable<BookDTO>, List<Book>>(modelDTOs);
return books;
}
public IEnumerable<BookDTO> ConvertModelsByDTOs(IEnumerable<Book> models)
{
var mapper = new MapperConfiguration(cfg => cfg.CreateMap<Book, BookDTO>()).CreateMapper();
var bookDTOs = mapper.Map<IEnumerable<Book>, List<BookDTO>>(models);
foreach (var bookDTO in bookDTOs)
{
bookDTO.Path = PathConstants.PAPH_BOOKS + bookDTO.Path;
bookDTO.Genre = _genreService.GetGenre(bookDTO.GenreId).Name;
}
return bookDTOs;
}
public BookDTO ConvertModelByDTO(Book model)
{
BookDTO bookDTO = new BookDTO()
{
Id = model.Id,
Name = model.Name,
Author = model.Author,
YearPublishing = model.YearPublishing,
PublishingHouse = model.PublishingHouse,
Price = model.Price,
Path = PathConstants.PAPH_BOOKS + model.Path,
IsNew = model.IsNew,
Code = model.Code,
Genre = _genreService.GetGenre(model.GenreId).Name,
GenreId = model.GenreId,
Info = model.Info,
IsDisplay = model.IsDisplay,
IsFavorite = model.IsFavorite,
YearOfWriting = model.YearOfWriting
};
return bookDTO;
}
}
}