public async Task GetRequestInfoByCodeNoData_Test()
        {
            var user = new Users
            {
                Id         = 1,
                UserName   = "******",
                FirstName  = "A",
                MiddleName = "B",
                LastName   = "C"
            };

            _dbContext.Set <Users>().Add(user);

            var userBookRequests = new UserBookRequests
            {
                Id          = 1,
                UserId      = 1,
                RequestCode = "ABCD",
                RequestDate = DateTime.Now,
                User        = user
            };

            _dbContext.Set <UserBookRequests>().Add(userBookRequests);
            await _dbContext.SaveChangesAsync();

            var efRepository = new EfRepository <UserBookRequests>(_dbContext);
            var getRequestInfoByCodeQuery = new GetRequestInfoByCodeQuery(efRepository);
            var result = await getRequestInfoByCodeQuery.ExecuteAsync("ABCDE");

            Assert.Null(result);
        }
Exemplo n.º 2
0
        public async Task <CommandResult> ExecuteAsync()
        {
            var userId = int.Parse(_httpContext?.User?.UserId());

            var slotAvaiable = 10 - (await _userBookRepository.TableNoTracking
                                     .Where(x => x.UserId == userId && (x.Status == (int)BookStatus.Borrowing || x.Status == (int)BookStatus.Pending))
                                     .CountAsync());

            if (slotAvaiable <= 0)
            {
                return(CommandResult.Failed(new CommandResultError()
                {
                    Code = (int)HttpStatusCode.LengthRequired,
                    Description = "Out of slot"
                }));
            }

            var bookCart = await _bookCartRepository.TableNoTracking.Where(x => x.UserId == userId && x.Status == (int)BookStatus.InOrder).ToListAsync();

            var listBookBorrow = await _bookRepository.TableNoTracking.Where(x => bookCart.Select(y => y.BookId).Contains(x.Id)).ToListAsync();

            if (listBookBorrow.Count() > slotAvaiable || listBookBorrow.Any(x => x.AmountAvailable == 0))
            {
                return(CommandResult.Failed(new CommandResultError()
                {
                    Code = (int)HttpStatusCode.LengthRequired,
                    Description = "Out of slot"
                }));
            }

            var listEnity = listBookBorrow.Select(x => new UserBooks
            {
                UserId       = userId,
                BookId       = x.Id,
                Status       = (int)BookStatus.Pending,
                DeadlineDate = DateTime.Now.Date.AddDays(x.MaximumDateBorrow)
            }).ToList();

            UserBookRequests request = new UserBookRequests();

            request.UserId      = userId;
            request.RequestDate = DateTime.Now;
            request.RequestCode = GenerationCode();
            request.UserBooks   = listEnity;

            if (await _bookRequestRepository.InsertAsync(request))
            {
                listBookBorrow.ForEach(book =>
                {
                    book.AmountAvailable = book.AmountAvailable - 1;
                });

                bookCart.ForEach(item =>
                {
                    item.Status = (int)BookStatus.Borrowing;
                });

                await _bookRepository.UpdateAsync(listBookBorrow);

                await _bookCartRepository.UpdateAsync(bookCart);

                return(CommandResult.SuccessWithData(request.RequestCode));
            }

            return(CommandResult.Failed());
        }