public async Task <IActionResult> GiveBookToUser(string UserId, int BookId) { var userNotReturnedBook = _context.TakenBooks.Where(e => e.UserId == UserId && e.Returned == false).Count(); if (userNotReturnedBook == 0) { DateTime localDate = DateTime.Now; TakenBooks take = new TakenBooks(); take.UserId = UserId; take.BookId = BookId; take.TakenDate = localDate; take.Returned = false; Book tempBook = _context.Books.Find(BookId); tempBook.Available = tempBook.Available - 1; _context.Books.Update(tempBook); await _context.TakenBooks.AddAsync(take); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } else { return(RedirectToAction(nameof(Index), new { returned = false })); } }
/* private void UpdateTakenLessons() * { * Table<Lesson> lessons = db.GetTable<Lesson>(); * * * foreach (var item in db.GetTable<Lesson>()) * { * var query = from u in db.GetTable<TakenLessons>() * where u.LessonID == item.ID * select u; * if (query.Count()==0) * { * TakenLessons taken = new TakenLessons(); * taken.LessonID = item.ID; * db.GetTable<TakenLessons>().InsertOnSubmit(taken); * db.SubmitChanges(); * } * } * }*/ public void SetBookToStudent() { GetStudents(); GetBooks(); Console.WriteLine("Enter ID of student");// get student id if (!int.TryParse(Console.ReadLine(), out int stID)) { Console.WriteLine("Uncorrect value"); return; } if (db.GetTable <Student>().SingleOrDefault(x => x.ID == stID) == null) { Console.WriteLine("There is no such student"); return; } Console.WriteLine("Enter ID of book"); if (!int.TryParse(Console.ReadLine(), out int bkID)) { Console.WriteLine("Uncorrect value"); return; } if (db.GetTable <Book>().SingleOrDefault(x => x.ID == bkID) == null) { Console.WriteLine("There is no such book"); return; } TakenBooks taken = new TakenBooks(); taken.BookID = bkID; taken.StudentID = stID; if (db.GetTable <TakenBooks>().Contains(taken)) { Console.WriteLine("Such entry already exists"); } else { Book book = db.GetTable <Book>().Single(x => x.ID == bkID); if (book.Quantity >= 1) { book.Quantity -= 1; db.GetTable <TakenBooks>().InsertOnSubmit(taken); db.SubmitChanges(); } else { Console.WriteLine("We don't have copy of this book"); } } }
public async Task <IActionResult> ReturnBookFromUserAsync(string UserId, int BookId) { TakenBooks takenBook = _context.TakenBooks.Where(e => e.BookId == BookId && e.UserId == UserId && e.Returned == false).First(); DateTime localDate = DateTime.Now; takenBook.Returned = true; takenBook.ReturnDate = localDate; Book tempBook = _context.Books.Find(BookId); tempBook.Available = tempBook.Available + 1; _context.Books.Update(tempBook); _context.TakenBooks.Update(takenBook); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public void ReturnBook(IBookModel book) { TakenBooks.Remove(book.ID); History.Add(new ReadingHistory(book.ID, book.IssueDate, book.ReturnDate)); }
public void TakeBook(IBookModel book) { TakenBooks.Add(book.ID); }
public void BookReturned(BookCopy copy) { TakenBooks.Remove(copy); copy.BookReturned(); }
public void BookTaken(BookCopy copy) { copy.BookTaken(this.ID); TakenBooks.Add(copy); }