protected void CreateUser_Click(object sender, EventArgs e) { var manager = Context.GetOwinContext().GetUserManager <ApplicationUserManager>(); var user = new AppUser() { UserName = Email.Text, Email = Email.Text, FirstName = FirstName.Text, LastName = LastName.Text, CountryID = int.Parse(Countries.SelectedValue) }; string filePathAndName = string.Empty; try { filePathAndName = FileUploadControl.Upload(); } catch (InvalidOperationException ex) { this.DivLabelErrorMessage.Visible = true; this.LabelErrorMessage.Text = ex.Message; return; } if (string.IsNullOrEmpty(filePathAndName)) { user.ImageID = this.data.Images.All().FirstOrDefault().ID; } else { var image = new Image { Path = filePathAndName }; this.data.Images.Add(image); user.Image = image; } try { IdentityResult result = manager.Create(user, Password.Text); if (result.Succeeded) { manager.AddToRole(user.Id, "user"); IdentityHelper.SignIn(manager, user, isPersistent: false); IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response); } else { ErrorMessage.Text = result.Errors.FirstOrDefault(); } } catch (System.Data.Entity.Validation.DbEntityValidationException error) { ErrorMessage.Text = error.EntityValidationErrors.FirstOrDefault().ValidationErrors.FirstOrDefault().ErrorMessage; } }
protected void OnCreateRecipeButtonClick(object sender, EventArgs e) { var loggedInUserName = HttpContext.Current.User.Identity.Name; var currentUserId = this.data.Users.All().FirstOrDefault(x => x.UserName == loggedInUserName).Id; var ingredientsForRecipe = GetIngredients(this.RecipeIngredients.Text); Recipe recipe = new Recipe() { Name = this.RecipeName.Text, Description = this.RecipeDescription.Text, CreatedOn = DateTime.Now, CreatorID = currentUserId, isHot = this.isHotDish, isCold = this.isColdDish, isSweet = this.isSweetDish, isVegitarian = this.isVegetarianDish, Latitude = 41.124588, Longitude = 13.713464, Ingredients = ingredientsForRecipe }; string filePathAndName = string.Empty; try { filePathAndName = FileUploadControl.Upload(); } catch (InvalidOperationException ex) { this.DivLabelErrorMessage.Visible = true; this.LabelErrorMessage.Text = ex.Message; return; } if (string.IsNullOrEmpty(filePathAndName)) { recipe.ImageID = this.data.Images.All().FirstOrDefault().ID; } else { var image = new Image { Path = filePathAndName }; this.data.Images.Add(image); recipe.Image = image; } this.data.Recipes.Add(recipe); this.data.SaveChanges(); Response.Redirect("~/User/MyRecipes"); }
protected void Submit_Click(object sender, EventArgs e) { if (Page.IsValid) { string filePathAndName = string.Empty; try { filePathAndName = FileUploadControl.Upload(); } catch (InvalidOperationException ex) { this.DivLabelErrorMessage.Visible = true; this.LabelErrorMessage.Text = ex.Message; return; } var loggedInUserName = HttpContext.Current.User.Identity.Name; var currentUserId = this.data.Users.All().FirstOrDefault(x => x.UserName == loggedInUserName).Id; var newImage = new Image { Path = filePathAndName }; this.data.Images.Add(newImage); this.data.SaveChanges(); var newArticle = new MasterChef.Models.Article.Article { Title = this.title.Value, ImageID = newImage.ID, Content = this.content.Value, CreatedOn = DateTime.Now, CreatorID = currentUserId }; this.data.Articles.Add(newArticle); this.data.SaveChanges(); Response.Redirect("~/Article/Browse"); } else { throw new InvalidOperationException("Error occured while saving the element!"); } }