示例#1
0
        private void addStudent(string student)
        {
            using (var contex = new LibraryEntities())
            {
                Student newStudent = new Student()
                {
                    Name = student
                };

                try
                {
                    contex.Students.Add(newStudent);
                    contex.SaveChanges();
                }
                catch (DbEntityValidationException dbEx)
                {
                    Exception raise = dbEx;
                    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    {
                        foreach (var validationError in validationErrors.ValidationErrors)
                        {
                            string message = string.Format("{0}:{1}",
                                                           validationErrors.Entry.Entity.ToString(),
                                                           validationError.ErrorMessage);
                            raise = new InvalidOperationException(message, raise);
                        }
                    }
                    throw raise;
                }
            }
        }
示例#2
0
 internal List <Book> GetBorrowedBooksList()
 {
     using (var contex = new LibraryEntities())
     {
         IQueryable <Book> result = (from b in contex.Books
                                     join bb in contex.BorrowedBooks on b.Id equals bb.BookId
                                     where bb.IsBorrowed == true
                                     select b
                                     );
         return(result.ToList());
     }
 }
示例#3
0
 internal void returnBook(int bookId, string student)
 {
     using (var contex = new LibraryEntities())
     {
         var result = contex.BorrowedBooks.SingleOrDefault(bb => bb.BookId == bookId && bb.IsBorrowed == true);
         if (result != null)
         {
             result.IsBorrowed = false;
             contex.SaveChanges();
         }
     }
 }
示例#4
0
 internal List <Book> BorrowedBookByStudent(string studentName)
 {
     using (var contex = new LibraryEntities())
     {
         IQueryable <Book> result = (from b in contex.Books
                                     join bb in contex.BorrowedBooks on b.Id equals bb.BookId
                                     join s in contex.Students on bb.StudentId equals s.Id
                                     where (s.Name == studentName && bb.IsBorrowed == true)
                                     select b
                                     );
         return(result.ToList());
     }
 }
示例#5
0
        internal void BorrowBook(int id, string studentName)
        {
            using (var contex = new LibraryEntities())
            {
                var studentExist = contex.Students.SingleOrDefault(s => s.Name == studentName);
                if (studentExist == null)
                {
                    addStudent(studentName);
                }

                registerBorrowBook(id, studentName);
            }
        }
示例#6
0
        private void addBorrowedBookRegister(int bookId, int studentId)
        {
            BorrowedBook borrowBookResgister = new BorrowedBook()
            {
                BookId = bookId, StudentId = studentId, IsBorrowed = true
            };

            using (var contex = new LibraryEntities())
            {
                contex.BorrowedBooks.Add(borrowBookResgister);
                contex.SaveChanges();
            }
        }
示例#7
0
        private void registerBorrowBook(int bookId, string studentName)
        {
            using (var contex = new LibraryEntities())
            {
                var result = contex.Books.SingleOrDefault(b => b.Id == bookId);
                if (result != null)
                {
                    result.BorrowedTimes++;
                    contex.SaveChanges();
                }

                var student = contex.Students.SingleOrDefault(s => s.Name == studentName);
                addBorrowedBookRegister(bookId, student.Id);
            }
        }
示例#8
0
 internal List <HistorialInfo> GetHistorialList()
 {
     using (var contex = new LibraryEntities())
     {
         IQueryable <HistorialInfo> result = (from b in contex.Books
                                              join bb in contex.BorrowedBooks on b.Id equals bb.BookId
                                              join s in contex.Students on bb.StudentId equals s.Id
                                              select new HistorialInfo {
             BookId = b.Id,
             Title = b.Title,
             Author = b.Author,
             Genre = b.Genre,
             Year = (int)b.Year,
             StudentName = s.Name,
             TotalTimeBorrowed = (int)b.BorrowedTimes
         });
         return(result.ToList());
     }
 }