private void AddAdmin(BookshopDbContext context) { string guid = Guid.NewGuid().ToString(); IdentityRole r = context.Roles.FirstOrDefault(x => x.Name == "Admin"); if (r != null) { IdentityUserRole identityUserRole = new IdentityUserRole() { RoleId = r.Id, UserId = guid }; string password = new PasswordHasher().HashPassword("Me.rahim29"); var s = Guid.NewGuid().ToString("D"); ApplicationUser applicationUser = new ApplicationUser() { Id = guid, Email = "*****@*****.**", PhoneNumber = "01673467667", EmailConfirmed = true, PasswordHash = password, Roles = { identityUserRole }, UidId = "011131148", SecurityStamp = s, UserName = "******" }; context.Users.Add(applicationUser); context.SaveChanges(); } }
private void AddRoles(BookshopDbContext context) { string[] roles = { "Admin", "User" }; foreach (string s in roles) { IdentityRole identityRole = context.Roles.FirstOrDefault(x => x.Name == s); if (identityRole == null) { IdentityRole role = new IdentityRole() { Name = s }; context.Roles.Add(role); } context.SaveChanges(); } }
private void AddDepartments(BookshopDbContext context) { context.Departments.AddOrUpdate( p => p.Name, new Department() { Name = "CSE" }, new Department() { Name = "EEE" }, new Department() { Name = "BBA" } ); context.SaveChanges(); }
private void AddCategory(BookshopDbContext context) { context.Categories.AddOrUpdate( p => p.Name, new Category() { Name = "Mathematics" }, new Category() { Name = "Physics" }, new Category() { Name = "English" } ); context.SaveChanges(); }
public bool SaveBook(UploadBookViewModel uploadBook, HttpPostedFileBase file, string userId) { List <Department> depts = new List <Department>(); depts.Add(db.Departments.Single(x => x.Id == uploadBook.Department)); Category category = db.Categories.Find(uploadBook.Category); Condition condition = db.Conditions.Find(uploadBook.Condition); Photo p = new Photo(); if (file != null) { string pic = file.FileName; string path = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/Content/img/books"), pic); file.SaveAs(path); p.Path = @"/Content/img/books/" + pic; } try { Book latestBook = new Book() { UserId = userId, UploadDateTime = DateTime.Now, Title = uploadBook.Title, Price = uploadBook.Price, Edition = uploadBook.Edition, Departments = depts, Category = category, CategoryId = category.Id, Approved = false, Author = uploadBook.Author, ConditionId = uploadBook.Condition, Condition = condition, AdditionalInfo = uploadBook.AdditionalInfo, }; db.Books.Add(latestBook); db.SaveChanges(); p.UserId = userId; p.Book = latestBook; p.UploadDateTime = DateTime.Now; db.Photos.Add(p); if (db.SaveChanges() > 0) { return(true); } else { return(false); } } catch (Exception e) { return(false); } }