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
using AutoMapper;
using AutomatedLibrarySystem.Domain.DTO;
using AutomatedLibrarySystem.Domain.Services;
using AutomatedLibrarySystem.WebApi.Infrastructure.Mapping;
using AutomatedLibrarySystem.WebApi.Models;
using Newtonsoft.Json.Linq;
using NLog;
using System.Web.Http;
namespace AutomatedLibrarySystem.WebApi.Controllers {
public class BookingController : ApiController {
private bool isDisposed = false;
private IMapper mapper;
private BookService bookService;
private ILogger logger;
public BookingController(BookService bookService) {
this.bookService = bookService;
logger = LogManager.GetCurrentClassLogger();
mapper = AutoMapperWebApiConfiguration.Configure().CreateMapper();
}
[HttpPost]
[Route("api/book/checkout")]
public IHttpActionResult CheckOutBook(int userCardId, int bookId) {
bookService.CheckOutBook(userCardId, bookId);
return Ok();
}
protected override void Dispose(bool disposing) {
if (!isDisposed && disposing) {
bookService.Dispose();
isDisposed = true;
}
base.Dispose(disposing);
}
}
}
public void CheckOutBook(int userCardId, int bookId) {
try {
var bookExtraData = unitOfWork.BookExtraData.GetSingle(x => x.Book.Id == bookId);
VerifyInStockAmount(bookExtraData.InStock - 1, bookExtraData.TotalAmount);
bookExtraData.InStock--;
unitOfWork.BookExtraData.Update(bookExtraData);
var transUserCard = (unitOfWork.UserCards.GetSingle(x => x.Id == userCardId));
transUserCard.Books.Add(unitOfWork.Books.GetSingle(x => x.Id == bookId));
unitOfWork.Commit();
} catch (EntityNotFoundException e) {
throw new BookNotFoundException(e.Message, e);
} catch (AmountOutOfRangeException e) {
throw new AmountOutOfRangeException(e.Message, e);
}
}