Пример #1
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()));
        }
Пример #2
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)));
        }
Пример #3
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()));
        }
Пример #4
0
        public IActionResult Create([Bind("Title", "Body")] BlogPost post)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

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

            _dbContext.Add(post);
            _dbContext.SaveChanges();

            return(RedirectToAction("Post", "Blog", new {
                year = post.Posted.Year,
                month = post.Posted.Month,
                key = post.Key
            }));
        }
Пример #5
0
        static void Main(string[] args)
        {
            using (var db = new BlogDataContext())
            {
                var blogUser  = db.BlogUsers.Where(x => x.UserId == "testuser").FirstOrDefault();
                var blogUser2 = db.BlogUsers.Where(x => x.UserId == "test01").FirstOrDefault();

                Console.WriteLine("user count : " + db.BlogUsers.Count());
                Console.WriteLine("article count : " + db.BlogArticle.Count());

                //BlogUsers user = new BlogUsers();
                //user.UserId = "test01";
                //user.UserName = "******";
                //user.BlogUrl = "test_blog_url";

                //db.Add(user);
                //db.SaveChanges();


                // Parent Article
                BlogArticle article      = new BlogArticle();
                int         articleCount = db.BlogArticle.Count();
                article.ArticleNo = articleCount + 1;
                article.WriteUser = blogUser.UserId;
                article.ParentNo  = article.ArticleNo;
                article.Content   = $"test content is {article.ArticleNo}";
                db.Add(article);
                //db.SaveChanges();

                // Child Article
                BlogArticle articleChild = new BlogArticle();
                articleChild.ArticleNo = article.ArticleNo + 1;
                articleChild.WriteUser = blogUser2.UserId;
                articleChild.Content   = $"child test content from parent article no {article.ArticleNo}";
                articleChild.ParentNo  = article.ArticleNo;
                db.Add(articleChild);
                //db.SaveChanges();

                // Not exists Blog User's Article
                BlogArticle articleOfNoneUser = new BlogArticle();
                articleCount = db.BlogArticle.Count();
                articleOfNoneUser.ArticleNo = articleCount + 1;
                articleOfNoneUser.ParentNo  = articleChild.ArticleNo;
                articleOfNoneUser.WriteUser = blogUser.UserId;
                articleOfNoneUser.Content   = $"this is none of parent's article";
                //db.Add(articleOfNoneUser);
                //db.SaveChanges();

                // Blog User Insert


                //db.Add(article);
                //db.Add(articleChild);
                //db.SaveChanges();

                Console.WriteLine("article count : " + db.BlogArticle.Count());
                string jsonResult = JsonConvert.SerializeObject(db.BlogArticle.ToList <BlogArticle>());
                Console.ReadLine();

                //List<Goods> goodsList = new List<Goods>();
                //Goods good1 = new Goods() { GoodsNo = 1, GoodsNm = "goods 1", Price = 1000, SetGoodsYn = false };
                //Goods good2 = new Goods() { GoodsNo = 2, GoodsNm = "goods 2", Price = 1500, SetGoodsYn = false };
                //goodsList.Add(good1);
                //goodsList.Add(good2);
                //List<Goods> partialGoods = new List<Goods>();
                //partialGoods.Add(good1);
                //partialGoods.Add(good2);

                //goodsList.Add(new Goods() { GoodsNo = 3, GoodsNm = "goods_3", Price = 2500, SetGoodsYn = true, CompliationGoods = partialGoods });

                //Goods good4 = new Goods() { GoodsNo = 4, GoodsNm = "goods_4", Price = 1000, SetGoodsYn = true, CompliationGoods = goodsList };
                //List<Goods> goodsList2 = new List<Goods>();
                //goodsList2.AddRange(goodsList);
                //goodsList2.Add(good4);

                //Goods good5 = new Goods() { GoodsNo = 4, GoodsNm = "goods_4", Price = 1000, SetGoodsYn = true, CompliationGoods = goodsList2 };
                //List<Goods> goodsList3 = new List<Goods>();
                //goodsList3.AddRange(goodsList2);
                //goodsList3.Add(good5);

                //string jsonResult2 = JsonConvert.SerializeObject(goodsList);
                //Console.ReadLine();
            }
        }