Пример #1
0
        public async Task <User> LogInAsync(User user)
        {
            User loggedUser = (await database.SelectAsync <User, User>(u => u.UserName == user.UserName)).FirstOrDefault();

            if (loggedUser == null || !loggedUser.Password.Equals(await encrypt.HashEncryptAsync(user.Password ?? throw new NullReferenceException("密码为空"), Convert.FromBase64String(loggedUser.Salt))))
            {
                throw new Exception("用户名或密码错误");
            }

            return(loggedUser);
        }
Пример #2
0
        public IObservable <ISBNInfoGetDataPackage> GetISBNInfo(IObservable <ISBNInfoGetDataPackage> getStream)
        {
            // 先查询一遍数据库
            IObservable <ISBNInfoGetDataPackage> databaseResult = getStream
                                                                  .Where(isbnGet => !string.IsNullOrWhiteSpace(isbnGet.ISBN))
                                                                  .Select(isbnGet =>
                                                                          Observable.FromAsync(async() =>
            {
                try
                {
                    isbnGet.ISBNInfo = (await database.SelectAsync <ISBNInfo, ISBNInfo>(isbn => isbn.ISBN == isbnGet.ISBN)).FirstOrDefault();
                }
                catch (Exception ex)
                {
                    isbnGet.Exceptions.Add(ex);
                }
                return(isbnGet);
            })
                                                                          ).Merge();
            // 把查到的筛选出来打包成最终结果的一部分
            IObservable <ISBNInfoGetDataPackage> getDbResult = databaseResult
                                                               .Where(isbnGet => isbnGet.ISBNInfo != null);
            // 把查不到的拿去用API查
            IObservable <ISBNInfoGetDataPackage> httpResult = httpISBNInfo.GetISBNInfo(databaseResult
                                                                                       .Where(isbnGet => isbnGet.ISBNInfo == null));
            // 把两次都查不到的筛选出来打包成最终结果的一部分
            IObservable <ISBNInfoGetDataPackage> getBadResult = httpResult
                                                                .Where(isbnGet => isbnGet.ISBNInfo == null);
            // 把API查到的筛选出来存一遍数据库, 再打包成最终结果的一部分
            IObservable <ISBNInfoGetDataPackage> getHTTPResult = httpResult
                                                                 .Where(isbnGet => isbnGet.ISBNInfo != null)
                                                                 .Select(isbnGet =>
                                                                         Observable.FromAsync(async() =>
            {
                try
                {
                    await database.InsertAsync(new ISBNInfo[] { isbnGet.ISBNInfo });
                }
                catch (Exception ex)
                {
                    isbnGet.Exceptions.Add(ex);
                }
                return(isbnGet);
            })
                                                                         ).Merge();

            return(Observable.Merge(getDbResult, getBadResult, getHTTPResult));  // 合并全部结果
        }
Пример #3
0
 public async Task <List <Book> > QueryAsync(Expression <Func <Book, bool> > whereExp = null, int pageIndex = 0, int pageSize = 0)
 {
     return(await database.SelectAsync <Book, Book>(whereExp, pageIndex : pageIndex, pageSize : pageSize));
 }
Пример #4
0
        public async Task <BorrowLog> BorrowBookAsync(User userId, Book bookId)
        {
            BorrowLog leaseLog = null;
            Book      bCheck   = (await database.SelectAsync <Book, Book>(b => b.Id == bookId.Id)).FirstOrDefault();
            User      uCheck   = (await database.SelectAsync <User, User>(u => u.Id == userId.Id)).FirstOrDefault();

            _ = (await database.SelectAsync <BorrowLog, BorrowLog>(null, pageIndex: 1, pageSize: 1)).FirstOrDefault();
            if (bCheck == null || uCheck == null)
            {
                throw new Exception("借阅人或目标不合法");
            }

            using IDatabaseTransactionManager transaction = database.StartTransaction();
            try
            {
                await database.ForUpdateAsync <Book, Book>(b => b.Id == bookId.Id, transaction : transaction.GetTransaction());

                Book bSnap = (await database.SelectAsync <Book, Book>(b => b.Id == bookId.Id, transaction: transaction.GetTransaction())).FirstOrDefault();
                if (bSnap == null)
                {
                    throw new Exception("借阅目标不合法");
                }

                if (bSnap.Margin <= 0)
                {
                    throw new Exception("借阅目标数量不足");
                }

                User uSnap = (await database.SelectAsync <User, User>(u => u.Id == userId.Id, transaction: transaction.GetTransaction())).FirstOrDefault();
                if (uSnap == null)
                {
                    throw new Exception("借阅人不合法");
                }

                List <BorrowLog> leaseLogs = await database.SelectAsync <BorrowLog, BorrowLog>(bl => bl.BookId == bookId.Id && bl.UserId == userId.Id && bl.GiveBack == null, transaction : transaction.GetTransaction());

                if (leaseLogs.Count > 0)
                {
                    throw new Exception("已有未归还的同名同书借阅记录");
                }

                int accreditedDays = credit.GetAccreditedDays(uSnap);
                if (accreditedDays == 0)
                {
                    throw new Exception("借阅人信誉过低");
                }

                if (await database.UpdateAsync <Book, int>(book => book.Margin - 1, book => book.Id == bSnap.Id, transaction: transaction.GetTransaction()) == 0)
                {
                    throw new Exception("更新书本库存失败");
                }

                leaseLog = new BorrowLog {
                    BookId = bSnap.Id, UserId = uSnap.Id, Deadline = DateTime.Today.AddDays(accreditedDays)
                };
                if (await database.InsertAsync(new BorrowLog[] { leaseLog }, transaction: transaction.GetTransaction()) == 0)
                {
                    throw new Exception("创建借阅记录失败");
                }

                transaction.Commit();
            }
            catch (Exception)
            {
                transaction.Rollback();
                throw;
            }
            return(leaseLog);
        }