public virtual bool DeleteBookFromCart(Guid cartID, Guid bookID) { using (TitanbraryContainer ctx = new TitanbraryContainer()) { try { //ctx.CartXBooks.Remove(new CartXBook //{ // BookID = bookID, // CartID = cartID //}); var result = ctx.CartXBooks.Where(c => c.CartID == cartID && c.BookID == bookID).FirstOrDefault(); //ctx.Entry(industry).State = System.Data.Entity.EntityState.Deleted; ctx.Entry(result).State = System.Data.Entity.EntityState.Deleted; ctx.SaveChanges(); } catch (Exception ex) { return(false); } } return(true); }
public virtual BookModel GetBookByBookID(Guid bookID) { List <BookModel> result = new List <BookModel>(); using (TitanbraryContainer ctx = new TitanbraryContainer()) { result = ctx.Books.Where(b => b.BookID == bookID).Select(b => new BookModel() { Name = b.Name, Author = b.Author, Publisher = b.Publisher, ISBN = b.ISBN, Edition = b.Edition, Year = b.Year, Quantity = b.Quantity, Language = b.Language, Picture = b.Picture, Keywords = b.Keywords, Active = b.Active, Description = b.Description, Timestamp = b.Timestamp, BookID = b.BookID }).ToList(); } return(result[0]); }
public virtual List <BookModel> SearchBooks(string searchString) { List <BookModel> result = new List <BookModel>(); using (TitanbraryContainer ctx = new TitanbraryContainer()) { result = ctx.Books.Where(b => b.Author.Contains(searchString) || b.Publisher.Contains(searchString) || b.ISBN.Contains(searchString) || b.Language.Contains(searchString) || b.Keywords.Contains(searchString) || b.Name.Contains(searchString) || b.Description.Contains(searchString)).Select(b => new BookModel() { Name = b.Name, Author = b.Author, Publisher = b.Publisher, ISBN = b.ISBN, Edition = b.Edition, Year = b.Year, Quantity = b.Quantity, Language = b.Language, Picture = b.Picture, Keywords = b.Keywords, Active = b.Active, Description = b.Description, Timestamp = b.Timestamp, BookID = b.BookID }).ToList(); } return(result); }
public virtual bool UpdateBook(BookModel book, ref bool isQuantityChanged) { using (TitanbraryContainer ctx = new TitanbraryContainer()) { try { var oldBook = ctx.Books.Where(b => b.BookID == book.BookID).FirstOrDefault(); if (oldBook.Quantity < book.Quantity) { isQuantityChanged = true; } oldBook.Active = book.Active; oldBook.Author = book.Author; oldBook.Description = book.Description; oldBook.Edition = book.Edition; oldBook.ISBN = book.ISBN; oldBook.Keywords = book.Keywords; oldBook.Language = book.Language; oldBook.Name = book.Name; oldBook.Picture = book.Picture; oldBook.Publisher = book.Publisher; oldBook.Quantity = book.Quantity; oldBook.Timestamp = DateTime.UtcNow; oldBook.Year = book.Year; ctx.SaveChanges(); } catch (Exception ex) { return(false); } } return(true); }
public virtual Guid GetUserInWaitlist(Guid bookID) { using (TitanbraryContainer ctx = new TitanbraryContainer()) { Guid userID = ctx.Waitlists.Where(w => w.BookID == bookID).OrderBy(w => w.Date).Select(w => w.UserID).FirstOrDefault(); return(userID); } }
public virtual CartModel GetCart(Guid cartId) { List <CartModel> result = new List <CartModel>(); using (TitanbraryContainer ctx = new TitanbraryContainer()) { result = ctx.Carts.Where(c => c.CartID == cartId && c.Completed == false).Select(c => new CartModel { CartID = c.CartID, UserID = c.UserID, CreatedDate = c.CreatedDate, ModifiedDate = c.ModifiedDate, BookList = c.CartXBooks.Where(cb => cb.CartID == c.CartID).Select(cb => new CartXBookModel { BookID = cb.BookID, Quantity = cb.Quantity }).ToList() }).ToList(); if (result.Count() == 0) { return(null); } foreach (var books in result) { books.Books = new List <BookModel>(); foreach (var book in books.BookList) { var detailedBook = new BookModel(); var b = ctx.Books.Where(bk => bk.BookID == book.BookID).FirstOrDefault(); detailedBook.BookID = b.BookID; detailedBook.Author = b.Author; detailedBook.Publisher = b.Publisher; detailedBook.Edition = b.Edition; detailedBook.Active = b.Active; detailedBook.Description = b.Description; detailedBook.ISBN = b.ISBN; detailedBook.Keywords = b.Keywords; detailedBook.Language = b.Language; detailedBook.Name = b.Name; detailedBook.Quantity = b.Quantity; detailedBook.Year = b.Year; detailedBook.Picture = b.Picture; foreach (var genre in b.Genres) { var g = ctx.Genres.Where(gr => gr.GenreID == genre.GenreID).FirstOrDefault(); detailedBook.GenreID = g.GenreID; } books.Books.Add(detailedBook); } } } return(result[0]); }
public void deleteOldCarts() { using (TitanbraryContainer ctx = new TitanbraryContainer()) { var carts = ctx.Carts.Where(c => c.ModifiedDate <= DateTime.Now.AddDays(-7) && c.Completed == false); foreach (var cart in carts) { ctx.CartXBooks.RemoveRange(ctx.CartXBooks.Where(cb => cb.CartID == cart.CartID)); } ctx.Carts.RemoveRange(carts); } }
public virtual CartModel GetCartByUserID(Guid userID) { var result = new CartModel(); result.BookList = new List <CartXBookModel>(); result.Books = new List <BookModel>(); using (TitanbraryContainer ctx = new TitanbraryContainer()) { var cart = ctx.Carts.Where(c => c.UserID == userID && c.Completed == false).FirstOrDefault(); if (cart == null) { return(null); } result.CartID = cart.CartID; result.UserID = cart.UserID; result.CreatedDate = cart.CreatedDate; result.ModifiedDate = cart.ModifiedDate; foreach (var book in cart.CartXBooks) { var books = new CartXBookModel(); var booksDetail = new BookModel(); books.BookID = book.BookID; books.Quantity = book.Quantity; result.BookList.Add(books); var getBook = ctx.Books.Where(b => b.BookID == book.BookID).FirstOrDefault(); booksDetail.BookID = getBook.BookID; booksDetail.Author = getBook.Author; booksDetail.Publisher = getBook.Publisher; booksDetail.Edition = getBook.Edition; booksDetail.Active = getBook.Active; booksDetail.Description = getBook.Description; booksDetail.ISBN = getBook.ISBN; booksDetail.Keywords = getBook.Keywords; booksDetail.Language = getBook.Language; booksDetail.Name = getBook.Name; booksDetail.Quantity = getBook.Quantity; booksDetail.Year = getBook.Year; booksDetail.Picture = getBook.Picture; foreach (var genre in getBook.Genres) { var g = ctx.Genres.Where(gr => gr.GenreID == genre.GenreID).FirstOrDefault(); booksDetail.GenreID = g.GenreID; } result.Books.Add(booksDetail); } } return(result); }
public virtual bool CreateCart(CartModel cart) { using (TitanbraryContainer ctx = new TitanbraryContainer()) { try { var cartId = Guid.NewGuid(); var target = new Cart(); target.CartID = Guid.NewGuid(); target.UserID = cart.UserID; target.Completed = false; target.CreatedDate = DateTime.UtcNow; target.ModifiedDate = DateTime.UtcNow; //List<CartXBook> bookList = new List<CartXBook>(); //target.CartXBooks = bookList; // ctx.Carts.Add(new Cart // { // CartID = cartId, // CreatedDate = cart.CreatedDate, // ModifiedDate = cart.ModifiedDate, // UserID = cart.UserID, // Completed = false //}); ctx.Carts.Add(target); ctx.SaveChanges(); foreach (var book in cart.BookList) { var t = new CartXBook(); t.CartID = target.CartID; t.BookID = book.BookID; t.Quantity = 1; ctx.CartXBooks.Add(t); //ctx.CartXBooks.Add(new CartXBook //{ // CartID = cartId, // BookID = book.BookID, // Quantity = 1 //}); ctx.SaveChanges(); } } catch (Exception ex) { return(false); } } return(true); }
public bool SaveAccountDAC(UserModel model) { var target = new User(); var result = false; try { using (TitanbraryContainer ctx = new TitanbraryContainer()) { target.UserID = new Guid(model.Id); target.FirstName = model.FirstName; target.LastName = model.LastName; target.LoginName = model.Email; target.Email = model.Email; target.Password = model.Password; target.Address = "123 Avenue"; target.City = "Fullerton"; target.State = "CA"; target.ZipCode = "92832"; target.Phone = "1114445555"; target.SQAnwer1 = "yellow"; target.SQAnswer2 = "black"; target.SQAnswer3 = "red"; target.DateOfBirth = DateTime.Now; target.MemberSince = DateTime.Now; ctx.Users.Add(target); ctx.SaveChanges(); result = true; } } catch (DbEntityValidationException ex) { // Retrieve the error messages as a list of strings. var errorMessages = ex.EntityValidationErrors .SelectMany(x => x.ValidationErrors) .Select(x => x.ErrorMessage); // Join the list to a single string. var fullErrorMessage = string.Join("; ", errorMessages); // Combine the original exception message with the new one. var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); // Throw a new DbEntityValidationException with the improved exception message. throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); } return(result); }
public virtual List <GenreModel> GetAllGenres() { List <GenreModel> result = new List <GenreModel>(); using (TitanbraryContainer ctx = new TitanbraryContainer()) { result = ctx.Genres.Select(g => new GenreModel() { Title = g.Title, GenreID = g.GenreID, Description = g.Description }).ToList(); } return(result); }
public virtual List <GenreModel> GetGenresByBookID(Guid bookID) { List <GenreModel> result = new List <GenreModel>(); using (TitanbraryContainer ctx = new TitanbraryContainer()) { result = ctx.Genres.Where(g => g.Books.Any(b => b.BookID == bookID)).Select(g => new GenreModel() { Title = g.Title, GenreID = g.GenreID, Description = g.Description }).ToList(); } return(result); }
public virtual GenreModel GetGenreByGenreID(Guid genreID) { List <GenreModel> result = new List <GenreModel>(); using (TitanbraryContainer ctx = new TitanbraryContainer()) { result = ctx.Genres.Where(g => g.GenreID == genreID).Select(g => new GenreModel() { Title = g.Title, GenreID = g.GenreID, Description = g.Description }).ToList(); } return(result[0]); }
public virtual bool Checkout(Guid cartID) { using (TitanbraryContainer ctx = new TitanbraryContainer()) { try { var oldCart = ctx.Carts.SingleOrDefault(c => c.CartID == cartID); oldCart.Completed = true; ctx.SaveChanges(); } catch (Exception ex) { return(false); } } return(true); }
public virtual bool UpdateGenre(GenreModel genre) { using (TitanbraryContainer ctx = new TitanbraryContainer()) { try { var oldGenre = ctx.Genres.SingleOrDefault(g => g.GenreID == genre.GenreID); oldGenre.Title = genre.Title; oldGenre.Description = genre.Description; ctx.SaveChanges(); } catch (Exception ex) { return(false); } } return(true); }
public virtual List <BookModel> FeaturedBooks() { List <BookModel> result = new List <BookModel>(); using (TitanbraryContainer ctx = new TitanbraryContainer()) { var bookXCart = ctx.CartXBooks.GroupBy(b => b.BookID).Select(c => new { BookID = c.Key, Total = c.Sum(d => d.Quantity) }).OrderByDescending(e => e.Total); result = ctx.Books.GroupJoin(bookXCart, b => b.BookID, cb => cb.BookID, (b, cb) => new { Name = b.Name, Author = b.Author, Publisher = b.Publisher, ISBN = b.ISBN, Edition = b.Edition, Year = b.Year, Quantity = b.Quantity, Language = b.Language, Picture = b.Picture, Keywords = b.Keywords, Active = b.Active, Description = b.Description, Timestamp = b.Timestamp, BookID = b.BookID, Total = cb.Where(a => a.BookID == b.BookID).Select(x => x.Total).FirstOrDefault() }).OrderByDescending(x => x.Total).Select(b => new BookModel() { Name = b.Name, Author = b.Author, Publisher = b.Publisher, ISBN = b.ISBN, Edition = b.Edition, Year = b.Year, Quantity = b.Quantity, Language = b.Language, Picture = b.Picture, Keywords = b.Keywords, Active = b.Active, Description = b.Description, Timestamp = b.Timestamp, BookID = b.BookID }).Take(3).ToList(); } return(result); }
public virtual bool CreateGenre(GenreModel genre) { using (TitanbraryContainer ctx = new TitanbraryContainer()) { try { ctx.Genres.Add(new Genre { Title = genre.Title, GenreID = Guid.NewGuid(), Description = genre.Description }); ctx.SaveChanges(); } catch (Exception ex) { return(false); } } return(true); }
public UserModel GetUserInfoDAC(ApplicationUser currentUser) { var result = new UserModel(); using (TitanbraryContainer ctx = new TitanbraryContainer()) { var account = ctx.Users.Where(act => act.UserID == new Guid(currentUser.Id)).FirstOrDefault(); if (account != null) { result.Id = account.UserID.ToString(); result.FirstName = account.FirstName; result.LastName = account.LastName; result.Email = account.Email; result.Address = account.Address; result.City = account.City; result.State = account.State; } } return(result); }
public virtual bool AddBookToCart(Guid cartID, CartXBookModel cartXBook) { using (TitanbraryContainer ctx = new TitanbraryContainer()) { try { ctx.CartXBooks.Add(new CartXBook { BookID = cartXBook.BookID, CartID = cartID, Quantity = cartXBook.Quantity }); ctx.SaveChanges(); } catch (Exception ex) { return(false); } } return(true); }
public virtual bool IsBookInWaitlist(Guid bookID) { using (TitanbraryContainer ctx = new TitanbraryContainer()) { try { if (ctx.Waitlists.Any(w => w.BookID == bookID)) { return(true); } else { return(false); } } catch (Exception ex) { return(false); } } }
public virtual bool AddBookToWaitlist(Guid bookID, Guid userID) { using (TitanbraryContainer ctx = new TitanbraryContainer()) { try { ctx.Waitlists.Add(new Waitlist { BookID = bookID, UserID = userID, Date = DateTime.Now, WaitlistID = Guid.NewGuid() }); ctx.SaveChanges(); } catch (Exception ex) { return(false); } } return(true); }
public virtual bool CreateBook(BookModel book) { using (TitanbraryContainer ctx = new TitanbraryContainer()) { try { var genre = ctx.Genres.Where(g => g.GenreID == book.GenreID).ToList(); ctx.Books.Add(new Book { Name = book.Name, Author = book.Author, Publisher = book.Publisher, ISBN = book.ISBN, Edition = book.Edition, Year = book.Year, Quantity = book.Quantity, Language = book.Language, Picture = book.Picture, Keywords = book.Keywords, Active = book.Active, Description = book.Description, Timestamp = DateTime.UtcNow, BookID = Guid.NewGuid(), Genres = genre }); ctx.SaveChanges(); } catch (Exception ex) { return(false); } } return(true); }
//private void ConfigureServices(IServiceCollection services) //{ // services.AddMvc(); // services.AddAuthorization(options => // { // options.AddPolicy("RequireAdministratorRole", policy => policy.RequireRole("Administrator")); // }); //} // In this method we will create default User roles and Admin user for login private void createRolesAndUsers() { using (ApplicationDbContext ctx = new ApplicationDbContext()) { var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(ctx)); var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(ctx)); // In Startup iam creating first Admin Role and creating a default Admin User if (!roleManager.RoleExists("Admin")) { //create admin role //var role = new RoleModel(); //role.Name = "Admin"; //role.RoleId = Guid.NewGuid().ToString(); //role.RoleName = role.Name; IdentityRole role = new IdentityRole("Admin"); roleManager.Create(role); //create admin account var user = new ApplicationUser(); user.UserName = "******"; user.Email = "*****@*****.**"; //create a password string pwd = "Admin123!"; user.UserRoles = "Admin"; var adminUser = UserManager.Create(user, pwd); //add default user to admin role if (adminUser.Succeeded) { var result = UserManager.AddToRole(user.Id, user.UserRoles); } using (TitanbraryContainer ct = new TitanbraryContainer()) { //save to user account var target = new User(); target.UserID = new Guid(user.Id); target.FirstName = "John"; target.LastName = "Doe"; target.LoginName = user.Email; target.Email = user.Email; target.Password = ""; target.Address = "123 Avenue"; target.City = "Fullerton"; target.State = "CA"; target.ZipCode = "92832"; target.Phone = "1114445555"; target.SQAnwer1 = "yellow"; target.SQAnswer2 = "black"; target.SQAnswer3 = "red"; target.DateOfBirth = DateTime.Now; target.MemberSince = DateTime.Now; ct.Users.Add(target); ct.SaveChanges(); } } //Create Manager role if (!roleManager.RoleExists("Manager")) { //var role = new RoleModel(); //role.Name = "Manager"; //role.RoleId = Guid.NewGuid().ToString(); //role.RoleName = role.Name; IdentityRole role = new IdentityRole("Manager"); roleManager.Create(role); //create admin account var user = new ApplicationUser(); user.UserName = "******"; user.Email = "*****@*****.**"; //create a password string pwd = "Manager123!"; user.UserRoles = "Manager"; var adminUser = UserManager.Create(user, pwd); //add default user to admin role if (adminUser.Succeeded) { var result = UserManager.AddToRole(user.Id, user.UserRoles); } using (TitanbraryContainer ct = new TitanbraryContainer()) { //save to user account var target = new User(); target.UserID = new Guid(user.Id); target.FirstName = "Jane"; target.LastName = "Doe"; target.LoginName = user.Email; target.Email = user.Email; target.Password = ""; target.Address = "123 Avenue"; target.City = "Fullerton"; target.State = "CA"; target.ZipCode = "92832"; target.Phone = "1114445555"; target.SQAnwer1 = "yellow"; target.SQAnswer2 = "black"; target.SQAnswer3 = "red"; target.DateOfBirth = DateTime.Now; target.MemberSince = DateTime.Now; ct.Users.Add(target); ct.SaveChanges(); } } //Create Customer role if (!roleManager.RoleExists("Customer")) { //var role = new RoleModel(); //role.Name = "Customer"; //role.RoleId = Guid.NewGuid().ToString(); //role.RoleName = role.Name; IdentityRole role = new IdentityRole("Customer"); roleManager.Create(role); } } }