Beispiel #1
0
        public IActionResult Add(int id)
        {
            try
            {
                if (id == UserModel.Id)
                {
                    return(Json(false.ToResult("不能关注自己")));
                }

                var entity = new Follow {
                    FollowAccountId = id
                };
                entity.AccountId  = UserModel.Id;
                entity.CreateTime = DateTime.Now;
                _context.Follow.Add(entity);
                _context.SaveChanges();
                return(Json(true.ToResult()));
            }
            catch (DbUpdateException ex)
            {
                return(Json(false.ToResult("请勿重复操作")));
            }
            catch (Exception ex)
            {
                return(Json(false.ToResult("未知错误,请联系管理人员")));
            }
        }
Beispiel #2
0
        public IActionResult CatalogAdd(CatalogModel model)
        {
            var entity = model.MapTo <CatalogModel, Catalog>();

            entity.CreateTime = DateTime.Now;
            entity.Cover      = model.Cover.Upload(_hostEnvironment.WebRootPath, "catalog");
            _context.Catalog.Add(entity);
            _context.SaveChanges();
            return(Json(true.ToResult()));
        }
Beispiel #3
0
        public IActionResult Update(AccountModel model)
        {
            if (!string.IsNullOrWhiteSpace(model.NickName) && Encoding.UTF8.GetByteCount(model.NickName) <= 16)
            {
                var entity = _context.Account.Find(model.Id);
                entity.NickName = model.NickName;
                _context.Account.Update(entity);
                _context.SaveChanges();
            }

            return(RedirectToAction("Info", "My"));
        }
Beispiel #4
0
        public IActionResult Publish(CommentModel model)
        {
            model.AccountId = UserModel.Id;

            if (model.AccountId == model.QuoteAccountId)
            {
                return(Json(false.ToResult("不能引用自己")));
            }

            // 发表评论
            var entity = model.MapTo <CommentModel, Comment>();

            entity.CreateTime = DateTime.Now;
            _context.Comment.Add(entity);
            int affect = _context.SaveChanges();

            if (affect > 0)
            {
                return(Json(true.ToResult()));
            }
            return(Json(false.ToResult()));
        }
Beispiel #5
0
        public IActionResult Feedback(FeedbackModel model)
        {
            var entity = model.MapTo <FeedbackModel, Feedback>();

            entity.CreateTime = DateTime.Now;
            _context.Feedback.Add(entity);
            int affect = _context.SaveChanges();

            if (affect > 0)
            {
                return(RedirectToAction("Success", "Home"));
            }

            return(RedirectToAction("Error", "Home"));
        }
Beispiel #6
0
        public IActionResult Register(AccountModel model)
        {
            if (!Regex.IsMatch(model.Account1, @"^[a-z|A-z|_]\w{3,29}$") || !Regex.IsMatch(model.Password, @"^\w{6,20}$") || string.IsNullOrWhiteSpace(model.NickName) || Encoding.UTF8.GetByteCount(model.NickName) > 16)
            {
                return(View(model));
            }

            var entity = new Account();

            entity.Account1   = model.Account1.ToLower();
            entity.NickName   = model.NickName;
            entity.Avatar     = "/images/avatar_none.png";
            entity.Password   = Md5Helper.Encrypt(model.Password);
            entity.CreateTime = DateTime.Now;
            _context.Account.Add(entity);

            _context.SaveChanges();

            return(RedirectToAction("Index", "Home"));
        }
Beispiel #7
0
        public IActionResult Detail(int id, int page = 1)
        {
            var model = _context.Topic.Find(id).MapTo <Topic, TopicModel>();                                   // 主题

            model.Catalog            = _context.Catalog.Find(model.CatalogId).MapTo <Catalog, CatalogModel>(); // 话题
            model.Catalog.TopicCount = _context.Topic.Where(x => x.CatalogId == model.CatalogId).Count();      // 主题总数
            model.Account            = _context.Account.Find(model.AccountId).MapTo <Account, AccountModel>(); // 用户

            /*****标签*****/
            //string tagSql = @"SELECT T1.Id, T1.TagName, T1.CreateTime FROM tag T1
            //    INNER JOIN topic_tag T2 ON T1.Id = T2.TagId
            //    WHERE T2.TopicId = @TopicId ORDER BY T1.Id ASC";
            //var parameter = _context.Database.GetDbConnection().CreateParameter("@TopicId", id);
            //model.TagList = _context.Tag.FromSqlRaw(tagSql, parameter).ToList().MapTo<List<Tag>, List<TagModel>>();   // 原生SQL写法
            //model.TagList = (from tag in _context.Tag join topictag in _context.TopicTag on tag.Id equals topictag.TagId where topictag.TopicId == id select tag).OrderBy(x => x.Id).ToList().MapTo<List<Tag>, List<TagModel>>();    // Linq写法
            model.TagList = _context.Tag.Join(_context.TopicTag.Where(x => x.TopicId == id), tag => tag.Id, topictag => topictag.TagId, (tag, topictag) => tag).OrderBy(x => x.Id).ToList().MapTo <List <Tag>, List <TagModel> >(); // Lamda写法

            model.IsThumbsUp   = _context.ThumbsUp.Any(x => x.ItemId == model.Id && x.IsThumb && x.AccountId == UserModel.Id && x.ItemType == ThumbsUpItemType.Topic.ToValue());                                                    // 当前登录人主题是否赞
            model.IsThumbsDown = _context.ThumbsUp.Any(x => x.ItemId == model.Id && !x.IsThumb && x.AccountId == UserModel.Id && x.ItemType == ThumbsUpItemType.Topic.ToValue());                                                   // 当前登录人主题是否踩
            model.IsFavorite   = _context.Favorite.Any(x => x.ItemId == model.Id && x.AccountId == UserModel.Id);                                                                                                                   // 当前登录人主题是否收藏

            // 评论列表,默认取15条
            model.CommentList = _context.Comment.Where(x => x.TopicId == id).OrderBy(x => x.Id).ToPagedList <Comment, CommentModel>(page, 15);
            // 评论所属用户
            var accountIdList = model.CommentList.Select(x => x.AccountId).ToList();
            var accounts      = _context.Account.Where(x => accountIdList.Contains(x.Id)).ToList().MapTo <List <Account>, List <AccountModel> >();
            // 评论引用用户
            var quoteIdList = model.CommentList.Select(x => x.QuoteAccountId).Distinct().ToList();
            var quotes      = _context.Account.Where(x => quoteIdList.Contains(x.Id)).ToList().MapTo <List <Account>, List <AccountModel> >();
            // 当前登录人评论是否赞/踩
            var commentIdList = model.CommentList.Select(x => x.Id).ToList();
            var thumbsUpList  = _context.ThumbsUp.Where(x => commentIdList.Contains(x.ItemId) && x.AccountId == UserModel.Id && x.ItemType == ThumbsUpItemType.Comment.ToValue()).ToList(); // 当前登录人评论赞/踩列表

            model.CommentList.ForEach(item =>
            {
                item.Account = accounts.FirstOrDefault(x => x.Id == item.AccountId); // 评论所属用户

                var quoteAccount = quotes.FirstOrDefault(x => x.Id == item.QuoteAccountId);
                if (quoteAccount != null)
                {
                    item.QuoteAccount = quoteAccount;                                         // 评论引用用户
                }
                item.IsThumbsUp   = thumbsUpList.Any(x => x.ItemId == item.Id && x.IsThumb);  // 当前登录人评论是否赞
                item.IsThumbsDown = thumbsUpList.Any(x => x.ItemId == item.Id && !x.IsThumb); // 当前登录人评论是否踩
            });

            _context.Database.ExecuteSqlRaw($"UPDATE topic SET TrailCount = TrailCount + 1 where Id = {id}");   // 更新浏览数
            _context.SaveChanges();
            // 插入浏览记录
            if (UserModel.IsLogin && UserModel.Id > 0)
            {
                _context.Trail.Add(new Trail
                {
                    AccountId  = UserModel.Id,
                    ItemId     = id,
                    CreateTime = DateTime.Now
                });
                _context.SaveChanges();
            }

            return(View(model));
        }