using AutoMapper using AutomatedLibrarySyst em Data Entities using Aut

  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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using AutoMapper;
using AutomatedLibrarySystem.Data.Entities;
using AutomatedLibrarySystem.Data.EntityFramework.Interfaces;
using AutomatedLibrarySystem.Data.Infrastructure.Exceptions;
using AutomatedLibrarySystem.Domain.DTO;
using AutomatedLibrarySystem.Domain.Infrastructure.Exceptions;
using AutomatedLibrarySystem.Domain.Infrastructure.Mapping;
using System;
using System.Collections.Generic;
namespace AutomatedLibrarySystem.Domain.Services {
public class SearchingService : IDisposable {
private bool isDisposed = false;
private IMapper mapper;
private ILibraryUnitOfWork unitOfWork;
public SearchingService(ILibraryUnitOfWork unitOfWork) {
this.unitOfWork = unitOfWork;
mapper = AutoMapperDomainConfiguration.Configure().CreateMapper();
}
#region Book
public BookDTO GetBook(int id) {
try {
var book = mapper.Map<Book, BookDTO>(
unitOfWork.Books.GetSingle(x =>
x.Id == id)
);
return book;
} catch (EntityNotFoundException e) {
throw new BookNotFoundException(e.Message, e);
}
}
public BookDTO GetBook(string title = null, string author = null, string genre = null, DateTime? published = null) {
try {
var book = mapper.Map<Book, BookDTO>(
unitOfWork.Books.GetSingle(x =>
title == null ? true : x.Title == title &&
author == null ? true : x.Author == author &&
genre == null ? true : x.Genre == genre &&
published == null ? true : x.Published == published)
);
return book;
} catch (EntityNotFoundException e) {
throw new BookNotFoundException(e.Message, e);
}
}
public IList<BookDTO> GetBooks() {
try {
var books = mapper.Map<IEnumerable<Book>, IList<BookDTO>>(
unitOfWork.Books.GetAll()
);
return books;
} catch (EntityNotFoundException e) {
throw new BookNotFoundException(e.Message, e);
}
}
public IList<BookDTO> GetBooks(int? id, string title, string author, string genre, DateTime published) {
try {
var books = mapper.Map<IEnumerable<Book>, IList<BookDTO>>(
unitOfWork.Books.FindBy(x =>
id == null ? true : x.Id == id &&
title == null ? true : x.Title == title &&
author == null ? true : x.Author == author &&
genre == null ? true : x.Genre == genre &&
published == null ? true : x.Published == published)
);
return books;
} catch (EntityNotFoundException e) {
throw new BookNotFoundException(e.Message, e);
}
}
#endregion
#region UserCard
public UserCardDTO GetUserCard(int id) {
try {
var userCard = mapper.Map<UserCard, UserCardDTO>(
unitOfWork.UserCards.GetSingle(x =>
x.Id == id)
);
return userCard;
} catch (EntityNotFoundException e) {
throw new UserCardNotFoundException(e.Message, e);
}
}
public UserCardDTO GetUserCard(string firstName, string lastName) {
try {
var userCard = mapper.Map<UserCard, UserCardDTO>(
unitOfWork.UserCards.GetSingle(x =>
x.FirstName == firstName &&
x.LastName == lastName)
);
return userCard;
} catch (EntityNotFoundException e) {
throw new UserCardNotFoundException(e.Message, e);
}
}
public IList<UserCardDTO> GetUserCards() {
try {
var userCards = mapper.Map<IEnumerable<UserCard>, IList<UserCardDTO>>(
unitOfWork.UserCards.GetAll()
);
return userCards;
} catch (EntityNotFoundException e) {
throw new UserCardNotFoundException(e.Message, e);
}
}
public IList<UserCardDTO> GetUserCards(Book book) {
try {
var userCards = mapper.Map<IEnumerable<UserCard>, IList<UserCardDTO>>(
unitOfWork.UserCards.FindBy(x =>
x.Books.Contains(book))
);
return userCards;
} catch (EntityNotFoundException e) {
throw new UserCardNotFoundException(e.Message, e);
}
}
#endregion
#region EmployeeCard
public EmployeeCardDTO GetEmployeeCard(int id) {
try {
var empCard = mapper.Map<EmployeeCard, EmployeeCardDTO>(
unitOfWork.EmployeeCards.GetSingle(x =>
x.Id == id)
);
return empCard;
} catch (EntityNotFoundException e) {
throw new EmployeeCardNotFoundException(e.Message, e);
}
}
public EmployeeCardDTO GetEmployeeCard(string firstName = null, string lastName = null, string position = null) {
try {
var empCard = mapper.Map<EmployeeCard, EmployeeCardDTO>(
unitOfWork.EmployeeCards.GetSingle(x =>
firstName == null ? true : x.FirstName == firstName &&
lastName == null ? true : x.LastName == lastName &&
position == null ? true : x.LastName == lastName)
);
return empCard;
} catch (EntityNotFoundException e) {
throw new EmployeeCardNotFoundException(e.Message, e);
}
}
public IList<EmployeeCardDTO> GetEmployeeCards() {
try {
var empCards = mapper.Map<IEnumerable<EmployeeCard>, IList<EmployeeCardDTO>>(
unitOfWork.EmployeeCards.GetAll()
);
return empCards;
} catch (EntityNotFoundException e) {
throw new EmployeeCardNotFoundException(e.Message, e);
}
}
public IList<EmployeeCardDTO> GetEmployeeCards(string firstName = null, string lastName = null, string position = null) {
try {
var empCards = mapper.Map<IEnumerable<EmployeeCard>, IList<EmployeeCardDTO>>(
unitOfWork.EmployeeCards.FindBy(x =>
firstName == null ? true : x.FirstName == firstName &&
lastName == null ? true : x.LastName == lastName &&
position == null ? true : x.LastName == lastName)
);
return empCards;
} catch (EntityNotFoundException e) {
throw new EmployeeCardNotFoundException(e.Message, e);
}
}
#endregion
public void Dispose() {
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
// GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing) {
if (!isDisposed && disposing) {
unitOfWork.Dispose();
isDisposed = true;
}
}
}
}