示例#1
0
        /// <inheritdoc />
        public async Task <bool> ApproveReceiveAsync(int requestId)
        {
            var request = await _requestRepository.GetAll()
                          .Include(x => x.Book)
                          .Include(x => x.User)
                          .Include(x => x.Owner)
                          .FirstOrDefaultAsync(x => x.Id == requestId);

            if (request == null)
            {
                return(false);
            }

            var book = await _bookRepository.FindByIdAsync(request.BookId);

            book.User  = request.User;
            book.State = BookState.Reading;
            _bookRepository.Update(book);
            await _bookRepository.SaveChangesAsync();

            request.ReceiveDate = DateTime.UtcNow;
            _requestRepository.Update(request);
            var affectedRows = await _requestRepository.SaveChangesAsync();

            if (request.Owner.IsEmailAllowed)
            {
                var emailMessage = new RequestMessage()
                {
                    OwnerName    = request.Owner.FirstName + " " + request.Owner.LastName,
                    BookName     = request.Book.Name,
                    RequestId    = request.Id,
                    UserName     = request.User.FirstName + " " + request.User.LastName,
                    OwnerAddress = new MailboxAddress($"{request.Owner.Email}")
                };
                await _emailSenderService.SendThatBookWasReceivedToPreviousOwnerAsync(emailMessage);
            }
            if (request.User.IsEmailAllowed)
            {
                var emailMessage = new RequestMessage()
                {
                    OwnerName    = request.User.FirstName + " " + request.User.LastName,
                    BookName     = request.Book.Name,
                    RequestId    = request.Id,
                    OwnerAddress = new MailboxAddress($"{request.User.Email}")
                };

                await _emailSenderService.SendThatBookWasReceivedToNewOwnerAsync(emailMessage);
            }

            await _notificationsService.NotifyAsync(
                request.Owner.Id,
                $"{request.User.FirstName} {request.User.LastName} has successfully received and started reading '{book.Name}'.",
                book.Id,
                NotificationAction.Open);

            await _notificationsService.NotifyAsync(
                request.User.Id,
                $"You became a current owner of the book '{book.Name}'",
                book.Id,
                NotificationAction.Open);

            await _hangfireJobScheduleService.DeleteRequestScheduleJob(requestId);

            return(affectedRows > 0);
        }