/// <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);
            }
        }
Beispiel #2
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);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Method to get user profile
        /// </summary>
        /// <param name="userDTO"></param>
        /// <returns></returns>
        public IUserDTO GetProfile(IUserDTO userDTO)
        {
            using (var context = new BufferOverflowDBEntities())
            {
                var user = context.User.SingleOrDefault(u => u.Id == userDTO.Id);
                if (user != null)
                {
                    EntityConverter.FillDTOFromEntity(user, userDTO);
                }

                return((userDTO.Id != 0) ? userDTO : null);
            }
        }
        public IAnswerDTO Edit(IAnswerDTO answerDTO)
        {
            using (var context = new BufferOverflowDBEntities())
            {
                var answerEntity = context.Answers.SingleOrDefault(a => a.Id == answerDTO.Id);

                if (answerEntity != null)
                {
                    EntityConverter.FillEntityFromDTO(answerDTO, answerEntity);
                }

                return(answerDTO);
            }
        }
        public List <IDataDTO> GetAnswers(IAnswerDTO answerDTO)
        {
            using (var context = new BufferOverflowDBEntities())
            {
                List <IDataDTO> listOfAnswers    = null;
                var             answerEntityList = context.Answers.Where(a => a.QuestionId == answerDTO.QuestionId).ToList();

                if (answerEntityList != null)
                {
                    listOfAnswers = new List <IDataDTO>();
                    foreach (var answerEntity in answerEntityList)
                    {
                        IAnswerDTO ansDTO = (IAnswerDTO)DTOFactory.Instance.Create(DTOType.AnswerDTO);
                        EntityConverter.FillDTOFromEntity(answerEntity, ansDTO);

                        // Getting user
                        var      userEntity = context.User.SingleOrDefault(u => u.Id == answerEntity.UserId);
                        IUserDTO userDTO    = (IUserDTO)DTOFactory.Instance.Create(DTOType.UserDTO);
                        EntityConverter.FillDTOFromEntity(userEntity, userDTO);
                        userDTO.Password = "";

                        // Getting  votes
                        var      voteEntity = context.Votes.SingleOrDefault(v => v.UserId == userEntity.Id && v.AnswerId == answerEntity.Id);
                        IVoteDTO voteDTO    = (IVoteDTO)DTOFactory.Instance.Create(DTOType.VoteDTO);
                        if (voteEntity != null)
                        {
                            EntityConverter.FillDTOFromEntity(voteEntity, voteDTO);
                        }
                        else
                        {
                            voteDTO.Vote = 0;
                        }

                        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.AnswerDetail = ansDTO;
                        dataDTO.UserDetail   = userDTO;
                        dataDTO.VoteDetail   = voteDTO;
                        dataDTO.TotalVote    = totalVotes;
                        listOfAnswers.Add(dataDTO);
                    }
                }
                Console.WriteLine("Hello hi " + listOfAnswers[0].TotalVote);
                return(listOfAnswers);
            }
        }
Beispiel #7
0
        /// <summary>
        /// Method to get all questions
        /// </summary>
        /// <returns></returns>
        public List <IDataDTO> GetQuestions()
        {
            using (var context = new BufferOverflowDBEntities())
            {
                List <IDataDTO> listOfQuestions    = null;
                var             questionEntityList = context.Questions.OrderByDescending(e => e.Created).ToList();

                if (questionEntityList != null)
                {
                    listOfQuestions = new List <IDataDTO>();
                    foreach (var questionEntity in questionEntityList)
                    {
                        IQuestionDTO questionDTO = (IQuestionDTO)DTOFactory.Instance.Create(DTOType.QuestionDTO);
                        EntityConverter.FillDTOFromEntity(questionEntity, questionDTO);

                        // Getting Question tags
                        var tagsEntityList = context.TagRelation.Where(t => t.QuestionId == questionEntity.Id).ToList();


                        List <ITagsDTO> tagName = new List <ITagsDTO>();
                        foreach (var tags in tagsEntityList)
                        {
                            ITagsDTO tagDTO = (ITagsDTO)DTOFactory.Instance.Create(DTOType.TagsDTO);
                            var      tag    = context.Tags.SingleOrDefault(t => t.Id == tags.TagId);
                            EntityConverter.FillDTOFromEntity(tag, tagDTO);
                            tagName.Add(tagDTO);
                        }

                        // Getting User
                        IUserDTO userDTO    = (IUserDTO)DTOFactory.Instance.Create(DTOType.UserDTO);
                        var      userEntity = context.User.SingleOrDefault(u => u.Id == questionEntity.UserId);
                        EntityConverter.FillDTOFromEntity(userEntity, userDTO);

                        IDataDTO dataDTO = (IDataDTO)DTOFactory.Instance.Create(DTOType.DataDTO);
                        dataDTO.QuestionDetail = questionDTO;
                        dataDTO.TagDetail      = tagName;
                        dataDTO.UserDetail     = userDTO;

                        listOfQuestions.Add(dataDTO);
                    }
                }

                return(listOfQuestions);
            }
        }
Beispiel #8
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);
            }
        }
Beispiel #9
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);;
            }
        }
Beispiel #10
0
 /// <summary>
 /// Method to Login user
 /// </summary>
 /// <param name="userDTO"></param>
 /// <returns></returns>
 public IUserDTO Login(IUserDTO userDTO)
 {
     using (var context = new BufferOverflowDBEntities())
     {
         var user = context.User.SingleOrDefault(u => u.Email == userDTO.Email);
         if (user != null)
         {
             string password  = user.Password;
             string Decrypted = EncryptionAndDecryption.Decrypt(password, EncryptionAndDecryption.EncryptionKey);
             bool   isValid   = (userDTO.Password == Decrypted);
             //IUserDTO userDto = null;
             if (isValid)
             {
                 EntityConverter.FillDTOFromEntity(user, userDTO);
             }
         }
         return((userDTO.Id != 0) ? userDTO: null);
     }
 }
Beispiel #11
0
        /// <summary>
        /// Method get all tags in table
        /// </summary>
        /// <returns></returns>
        public List <ITagsDTO> GetTags()
        {
            using (var context = new BufferOverflowDBEntities())
            {
                var tagsEntity = context.Tags.ToList();

                List <ITagsDTO> listOfTags = null;

                if (tagsEntity != null)
                {
                    foreach (var tags in tagsEntity)
                    {
                        ITagsDTO tagsDTO = (ITagsDTO)DTOFactory.Instance.Create(DTOType.TagsDTO);
                        EntityConverter.FillDTOFromEntity(tagsEntity, tagsDTO);
                        listOfTags.Add(tagsDTO);
                    }
                }

                return(listOfTags);
            }
        }
Beispiel #12
0
        /// <summary>
        /// Method to search question with given tag
        /// </summary>
        /// <param name="tagDTO"></param>
        /// <returns></returns>
        public List <IQuestionDTO> SearchQuestion(ITagsDTO tagDTO)
        {
            using (var context = new BufferOverflowDBEntities())
            {
                var questionIds = context.TagRelation.Where(t => t.Id == tagDTO.Id).ToList();
                List <IQuestionDTO> listOfQuestions = null;

                if (questionIds != null)
                {
                    foreach (var question in questionIds)
                    {
                        var          questionEntity = context.Questions.SingleOrDefault(q => q.Id == question.QuestionId);
                        IQuestionDTO questionDTO    = (IQuestionDTO)DTOFactory.Instance.Create(DTOType.QuestionDTO);
                        EntityConverter.FillDTOFromEntity(questionEntity, questionDTO);
                        listOfQuestions.Add(questionDTO);
                    }
                }

                return(listOfQuestions);
            }
        }
Beispiel #13
0
        /// <summary>
        /// Method to search question with given title
        /// </summary>
        /// <param name="questionDTO"></param>
        /// <returns></returns>
        public List <IQuestionDTO> Search(IQuestionDTO questionDTO)
        {
            using (var context = new BufferOverflowDBEntities())
            {
                var questionEntityList = context.Questions.OrderByDescending(e => e.Created).Where(q => q.Title == questionDTO.Title).ToList();
                List <IQuestionDTO> listOfQuestions = null;

                if (questionEntityList != null)
                {
                    listOfQuestions = new List <IQuestionDTO>();
                    foreach (var questionEntity in questionEntityList)
                    {
                        IQuestionDTO quesDTO = (IQuestionDTO)DTOFactory.Instance.Create(DTOType.QuestionDTO);
                        EntityConverter.FillDTOFromEntity(questionEntity, quesDTO);
                        listOfQuestions.Add(quesDTO);
                    }
                }

                return(listOfQuestions);
            }
        }
Beispiel #14
0
        /// <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);;
            }
        }
Beispiel #15
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);
            }
        }
Beispiel #16
0
        /// <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);
            }
        }
Beispiel #17
0
        /// <summary>
        /// Method to get question detail
        /// </summary>
        /// <param name="questionDTO"></param>
        /// <returns></returns>
        public IDataDTO Details(IQuestionDTO questionDTO)
        {
            using (var context = new BufferOverflowDBEntities())
            {
                IDataDTO dataDTO        = (IDataDTO)DTOFactory.Instance.Create(DTOType.DataDTO);
                var      questionDetail = context.Questions.SingleOrDefault(q => q.Id == questionDTO.Id);

                if (questionDetail != null)
                {
                    dataDTO.QuestionDetail = (IQuestionDTO)DTOFactory.Instance.Create(DTOType.QuestionDTO);
                    EntityConverter.FillDTOFromEntity(questionDetail, dataDTO.QuestionDetail);

                    // Geting Question tags
                    var             tagRelationEntity = context.TagRelation.Where(t => t.QuestionId == questionDetail.Id).ToList();
                    List <ITagsDTO> tags = new List <ITagsDTO>();
                    foreach (var tag in tagRelationEntity)
                    {
                        var      tagEntity = context.Tags.SingleOrDefault(t => t.Id == tag.TagId);
                        ITagsDTO tagsDTO   = (ITagsDTO)DTOFactory.Instance.Create(DTOType.TagsDTO);
                        EntityConverter.FillDTOFromEntity(tagEntity, tagsDTO);
                        tags.Add(tagsDTO);
                    }
                    dataDTO.TagDetail = tags;

                    // Getting User Detail
                    var userEntity = context.User.SingleOrDefault(u => u.Id == questionDetail.UserId);
                    dataDTO.UserDetail = (IUserDTO)DTOFactory.Instance.Create(DTOType.UserDTO);
                    EntityConverter.FillDTOFromEntity(userEntity, dataDTO.UserDetail);
                }
                else
                {
                    dataDTO.QuestionDetail.Id = 0;
                }

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