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 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 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 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 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 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 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 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); } } }