Exemple #1
0
        /// <summary>
        /// Method to create tag
        /// </summary>
        /// <param name=""></param>
        public bool Create(List <ITagsDTO> listOfTags, int questionId)
        {
            using (var context = new BufferOverflowDBEntities())
            {
                foreach (var tags in listOfTags)
                {
                    var isExist = context.Tags.SingleOrDefault(t => t.Tag == tags.Tag);
                    if (isExist == null)
                    {
                        Tags tagEntity = new Tags();
                        EntityConverter.FillEntityFromDTO(tags, tagEntity);
                        isExist = context.Tags.Add(tagEntity);
                        context.SaveChanges();
                    }
                    TagRelation tagRelationEntity = context.TagRelation.SingleOrDefault(t => t.TagId == isExist.Id && t.QuestionId == questionId);
                    if (tagRelationEntity == null)
                    {
                        tagRelationEntity            = new TagRelation();
                        tagRelationEntity.QuestionId = questionId;
                        tagRelationEntity.TagId      = isExist.Id;
                        context.TagRelation.Add(tagRelationEntity);
                        context.SaveChanges();
                    }
                }

                return(true);
            }
        }
        public IDataDTO CreateVote(IVoteDTO voteDTO)
        {
            using (var context = new BufferOverflowDBEntities())
            {
                var voteEntity = context.Votes.SingleOrDefault(v => v.UserId == voteDTO.UserId && v.AnswerId == voteDTO.AnswerId);
                //context.Votes.Where(v => v.Id == voteDTO.Id).Distinct();
                if (voteEntity == null)
                {
                    Votes vote = new Votes();
                    EntityConverter.FillEntityFromDTO(voteDTO, vote);
                    context.Votes.Add(vote);
                    context.SaveChanges();
                }
                else if (voteEntity.Vote != voteDTO.Vote)
                {
                    voteEntity.Vote = voteDTO.Vote;
                    context.SaveChanges();
                }

                var countUp   = context.Votes.Where(c => c.Vote == 1 && c.AnswerId == voteDTO.AnswerId).Count();
                var countDown = context.Votes.Where(c => c.Vote == 2 && c.AnswerId == voteDTO.AnswerId).Count();

                int totalVotes = countUp - countDown;

                IDataDTO dataDTO = (IDataDTO)DTOFactory.Instance.Create(DTOType.DataDTO);
                dataDTO.TotalVote  = totalVotes;
                dataDTO.VoteDetail = voteDTO;

                return(dataDTO);
            }
        }
        /// <summary>
        /// Method to delete answer
        /// </summary>
        /// <param name="answerDTO"></param>
        /// <returns></returns>
        public IAnswerDTO Delete(IAnswerDTO answerDTO)
        {
            using (var context = new BufferOverflowDBEntities())
            {
                var deleteAnswer = context.Answers
                                   .SingleOrDefault(a => a.Id == answerDTO.Id);

                if (deleteAnswer != null)
                {
                    // Delete Vote
                    var deleteVote = context.Votes.Where(v => v.AnswerId == answerDTO.Id).ToList();
                    foreach (var vote in deleteVote)
                    {
                        context.Votes.Remove(vote);
                        context.SaveChanges();
                    }

                    // Delete Answer
                    var answer = context.Answers.Remove(deleteAnswer);
                    context.SaveChanges();
                    var question = context.Questions.SingleOrDefault(q => q.Id == answer.QuestionId);
                    question.AnswerCount--;
                    context.SaveChanges();
                    EntityConverter.FillDTOFromEntity(answer, answerDTO);
                }
                return(answerDTO);
            }
        }
        /// <summary>
        /// Method to create answer
        /// </summary>
        /// <param name="answerDTO"></param>
        /// <returns></returns>
        public IAnswerDTO Create(IAnswerDTO answerDTO)
        {
            using (var context = new BufferOverflowDBEntities())
            {
                Answers answerEntity = new Answers();
                EntityConverter.FillEntityFromDTO(answerDTO, answerEntity);

                var answer = context.Answers.Add(answerEntity);
                context.SaveChanges();

                if (answer != null)
                {
                    var question = context.Questions.SingleOrDefault(q => q.Id == answer.QuestionId);
                    question.AnswerCount++;
                    context.SaveChanges();
                    EntityConverter.FillDTOFromEntity(answer, answerDTO);
                }

                return((answerDTO.Id != 0) ? answerDTO : null);;
            }
        }
Exemple #5
0
        /// <summary>
        /// Method to delete question
        /// </summary>
        /// <param name="questionDTO"></param>
        /// <returns></returns>
        public IQuestionDTO Delete(IQuestionDTO questionDTO)
        {
            using (var context = new BufferOverflowDBEntities())
            {
                var deleteQuestion = context.Questions
                                     .SingleOrDefault(q => q.Id == questionDTO.Id);

                if (deleteQuestion != null)
                {
                    // Deleting Answer
                    var deleteAnswers = context.Answers.Where(a => a.QuestionId == questionDTO.Id).ToList();

                    foreach (var answer in deleteAnswers)
                    {
                        var votesEntity = context.Votes.Where(v => v.AnswerId == answer.Id).ToList();
                        foreach (var vote in votesEntity)
                        {
                            context.Votes.Remove(vote);
                            context.SaveChanges();
                        }
                        context.Answers.Remove(answer);
                        context.SaveChanges();
                    }

                    // Deleting from Tag Relation
                    var deleteTagRelation = context.TagRelation.Where(t => t.QuestionId == questionDTO.Id).ToList();
                    foreach (var tag in deleteTagRelation)
                    {
                        context.TagRelation.Remove(tag);
                        context.SaveChanges();
                    }

                    // Deleting Question
                    var question = context.Questions.Remove(deleteQuestion);
                    context.SaveChanges();
                    EntityConverter.FillDTOFromEntity(question, questionDTO);
                }
                return(questionDTO);
            }
        }
Exemple #6
0
        /// <summary>
        /// Method to edit question
        /// </summary>
        /// <param name="questionDTO"></param>
        /// <returns></returns>
        public IQuestionDTO Edit(IQuestionDTO questionDTO)
        {
            using (var context = new BufferOverflowDBEntities())
            {
                var questionEntity = context.Questions.SingleOrDefault(q => q.Id == questionDTO.Id);
                if (questionEntity != null)
                {
                    EntityConverter.FillEntityFromDTO(questionDTO, questionEntity);
                    context.SaveChanges();
                }
                else
                {
                    questionDTO.Id = 0;
                }

                return((questionDTO.Id != 0) ? questionDTO : null);
            }
        }
Exemple #7
0
        /// <summary>
        /// Method to create question
        /// </summary>
        /// <param name="questionDTO"></param>
        /// <returns></returns>
        public IQuestionDTO Create(IQuestionDTO dataDTO)
        {
            using (var context = new BufferOverflowDBEntities())
            {
                Questions questionEntity = new Questions();
                EntityConverter.FillEntityFromDTO(dataDTO, questionEntity);

                var question = context.Questions.Add(questionEntity);
                context.SaveChanges();

                IQuestionDTO questionDTO = (IQuestionDTO)DTOFactory.Instance.Create(DTOType.QuestionDTO);
                if (question != null)
                {
                    EntityConverter.FillDTOFromEntity(question, dataDTO);
                }

                return((dataDTO.Id != 0) ? dataDTO : null);;
            }
        }
        /// <summary>
        /// Method to register user
        /// </summary>
        /// <param name="userDTO"></param>
        /// <returns></returns>
        public IUserDTO Register(IUserDTO userDTO)
        {
            using (var context = new BufferOverflowDBEntities())
            {
                User user = new User();
                EntityConverter.FillEntityFromDTO(userDTO, user);
                string Password = user.Password;

                string Encrypted = EncryptionAndDecryption.Encrypt(Password, EncryptionAndDecryption.EncryptionKey);
                user.Password = Encrypted;

                bool isExist = context.User.Any(u => u.Email == user.Email);
                if (!isExist)
                {
                    user = context.User.Add(user);
                    context.SaveChanges();
                    EntityConverter.FillDTOFromEntity(user, userDTO);
                }

                return((userDTO.Id != 0) ? userDTO : null);
            }
        }