Пример #1
0
        protected void ButtonDeleteGroup_Command(object sender, CommandEventArgs e)
        {
            int groupId = Convert.ToInt32(e.CommandArgument);
            var context = new ApplicationDbContext();

            try
            {
                var selectedGroup = context.Groups.FirstOrDefault(g => g.GroupId == groupId);
                context.Groups.FirstOrDefault(g => g.GroupId == groupId).Messages.Clear();
                context.Groups.Remove(selectedGroup);
                context.SaveChanges();
                ErrorSuccessNotifier.AddSuccessMessage("Group deleted!");
            }
            catch (Exception ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }

            Response.Redirect(Request.Url.AbsoluteUri);
        }
Пример #2
0
        protected void OnButtonShowDeleteBookPanel_Command(object sender, CommandEventArgs e)
        {
            using (var context = new LibrarySystemEntities())
            {
                int bookId = Convert.ToInt32(e.CommandArgument);
                var book   = context.Books.Find(bookId);

                if (book != null)
                {
                    this.ShowDeleteBookPanel();
                    this.ButtonDeleteBook.CommandArgument = bookId.ToString();

                    this.TextBoxDeleteBookTitle.Text = book.Title;
                }
                else
                {
                    ErrorSuccessNotifier.AddErrorMessage("This book does not exist anymore");
                }
            }
        }
Пример #3
0
        protected void MainContent_LinkButtonConfirmDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int selectedBookId           = int.Parse(this.TextBoxDeleteBookId.Text);
                ApplicationDbContext context = new ApplicationDbContext();
                var selectedBook             = context.Books.Find(selectedBookId);
                context.Books.Remove(selectedBook);
                context.SaveChanges();
                this.GridViewBooks.DataBind();

                this.MainContent_PanelDelete.Visible = false;

                ErrorSuccessNotifier.AddInfoMessage("You have successfully deleted book with title: " + selectedBook.Title);
            }
            catch (Exception ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }
        }
Пример #4
0
        protected void OnBtnVote_Command(object sender, CommandEventArgs e)
        {
            var dbContext = new PollSystemEntities();
            int answerId  = Convert.ToInt32(e.CommandArgument);

            var answer = dbContext.Answers.Find(answerId);

            if (answer != null)
            {
                answer.Votes++;
                dbContext.SaveChanges();

                ErrorSuccessNotifier.AddInfoMessage("You voted for " + answer.Text);
                Response.Redirect("~/VotingResults.aspx?questionId=" + answer.QuestionId);
            }
            else
            {
                ErrorSuccessNotifier.AddErrorMessage("This answer does not exist anymore");
            }
        }
Пример #5
0
        protected void OnButtonCreateCategory_Command(object sender, CommandEventArgs e)
        {
            using (var context = new LibrarySystemEntities())
            {
                try
                {
                    string categoryName = this.TextBoxCreateCategoryName.Text.Trim();
                    if (categoryName == "")
                    {
                        ErrorSuccessNotifier.AddErrorMessage("The category cannot be empty");
                        return;
                    }

                    var existingCategory = context.Categories.FirstOrDefault(
                        cat => cat.Name.ToLower() == categoryName.ToLower());

                    if (existingCategory != null)
                    {
                        this.PanelCreateCategory.Visible = false;
                        ErrorSuccessNotifier.AddErrorMessage("This category already exists");
                        return;
                    }

                    var newCategory = new Category()
                    {
                        Name = categoryName
                    };

                    context.Categories.Add(newCategory);
                    context.SaveChanges();

                    ErrorSuccessNotifier.AddSuccessMessage("Category created");
                    this.GridViewCategories.SetPageIndex(this.GridViewCategories.PageIndex);
                    this.PanelCreateCategory.Visible = false;
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
            }
        }
Пример #6
0
        private bool ValidateISBN(string ISBN)
        {
            var isValid = true;

            if (string.IsNullOrEmpty(ISBN))
            {
                return(true);
            }
            else if (ISBN.Length != 17)
            {
                ErrorSuccessNotifier.AddErrorMessage("ISBN's length is exactly 17 symbols: 978-954-400-773-7");
                isValid = false;
            }
            else if (!Regex.IsMatch(ISBN, @"^[\d-]{17}$"))
            {
                ErrorSuccessNotifier.AddErrorMessage("ISBN consists only of digits and '-': 978-954-400-773-7");
                isValid = false;
            }

            return(isValid);
        }
Пример #7
0
        protected void SaveEdit_Command(object sender, CommandEventArgs e)
        {
            try
            {
                int categoryId = Convert.ToInt32(e.CommandArgument);

                Entities context  = new Entities();
                Category category = context.Categories.Find(categoryId);

                category.CategoryName = this.TextBox_CategoryName.Text != string.Empty ? this.TextBox_CategoryName.Text : null;
                context.SaveChanges();

                this.pageMode = Mode.View;
                ErrorSuccessNotifier.AddSuccessMessage("The category was edited successfully!");
            }
            catch (Exception ex)
            {
                this.pageMode = Mode.Edit;
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }
        }
Пример #8
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var searchText = Request.Params["q"];

            this.LiteralSearchTitle.Text = searchText;
            var searchTextToLower = searchText.ToLower();
            var context           = new ApplicationDbContext();

            try
            {
                var books = context.Books
                            .Where(x => x.Title.ToLower().Contains(searchTextToLower) || x.Author.ToLower().Contains(searchTextToLower))
                            .OrderBy(b => b.Title).ThenBy(t => t.Author);
                this.RepeaterSearchResults.DataSource = books.ToList();
                this.RepeaterSearchResults.DataBind();
            }
            catch (Exception ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }
        }
Пример #9
0
        protected void OnButtonShowEditCategory_Command(object sender, CommandEventArgs e)
        {
            using (var context = new LibrarySystemEntities())
            {
                int categoryId = Convert.ToInt32(e.CommandArgument);
                var category   = context.Categories.Find(categoryId);

                if (category != null)
                {
                    this.PanelCreateCategory.Visible        = false;
                    this.PanelDeleteCategory.Visible        = false;
                    this.PanelEditCategory.Visible          = true;
                    this.TextBoxEditCategoryName.Text       = category.Name;
                    this.ButtonSaveCategory.CommandArgument = categoryId.ToString();
                }
                else
                {
                    ErrorSuccessNotifier.AddErrorMessage("This category does not exist anymore");
                }
            }
        }
Пример #10
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var context = new ApplicationDbContext();
            ICollection <Category> categories = null;

            try
            {
                categories = context.Categories.OrderBy(c => c.Title).ToList();
                if (categories == null || categories.Count == 0)
                {
                    (this.ListViewCategories.FindControl("NoResults") as Literal).Text = "No books in this category.";
                }
            }
            catch (Exception ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }

            this.ListViewCategories.DataSource = categories;
            this.ListViewCategories.DataBind();
        }
Пример #11
0
        public void ListViewComments_InsertItem()
        {
            try
            {
                var db   = new NoticeboardEntities();
                var user = db.AspNetUsers.FirstOrDefault(u => u.UserName == this.User.Identity.Name);
                var cont = ((TextBox)this.FindControlRecursive(this, "TextBoxComment")).Text;
                if (cont.Length >= 5000)
                {
                    var ex = new Exception("Comment must be less than 5000 symbols!");
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                    return;
                }

                var comment = new Comment
                {
                    Content     = cont,
                    PostId      = this.postId,
                    AspNetUser  = user,
                    CommentDate = DateTime.Now
                };
                db.Comments.Add(comment);
                try
                {
                    db.SaveChanges();
                    ErrorSuccessNotifier.AddSuccessMessage("Comment created successfully");
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex.Message);
                }

                var uPanel = (UpdatePanel)this.FindControlRecursive(this, "UpdatePanelComments");
                uPanel.Update();
            }
            catch (Exception ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }
        }
Пример #12
0
        protected void Page_PreRender(object sender, EventArgs e)
        {
            var postId = Convert.ToInt32(this.Request.Params["postId"]);

            using (var context = new NoticeboardEntities())
            {
                if (postId == 0)
                {
                    this.LabelPostTitle.Text = "All";
                    return;
                }

                try
                {
                    this.LabelPostTitle.Text = context.Posts.Find(postId).Title;
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
            }
        }
Пример #13
0
        protected void MainContent_LinkButtonEditSave_Click(object sender, EventArgs e)
        {
            try
            {
                ApplicationDbContext context = new ApplicationDbContext();
                int id           = int.Parse(this.TextBoxCategoryEditId.Text);
                var selectedItem = context.Categories.Find(id);
                selectedItem.Name = this.TextBoxCategoryEdit.Text;
                context.SaveChanges();
                this.GridViewCategories.DataBind();

                ErrorSuccessNotifier.AddInfoMessage("You have successfully edited category to: " + TextBoxCategoryEdit.Text);

                this.TextBoxCategoryEditId.Text    = "";
                this.TextBoxCategoryEdit.Text      = "";
                this.MainContent_PanelEdit.Visible = false;
            }
            catch (Exception ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }
        }
Пример #14
0
        protected void YesDelete_Command(object sender, CommandEventArgs e)
        {
            try
            {
                int bookId = Convert.ToInt32(e.CommandArgument);

                Entities context = new Entities();
                Book     book    = context.Books.Find(bookId);

                context.Books.Remove(book);

                context.SaveChanges();

                this.pageMode = Mode.View;
                ErrorSuccessNotifier.AddSuccessMessage("The book was deleted successfully!");
            }
            catch (Exception ex)
            {
                this.pageMode = Mode.Delete;
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }
        }
Пример #15
0
        public void GridViewCategories_UpdateItem(int id)
        {
            Category item = this.dbContext.Categories.Find(id);

            if (item == null)
            {
                // The item wasn't found
                ModelState.AddModelError("", String.Format("Item with id {0} was not found", id));
                return;
            }

            if (!TryUpdateModel(item))
            {
                ErrorSuccessNotifier.AddErrorMessage("Cannot update category!");
            }

            if (ModelState.IsValid)
            {
                this.dbContext.SaveChanges();
                ErrorSuccessNotifier.AddSuccessMessage("Category updated.");
            }
        }
        protected void LogIn(object sender, EventArgs e)
        {
            if (IsValid)
            {
                // Validate the user password
                var manager       = Context.GetOwinContext().GetUserManager <ApplicationUserManager>();
                var signinManager = Context.GetOwinContext().GetUserManager <ApplicationSignInManager>();

                // This doen't count login failures towards account lockout
                // To enable password failures to trigger lockout, change to shouldLockout: true
                var result = signinManager.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout: false);

                switch (result)
                {
                case SignInStatus.Success:
                    ErrorSuccessNotifier.AddSuccessMessage("Login Successful!!!");
                    IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                    break;

                case SignInStatus.LockedOut:
                    Response.Redirect("/Account/Lockout");
                    break;

                case SignInStatus.RequiresVerification:
                    Response.Redirect(String.Format("/Account/TwoFactorAuthenticationSignIn?ReturnUrl={0}&RememberMe={1}",
                                                    Request.QueryString["ReturnUrl"],
                                                    RememberMe.Checked),
                                      true);
                    break;

                case SignInStatus.Failure:
                default:
                    FailureText.Text     = "Invalid login attempt";
                    ErrorMessage.Visible = true;
                    ErrorSuccessNotifier.AddErrorMessage("Opps!!! You have entered something wrong!!!");
                    break;
                }
            }
        }
Пример #17
0
        private bool ValidateUrl(string url)
        {
            var isValid = true;

            if (string.IsNullOrEmpty(url))
            {
                return(true);
            }
            else if (url.Length > 256 || url.Length < 12)
            {
                ErrorSuccessNotifier.AddErrorMessage("Web Site Address must be between 12 and 256 symbols");
                isValid = false;
            }
            else if (!Regex.IsMatch(url, @"^http[s]?://[A-Za-z/\.-]+$"))
            {
                ErrorSuccessNotifier.AddErrorMessage("Web Site Address must start with http:// or https:// " +
                                                     " and have letters, dots and hyphons in it : http://example-book.com/first-page or https://book.example.com/laset/minor-change");
                isValid = false;
            }

            return(isValid);
        }
Пример #18
0
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            using (var context = new NoticeboardEntities())
            {
                var categoryId = Convert.ToInt32(this.Request.Params["categoryId"]);

                try
                {
                    var category = context.Categories.Find(categoryId);
                    category.Title = this.TextBoxCategoryTitle.Text;
                    context.SaveChanges();

                    ErrorSuccessNotifier.AddInfoMessage("Category successfully edited.");
                    ErrorSuccessNotifier.ShowAfterRedirect = true;
                    this.Response.Redirect("Categories.aspx", false);
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
            }
        }
Пример #19
0
        public void FormViewBook_InsertItem()
        {
            var book = new Book();

            this.TryUpdateModel(book);

            if (!this.ModelState.IsValid)
            {
                this.SetFormValidity(false);
                this.DisplayErrorMessage();
                return;
            }
            else
            {
                this.SetFormValidity(true);
            }

            this.data.Books.Add(book);
            this.data.SaveChanges();
            this.CloseForm();
            ErrorSuccessNotifier.AddSuccessMessage(MessageBookCreated);
        }
Пример #20
0
        protected void ButtonSave_Click(object sender, EventArgs e)
        {
            using (var context = new StichtiteForumEntities())
            {
                var commentId = Convert.ToInt32(this.Request.Params["commentId"]);

                try
                {
                    var comment = context.Comments.Find(commentId);
                    comment.Content = this.TextBoxCommentContent.Text;
                    context.SaveChanges();

                    ErrorSuccessNotifier.AddInfoMessage("Comment successfully edited.");
                    ErrorSuccessNotifier.ShowAfterRedirect = true;
                    this.Response.Redirect("Comments.aspx?postId=" + comment.PostId, false);
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
            }
        }
        public void EditCategoryLV_InsertItem()
        {
            var newCategory = new Category();

            this.TryUpdateModel(newCategory);

            if (this.ModelState.IsValid == false)
            {
                ErrorSuccessNotifier.AddErrorMessage(CouldNotAddCategoryMsg);
                return;
            }

            if (this.db.Categories.Any(c => c.Name == newCategory.Name))
            {
                ErrorSuccessNotifier.AddErrorMessage(CategoryNameExistsMsg);
                return;
            }

            this.db.Categories.Add(newCategory);
            this.db.SaveChanges();
            ErrorSuccessNotifier.AddSuccessMessage(CategoryCreatedMsg);
        }
Пример #22
0
        protected void SaveEditedCategory_Command(object sender, CommandEventArgs e)
        {
            //Edit question
            var textBoxEditCategory = this.FormViewEditCategory.FindControl("TextBoxEditCategory") as TextBox;
            var categoryTitle       = textBoxEditCategory.Text;
            var context             = new ApplicationDbContext();
            var categoryId          = Convert.ToInt32(e.CommandArgument);

            try
            {
                var category = context.Categories.FirstOrDefault(c => c.CategoryId == categoryId);
                category.Title = categoryTitle;
                context.SaveChanges();
                ErrorSuccessNotifier.AddSuccessMessage(string.Format("Category \"{0}\" created!", categoryTitle));
            }
            catch (Exception ex)
            {
                ErrorSuccessNotifier.AddErrorMessage(ex);
            }

            Response.Redirect(Request.Url.AbsoluteUri);
        }
Пример #23
0
        public void FormViewCategory_InsertItem()
        {
            var category = new Category();

            this.TryUpdateModel(category);

            if (!this.ModelState.IsValid)
            {
                this.SetFormValidity(false);
                this.DisplayErrorMessage();
                return;
            }
            else
            {
                this.SetFormValidity(true);
            }

            this.data.Categories.Add(category);
            this.data.SaveChanges();
            this.CloseForm();
            ErrorSuccessNotifier.AddSuccessMessage(MessageCategoryCreated);
        }
Пример #24
0
        public void GridViewLectures_UpdateItem(int id)
        {
            var context = new AcademyDbContext();

            Forum.Models.Lecture item = context.Lectures.Find(id);
            if (item == null)
            {
                ModelState.AddModelError("", String.Format("Item with id {0} was not found", id));
                return;
            }

            TryUpdateModel(item);
            if (ModelState.IsValid)
            {
                context.SaveChanges();
                ErrorSuccessNotifier.AddInfoMessage("Lecture updated successfully.");
            }
            else
            {
                ErrorSuccessNotifier.AddErrorMessage("There was an error updating the lecture. Please try again.");
            }
        }
Пример #25
0
        public void ListViewArticles_InsertItem()
        {
            var articleProjection = new ArticleProjection();

            this.TryUpdateModel(articleProjection);
            var categoryId = int.Parse(articleProjection.CategoryName);

            if (string.IsNullOrEmpty(articleProjection.Title) || string.IsNullOrEmpty(articleProjection.Content))
            {
                ErrorSuccessNotifier.AddMessage(new NotificationMessage()
                {
                    Text     = "Cannot create article with emptry title/content.",
                    AutoHide = false,
                    Type     = MessageType.Warning
                });

                return;
            }

            var article = new Article()
            {
                Title       = articleProjection.Title,
                Content     = articleProjection.Content,
                AuthorId    = this.userIdProvider.GetUserId(),
                DateCreated = DateTime.Now,
                CategoryId  = categoryId
            };

            if (this.ModelState.IsValid)
            {
                this.context.Articles.Add(article);
                this.TryUpdateOrShowMessage(new NotificationMessage()
                {
                    Text     = "Article was created successfully.",
                    AutoHide = false,
                    Type     = MessageType.Success
                });
            }
        }
Пример #26
0
        protected void CreateNewBookButton_Click(object sender, EventArgs e)
        {
            int newBookCategoryId;
            var validCategoryId = int.TryParse(this.NewBookCategoryDropDown.SelectedValue, out newBookCategoryId);

            if (!ValidateInsertFields() || !validCategoryId)
            {
                return;
            }

            using (var dbContext = new LibrarySystemEntities())
            {
                try
                {
                    dbContext.Books.Add(new Book
                    {
                        Title       = this.NewBookTitle.Text,
                        Author      = this.NewBookAuthor.Text,
                        ISBN        = this.NewBookISBN.Text,
                        Url         = this.NewBookUrl.Text,
                        Description = this.NewBookDescription.InnerText,
                        Category    = dbContext.Categories.Find(newBookCategoryId)
                    });
                    dbContext.SaveChanges();
                    this.CleanAndHideInsertForm();

                    ErrorSuccessNotifier.AddSuccessMessage("Created!");
                }
                catch (EntityDataSourceValidationException ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex.Message);
                }
            }
        }
Пример #27
0
        protected void LogIn(object sender, EventArgs e)
        {
            if (IsValid)
            {
                bool   error    = false;
                string username = this.UserName.Text;
                if (username == "")
                {
                    ErrorSuccessNotifier.AddErrorMessage("The username field is required");
                    error = true;
                }

                string password = this.Password.Text;
                if (password == "")
                {
                    ErrorSuccessNotifier.AddErrorMessage("The password field is required");
                    error = true;
                }

                if (error)
                {
                    return;
                }

                // Validate the user password
                IAuthenticationManager manager = new AuthenticationIdentityManager(new IdentityStore()).Authentication;
                IdentityResult         result  = manager.CheckPasswordAndSignIn(Context.GetOwinContext().Authentication, UserName.Text, Password.Text, RememberMe.Checked);
                if (result.Success)
                {
                    OpenAuthProviders.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                }
                else
                {
                    FailureText.Text     = result.Errors.FirstOrDefault();
                    ErrorMessage.Visible = true;
                }
            }
        }
        protected void EditMyGroup_Command(object sender, CommandEventArgs e)
        {
            int groupId = int.Parse(Request.Params["id"]);

            var context       = new ApplicationDbContext();
            var selectedGroup = context.Groups.FirstOrDefault(g => g.GroupId == groupId);

            if (selectedGroup.Creator == User.Identity.Name || User.IsInRole("Admin"))
            {
                try
                {
                    selectedGroup.Title = this.TextBoxUpdatedGroupTitle.Text;
                    context.SaveChanges();
                    ErrorSuccessNotifier.AddSuccessMessage("Group title edited!");
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
            }

            Response.Redirect("~/Groups");
        }
Пример #29
0
        protected void GridViewCourses_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int id             = (int)e.Keys["Id"];
            var context        = new AcademyDbContext();
            var existingCourse = context.Courses.FirstOrDefault(c => c.Id == id);
            var lectures       = existingCourse.Lectures.ToList();

            foreach (var lecture in lectures)
            {
                context.Homeworks.RemoveRange(lecture.Homeworks);
                context.Lectures.Remove(lecture);
            }

            context.SaveChanges();

            context.Courses.Remove(existingCourse);
            context.SaveChanges();

            e.Cancel = true;
            (sender as GridView).DataBind();

            ErrorSuccessNotifier.AddInfoMessage("Course successfully deleted.");
        }
Пример #30
0
        protected void ButtonCreate_OnClick(object sender, EventArgs e)
        {
            var category = new Category
            {
                Title = this.TextBoxCategoryTitle.Text
            };

            using (var context = new StichtiteForumEntities())
            {
                try
                {
                    context.Categories.Add(category);
                    context.SaveChanges();
                    ErrorSuccessNotifier.AddSuccessMessage("Category added successfully!");
                    ErrorSuccessNotifier.ShowAfterRedirect = true;
                    this.Response.Redirect("Categories.aspx", false);
                }
                catch (Exception ex)
                {
                    ErrorSuccessNotifier.AddErrorMessage(ex);
                }
            }
        }