public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user try { using (var db = new SDCContext()) { var avatar = db.Avatars.Where(a => a.CustomForUserId == 0).OrderBy(a=>a.Id).First(); WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { Email = model.Email, Avatar_Id = avatar.Id, // by default, use the first avatar that is available. LastSeen = DateTime.Now, IsLocked = false, ShowEmail = false, City_Id = 1 }); Roles.AddUsersToRole(new string[] { model.UserName }, RolesCustom.USER); if (WebSecurity.Login(model.UserName, model.Password)) { var profile = db.UserProfiles .Include(p => p.Avatar) .Include(p => p.Country.Language) .First(p => p.UserName == model.UserName); //default page size:10 profile.PageSize = 10; profile.Created = DateTime.Now; profile.City = db.Cities.Include(c=>c.Country).First(); profile.Country = profile.City.Country; //create default shelf Shelf defaultShelf = new Shelf() { CreationDate = DateTime.Now, Name = String.Format("{0}'s shelf", model.UserName), IsVisible = true, Owner = profile }; db.Shelves.Add(defaultShelf); db.SaveChanges(); SaveLoginTrace(model.UserName, db); profile.Role = Roles.GetRolesForUser(model.UserName)[0]; profile.Shelves = db.Shelves.Where(p => p.Owner.UserId == profile.UserId).ToList(); Session["UserInfo"] = profile; Session["UserInfoEx"] = profile.GetExtendedInfo(db); } return RedirectToAction("Index", "Home"); } } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } // If we got this far, something failed, redisplay form return View(model); }
private void LoadBooks(SDCContext db, UserProfile profile, Shelf shelf) { int perShelf = _rnd.Next(10, 50); int startindex = Progress; for(int i = startindex; i < startindex + perShelf && Progress < _max; i++) { string isbn, title, author, publisher, imgurl; int year; try { isbn = _booksSplit[i, 0]; title = _booksSplit[i, 1]; author = _booksSplit[i, 2]; year = Int32.Parse(_booksSplit[i, 3]); publisher = _booksSplit[i, 4]; imgurl = _booksSplit[i, 5]; }catch(Exception) { continue; } DateTime d1 = DateTime.Now; Author a = null; if (!_authorsSet.ContainsKey(author)) { a = new Author() { Name = author, AddedDate = DateTime.Now, AddedBy = profile, LastModifiedBy = profile, IsVerified = true }; db.Authors.Add(a); _authorsSet.Add(author, a); } else { a = _authorsSet[author]; } Debug.WriteLine("Author lookup in " + DateTime.Now.Subtract(d1).TotalMilliseconds); DateTime d2 = DateTime.Now; Publisher p = null; if (!_publishersSet.ContainsKey(publisher)) { p = new Publisher() { Name = publisher, AddedBy = profile, IsVerified = true }; db.Publishers.Add(p); _publishersSet.Add(publisher, p); } else { p = _publishersSet[publisher]; } Debug.WriteLine("Author lookup in " + DateTime.Now.Subtract(d2).TotalMilliseconds); Book book = new Book() { Authors = new List<Author>(new Author[] { a }), Title = title, AddedDate = DateTime.Now, Language = _lang, Shelf = shelf, ISBN = isbn, Publisher = p, Year = year, Description = _booksSplit[i, 6], Pages = _rnd.Next(1, 500), Price = _rnd.Next(10, 100) }; //3 random genres bongo bong! var bookGenres = _allGenres.OrderBy(x => Guid.NewGuid().ToString()).Take(3).ToArray(); book.Genres.Add(bookGenres[0]); db.Books.Add(book); //book pictures BookPicture bp = new BookPicture() { Book = book, Key = null, Url = imgurl, IsMain = true, Title = "img title" }; //book.Pictures = new List<BookPicture>(); book.Pictures.Add(bp); Progress++; if (Cancel) break; } }
public ActionResult NewShelf(ShelvesViewModel model) { if (String.IsNullOrEmpty(model.Name)) { return RedirectToAction("Index"); } UserProfile profile = null; //save using (var db = new SDCContext()) { profile = db.UserProfiles.Find(((UserProfile)Session["UserInfo"]).UserId); Shelf newShelf = new Shelf() { CreationDate = DateTime.Now, Name = model.Name, IsVisible = model.IsVisible, Owner = profile }; db.Shelves.Add(newShelf); db.SaveChanges(); Session["UserInfoEx"] = profile.GetExtendedInfo(db); } return RedirectToAction("Index"); }
private UserProfile CreateUser(SDCContext db, string firstName, string lastName, out Shelf shelf) { string username = firstName + "." + lastName; string email = username + "@gmail.com"; string password = "******"; WebSecurity.CreateUserAndAccount(username, password, new { Email = email, Avatar_Id = 1, // by default, use the first avatar that is available. LastSeen = DateTime.Now, IsLocked = false, ShowEmail = false, City_Id = 1 }); Roles.AddUsersToRole(new string[] { username }, RolesCustom.USER); //get profile var profile = db.UserProfiles.FirstOrDefault(p => p.UserName == username); profile.Country = _country; profile.City = _city; profile.PageSize = 10; //create default shelf //create default shelf Shelf defaultShelf = new Shelf() { CreationDate = DateTime.Now, Name = String.Format("{0}'s shelf", username), IsVisible = true, Owner = profile }; db.Shelves.Add(defaultShelf); shelf = defaultShelf; return profile; }