예제 #1
0
        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();
            }
        }
예제 #2
0
 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();
     }
 }
예제 #3
0
 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();
 }
예제 #4
0
 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();
 }
예제 #5
0
        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);
            }
        }