예제 #1
0
        public async Task <IActionResult> CancelAppointing(string barcode)
        {
            if (ModelState.IsValid)
            {
                StudentInfo student = _lendingInfoDbContext.Students.Include(s => s.KeepingBooks).ThenInclude(k => k.Book).ThenInclude(b => b.Appointments)
                                      .FirstOrDefault(s => s.UserName == User.Identity.Name);
                if (student.AppointingBookBarCode == barcode)
                {
                    student.AppointingBookBarCode = null;
                    AppointmentOrLending targetAppointment = student.KeepingBooks.FirstOrDefault(a => a.BookId == barcode);
                    student.KeepingBooks.Remove(targetAppointment);
                    Book targetBook = targetAppointment.Book;

                    targetBook.AppointedLatestTime = targetBook.Appointments.Any(a => a.AppointingDateTime.HasValue) ?
                                                     targetBook.Appointments.Max(a => a.AppointingDateTime) : null;

                    await _lendingInfoDbContext.SaveChangesAsync();

                    TempData["message"] = "取消成功";
                }
            }
            return(RedirectToAction("PersonalInfo"));
        }
예제 #2
0
        public async Task <IActionResult> EditLendingInfo([Bind("BarCode,ISBN,BorrowTime,KeeperId,AppointedLatestTime,State")] Book book)
        {
            if (ModelState.IsValid)
            {
                if (book.BorrowTime > DateTime.Now)
                {
                    ModelState.AddModelError("", "请检查外借时间");
                    return(View(book));
                }
                if (book.AppointedLatestTime.HasValue)
                {
                    if (book.AppointedLatestTime < DateTime.Now)
                    {
                        ModelState.AddModelError("", "请检查预约时间");
                        return(View(book));
                    }

                    if (book.KeeperId == null)
                    {
                        ModelState.AddModelError("", "不存在该学生");
                        return(View(book));
                    }
                }

                StudentInfo student = await _lendingInfoDbContext.Students.Include(s => s.KeepingBooks).FirstOrDefaultAsync(s => s.UserName == book.KeeperId);

                Book addedBook = _lendingInfoDbContext.Books
                                 .Include(b => b.Keeper).ThenInclude(k => k.KeepingBooks)
                                 .FirstOrDefault(b => b.BarCode == book.BarCode);
                if (addedBook == null)
                {
                    return(RedirectToAction("Books", new { isbn = book.ISBN }));
                }

                StudentInfo          preStudent    = addedBook.Keeper;
                AppointmentOrLending targetLending =
                    preStudent?.KeepingBooks.FirstOrDefault(b => b.BookId == addedBook.BarCode);

                addedBook.AppointedLatestTime = book.AppointedLatestTime;
                addedBook.State      = book.State;
                addedBook.BorrowTime = book.BorrowTime;
                addedBook.MatureTime = null;

                preStudent?.KeepingBooks.Remove(targetLending);

                if (addedBook.BorrowTime.HasValue)
                {
                    if (book.KeeperId == null)
                    {
                        ModelState.AddModelError("", "请检查借阅者");
                        return(View(book));
                    }

                    if (student == null)
                    {
                        ModelState.AddModelError("", "不存在该学生");
                        return(View(book));
                    }
                    if (student != null)
                    {
                        if (student.KeepingBooks.Count >= student.MaxBooksNumber)
                        {
                            TempData["message"] = "该学生借书已超过上限";
                        }

                        addedBook.State = BookState.Borrowed;
                        student.KeepingBooks.Add(new AppointmentOrLending()
                        {
                            BookId    = addedBook.BarCode,
                            StudentId = student.UserName
                        });
                        addedBook.Keeper = student;
                    }
                    addedBook.MatureTime = addedBook.BorrowTime + TimeSpan.FromDays(28);
                }


                TempData["message"] = "保存成功";
                await _lendingInfoDbContext.SaveChangesAsync();

                return(RedirectToAction("Books", new { isbn = book.ISBN }));
            }
            return(View(book));
        }