public string CheckOutBook(int bookID, int readerID) { using (var db = new favlEntities()) { var book = db.Books.Find(bookID); if (book == null) { return("Book not found"); } var reader = db.Readers.Find(readerID); if (reader == null) { return("Reader not found"); } book.CheckedOutTo = readerID; book.CheckedOutDate = DateTime.UtcNow; book.TotalCheckouts += 1; reader.TotalCheckouts += 1; db.CheckOuts.Add(new CheckOut() { BookID = bookID, ReaderID = readerID, LibraryID = book.LibraryID, CheckOutDate = DateTime.UtcNow }); db.SaveChanges(); return("ok"); } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { return; } using (var db = new favlEntities()) { var barcode = Request.Form["Barcode"].Trim(); db.Books.Add(new Book { Title = Request.Form["Title"].Trim(), AuthorFirst = Request.Form["AuthorFirst"].Trim(), AuthorMiddle = Request.Form["AuthorMiddle"].Trim(), AuthorLast = Request.Form["AuthorLast"].Trim(), Barcode = string.IsNullOrEmpty(barcode) ? null : barcode + " (EAN_13)", LibraryID = int.Parse(Request.Form["LibraryID"]) }); db.SaveChanges(); } Response.Redirect("books.aspx"); }
public Book ReturnBook(int bookID) { using (var db = new favlEntities()) { var book = db.Books.Find(bookID); if (book == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } book.CheckedOutTo = null; book.CheckedOutDate = null; db.SaveChanges(); return(new Book { Id = book.Id, Title = book.Title, AuthorFirst = book.AuthorFirst, AuthorMiddle = book.AuthorMiddle, AuthorLast = book.AuthorLast, Barcode = book.Barcode }); } }
public Reader AddReader([FromBody] Reader reader) { using (var db = new favlEntities()) { if (string.IsNullOrEmpty(reader.Barcode)) { reader.Barcode = null; } else { // throw error if another reader has same barcode if (db.Readers.FirstOrDefault(r => r.Barcode == reader.Barcode) != null) { throw new HttpResponseException(HttpStatusCode.Conflict); } } var addedReader = db.Readers.Add(reader); db.SaveChanges(); return(new Reader { Id = addedReader.Id, FirstName = addedReader.FirstName, MiddleName = addedReader.MiddleName, LastName = addedReader.LastName, Barcode = addedReader.Barcode, TotalCheckouts = addedReader.TotalCheckouts }); } }
protected void Page_Load(object sender, EventArgs e) { if (string.IsNullOrEmpty(Request.QueryString["id"])) { Response.Redirect("books.aspx"); return; } var bookId = int.Parse(Request.QueryString["id"]); using (var db = new favlEntities()) { var book = db.Books.Find(bookId); if (book != null) { if (IsPostBack) { var barcode = Request.Form["Barcode"].Trim(); book.Title = Request.Form["Title"].Trim(); book.AuthorFirst = Request.Form["AuthorFirst"].Trim(); book.AuthorMiddle = Request.Form["AuthorMiddle"].Trim(); book.AuthorLast = Request.Form["AuthorLast"].Trim(); book.Barcode = string.IsNullOrEmpty(barcode) ? null : barcode + " (EAN_13)"; book.LibraryID = int.Parse(Request.Form["LibraryID"]); db.SaveChanges(); Response.Redirect("books.aspx"); return; } var originalBarcode = book.Barcode; if (!string.IsNullOrEmpty(originalBarcode) && originalBarcode.Length > 13) { originalBarcode = originalBarcode.Substring(0, 13); } var s = new HtmlGenericControl("script") { InnerHtml = "var book = " + JsonConvert.SerializeObject( new Book { Id = book.Id, Title = book.Title, AuthorFirst = book.AuthorFirst, AuthorMiddle = book.AuthorMiddle, AuthorLast = book.AuthorLast, Barcode = originalBarcode ?? string.Empty, LibraryID = book.LibraryID }) }; insertBook.Controls.Add(s); } } ; }
protected void Page_Load(object sender, EventArgs e) { if (string.IsNullOrEmpty(Request.QueryString["id"])) { Response.Redirect("librarians.aspx"); return; } var librarianId = int.Parse(Request.QueryString["id"]); using (var db = new favlEntities()) { var librarian = db.Librarians.Find(librarianId); if (librarian != null) { if (IsPostBack) { var barcode = Request.Form["Barcode"].Trim(); librarian.FirstName = Request.Form["FirstName"].Trim(); librarian.LastName = Request.Form["LastName"].Trim(); librarian.Username = Request.Form["Username"].Trim(); librarian.Barcode = string.IsNullOrEmpty(barcode) ? null : barcode + " (CODE_128)"; librarian.LibraryID = int.Parse(Request.Form["LibraryID"]); db.SaveChanges(); Response.Redirect("librarians.aspx"); return; } var originalBarcode = librarian.Barcode; if (!string.IsNullOrEmpty(originalBarcode) && originalBarcode.Length > 7) { originalBarcode = originalBarcode.Substring(0, 7); } var s = new HtmlGenericControl("script") { InnerHtml = "var librarian = " + JsonConvert.SerializeObject( new Librarian { Id = librarian.Id, FirstName = librarian.FirstName, LastName = librarian.LastName, Username = librarian.Username, Barcode = originalBarcode ?? string.Empty, LibraryID = librarian.LibraryID }) }; insertLibrarian.Controls.Add(s); } } ; }
protected void Page_Load(object sender, EventArgs e) { if (string.IsNullOrEmpty(Request.QueryString["id"]) || !((Master as Admin)?.IsGod ?? false)) { Response.Redirect("libraries.aspx"); return; } var libraryId = int.Parse(Request.QueryString["id"]); using (var db = new favlEntities()) { var library = db.Libraries.Find(libraryId); if (library == null) { Response.Redirect("libraries.aspx"); return; } foreach (var reader in db.Readers.Where(r => r.LibraryID == library.Id).ToList()) { db.Readers.Remove(reader); } foreach (var book in db.Books.Where(b => b.LibraryID == library.Id).ToList()) { db.Books.Remove(book); } foreach (var librarian in db.Librarians.Where(l => l.LibraryID == library.Id).ToList()) { db.Librarians.Remove(librarian); } foreach (var checkOut in db.CheckOuts.Where(l => l.LibraryID == library.Id).ToList()) { db.CheckOuts.Remove(checkOut); } foreach (var checkOut in db.CheckOutsByDays.Where(l => l.LibraryID == library.Id).ToList()) { db.CheckOutsByDays.Remove(checkOut); } db.Libraries.Remove(library); db.SaveChanges(); } Response.Redirect("libraries.aspx"); }
public void DeleteReader(int userID) { using (var db = new favlEntities()) { var reader = db.Readers.Find(userID); if (reader == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } db.Readers.Remove(reader); db.SaveChanges(); } }
protected void Page_Load(object sender, EventArgs e) { var sb = new StringBuilder(); int.TryParse(Request.QueryString["libraryID"], out int libraryId); using (var db = new favlEntities()) { var books = db.Books.Where(b => libraryId == 0 || b.LibraryID == libraryId) .OrderBy(b => b.Id) .Take(ROWS * COLUMNS) .ToList(); var corrected = 0; for (var i = 0; i < books.Count; ++i) { corrected += CorrectBarCode(books[i]); var x = i % COLUMNS == 0 ? LEFT_MARGIN : LEFT_MARGIN + CARD_WIDTH + GUTTER; var row = i / COLUMNS; var y = TOP_MARGIN + row * CARD_HEIGHT; var BookTitle = HttpUtility.HtmlEncode(rxBarcodeSuffix.Replace(books[i].Title, string.Empty)); var BookAuthor = HttpUtility.HtmlEncode( $"{books[i].AuthorFirst} {books[i].AuthorMiddle} {books[i].AuthorLast}".Trim()); var LibraryName = HttpUtility.HtmlEncode(books[i].Library.Name); var BookBarcode = HttpUtility.HtmlEncode(rxBarcodeSuffix.Replace(books[i].Barcode, string.Empty)); sb.AppendLine($@"<div class='card' style='top: {y:0.00}in; left: {x:0.00}in;'><table><tr><td> <h1>{BookTitle}</h1> <h2>{BookAuthor}</h2> <h3>{LibraryName}</h3> </td></tr></table><div class='barcode'><svg data-barcode='{ BookBarcode }' xmlns='http://www.w3.org/2000/svg' version='1.1'></svg></div></div>"); } if (corrected > 0) { db.SaveChanges(); } } insertCards.InnerHtml = sb.ToString(); }
protected void Page_Load(object sender, EventArgs e) { if (string.IsNullOrEmpty(Request.QueryString["id"])) { Response.Redirect("libraries.aspx"); return; } var libraryId = int.Parse(Request.QueryString["id"]); using (var db = new favlEntities()) { var library = db.Libraries.Find(libraryId); if (library != null) { if (IsPostBack) { library.Name = Request.Form["Name"].Trim(); library.Village = Request.Form["Village"].Trim(); library.Country = Request.Form["Country"].Trim(); db.SaveChanges(); Response.Redirect("libraries.aspx"); return; } var s = new HtmlGenericControl("script") { InnerHtml = "var library = " + JsonConvert.SerializeObject( new Library { Id = library.Id, Name = library.Name, Village = library.Village, Country = library.Country }) }; insertLibrary.Controls.Add(s); } } ; }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { return; } if (Request.Form["Password"] != Request.Form["Password2"]) { passwordError.Visible = true; return; } passwordError.Visible = false; using (var db = new favlEntities()) { string hash, salt; PW.Encrypt(Request.Form["Password"], out hash, out salt); var barcode = Request.Form["Barcode"].Trim(); var librarian = new Librarian { FirstName = Request.Form["FirstName"].Trim(), LastName = Request.Form["LastName"].Trim(), Username = Request.Form["Username"].Trim(), IsAdmin = false, PasswordHash = hash, PasswordSalt = salt, Barcode = string.IsNullOrEmpty(barcode) ? null : barcode + " (CODE_128)", LibraryID = int.Parse(Request.Form["LibraryID"]) }; db.Librarians.Add(librarian); db.SaveChanges(); } Response.Redirect("librarians.aspx"); }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { return; } using (var db = new favlEntities()) { db.Libraries.Add(new Library { Name = Request.Form["Name"], Village = Request.Form["Village"], Country = Request.Form["Country"] }); db.SaveChanges(); } Response.Redirect("libraries.aspx"); }
public Reader EditReader(int userID, [FromBody] Reader reader) { using (var db = new favlEntities()) { var existingReader = db.Readers.Find(userID); if (existingReader == null) { throw new HttpResponseException(HttpStatusCode.NotFound); } // check if new barcode is different from existing barcode if (existingReader.Barcode != reader.Barcode && !string.IsNullOrEmpty(reader.Barcode)) { // throw error if another reader has same barcode if (db.Readers.FirstOrDefault(r => r.Barcode == reader.Barcode) != null) { throw new HttpResponseException(HttpStatusCode.Conflict); } } existingReader.FirstName = reader.FirstName; existingReader.MiddleName = reader.MiddleName; existingReader.LastName = reader.LastName; existingReader.Barcode = string.IsNullOrEmpty(reader.Barcode) ? null : reader.Barcode; existingReader.LibraryID = reader.LibraryID; db.SaveChanges(); return(new Reader { Id = existingReader.Id, FirstName = existingReader.FirstName, MiddleName = existingReader.MiddleName, LastName = existingReader.LastName, Barcode = existingReader.Barcode, TotalCheckouts = existingReader.TotalCheckouts }); } }
protected void Page_Load(object sender, EventArgs e) { if (string.IsNullOrEmpty(Request.QueryString["id"])) { Response.Redirect("librarians.aspx"); return; } var librarianId = int.Parse(Request.QueryString["id"]); using (var db = new favlEntities()) { var librarian = db.Librarians.Find(librarianId); if (librarian != null) { db.Librarians.Remove(librarian); db.SaveChanges(); } } ; Response.Redirect("librarians.aspx"); }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { return; } using (var db = new favlEntities()) { var barcode = Request.Form["Barcode"].Trim(); db.Readers.Add(new Reader { FirstName = Request.Form["ReaderFirst"].Trim(), MiddleName = Request.Form["ReaderMiddle"].Trim(), LastName = Request.Form["ReaderLast"].Trim(), Barcode = string.IsNullOrEmpty(barcode) ? null : barcode + " (CODE_128)", LibraryID = int.Parse(Request.Form["LibraryID"]) }); db.SaveChanges(); } Response.Redirect("readers.aspx"); }
public Book AddBook([FromBody] Book book) { using (var db = new favlEntities()) { if (string.IsNullOrEmpty(book.Barcode)) { book.Barcode = string.Empty; } var addedBook = db.Books.Add(book); db.SaveChanges(); return(new Book { Id = addedBook.Id, AuthorFirst = addedBook.AuthorFirst, AuthorMiddle = addedBook.AuthorMiddle, AuthorLast = addedBook.AuthorLast, Barcode = addedBook.Barcode, LibraryID = addedBook.LibraryID, }); } }
public string TestCheckOutBook(int bookID, int readerID) { using (var db = new favlEntities()) { var book = db.Books.Find(bookID); if (book == null) { return("Book not found"); } var reader = db.Readers.Find(readerID); if (reader == null) { return("Reader not found"); } book.CheckedOutTo = readerID; var random = new Random(); var bookCheckedOutDate = DateTime.UtcNow.AddMinutes(-Math.Floor(random.NextDouble() * 21600)); book.CheckedOutDate = bookCheckedOutDate; book.TotalCheckouts += 1; reader.TotalCheckouts += 1; db.CheckOuts.Add(new CheckOut() { BookID = bookID, ReaderID = readerID, LibraryID = book.LibraryID, CheckOutDate = bookCheckedOutDate }); db.SaveChanges(); return("ok"); } }