public void Create_User_And_Save() { Library library = new Library(); User user = new User("Johny", "John", "Smith"); library.AddUser(user); IList<User> users = library.GetUsers(); Assert.AreEqual(1, users.Count, "Did not get a new user into the Library"); }
public void CheckOutCopyOf(Book book1, User user) { // remember transactions! // find if book has free copies // if not, throw NoFreeBookException // find copy id # // add new row to the checkouts table, with the proper data //booksRepository.CheckOut(book1, user, TimeSpan.FromDays(14) ); }
public IList<User> GetUsers() { return With.Transaction<IList<User>>(delegate(SqlCommand command) { IList<User> users = new List<User>(); command.CommandType = CommandType.Text; command.CommandText = "SELECT Username, Firstname, Lastname,Id FROM Users"; using (SqlDataReader sdr = command.ExecuteReader()) { while (sdr.Read()) { User user = new User(); user.Username = sdr.GetString(0); user.Firstname = sdr.GetString(1); user.Lastname = sdr.GetString(2); user.ID = sdr.GetInt32(3); users.Add(user); } } return users; }); }
public void Create(User user) { try { With.Transaction(delegate(SqlCommand command) { command.CommandType = System.Data.CommandType.StoredProcedure; command.CommandText = InsertUserSp; command.Parameters.AddWithValue("username", user.Username); command.Parameters.AddWithValue("firstname", user.Firstname); command.Parameters.AddWithValue("lastname", user.Lastname); user.ID = Convert.ToInt32(command.ExecuteScalar()); }); } catch (SqlException e) { switch (e.Number) { case 2627: throw new DuplicateNameException("Username '" + user.Username + "' already exists", e); } throw; } }
public ICollection<Book> GetBooksCheckedOutBy(User user) { return booksRepository.GetBooksCheckedOutBy(user); }
//*** Users ***// public void AddUser(User user) { usersRepository.Create(user); users.Add(user); }
public ICollection<Book> GetBooksCheckedOutBy(User user) { return With.Transaction<ICollection<Book>>(delegate(SqlCommand command) { command.CommandType = System.Data.CommandType.StoredProcedure; command.CommandText = "BookGetAllCheckedOutByUser"; command.Parameters.AddWithValue("userid", user.ID); return GetBooksListFromCommand(command); }); }
/// <summary> /// This is not working! /// Left for home /// </summary> public void CheckOut(Book book, User user, TimeSpan checkOutDuration) { With.Transaction(IsolationLevel.Serializable, delegate(SqlCommand command) { command.CommandType = CommandType.StoredProcedure; command.CommandText = BookFindAvailableCopy; command.Parameters.AddWithValue("isbn", book.ISBN); object value = command.ExecuteScalar(); int copyId = (int)value; command.CommandType = CommandType.Text; command.CommandText = @"INSERT INTO CheckOuts(BookCopyId, UserId, CheckedOutAt, DueDate) VALUES(@copyId, @userId, @checkOutAt, @dueDate)"; command.Parameters.AddWithValue("copyId", copyId); command.Parameters.AddWithValue("userId", user.ID); command.Parameters.AddWithValue("checkOutAt", DateTime.Today); command.Parameters.AddWithValue("dueDate", DateTime.Today.Add(checkOutDuration)); command.ExecuteNonQuery(); }); }
public void Can_Check_A_Book_From_Library() { Library library = CreateTestLibrary(); Book book1 = new Book("1-1231-13", "War & Peace vol 1"); library.AddBook(book1); User user = new User("read-alot", "read", "a lot"); library.AddUser(user); ICollection<Book> books = library.GetBooksCheckedOutBy(user); Assert.AreEqual(0, books.Count); library.CheckOutCopyOf(book1, user); books = library.GetBooksCheckedOutBy(user); Assert.AreEqual(1, books.Count); }