Exemple #1
0
        public IActionResult Create(Comment comment)
        {
            if (ModelState.IsValid)
            {
                // Set CreateDate and LastUpdateDate
                comment.CreatedDate     = DateTime.Now;
                comment.LastUpdatedDate = DateTime.Now;

                string authorId = context
                                  .Users
                                  .Where(u => u.UserName == User.Identity.Name)
                                  .SingleOrDefault()
                                  .Id;

                comment.AuthorId = authorId;

                Topic topic = context.Topics.Find(comment.TopicId);
                topic.LastUpdatedDate = DateTime.Now;

                context.Comments.Add(comment);
                context.SaveChanges();

                return(Redirect($"/Topic/Details/{comment.TopicId}"));
            }

            return(View(comment));
        }
        public IActionResult Create(string categoryName, Topic topic)
        {
            if (ModelState.IsValid)
            {
                //set CreateDate and LastUpdatedDate
                topic.CreatedDate     = DateTime.Now;
                topic.LastUpdatedDate = DateTime.Now;
                //get user id
                string authorId = context.Users
                                  .Where(u => u.UserName == User.Identity.Name)
                                  .First()
                                  .Id;
                //set topic author id
                topic.AuthorId = authorId;

                if (!context.Categories.Any(c => c.Name == categoryName))
                {
                    return(View(topic));
                }
                int categoryId = context.Categories.SingleOrDefault(c => c.Name == categoryName).Id;

                topic.CategoryId = categoryId;


                //save topic
                context.Topics.Add(topic);
                context.SaveChanges();
                return(RedirectToAction("Index", "Home"));
            }

            return(View(topic));
        }
Exemple #3
0
        public void CreateTopic(Topic topic)
        {
            // add the new topic
            _db.Topics.Add(topic);

            // persist the changes into the database
            _db.SaveChanges();
        }
        public void CreatePost(Post post)
        {
            // add the new post
            _db.Posts.Add(post);

            // persist the changes into the database
            _db.SaveChanges();
        }
 public void AddTheme(string model)
 {
     _context.Themes.Add(new Theme()
     {
         Theme_type = model
     });
     _context.SaveChanges();
 }
        public User Create(string username, int password)
        {
            //Suzdavame si usera, i User.cs klasa imame konstruktor koito priema samo username i parola koito shte polzvame
            var user = new User(username, password);

            context.Users.Add(user);
            context.SaveChanges();

            return(user);
        }
Exemple #7
0
        public User Create(string username, string password)
        {
            var user = new User(username, password);

            context.Users.Add(user);

            context.SaveChanges();

            return(user);
        }
        public Post Create(Category category, string title, string content, User author)
        {
            //Suzdavame si samiq post

            Post post = new Post(title, content, category, author);

            context.Add(post);
            context.SaveChanges();

            return(post);
        }
        public TModel Create <TModel>(string username, string password)
        {
            User user = new User(username, password);

            context.Users.Add(user);
            context.SaveChanges();

            TModel dto = Mapper.Map <TModel>(user);

            return(dto);
        }
Exemple #10
0
        public ActionResult Create([Bind(Include = "CategoryId,CategoryName,CategoryDescription")] Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.Add(category);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(category));
        }
Exemple #11
0
        public ActionResult Create([Bind(Include = "CategoryId,Name,Description,ParentId")] Category category)
        {
            if (ModelState.IsValid)
            {
                db.Categories.Add(category);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ParentId = new SelectList(db.Categories, "CategoryId", "Name", category.ParentId);
            return(View(category));
        }
        public ActionResult Create([Bind(Include = "MessageId,SenderId,ReceiverId,Text,SendDate,Seen")] Message message)
        {
            if (ModelState.IsValid)
            {
                db.Messages.Add(message);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ReceiverId = new SelectList(db.Users, "Id", "UserName", message.ReceiverId);
            ViewBag.SenderId   = new SelectList(db.Users, "Id", "UserName", message.SenderId);
            return(View(message));
        }
Exemple #13
0
        public ActionResult Create([Bind(Include = "PostId,Body,AuthorId,CreatedTime,ThreadId")] Post post)
        {
            if (ModelState.IsValid)
            {
                db.Posts.Add(post);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.AuthorId = new SelectList(db.Users, "Id", "UserName", post.AuthorId);
            ViewBag.ThreadId = new SelectList(db.Threads, "ThreadId", "ThreadTitle", post.ThreadId);
            return(View(post));
        }
        public ActionResult Create([Bind(Include = "ThreadId,ThreadTitle,IsPinned,AuthorId,CategoryId")] Thread thread)
        {
            if (ModelState.IsValid)
            {
                db.Threads.Add(thread);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.AuthorId   = new SelectList(db.Users, "Id", "UserName", thread.AuthorId);
            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "Name", thread.CategoryId);
            return(View(thread));
        }
Exemple #15
0
        public ActionResult Create([Bind(Include = "Id, Body, Date")] Post post)
        {
            if (ModelState.IsValid)
            {
                post.Date = DateTime.Now;
                db.Posts.Add(post);
                db.SaveChanges();

                return(RedirectToAction("Index"));
            }

            return(View(post));
        }
Exemple #16
0
 public ActionResult Adicionar([Bind(Include = "ID,Nome,Login,Senha,PerfilDoUsuario")] Usuario usuario)
 {
     if (ModelState.IsValid)
     {
         if (AutenticarUsuario.VerificarSessaoUsuario() && AutenticarUsuario.VerificarSessaoUsuarioAdm())
         {
             db.Usuarios.Add(usuario);
             db.SaveChanges();
             return(RedirectToAction("Index"));
         }
         return(View(usuario));
     }
     return(Redirect("/Login"));
 }
        public Reply Create(string replyText, int postId, int authorId)
        {
            var reply = new Reply()
            {
                Content  = replyText,
                PostId   = postId,
                AuthorId = authorId
            };

            context.Replies.Add(reply);
            context.SaveChanges();

            return(reply);
        }
Exemple #18
0
        public IActionResult Delete(int id)
        {
            Topic topic = context.Topics
                          .Include(t => t.Author)
                          .SingleOrDefault(m => m.Id == id);

            if (topic != null)
            {
                context.Topics.Remove(topic);
                context.SaveChanges();
            }

            return(RedirectToAction("Index", "Home"));
        }
        public HttpResponseMessage PostRegisterUser(UserModel model)
        {
            var responceMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new ForumDbContext();
                using (context)
                {
                    this.ValidateUsername(model.Username);
                    this.ValidateNickname(model.Nickname);
                    this.ValidateAuthCode(model.AuthCode);

                    var usernameToLower = model.Username.ToLower();
                    var nicknameToLower = model.Nickname.ToLower();

                    var user = context.Users.FirstOrDefault(usr =>
                                                            usr.Username == usernameToLower || usr.Nickname.ToLower() == nicknameToLower);

                    if (user != null)
                    {
                        throw new InvalidOperationException("User Exists");
                    }

                    user = new User()
                    {
                        Username = usernameToLower,
                        Nickname = model.Nickname,
                        AuthCode = model.AuthCode
                    };

                    context.Users.Add(user);
                    context.SaveChanges();

                    user.SessionKey = this.GenerateSessionKey(user.Id);
                    context.SaveChanges();

                    var loggedModel = new LoggedUserModel()
                    {
                        SessionKey = user.SessionKey,
                        Nickname   = user.Nickname
                    };

                    var responce = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel);

                    return(responce);
                }
            });

            return(responceMsg);
        }
Exemple #20
0
        public IActionResult Create(Category category)
        {
            string authorId = context.Users.Where(u => u.UserName == this.User.Identity.Name)
                              .First()
                              .Id;

            category.AuthorId = authorId;
            if (ModelState.IsValid)
            {
                context.Categories.Add(category);
                context.SaveChanges();
                return(RedirectToAction("Index", "Home"));
            }
            return(View(category));
        }
Exemple #21
0
        public TModel Create <TModel>(string replyText, int postId, int authorId)
        {
            var reply = new Reply()
            {
                AuthorId = authorId,
                Content  = replyText,
                PostId   = postId
            };

            context.Replies.Add(reply);
            context.SaveChanges();

            var dto = Mapper.Map <TModel>(reply);

            return(dto);
        }
Exemple #22
0
        public ActionResult Edit(PostViewModel model)
        {
            if (ModelState.IsValid)
            {
                using (var db = new ForumDbContext())
                {
                    var post = db.Posts
                               .Find(model.PostId);

                    if (post == null || !IsAuthorized(post))
                    {
                        return(HttpNotFound());
                    }

                    post.Topic   = model.Topic;
                    post.Content = model.Content;

                    //db.Entry(post).State = EntityState.Modified;
                    db.SaveChanges();
                }

                return(RedirectToAction("ViewPost", new { id = model.PostId }));
            }

            return(View(model));
        }
        public Reply CreateReply(int postId, string content, User author)
        {
            Post post = context.Posts.Find(postId);

            Reply reply = new Reply()
            {
                Content = content,
                Post    = post,
                Author  = author
            };

            context.Replies.Add(reply);
            context.SaveChanges();

            return(reply);
        }
        public HttpResponseMessage PutLogoutUser(LoggedUserModel model)
        {
            var responceMsg = this.PerformOperationAndHandleExceptions(() =>
            {
                var context = new ForumDbContext();
                using (context)
                {
                    var sessionKey = model.SessionKey;

                    var user = context.Users.FirstOrDefault(usr =>
                                                            usr.SessionKey == sessionKey);


                    if (user == null)
                    {
                        throw new InvalidOperationException("Something went terribly wrong");
                    }


                    user.SessionKey = null;
                    context.SaveChanges();

                    var responce = this.Request.CreateResponse(HttpStatusCode.NoContent);

                    return(responce);
                }
            });

            return(responceMsg);
        }
Exemple #25
0
        public ActionResult Create(Post post)
        {
            if (ModelState.IsValid)
            {
                using (var db = new ForumDbContext())
                {
                    //var authorId = User.Identity.GetUserId();
                    //post.AuthorId = authorId;

                    Post dbPost = new Post
                    {
                        CategoryId = post.CategoryId,
                        Topic      = post.Topic,
                        Content    = post.Content,
                        AuthorId   = User.Identity.GetUserId()
                    };



                    db.Posts.Add(dbPost);
                    db.SaveChanges();


                    return(RedirectToAction("Index"));
                }
            }

            return(View(post));
        }
        public ActionResult CreateNewThread(string threadtitle, string threadcontent, int boardId, string userId)
        {
            using (ForumDbContext context = new ForumDbContext()) {
                Board board = context.Boards.Find(boardId);

                Thread thread = new Thread()
                {
                    Subject = threadtitle, BoardId = boardId, Board = board, Created = DateTime.UtcNow, CreatedBy = userId
                };
                context.Threads.Add(thread);

                StringBuilder sb = new StringBuilder(HttpUtility.HtmlEncode(threadcontent));
                sb.Replace("&gt;", ">"); //Because markdown-quotation

                Post post = new Post()
                {
                    Subject = thread.Subject, Content = sb.ToString(), Thread = thread, ThreadId = thread.ThreadId, Created = DateTime.UtcNow, CreatedBy = userId
                };
                thread.Posts.Add(post);



                //Post post = new Post() { Subject = thread.Subject, Content = threadcontent, Thread = thread, ThreadId = thread.ThreadId, Created = DateTime.UtcNow, CreatedBy = userId };
                //thread.Posts.Add(post);

                //Generera ett post-objekt också, sen lägg till i thread
                context.Posts.Add(post);
                context.SaveChanges();

                return(RedirectToAction("ShowBoard", new { id = boardId, _index = 0 }));
            }
        }
        public ActionResult DeletePost(int id)
        {
            using (ForumDbContext context = new ForumDbContext()) {
                bool ThreadWasDeleted = false;

                Post post = context.Posts.Find(id);

                int threadId = post.ThreadId;

                if (post != null)
                {
                    context.Posts.Remove(post);



                    //Is it the last post? Then delete the thread too.
                    if (context.Posts.Where(p => p.ThreadId == threadId).Count() == 1)
                    {
                        context.Threads.Remove(context.Threads.Find(threadId));
                        ThreadWasDeleted = true;
                    }
                    context.SaveChanges();
                }

                if (ThreadWasDeleted == true)
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    return(RedirectToAction("ShowThread", new { id = post.ThreadId, _index = 0 }));
                }
            }
        }
        public ActionResult CreateNewPost([ModelBinder(typeof(AllowHtmlBinder))] string postcontent, int threadId, string userId)
        {
            using (ForumDbContext context = new ForumDbContext()) {
                Thread thread = context.Threads.Find(threadId);

                const int MAX_LENGHT = 1000;
                if (postcontent.Length > MAX_LENGHT)
                {
                    postcontent = postcontent.Substring(0, MAX_LENGHT);
                }

                StringBuilder sb = new StringBuilder(HttpUtility.HtmlEncode(postcontent));
                sb.Replace("&gt;", ">"); //Because markdown-quotation

                Post post = new Post()
                {
                    Subject = thread.Subject, Content = sb.ToString(), Thread = thread, ThreadId = threadId, Created = DateTime.UtcNow, CreatedBy = userId
                };
                thread.Posts.Add(post);
                context.Posts.Add(post);
                context.SaveChanges();

                return(RedirectToAction("ShowThread", new { id = threadId, _index = 0 }));
            }
        }
        public ActionResult CadastrarUsuario(Usuario usuario)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    ForumDbContext ctx = new ForumDbContext();

                    var senhaCriptografada = FormsAuthentication.HashPasswordForStoringInConfigFile(usuario.Senha, "MD5");

                    usuario.Senha           = senhaCriptografada;
                    usuario.PerfilDoUsuario = Convert.ToInt16(ListaDePerfisDoUsuario.Usuario);

                    ctx.Usuarios.Add(usuario);
                    ctx.SaveChanges();

                    ViewBag.StatusCadastro = "Cadastro realizado com sucesso.";
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
            }

            return(View("Index"));
        }
        public ActionResult Edit(User user, String role)
        {
            if (ModelState.IsValid)
            {
                // Stamp so the identity doesn't crash
                user.SecurityStamp = Guid.NewGuid().ToString();

                db.Entry(user).State = EntityState.Modified;
                db.SaveChanges();

                // Delete all roles from user and add new one
                userManager.GetRoles(user.Id).ForEach(e => userManager.RemoveFromRole(user.Id, e));
                userManager.AddToRole(user.Id, role);
                return(RedirectToAction("Index"));
            }
            return(View(user));
        }