Ejemplo n.º 1
0
        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 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();
            }
        }