public async Task <Question> Store(Question question) { //So there would be the same for the creation var now = DateTime.Now; var dbQ = new DbQuestion() { Content = question.Content, Title = question.Title, Created = now, LastUpdated = now, }; var user = await dbcontext.Users.SingleOrDefaultAsync(u => u.UserName == question.Author); if (user == null) { return(null); } dbQ.User = user; var topic = await dbcontext.Topics.FirstOrDefaultAsync(t => t.Id == question.Topic.Id); dbQ.Topic = topic; await dbcontext.Questions.AddAsync(dbQ); await dbcontext.SaveChangesAsync(); return(DbMapper.MapDbQuestion(dbQ)); }
public async Task <Question> Update(Question question, bool updateTime = true) { var q = await dbcontext.Questions.SingleOrDefaultAsync(q => q.Id == question.Id); if (q != null) { var moderator = await dbcontext.Users.SingleOrDefaultAsync(u => u.UserName == question.Moderator); var topic = await dbcontext.Topics.SingleOrDefaultAsync(t => t.Id == question.Topic.Id); if (topic != null) { q.Topic = topic; } q.Moderator = moderator; q.ModeratorMessage = question.ModeratorMessage; q.Content = question.Content; q.Title = question.Title; q.Type = question.Type; q.Closed = question.Closed; if (updateTime) { q.LastUpdated = DateTime.Now; } await dbcontext.SaveChangesAsync(); } return(DbMapper.MapDbQuestion(q)); }
public async Task <ICollection <Question> > List() { return(await dbcontext.Questions .Include(q => q.User) .Include(q => q.Moderator) .Include(q => q.Topic) .Select(q => DbMapper.MapDbQuestion(q)).ToListAsync()); }
public async Task <Question> FindById(int id) { var q = await dbcontext.Questions .Include(q => q.User) .Include(q => q.Moderator) .Include(q => q.Topic) .SingleAsync(q => q.Id == id); return(DbMapper.MapDbQuestion(q)); }
public async Task <ICollection <Question> > GetQuestionsPaged(int pagenum, int pagesize) { return(await dbcontext.Questions .Include(q => q.User) .Include(q => q.Topic) .Skip((pagenum - 1) * pagesize) .Take(pagesize) .Select(q => DbMapper.MapDbQuestion(q)) .ToListAsync()); }
public async Task <QuestionSearchResult> Search(QuestionSearchParams request) { var query = dbcontext.Questions .Include(q => q.User) .Include(q => q.Topic) .Where(q => EF.Functions.Like(q.Title, $"%{request.Anywhere}%") || EF.Functions.Like(q.Content, $"%{request.Anywhere}%")); if (!String.IsNullOrEmpty(request.Title)) { query = query.Where(q => EF.Functions.Like(q.Title, $"%{request.Title}%")); } if (!String.IsNullOrEmpty(request.Content)) { query = query.Where(q => EF.Functions.Like(q.Content, $"%{request.Content}%")); } if (request.TopicId != null) { query = query.Where(q => q.Topic.Id == request.TopicId); } if (!string.IsNullOrEmpty(request.Username)) { query = query.Where(q => q.User.UserName == request.Username); } if (!request.IncludeHidden) { query = query.Where(q => q.Type != QuestionType.HiddenByModerator); } if (request.OnlyHidden) { query = query.Where(q => q.Type == QuestionType.HiddenByModerator); } var count = await query.CountAsync(); query = query .OrderByDescending(q => q.LastUpdated) .Skip((request.Page - 1) * request.CountPerPage) .Take(request.CountPerPage); var list = await query.Select(q => DbMapper.MapDbQuestion(q)).ToListAsync(); return(new QuestionSearchResult() { Page = request.Page, PageSize = request.CountPerPage, PageCount = (int)Math.Ceiling(((float)count) / request.CountPerPage), Count = count, Questions = list, }); }