public ActionResult Profile(user profile) { var loggedUser = GetLoggedUser(); if (loggedUser == null) { return RedirectToAction("LogOn", "Account"); } if (ModelState.IsValid) { using (TransactionScope scope = new TransactionScope()) { var mem = db.memberships.Where(m => m.membership_id == loggedUser.membership_id).SingleOrDefault(); mem.email = profile.membership.email; db.SaveChanges(); db.Entry(profile).State = EntityState.Modified; profile.membership = mem; HttpPostedFileBase file = Request.Files[0] as HttpPostedFileBase; if (file.ContentLength > 0) { var fileName = Path.GetFileNameWithoutExtension(file.FileName); var fileExt = Path.GetExtension(file.FileName); var destinationDirectory = Server.MapPath("~/Images/Profile/"); var newFileName = profile.id + "_" + profile.last_name + fileExt; var path = Path.Combine(destinationDirectory, newFileName); try { if (!Directory.Exists(destinationDirectory)) { Directory.CreateDirectory(destinationDirectory); } file.SaveAs(path); var thumb = profile.id + "_" + profile.last_name + "_S" + fileExt; CreateThumbnail(path, 100, 100, Path.Combine(destinationDirectory, thumb)); profile.profile_photo = Url.Content("~/Images/Profile/" + thumb); } catch { profile.profile_photo = ""; } finally { FileInfo _file = new FileInfo(path); if (_file.Exists) { _file.Delete(); } } } db.SaveChanges(); scope.Complete(); } return RedirectToAction("Index", "User"); } string[] _g = new string[] { "Unspecified", "Male", "Female" }; ViewBag.gender = new SelectList(_g); return View(profile); }
public ActionResult InstallFakeUsers() { //fake users for (int count = 0; count < 30; count++) { using (TransactionScope scope = new TransactionScope()) { membership _m = new membership { email = RandomString(6) + "@" + RandomString(6) + ".com", joined_date = DateTime.Now.AddMonths(-1).AddDays(count), is_approved = "Y", username = "******" + (count + 1).ToString(), password = "******" + (count + 1).ToString() }; db.memberships.Add(_m); db.SaveChanges(); user _u = new user { membership = _m, last_name = RandomString(6), first_name = RandomString(6), gender = Gender.Unspecified.ToString(), about = RandomString(200) }; db.users.Add(_u); db.SaveChanges(); scope.Complete(); } } return RedirectToAction("Index", "User"); }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { try { //start transaction block using (TransactionScope scope = new TransactionScope()) { /* * check if username is already present * in the membership table * SELECT membership_id FROM membership * WHERE username = "******" LIMIT 0,1 */ var isUsernameTaken = (from m in db.memberships where m.username == model.UserName select m.membership_id).SingleOrDefault(); if (isUsernameTaken > 1) { ModelState.AddModelError("", "Username is already taken. Please choose another"); return View(model); } else { /* * CREATE a new record in member with user * provided username,password and email * INSERT INTO membership * (username,password,email,is_approved) * VALUES (?,?,?,?) * * we are skipping email validation for * the demo due lack of time :( * no password hashing for the sake of simplity right now */ var newMember = new membership { username = model.UserName, password = model.Password, email = model.Email, is_approved = "Y", joined_date = DateTime.Now }; db.memberships.Add(newMember); db.SaveChanges(); /* NOW create a user record with the * membership_id of previous insert. INSERT DEFAULT DATA FOR NOW. * INSERT INTO users (gender,membership_id) VALUES (?,?) */ var newUser = new user { gender = Gender.Unspecified.ToString(), membership = newMember }; db.users.Add(newUser); db.SaveChanges(); //everything is OK. Set the username in session Session.Add("LoggedInUser", model.UserName); //transaction complete scope.Complete(); return Redirect(string.Format("/User/{0}/Index", model.UserName)); } } } catch (Exception ex) { //exception occured. transaction scope is incomplete //so no need to rollback. display some generic error ModelState.AddModelError("", "Could not register"); } } // If we got this far, something failed, redisplay form return View(model); }