Exemple #1
0
        public Book Create(Book book)
        {
            var bookEntity = _context.Add(book);

            _context.SaveChanges();
            return(bookEntity.Entity);
        }
Exemple #2
0
 /// <summary>
 /// Inserts an author and a book into database
 /// </summary>
 /// <param name="authorname">The Name of an author of a book</param>
 /// <param name="bookname">The boook's name</param>
 public void AddAnAuthorAndBook(string authorname, string bookname, string connectionString)
 {
     using (BookLibContext db = new BookLibContext())
     {
         var book   = db.Books.FirstOrDefault(b => b.BookName == bookname);
         var author = db.Authors.FirstOrDefault(a => a.Name == authorname);
         if (book == null)
         {
             if (author == null)
             {
                 Author au = new Author {
                     Name = authorname
                 };
                 Book bo = new Book {
                     BookName = bookname, Authors = au
                 };
                 db.Authors.Add(au);
                 db.Books.Add(bo);
                 db.SaveChanges();
             }
             else
             {
                 Book bo = new Book {
                     BookName = bookname, Authors = author
                 };
                 db.Books.Add(bo);
                 db.SaveChanges();
             }
         }
         else
         {
             throw new DuplicateNameException("There is such book in database");
         }
     }
 }
Exemple #3
0
 /// <summary>
 /// Updates the reading progress of a book
 /// </summary>
 /// <param name="finishpage">The page on which you have ended</param>
 /// <param name="bookname">The name of the book you are reading</param>
 public void UpdateProgress(string finishpage, string bookname, string connectionString)
 {
     using (BookLibContext db = new BookLibContext())
     {
         var progress = db.ReadingProgresses.FirstOrDefault(rp => rp.Books.BookName == bookname);
         if (progress == null)
         {
             throw new Exception("There is no such book in database");
         }
         else
         {
             int result;
             if (int.TryParse(finishpage, out result) | finishpage == "Finished")
             {
                 progress.FinishPage = finishpage;
                 db.ReadingProgresses.Update(progress);
                 db.SaveChanges();
             }
             else
             {
                 throw new Exception("Enter the number, please");
             }
         }
     }
 }
Exemple #4
0
 /// <summary>
 /// Deletes an author from a database with all theirs book
 /// </summary>
 /// <param name="authorname">The name of an author to delete</param>
 public void DeleteAnAuthor(string authorname, string connectionString)
 {
     using (BookLibContext db = new BookLibContext())
     {
         var author = db.Authors.FirstOrDefault(a => a.Name == authorname);
         if (author == null)
         {
             throw new Exception("There is no such author in database");
         }
         else
         {
             db.Authors.Remove(author);
             db.SaveChanges();
         }
     }
 }
Exemple #5
0
 /// <summary>
 /// Deletes a book from a database
 /// </summary>
 /// <param name="bookname">The name of a book to delete</param>
 public void DeleteABook(string bookname, string connectionString)
 {
     using (BookLibContext db = new BookLibContext())
     {
         var book = db.Books.FirstOrDefault(b => b.BookName == bookname);
         if (book == null)
         {
             throw new Exception("There is no such book in database");
         }
         else
         {
             db.Books.Remove(book);
             db.SaveChanges();
         }
     }
 }
Exemple #6
0
 /// <summary>
 /// Updates the name of a book
 /// </summary>
 /// <param name="bookname">The name of book which you want to change></param>
 /// <param name="newbookname">The new name of a book</param>
 public void UpdateBookName(string bookname, string newbookname, string connectionString)
 {
     using (BookLibContext db = new BookLibContext())
     {
         var book = db.Books.FirstOrDefault(a => a.BookName == bookname);
         if (book == null)
         {
             throw new Exception("There is no such book in database");
         }
         else
         {
             book.BookName = newbookname;
             db.Books.Update(book);
             db.SaveChanges();
         }
     }
 }
Exemple #7
0
 public void Create(Library library)
 {
     _bookLibContext.Libraries.Add(library);
     _bookLibContext.SaveChanges();
 }
Exemple #8
0
 public User Register(User user)
 {
     _context.Users.Add(user);
     _context.SaveChanges();
     return(GetUser(user.Login));
 }
Exemple #9
0
        public string ChangeBookingStatus(int bookId, int userId, BookingStatus bookingStatus)
        {
            if (BookingStatus.Booked == bookingStatus)
            {
                if (_context.Queues.Any(x => x.BookingStatus == BookingStatus.Expired && x.UserId == userId))
                {
                    return("У вас есть просроченные сдачи, вы не можете брать новые книги");
                }
                if (_context.Queues.Any(x => x.BookingStatus == BookingStatus.Waiting && x.BookId == bookId))
                {
                    var targetDate = _context
                                     .Queues
                                     .Where(x => x.BookingStatus == BookingStatus.Waiting && x.BookId == bookId)
                                     .OrderByDescending(x => x.Deadline)
                                     .First()
                                     .Deadline
                                     .AddDays(7);
                    _context.Queues.Add(new Queue()
                    {
                        Deadline = targetDate, BookId = bookId, UserId = userId, BookingStatus = BookingStatus.Waiting
                    });
                    _context.SaveChanges();
                    return("Вы успешно заняли место в очереди");
                }
                if (_context.Queues.Any(x => x.BookingStatus == BookingStatus.Expired && x.BookId == bookId))
                {
                    var targetDate = DateTime.Today.AddDays(7);
                    _context.Queues.Add(new Queue()
                    {
                        Deadline = targetDate, BookId = bookId, UserId = userId, BookingStatus = BookingStatus.Waiting
                    });
                    _context.SaveChanges();
                    return("Вы успешно заняли место в очереди");
                }
                if (_context.Queues.Any(x => x.BookingStatus == BookingStatus.Booked && x.BookId == bookId))
                {
                    var targetDate = _context.Queues.FirstOrDefault(x => x.BookingStatus == BookingStatus.Booked && x.BookId == bookId).Deadline;
                    _context.Queues.Add(new Queue()
                    {
                        Deadline = targetDate.AddDays(7), BookId = bookId, UserId = userId, BookingStatus = BookingStatus.Waiting
                    });
                    _context.SaveChanges();
                    return("Вы успешно заняли место в очереди");
                }
                _context.Queues.Add(new Queue()
                {
                    BookId        = bookId,
                    BookingStatus = BookingStatus.Booked,
                    Deadline      = DateTime.Today.AddDays(7),
                    UserId        = userId
                });
                _context.SaveChanges();
                return($"Вы взяли книгу до {DateTime.Today.AddDays(7).ToShortDateString()}");
            }
            if (BookingStatus.Returned == bookingStatus)
            {
                var msg       = "Вы вернули книгу";
                var bookQueue = _context.Queues.Where(x => x.BookId == bookId)
                                .OrderBy(x => x.Deadline)
                                .ToList();
                //находим текущего владельца (статус "На руках" или "Просрочено")
                var current = bookQueue.FirstOrDefault(x => new[] { BookingStatus.Booked, BookingStatus.Expired }.Contains(x.BookingStatus));
                //меняем статус на "Вернул"
                var prevDeadLine = current.Deadline;
                if (current.Deadline != DateTime.Today && current.BookingStatus == BookingStatus.Booked)
                {
                    current.Deadline = DateTime.Today;
                }

                current.BookingStatus = BookingStatus.Returned;

                _context.Queues.Update(current);
                _context.SaveChanges();
                //если текущая дата дедлайна не равна сегодня, нужно причесать другие дедлайны
                if (prevDeadLine != DateTime.Today)
                {
                    //обновляем дедлайны
                    UpdateWaiters(bookId);
                }
                //ищем ожидающих на очереди
                var waitList = bookQueue
                               .Where(x => x.BookingStatus == BookingStatus.Waiting).ToList();
                if (!waitList.Any())
                {
                    return(msg);
                }
                //если таковые есть, то дергаем того, у кого дедлайн ближайший
                var minDeadline = waitList.Min(x => x.Deadline);
                var waiter      = waitList.FirstOrDefault(x => x.Deadline == minDeadline);
                //меняем статус ожидающего на "Забронено"
                waiter.BookingStatus = BookingStatus.Booked;
                _context.Queues.Update(waiter);
                _context.SaveChanges();
                return(msg);
            }
            return(string.Empty);
        }