Пример #1
0
 public ActionResult Create(Blog blog)
 {
     if (ModelState.IsValid)
     {
         _db.Blogs.Add(blog);
         _db.SaveChanges();
         return(RedirectToAction("Index"));
     }
     return(View(blog));
 }
        public Comment Post(string postKey, [FromBody]Comment comment)
        {
            var post = _db.Posts.FirstOrDefault(x => x.Key == postKey);
            if (post == null)
                return null;
            comment.Post = post;
            comment.Posted = DateTime.Now;
            comment.Author = User.Identity.Name;

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

            return comment;
        }
Пример #3
0
        public ActionResult AddComment([FromBody] Comment comment)
        {
            if (HttpContext.Session.GetInt32("userId") == null ||
                comment.UserId != HttpContext.Session.GetInt32("userId"))
            {
                return(Ok(Result.Fail("登录信息已过期,请重新登录")));
            }

            var article = _blogDataContext.Article.Find(comment.ArticleId);

            if (article == null)
            {
                return(Ok(Result.Fail("找不到所评论的文章")));
            }

            comment.DeliverDate = DateTime.Now;
            _blogDataContext.Add(comment);
            article.CommitCount++;
            _blogDataContext.Update(article);
            try
            {
                _blogDataContext.SaveChanges();
            }
            catch
            {
                return(Ok(Result.Fail("保存评论失败")));
            }

            return(Ok(Result.Success(comment)));
        }
Пример #4
0
        public ActionResult SignUp([FromBody] SignUpInfor signUpInfor)
        {
            User user = new User()
            {
                Username = signUpInfor.Username,
                Nickname = signUpInfor.Nickname,
                Salt     = Guid.NewGuid()
            };
            //加盐储存密码
            var salt = user.Salt.ToString();

            byte[] passwordAndSaltBytes = System.Text.Encoding.UTF8.GetBytes(signUpInfor.Password + salt);
            byte[] hashBytes            = new System.Security.Cryptography.SHA256Managed().ComputeHash(passwordAndSaltBytes);
            string hashString           = Convert.ToBase64String(hashBytes);

            user.PasswordHash = hashString;

            if (_blogDataContext.User.FirstOrDefault(i => i.Username == user.Username) != null)
            {
                return(Ok(Result.Fail("该用户已存在")));
            }

            _blogDataContext.Add(user);
            try
            {
                _blogDataContext.SaveChanges();
            }
            catch
            {
                return(Ok(Result.Fail("注册失败")));
            }
            return(Ok(Result.Success()));
        }
Пример #5
0
        public IActionResult Create(Post post) //method signature must be different from the one above, despite the attributes HttpGet and HttpPost. So a param can be added.
        {                                      //Here we are passing a complex type. The Post object as a param to thsi controller action. There are some properties in the Post class that a user should not
         //be posting values for, such as the Author or Date (Posted). Three ways to handle this below. In this case I am using option 3, but it not always be this way.
         //It varies based on situation.

            if (!ModelState.IsValid) //this returns a simple yes or no answer as to whether the user has satisfied all of the model validation rules in the Post class
            {
                return(View());
            }

            post.Authors = User.Identity.Name;
            post.Posted  = DateTime.Now;

            /* Notice how saving the post to the database is a two step process. Entity Framework works on the Unit of Work pattern,
             * which is really just a fancy way of saying that first you tell the data context everything that you want to do. For example,
             * adding a new post to the database. Then, the second step is to tell the data context to actually go ahead and execute what you've asked for,
             * which we do by calling the save changes */
            _db.Posts.Add(post);
            _db.SaveChanges();

            //return View();
            //update the Create action to Redirect to the new Post once it's been successfully created.
            //Notice how I'm passing in the parameters using an anonymous type, just like the ActionLink Helper in the View.
            return(RedirectToAction("Post", "Blog", new
            {
                year = post.Posted.Year,
                month = post.Posted.Month,
                key = post.Key
            }));
        }
Пример #6
0
        public IActionResult Create(Post post)
        {
            //Erik - 10/6/2017 Validate, input form validated data is valid
            if (!ModelState.IsValid)
            {
                return(View());
            }

            #region Overriding Author and Posted Values to disallow setting the properties manually upstream
            post.Author = User.Identity.Name;
            post.Posted = DateTime.Now;
            #endregion

            _db.Posts.Add(post);
            _db.SaveChanges();
            #region Depricated - RedirectToAction
            //return View();
            #endregion
            return(RedirectToAction("Post", "Blog", new
            {
                year = post.Posted.Year,
                month = post.Posted.Month,
                key = post.Key
            }));
        }
Пример #7
0
        public IActionResult Create(Post post)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            _db.Posts.Add(new Post {
                Title  = post.Title,
                Body   = post.Body,
                Author = "Alican Demirtas",
                Posted = DateTime.Now
            });
            _db.SaveChanges();

            return(RedirectToAction(
                       "Post",
                       "Blog",
                       new
            {
                year = post.Posted.Year,
                month = post.Posted.Month,
                key = post.Key
            }
                       ));
        }
Пример #8
0
        public ActionResult EditArticle([FromBody] Article article)
        {
            var userId = HttpContext.Session.GetInt32("userId");

            if (userId == null)
            {
                return(Ok(Result.Fail("登录信息已过期,请重新登录")));
            }

            article.UserId = userId.Value;
            if (article.Id == 0)
            {
                //id为0是添加文章
                article.DeliverTime = DateTime.Now;
                _blogDataContext.Add(article);
            }
            else
            {
                //id不为-1是修改文章
                var oldArticle = _blogDataContext.Article.Find(article.Id);
                if (oldArticle == null)
                {
                    return(Ok(Result.Fail("未找到修改的文章,请退出重试")));
                }
                else if (oldArticle.UserId != article.UserId)
                {
                    return(Ok(Result.Fail("修改的文章不属于当前用户")));
                }

                oldArticle.Title   = article.Title;
                oldArticle.Content = article.Content;
                _blogDataContext.Article.Update(oldArticle);
            }

            try
            {
                _blogDataContext.SaveChanges();
            }
            catch (Exception e)
            {
                return(Ok(Result.Fail(e.Message)));
            }

            return(Ok(Result.Success()));
        }
        public IActionResult Post(string postKey, [FromBody] Comment comment)
        {
            var post = _db.Posts.FirstOrDefault(p => p.Key == postKey);

            if (post == null)
            {
                return(NotFound());
            }

            comment.Post   = post;
            comment.Posted = DateTime.Now;
            comment.Author = User.Identity.Name;

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

            return(StatusCode(201, comment));
        }
Пример #10
0
        public Comment Post(string postKey, [FromBody] Comment comment) //From body of the http request
        {
            var post = _db.Posts.FirstOrDefault(p => p.Key == postKey);

            if (post == null)
            {
                return(null);
            }

            comment.Post   = post;
            comment.Posted = DateTime.Now;
            comment.Author = User.Identity.Name;

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

            return(comment); //return the saved comment back to client
        }
Пример #11
0
        public HttpResponseMessage CreatePost(Post post)
        {
            if (post == null)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }

            try
            {
                db.Posts.Add(post);
                db.SaveChanges();
                var result = post;
                return(Request.CreateResponse(HttpStatusCode.OK, result));
            }
            catch (Exception)
            {
                return(Request.CreateResponse(HttpStatusCode.BadRequest));
            }
        }
Пример #12
0
        public IActionResult Visa(Visa visa)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            _db.VisaTypes.Add(visa);
            _db.SaveChanges();

            return(View());
        }
Пример #13
0
        public IActionResult QuestionCreate(InterviewQuestion postt)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            _db.QuestionAnswerr.Add(postt);
            _db.SaveChanges();

            return(View());
        }
Пример #14
0
        public ActionResult DeleteBlog(int id)
        {
            _context = new BlogDataContext();
            var blog = _context.Blogs.SingleOrDefault(b => b.Id == id);

            if (blog != null)
            {
                _context.Blogs.Remove(blog);
                _context.SaveChanges();
            }

            return(RedirectToAction("Index"));
        }
Пример #15
0
        public ActionResult AddPost([Bind(Include = "Id,Title,Author,Content,Date")] Post post)
        {
            if (ModelState.IsValid)
            {
                post.Date   = DateTime.Now;
                post.Author = "John Smith";
                db.Posts.Add(post);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(RedirectToAction("Index"));
        }
Пример #16
0
        public ActionResult Create(Post post)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }
            post.Author = User.Identity.Name;
            post.Posted = DateTime.Now;

            _db.Posts.Add(post);
            _db.SaveChanges();

            return(RedirectToAction("Post", "Blog", new { year = post.Posted.Year, month = post.Posted.Month, key = post.Key }));
        }
Пример #17
0
        public IActionResult PostRegister(Phonebook phonebook)
        {
            if (!ModelState.IsValid)
            {
                return(View(phonebook));
            }
            //Priority!
            _dbcontext.phonebooks.Update(phonebook);

            _dbcontext.SaveChanges();
            //await _dbcontext.SaveChangesAsync();
            return(RedirectToAction("Contact"));
            //return View("Contact");
        }
        public IActionResult Create(Post post)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            post.Author = User.Identity.Name;
            post.Posted = DateTime.Now;

            _db.Posts.Add(post);
            _db.SaveChanges();
            return(View());
        }
        //public void Post([FromBody]string value)
        #endregion
        public Comment Post(string postKey, [FromBody] Comment comment)
        {
            #region Retrieve associated Post
            var post = _db.Posts.FirstOrDefault(x => x.Key == postKey);
            #endregion

            if (post == null)
            {
                return(null);
            }

            #region If Post exists, associate post, set posted date and author then save to database
            comment.Post   = post;
            comment.Posted = DateTime.Now;
            comment.Author = User.Identity.Name;

            _db.Comments.Add(comment);
            _db.SaveChanges();
            #endregion

            //Erik - 10/9/2017 Return persisted comment to api caller
            return(comment);
        }
Пример #20
0
        public IActionResult Create(Post post)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            post.Author = User.Identity.Name;
            post.Posted = DateTime.Now;

            _blogDataContext.Posts.Add(post);
            _blogDataContext.SaveChanges();

            return(View("index", _blogDataContext.Posts.ToArray()));
        }
Пример #21
0
        public IActionResult Create(Post p)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }



            p.Posted = DateTime.Now;

            _db.Posts.Add(p);
            _db.SaveChanges();
            return(RedirectToAction("Post", "Blog", new { year = p.Posted.Year, month = p.Posted.Month, key = p.Key }));
        }
Пример #22
0
        static void Main(string[] args)
        {
            using (BlogDataContext db = new BlogDataContext())
            {
                Blog blog = new Blog
                {
                    BlogId = 0,
                    Name   = "New blog",
                    Url    = "teste"
                };
                db.Blogs.Add(blog);
                db.SaveChanges();
            }

            Console.ReadKey();
        }
Пример #23
0
        public IActionResult Create(Post post57)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            post57.Author57 = User.Identity.Name;
            post57.Posted57 = DateTime.Now;

            _db57.Posts57.Add(post57);
            _db57.SaveChanges();

            return(RedirectToAction("Post", "Blog57", new
            {
                year = post57.Posted57.Year,
                month = post57.Posted57.Month,
                key = post57.Key57
            }));
        }
        //public IActionResult Create([Bind("Title", "Body")]Post post) //--> Bind definuje ktore properties mozu byt naplnene uzivatelom z formu
        public IActionResult Create(Post post) //mozne namiesto Post pouzit novy objekt (pr. CreatePostRequest), ktory obsahuje len property naplnene vo forme
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            //popr je mozne overridnut hodnoty pre properties ktore mohol uzivatel zadat, no nemali by byt samotnym uzivatelom menene
            post.Author = User.Identity.Name;
            post.Posted = DateTime.Now;

            //EF pracuje na principe Unit of work patternu
            _db.Posts.Add(post); //informuje co je treba urobit --> pridat entitu
            _db.SaveChanges();   // vykonanie pozadovanych zmien  --> ulozenie do DB

            return(RedirectToAction("Post", "Blog", new
            {
                year = post.Posted.Year,
                month = post.Posted.Month,
                key = post.Key
            }));
        }
Пример #25
0
        public ActionResult EditBlog(BlogViewModel blogView, HttpPostedFileBase photoFile)
        {
            _context = new BlogDataContext();
            var blog = _context.Blogs.SingleOrDefault(b => b.Id == blogView.Id);

            if (blog != null)
            {
                blog.Content = blogView.Content;
                blog.Title   = blogView.Title;
                if (photoFile != null)
                {
                    blog.Photo = $@"{DateTime.UtcNow.Ticks}_{photoFile.FileName}";
                    //_blog.Photo = $@"{Guid.NewGuid()}{photoFile.FileName}";

                    var path = Path.Combine(Server.MapPath("~/Content/Upload/Photos"), blog.Photo);
                    photoFile.SaveAs(path);
                }

                _context.SaveChanges();
                return(RedirectToAction("Index"));
            }
            return(RedirectToAction("EditBlog", new { id = blogView.Id }));
        }
        public IActionResult Create(Post post)
        {
            // If model isn't valid send people back to View to fix errors
            if (!ModelState.IsValid)
            {
                return(View());
            }

            // Overwriting data that don't want the user to set
            post.Author = User.Identity.Name;
            post.Posted = DateTime.Now;

            // Save to DB
            _db.Posts.Add(post);
            _db.SaveChanges();

            return(RedirectToAction("Post", "Blog", new
            {
                year = post.Posted.Year,
                month = post.Posted.Month,
                key = post.Key
            }));
        }
Пример #27
0
        public ActionResult Create(BlogViewModel model, HttpPostedFileBase photoFile)
        {
            var _blog = new Blogs();

            _blog.Title    = model.Title;
            _blog.Content  = model.Content;
            _blog.PostDate = DateTime.Now;

            if (photoFile != null)
            {
                _blog.Photo = $@"{DateTime.UtcNow.Ticks}_{photoFile.FileName}";
                //_blog.Photo = $@"{Guid.NewGuid()}{photoFile.FileName}";

                var path = Path.Combine(Server.MapPath("~/Content/Upload/Photos"), _blog.Photo);
                photoFile.SaveAs(path);
            }

            BlogDataContext _context = new BlogDataContext();

            _context.Blogs.Add(_blog);
            _context.SaveChanges();
            return(RedirectToAction("Index"));
        }
Пример #28
0
        public IActionResult Create(Interview interview)
        {
            if (interview.visa_ID == 0)
            {
                ModelState.AddModelError("", "Select Country");
            }

            // ------- Getting selected Value ------- //
            int SelectValue = interview.visa_ID;

            ViewBag.SelectedValue = interview.visa_ID;



            // ------- Setting Data back to ViewBag after Posting Form ------- //

            List <Visa> countrylist = new List <Models.Visa>();

            countrylist = (from product in _db.VisaTypes
                           select product).ToList();

            countrylist.Insert(0, new Visa {
                id = 0, name = "Select"
            });
            ViewBag.ListofCountry = countrylist;

            if (!ModelState.IsValid)
            {
                return(View());
            }

            _db.Interview.Add(interview);
            _db.SaveChanges();

            return(View());
        }
Пример #29
0
 public void Dispose()
 {
     context.SaveChanges();
 }
Пример #30
0
 public void Delete(int id)
 {
     blog.Set <T>().Remove(GetByID(id));
     blog.SaveChanges();
 }