Ejemplo n.º 1
0
        public void Create(CommentCreateRequestDTO request)
        {
            var entity = _mapper.Map <Comment>(request);

            entity.AccountId = _identityUser.Id;
            _snsdbContext.Comments.Add(entity);
            _snsdbContext.SaveChanges();
        }
Ejemplo n.º 2
0
        public void Create(FeedbackCreateRequestDTO request)
        {
            var entity = _mapper.Map <Feedback>(request);

            _snsdbContext.Feedbacks.Add(entity);
            _snsdbContext.SaveChanges();
        }
Ejemplo n.º 3
0
        public LoginAuthModel Register(AccountRegisterRequestDTO request)
        {
            int     index  = RandomHelper.Default.Next(1, 101);
            Account entity = new Account
            {
                Account1 = request.Account,
                NickName = request.NickName,
                Pwd      = Md5Helper.Encrypt(request.Password),
                Avatar   = $"/heads/{index}_100.gif",
                Intro    = string.Empty
            };

            _snsdbContext.Accounts.Add(entity);
            _snsdbContext.SaveChanges();

            return(EnityToModel(entity));
        }
Ejemplo n.º 4
0
        public void CreateOrRemove(int followId)
        {
            if (followId == _identityUser.Id)
            {
                throw new CodeException("不能关注自己");
            }

            var entity = _snsdbContext.Follows.FirstOrDefault(x => x.AccountId == _identityUser.Id && x.FollowId == followId);

            if (entity != null)
            {
                _snsdbContext.Follows.Remove(entity);
            }
            else
            {
                entity = new Follow
                {
                    AccountId = _identityUser.Id,
                    FollowId  = followId
                };
                _snsdbContext.Follows.Add(entity);
            }
            _snsdbContext.SaveChanges();
        }
Ejemplo n.º 5
0
        public void CreateOrRemove(int articleId)
        {
            if (_snsdbContext.Articles.Any(x => x.Id == articleId && x.AccountId == _identityUser.Id))
            {
                throw new CodeException("不能收藏自己");
            }

            var entity = _snsdbContext.Favorites.FirstOrDefault(x => x.AccountId == _identityUser.Id && x.ArticleId == articleId);

            if (entity != null)
            {
                _snsdbContext.Favorites.Remove(entity);
            }
            else
            {
                entity = new Favorite
                {
                    AccountId = _identityUser.Id,
                    ArticleId = articleId
                };
                _snsdbContext.Favorites.Add(entity);
            }
            _snsdbContext.SaveChanges();
        }
Ejemplo n.º 6
0
        public int CreateOrUpdate(ArticleCreateOrUpdateRequestDTO request)
        {
            if (!_snsdbContext.Articles.Any(x => x.Id == request.Id))
            {
                Article article = new Article
                {
                    Title       = request.Title,
                    AccountId   = _identityUser.Id,
                    ContentType = ContentTypeEnum.Article.ToValue()
                };
                ArticleContent content = new ArticleContent
                {
                    Body = request.Body
                };
                Topic topic = _snsdbContext.Topics.AsNoTracking().FirstOrDefault(x => x.Name == request.TopicName);

                _snsdbContext.ExecuteTransaction(() =>
                {
                    // 文章
                    _snsdbContext.Articles.Add(article);
                    _snsdbContext.SaveChanges();
                    // 内容
                    content.ArticleId = article.Id;
                    _snsdbContext.ArticleContents.Add(content);
                    _snsdbContext.SaveChanges();
                    // 话题
                    if (topic == null)
                    {
                        topic = new Topic
                        {
                            Name = request.TopicName
                        };
                        _snsdbContext.Topics.Add(topic);
                        _snsdbContext.SaveChanges();
                    }
                    // 文章|话题 关系
                    ArticleTopic articleTopic = new ArticleTopic
                    {
                        ArticleId = article.Id,
                        TopicId   = topic.Id
                    };
                    _snsdbContext.ArticleTopics.Add(articleTopic);
                    _snsdbContext.SaveChanges();
                });
                return(article.Id);
            }
            else
            {
                // 文章
                Article article = _snsdbContext.Articles.Find(request.Id);
                article.Title          = request.Title;
                article.LastUpdateTime = DateTime.Now;
                // 内容
                ArticleContent articleContent = _snsdbContext.ArticleContents.FirstOrDefault(x => x.ArticleId == article.Id);
                if (articleContent != null)
                {
                    articleContent.Body = request.Body;
                }
                // 话题
                List <ArticleTopic> articleTopics = _snsdbContext.ArticleTopics.Where(x => x.ArticleId == article.Id).ToList();
                _snsdbContext.ArticleTopics.RemoveRange(articleTopics); // 先清空老的关系
                Topic topic = _snsdbContext.Topics.AsNoTracking().FirstOrDefault(x => x.Name == request.TopicName);
                _snsdbContext.ExecuteTransaction(() =>
                {
                    if (topic == null)
                    {
                        topic = new Topic
                        {
                            Name = request.TopicName
                        };
                        _snsdbContext.Topics.Add(topic);
                        _snsdbContext.SaveChanges();
                    }

                    // 文章|话题 关系
                    ArticleTopic articleTopic = new ArticleTopic
                    {
                        ArticleId = article.Id,
                        TopicId   = topic.Id
                    };
                    _snsdbContext.ArticleTopics.Add(articleTopic);
                    _snsdbContext.SaveChanges();
                });
                return(article.Id);
            }
        }