public async Task RequestAsync(ReservationMessage message) { var reservation = await _db.Reservations .Include(x => x.Loans) .Include(x => x.Member) .FirstOrDefaultAsync(x => x.Number == message.Number); if (reservation == null) { reservation = new Reservation { RequestDate = DateTime.UtcNow, Status = StatusReservation.Opened, Number = message.Number, Member = await _db.Members.FirstOrDefaultAsync(x => x.DocumentId == message.MemberId) }; if (reservation.Member == null) { reservation.Member = new Member { DocumentId = message.MemberId, Name = message.MemberName }; } reservation.Loans = new List <Loan>(); foreach (var item in message.Items) { var loan = await GetLoanAsync(_db, item, reservation.RequestDate); reservation.Loans.Add(loan); } await _db.AddAsync(reservation); } else { reservation.Loans = new List <Loan>(); foreach (var item in message.Items) { var loan = await GetLoanAsync(_db, item, reservation.RequestDate); reservation.Loans.Add(loan); } _db.Update(reservation); } await _db.SaveChangesAsync(); await SendDtoAsync(reservation.Id); }
public async Task <bool> Handle(ReserveBookCommand request, CancellationToken cancellationToken) { var reservation = await _db.Reservations .Include(x => x.Loans) .Include(x => x.Member) .FirstOrDefaultAsync(x => x.Number == request.Number, cancellationToken); if (reservation == null) { reservation = new Reservation { RequestDate = _dateTimeService.Now(), Status = StatusReservation.Opened, Number = request.Number, Member = await _db.Members.FirstOrDefaultAsync(x => x.DocumentId == request.MemberId, cancellationToken) }; if (reservation.Member == null) { reservation.Member = new Member { DocumentId = request.MemberId, Name = request.MemberName }; } await _db.AddAsync(reservation, cancellationToken); } reservation.Loans = new List <Loan>(); foreach (var item in request.Items) { var loan = new Loan { Copy = await _db.Copies.FirstOrDefaultAsync(x => x.Number == item.Number, cancellationToken), DueDate = reservation.RequestDate.AddDays(7) }; reservation.Loans.Add(loan); } await _db.SaveChangesAsync(cancellationToken); var message = new PublishReservationEventCommand { ReservationId = reservation.Id }; await _bus.SendAsync(ContextNames.Queue.Library, message); return(true); }
public async Task <bool> Handle(UpdateBookCommand request, CancellationToken cancellationToken) { var book = await _db.Books.FirstOrDefaultAsync(x => x.Title == request.Title, cancellationToken); if (book != null) { book.Author = request.Author; book.Language = request.Language; await _db.SaveChangesAsync(cancellationToken); } return(true); }
public async Task UpdateAsync(BookMessage message) { var book = await _db.Books.FirstOrDefaultAsync(x => x.Title == message.Title); if (book != null) { book.Author = message.Author; book.Language = message.Language; _db.Update(book); await _db.SaveChangesAsync(); } }
public async Task <bool> Handle(ExpireReservationCommand request, CancellationToken cancellationToken) { var reservation = await _db.Reservations.FirstOrDefaultAsync(x => x.Id == request.ReservationId, cancellationToken); reservation.Status = StatusReservation.Expired; await _db.SaveChangesAsync(cancellationToken); var message = new PublishReservationEventCommand { ReservationId = reservation.Id }; await _bus.SendAsync(ContextNames.Queue.Library, message); return(true); }