Example #1
0
        public async Task <Question> CreateQuestion(PangulDbContext db, UserContext user, CreateNewQuestion model)
        {
            model.UserContext = user;
            model.TopicRef    = await _topicService.RequireTopic(db, user, model.Topic);

            return(await _createNewQuestion.Execute(db, model));
        }
Example #2
0
        public async Task <Answer> Execute(PangulDbContext db, CreateNewAnswer command)
        {
            command.Validate();

            // Fetch the question
            var question = await _getQuestion.Execute(db, new GetQuestion()
            {
                UserContext      = command.UserContext,
                QuestionId       = command.QuestionId,
                LightWeightOnly  = true,
                IgnoreRowVersion = true
            });

            if (question == null)
            {
                throw new PangulCommandFailedException(CommandFailureType.InvalidRelation, $"No such question: {command.QuestionId}");
            }

            // Create answer
            var answer = new Answer()
            {
                Body             = command.Body,
                User             = command.UserContext.User,
                QuestionId       = question.QuestionId,
                AnswerGlobalMeta = new AnswerGlobalMeta()
                {
                    Votes = 0
                }
            };

            await db.Answer.AddAsync(answer);

            // Return instance
            return(answer);
        }
Example #3
0
 public async Task <User> Get(PangulDbContext db, string userId)
 {
     return(await _getUserDetails.Execute(db, new GetUserDetails()
     {
         UserId = userId
     }));
 }
Example #4
0
        public async Task Execute(PangulDbContext db, UpdateAnswerMeta command)
        {
            command.Validate();

            var meta = await db.AnswerMeta.FirstOrDefaultAsync(i =>
                                                               i.UserId == command.UserContext.User.UserId &&
                                                               i.AnswerId == command.Derived.AnswerId &&
                                                               i.RowVersion == command.Derived.RowVersion);

            // Maybe just inserted?
            if (meta == null)
            {
                meta = db.AnswerMeta.Local.AsQueryable().FirstOrDefault(i =>
                                                                        i.UserId == command.UserContext.User.UserId && i.AnswerId == command.Derived.AnswerId);
            }

            if (meta == null)
            {
                throw new PangulCommandFailedException(CommandFailureType.MissingData, $"No such AnswerMeta ({command.AnswerId})");
            }

            // Update properties
            meta.RowVersion = command.Derived.RowVersion;
            meta.Votes      = command.Votes;
        }
Example #5
0
        public async Task <Topic> RequireTopic(PangulDbContext db, UserContext user, string topic)
        {
            var existingTopic = await _getTopic.Execute(db, new GetTopic()
            {
                UserContext      = user,
                TopicName        = topic,
                IgnoreRowVersion = true
            });

            if (existingTopic != null)
            {
                return(existingTopic);
            }

            using (var outOfBandContext = db.CreateScope())
            {
                var newTopic = await _createTopic.Execute(outOfBandContext, new CreateTopic()
                {
                    UserContext = user,
                    TopicName   = topic
                });

                await outOfBandContext.SaveChangesAsync();

                return(newTopic);
            }
        }
Example #6
0
        public async Task <AnswerMeta> Execute(PangulDbContext db, CreateNewAnswerMeta command)
        {
            command.Validate();

            if (command.SkipIfExisting)
            {
                var instance = await db.AnswerMeta.FirstOrDefaultAsync(i =>
                                                                       i.UserId == command.UserContext.User.UserId && i.AnswerId == command.Derived.AnswerId);

                if (instance != null)
                {
                    return(instance);
                }
            }

            using (var innerDb = db.CreateScope())
            {
                var newInstance = new AnswerMeta()
                {
                    UserId   = command.UserContext.User.UserId,
                    AnswerId = command.Derived.AnswerId,
                    Votes    = 0
                };

                innerDb.AnswerMeta.Add(newInstance);
                await innerDb.SaveChangesAsync();
            }

            return(await db.AnswerMeta.FirstOrDefaultAsync(i => i.UserId == command.UserContext.User.UserId && i.AnswerId == command.Derived.AnswerId));
        }
Example #7
0
        public async Task UpdateQuestionTopic(PangulDbContext db, UserContext user, UpdateQuestionTopic model)
        {
            model.UserContext   = user;
            model.Derived.Topic = await _topicService.RequireTopic(db, user, model.TopicName);

            await _updateQuestionTopic.Execute(db, model);
        }
        public async Task <Question> Execute(PangulDbContext db, CreateNewQuestion command)
        {
            command.Validate();

            // Create question
            var question = new Question()
            {
                Title              = command.Title,
                Body               = command.Body,
                User               = command.UserContext.User,
                TopicId            = command.TopicRef.TopicId,
                TimeCreated        = DateTimeOffset.Now,
                QuestionGlobalMeta = new QuestionGlobalMeta()
                {
                    Votes = 0
                }
            };

            await db.Question.AddAsync(question);

            // Create tags
            if (command.Tags != null && command.Tags.Length > 0)
            {
                await db.QuestionTag.AddRangeAsync(command.Tags.Where(t => !string.IsNullOrWhiteSpace(t)).Select(t => new QuestionTag
                {
                    Tag      = t,
                    Question = question,
                }));
            }

            // Return instance
            return(question);
        }
Example #9
0
        public async Task Execute(PangulDbContext context, CreateNewUser command)
        {
            command.Validate();
            await GuardNoExistingLogin(context, command);

            // Make or find user
            var user = await context.Users.FirstOrDefaultAsync(i => i.Login.Username == command.Username) ?? new User()
            {
                UserContact = new UserContact()
                {
                    Email     = "",
                    Firstname = "",
                    Lastname  = ""
                }
            };

            // Create login
            var login = new Login()
            {
                Username = command.Username,
                User     = user
            };

            context.Logins.Add(login);
        }
Example #10
0
        public async Task <Answer> UpdateExistingAnswer(PangulDbContext db, UserContext user, UpdateAnswer model)
        {
            model.UserContext = user;
            await _updateAnswer.Execute(db, model);

            return(await GetAnswer(db, user, model.AnswerId));
        }
Example #11
0
        public async Task <Question> Execute(PangulDbContext db, GetQuestion query)
        {
            query.Validate();

            var qStatement = db.Question
                             .Where(q => q.QuestionId == query.Derived.QuestionId);

            if (!query.IgnoreRowVersion)
            {
                qStatement = qStatement.Where(q => q.RowVersion == query.Derived.RowVersion);
            }

            if (query.LightWeightOnly)
            {
                return(await qStatement.FirstOrDefaultAsync());
            }

            var question = await qStatement
                           .Include(i => i.Tags)
                           .Include(i => i.Meta)
                           .Include(i => i.Topic)
                           .FirstOrDefaultAsync();

            if (question != null)
            {
                question.CanEdit = await _internalUserPermissionService.HasWriteAccessFor(question, query.UserContext);
            }

            return(question);
        }
Example #12
0
        public async Task Execute(PangulDbContext db, DeleteTopic command)
        {
            command.Validate();

            var topic = await _getTopic.Execute(db, new GetTopic()
            {
                TopicId     = command.TopicId,
                RowVersion  = command.RowVersion,
                UserContext = command.UserContext
            });

            if (topic == null)
            {
                throw new PangulCommandFailedException(CommandFailureType.MissingData, $"No such topic ({command.DerivedProperties.TopicId})");
            }

            // We cannot delete a topic if there are still attached questions
            var anyQuestions = await db.Question.AnyAsync(i => i.TopicId == command.DerivedProperties.TopicId);

            if (anyQuestions)
            {
                throw new PangulCommandFailedException(CommandFailureType.ConstraintFailed,
                                                       $"Unable to delete topic {command.DerivedProperties.TopicId} while there are still attached questions");
            }

            db.Topic.Remove(topic);
        }
Example #13
0
        public async Task Backup(PangulDbContext db, Question question, DataBackupConfiguration backupConfiguration)
        {
            RequireBackupFolder(backupConfiguration);
            var snapshot = await QuestionSnapshot(db, question);

            PersistBackupObject(snapshot, $"question{question.QuestionId}.{backupConfiguration.Serializer.Extension}", backupConfiguration);
        }
Example #14
0
        public async Task <SearchResult> PerformQuery(SearchQuery parsedQuery, PangulDbContext db, UserContext user, int offset, int limit)
        {
            SetQueryTopic(parsedQuery);
            var results = new SearchResultAggregator(offset, limit);

            if (results.Push(await MatchTitle(parsedQuery, db, user)))
            {
                return(results.AsResult());
            }

            if (results.Push(await MatchTag(parsedQuery, db, user)))
            {
                return(results.AsResult());
            }

            if (results.Push(await MatchQuestionBody(parsedQuery, db, user)))
            {
                return(results.AsResult());
            }

            if (results.Push(await MatchAnswerBody(parsedQuery, db, user)))
            {
                return(results.AsResult());
            }

            if (results.Push(await MatchAll(parsedQuery, db, user)))
            {
                return(results.AsResult());
            }

            return(results.AsResult());
        }
Example #15
0
        public async Task UpdateQuestion(PangulDbContext db, UserContext user, UpdateQuestion model)
        {
            model.UserContext   = user;
            model.Derived.Topic = model.Topic == null ? null : await _topicService.RequireTopic(db, user, model.Topic);

            await _updateQuestion.Execute(db, model);
        }
        public async Task Execute(PangulDbContext db, PurgeQuestionData command)
        {
            command.Validate();
            db.RequireActiveTransaction();

            // Remove answers
            foreach (var answer in db.Answer.Where(i => i.QuestionId == command.Question.QuestionId))
            {
                await Execute(db, new PurgeAnswerData()
                {
                    Answer      = answer,
                    UserContext = command.UserContext
                });
            }

            // Remove meta objects
            var meta = await db.QuestionMeta.Where(i => i.QuestionId == command.Question.QuestionId).ToListAsync();

            db.QuestionMeta.RemoveRange(meta);

            // Remove global meta
            var global = await db.QuestionGlobalMeta.Where(i => i.QuestionGlobalMetaId == command.Question.QuestionGlobalMetaId).FirstOrDefaultAsync();

            db.QuestionGlobalMeta.Remove(global);

            // Remove answer
            db.Question.Remove(command.Question);
        }
Example #17
0
        public async Task <QuestionMeta> Execute(PangulDbContext db, GetQuestionMeta query)
        {
            query.Validate();

            return(await db.QuestionMeta
                   .Where(q => q.Question.QuestionId == query.Derived.QuestionId && q.UserId == query.UserContext.User.UserId)
                   .FirstOrDefaultAsync());
        }
Example #18
0
        public async Task <AnswerMeta> Execute(PangulDbContext db, GetAnswerMeta query)
        {
            query.Validate();

            return(await db.AnswerMeta
                   .Where(m => m.AnswerId == query.Derived.AnswerId && m.UserId == query.UserContext.User.UserId)
                   .FirstOrDefaultAsync());
        }
Example #19
0
 public async Task <Answer> CreateAnswer(PangulDbContext db, UserContext user, string questionId, string answerBody)
 {
     return(await _createNewAnswer.Execute(db, new CreateNewAnswer()
     {
         UserContext = user,
         QuestionId = questionId,
         Body = answerBody,
     }));
 }
Example #20
0
 public async Task <Answer> GetAnswer(PangulDbContext db, UserContext user, string answerId)
 {
     return(await _getAnswer.Execute(db, new GetAnswer()
     {
         UserContext = user,
         AnswerId = answerId,
         IgnoreRowVersion = true
     }));
 }
Example #21
0
        private IQueryable <Topic> PartialQuery(PangulDbContext db, GetTopic query)
        {
            if (query.DerivedProperties.QueryByName)
            {
                return(db.Topic.Where(i => i.Name == query.DerivedProperties.TopicName));
            }

            return(db.Topic.Where(i => i.TopicId == query.DerivedProperties.TopicId));
        }
Example #22
0
        public Task <IEnumerable <IQueryable <Question> > > Execute(PangulDbContext context, GetUserQuestions query)
        {
            query.Validate();

            var dbResults    = context.Question.Where(i => i.UserId == query.UserContext.User.UserId);
            var localResults = context.Question.Local.Where(i => i.UserId == query.UserContext.User.UserId).AsQueryable();

            return(Deferred.For <IEnumerable <IQueryable <Question> > >(new[] { dbResults, localResults }));
        }
Example #23
0
 public async Task <Topic> GetTopic(PangulDbContext db, UserContext user, string topicId)
 {
     return(await _getTopic.Execute(db, new GetTopic()
     {
         UserContext = user,
         TopicId = topicId,
         IgnoreRowVersion = true
     }));
 }
Example #24
0
 public Task <AnswerMeta> RequireAnswerMetaForUser(PangulDbContext db, UserContext user, string answerId)
 {
     return(_createAnswerMeta.Execute(db, new CreateNewAnswerMeta()
     {
         UserContext = user,
         AnswerId = answerId,
         SkipIfExisting = true
     }));
 }
Example #25
0
 public Task DeleteTopic(PangulDbContext db, UserContext user, string topicId, string rowVersion)
 {
     return(_deleteTopic.Execute(db, new DeleteTopic()
     {
         UserContext = user,
         TopicId = topicId,
         RowVersion = rowVersion
     }));
 }
Example #26
0
 public Task <Question> GetQuestion(PangulDbContext db, UserContext user, string questionId)
 {
     return(_getQuestion.Execute(db, new GetQuestion()
     {
         UserContext = user,
         QuestionId = questionId,
         IgnoreRowVersion = true,
     }));
 }
Example #27
0
        private static async Task GuardNoExistingLogin(PangulDbContext context, CreateNewUser command)
        {
            var conflictingLogin = await context.Logins.FirstOrDefaultAsync(i => i.Username == command.Username);

            if (conflictingLogin != null)
            {
                throw new PangulCommandFailedException(CommandFailureType.ConflictingData, "Specified login already exists");
            }
        }
Example #28
0
 public Task <QuestionMeta> RequireQuestionMetaForUser(PangulDbContext db, UserContext user, string questionId)
 {
     return(_createQuestionMeta.Execute(db, new CreateNewQuestionMeta()
     {
         UserContext = user,
         QuestionId = questionId,
         SkipIfExisting = true
     }));
 }
Example #29
0
        public async Task <AnswerGlobalMeta> Execute(PangulDbContext db, GetAnswerGlobalMeta query)
        {
            query.Validate();

            var queryStatement = from meta in db.AnswerGlobalMeta
                                 join answer in db.Answer on meta.AnswerGlobalMetaId equals answer.AnswerGlobalMetaId
                                 where answer.AnswerId == query.AnswerId
                                 select meta;

            return(await queryStatement.FirstOrDefaultAsync());
        }
Example #30
0
        public Task <QuestionGlobalMeta> Execute(PangulDbContext db, GetQuestionGlobalMeta query)
        {
            query.Validate();

            var queryStatement = from meta in db.QuestionGlobalMeta
                                 join question in db.Question on meta.QuestionGlobalMetaId equals question.QuestionGlobalMetaId
                                 where question.QuestionId == query.QuestionId
                                 select meta;

            return(queryStatement.FirstOrDefaultAsync());
        }