Ejemplo n.º 1
0
 public Model.Reader GetReader(int readerId)
 {
     using (LibDataContext lib = new LibDataContext(ConnectionString)) {
         Reader readerEntity = lib.Readers.Where(r => r.Id == readerId).FirstOrDefault();
         return(new Model.Reader(readerId, readerEntity.FirstName, readerEntity.LastName, readerEntity.Books.Count));
     }
 }
Ejemplo n.º 2
0
 public void AddBook(Model.Book book)
 {
     using (LibDataContext lib = new LibDataContext(ConnectionString)) {
         lib.Books.InsertOnSubmit(new Book(book.IdNumber, (int)book.CatalogId, (int)book.ReaderId));
         lib.SubmitChanges();
     }
 }
Ejemplo n.º 3
0
 public Model.Book GetBook(int bookId)
 {
     using (LibDataContext lib = new LibDataContext(ConnectionString)) {
         Book bookEntity = lib.Books.Where(b => b.IdNumber == bookId).FirstOrDefault();
         return(new Model.Book(bookEntity.CatalogId, bookEntity.ReaderId, bookId));
     }
 }
Ejemplo n.º 4
0
 public Model.Catalog GetCatalog(int catalogId)
 {
     using (LibDataContext lib = new LibDataContext(ConnectionString)) {
         Catalog catalogEntity = lib.Catalogs.Where(c => c.Id == catalogId).FirstOrDefault();
         return(new Model.Catalog(catalogEntity.Id, catalogEntity.Author, catalogEntity.Title, catalogEntity.Books.Count));
     }
 }
Ejemplo n.º 5
0
 public void DeleteBook(int bookId)
 {
     using (LibDataContext lib = new LibDataContext(ConnectionString)) {
         Book bookEntity = lib.Books.Where(b => b.IdNumber == bookId).FirstOrDefault();
         lib.Books.DeleteOnSubmit(bookEntity);
         lib.SubmitChanges();
     }
 }
Ejemplo n.º 6
0
 public void DeleteCatalog(int catalogId)
 {
     using (LibDataContext lib = new LibDataContext(ConnectionString)) {
         Catalog catalogEntity = lib.Catalogs.Where(c => c.Id == catalogId).FirstOrDefault();
         lib.Catalogs.DeleteOnSubmit(catalogEntity);
         lib.SubmitChanges();
     }
 }
Ejemplo n.º 7
0
 public void DeleteReader(int readerId)
 {
     using (LibDataContext lib = new LibDataContext(ConnectionString)) {
         Reader readerEntity = lib.Readers.Where(r => r.Id == readerId).FirstOrDefault();
         lib.Readers.DeleteOnSubmit(readerEntity);
         lib.SubmitChanges();
     }
 }
Ejemplo n.º 8
0
 public void UpdateBook(Model.Book book)
 {
     using (LibDataContext lib = new LibDataContext(ConnectionString)) {
         Book bookEntity = lib.Books.Where(b => b.IdNumber == book.IdNumber).FirstOrDefault();
         bookEntity.CatalogId = book.CatalogId;
         bookEntity.ReaderId  = book.ReaderId;
         lib.SubmitChanges();
     }
 }
Ejemplo n.º 9
0
 public void DeleteMaxBook()
 {
     using (LibDataContext lib = new LibDataContext(ConnectionString)) {
         int  maxId      = lib.Books.Max(b => b.IdNumber);
         Book bookEntity = lib.Books.Where(b => b.IdNumber == maxId).FirstOrDefault();
         lib.Books.DeleteOnSubmit(bookEntity);
         lib.SubmitChanges();
     }
 }
Ejemplo n.º 10
0
        public IEnumerable <Model.Reader> GetAllReaders()
        {
            using (LibDataContext lib = new LibDataContext(ConnectionString)) {
                List <Reader>       readerEntities = lib.Readers.ToList();
                List <Model.Reader> readerModels   = new List <Model.Reader>();

                foreach (Reader readerEntity in readerEntities)
                {
                    readerModels.Add(new Model.Reader(readerEntity.Id, readerEntity.FirstName, readerEntity.LastName, readerEntity.Books.Count));
                }
                return(readerModels);
            }
        }
Ejemplo n.º 11
0
 public IEnumerable <Model.Catalog> GetAllCatalogs()
 {
     using (LibDataContext lib = new LibDataContext(ConnectionString)) {
         List <Catalog>       catalogEntities = lib.Catalogs.ToList();
         List <Model.Catalog> catalogModels   = new List <Model.Catalog>();
         foreach (Catalog catalogEntity in catalogEntities)
         {
             List <Book> bookEntities = catalogEntity.Books.Where(b => b.ReaderId == null).ToList();
             catalogModels.Add(new Model.Catalog(catalogEntity.Author, catalogEntity.Title, bookEntities.Count));
         }
         return(catalogModels);
     }
 }
Ejemplo n.º 12
0
 public void AddCatalog(Model.Catalog catalog)
 {
     using (LibDataContext lib = new LibDataContext(ConnectionString)) {
         ValidationResult authorValidation = varcharValidator.Validate(catalog.Author, null);
         if (!authorValidation.IsValid)
         {
             return;
         }
         ValidationResult titleValidation = varcharValidator.Validate(catalog.Title, null);
         if (!titleValidation.IsValid)
         {
             return;
         }
         lib.Catalogs.InsertOnSubmit(new Catalog(catalog.CatalogId, catalog.Author, catalog.Title));
         lib.SubmitChanges();
     }
 }
Ejemplo n.º 13
0
 public void AddReader(Model.Reader reader)
 {
     using (LibDataContext lib = new LibDataContext(ConnectionString)) {
         ValidationResult firstNameValidation = varcharValidator.Validate(reader.FirstName, null);
         if (!firstNameValidation.IsValid)
         {
             return;
         }
         ValidationResult lastNameValidation = varcharValidator.Validate(reader.LastName, null);
         if (!lastNameValidation.IsValid)
         {
             return;
         }
         lib.Readers.InsertOnSubmit(new Reader(reader.Id, reader.FirstName, reader.LastName));
         lib.SubmitChanges();
     }
 }
Ejemplo n.º 14
0
 public bool UserCanRentBook(string author, string title)
 {
     using (LibDataContext lib = new LibDataContext(ConnectionString)) {
         Catalog catalogEntity = (from _catalog in lib.Catalogs
                                  where _catalog.Author == author && _catalog.Title == title
                                  select _catalog).SingleOrDefault();
         if (catalogEntity != null)
         {
             Book bookEntity = catalogEntity.Books.Where(b => b.ReaderId == null).FirstOrDefault();
             if (bookEntity != null)
             {
                 return(true);
             }
         }
         return(false);
     }
 }
Ejemplo n.º 15
0
 public Model.Book GetBook(string author, string title)
 {
     using (LibDataContext lib = new LibDataContext(ConnectionString)) {
         Catalog catalogEntity = (from _catalog in lib.Catalogs
                                  where _catalog.Author == author && _catalog.Title == title
                                  select _catalog).SingleOrDefault();
         if (catalogEntity != null)
         {
             Book bookEntity = catalogEntity.Books.FirstOrDefault();
             return(bookEntity != null ? new Model.Book(bookEntity.CatalogId, bookEntity.IdNumber) : null);
         }
         else
         {
             return(null);
         }
     }
 }
Ejemplo n.º 16
0
 public IEnumerable <Model.Catalog> GetReadersCatalogs(int readerId)
 {
     using (LibDataContext lib = new LibDataContext(ConnectionString)) {
         IEnumerable <Catalog> catalogEntities = (from _book in lib.Books
                                                  where _book.ReaderId == readerId
                                                  select _book.Catalog).Distinct();
         Reader readerEntity = (from _reader in lib.Readers
                                where _reader.Id == readerId
                                select _reader).SingleOrDefault();
         List <Model.Catalog> catalogModels = new List <Model.Catalog>();
         foreach (Catalog catalogEntity in catalogEntities)
         {
             List <Book> bookEntities = catalogEntity.Books.Where(b => b.ReaderId == readerEntity.Id).ToList();
             catalogModels.Add(new Model.Catalog(catalogEntity.Author, catalogEntity.Title, bookEntities.Count));
         }
         return(catalogModels);
     }
 }
Ejemplo n.º 17
0
 public void UpdateReader(Model.Reader reader)
 {
     using (LibDataContext lib = new LibDataContext(ConnectionString)) {
         ValidationResult firstNameValidation = varcharValidator.Validate(reader.FirstName, null);
         if (!firstNameValidation.IsValid)
         {
             return;
         }
         ValidationResult lastNameValidation = varcharValidator.Validate(reader.LastName, null);
         if (!lastNameValidation.IsValid)
         {
             return;
         }
         Reader readerEntity = lib.Readers.Where(r => r.Id == reader.Id).FirstOrDefault();
         readerEntity.FirstName = reader.FirstName;
         readerEntity.LastName  = reader.LastName;
         lib.SubmitChanges();
     }
 }
Ejemplo n.º 18
0
 public void UpdateCatalog(Model.Catalog catalog)
 {
     using (LibDataContext lib = new LibDataContext(ConnectionString)) {
         ValidationResult authorValidation = varcharValidator.Validate(catalog.Author, null);
         if (!authorValidation.IsValid)
         {
             return;
         }
         ValidationResult titleValidation = varcharValidator.Validate(catalog.Title, null);
         if (!titleValidation.IsValid)
         {
             return;
         }
         Catalog catalogEntity = lib.Catalogs.Where(c => c.Id == catalog.CatalogId).FirstOrDefault();
         catalogEntity.Author = catalog.Author;
         catalogEntity.Title  = catalog.Title;
         lib.SubmitChanges();
     }
 }
Ejemplo n.º 19
0
 public bool UserCanReturnBook(string author, string title, int readerId)
 {
     using (LibDataContext lib = new LibDataContext(ConnectionString)) {
         Catalog catalogEntity = (from _catalog in lib.Catalogs
                                  where _catalog.Author == author && _catalog.Title == title
                                  select _catalog).SingleOrDefault();
         Reader readerEntity = (from _reader in lib.Readers
                                where _reader.Id == readerId
                                select _reader).SingleOrDefault();
         if (catalogEntity != null && readerEntity != null)
         {
             Book bookEntity = readerEntity.Books.FirstOrDefault(b => b.Catalog == catalogEntity);
             if (bookEntity != null)
             {
                 return(true);
             }
         }
         return(false);
     }
 }
Ejemplo n.º 20
0
        public void RentBook(string author, string title, int readerId)
        {
            using (LibDataContext lib = new LibDataContext(ConnectionString)) {
                Catalog catalogEntity = (from _catalog in lib.Catalogs
                                         where _catalog.Author == author && _catalog.Title == title
                                         select _catalog).SingleOrDefault();

                Reader readerEntity = lib.Readers.Where(r => r.Id == readerId).SingleOrDefault();

                if (catalogEntity != null && readerEntity != null)
                {
                    Book bookEntity = catalogEntity.Books.Where(b => b.ReaderId == null && b.CatalogId == catalogEntity.Id).FirstOrDefault();
                    if (bookEntity != null)
                    {
                        bookEntity.ReaderId = readerEntity.Id;
                        lib.SubmitChanges();
                    }
                }
            }
        }