From 62facb34239c12c7dafa07694e264fcc1de8241f Mon Sep 17 00:00:00 2001 From: shept Date: Fri, 14 Mar 2025 23:15:33 +0500 Subject: [PATCH] completed QuestionRepository.cs small readability fixes for SurveyRepository.cs --- .../Repositories/QuestionRepository.cs | 33 +++++++++++-------- .../Repositories/SurveyRepository.cs | 6 ++-- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs index ce448a6..d124a8e 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs @@ -1,3 +1,4 @@ +using Microsoft.EntityFrameworkCore; using SurveyLib.Core.Models; using SurveyLib.Core.Repositories; using SurveyLib.Infrastructure.EFCore.Data; @@ -13,38 +14,42 @@ public class QuestionRepository : IQuestionRepository _context = context; } - public Task GetByIdAsync(int id) + public async Task GetByIdAsync(int id) { - throw new NotImplementedException(); + return await _context.Questions.FirstOrDefaultAsync(q => q.Id == id); } - public Task> GetAllAsync() + public async Task> GetAllAsync() { - throw new NotImplementedException(); + return await _context.Questions.ToListAsync(); } - public Task AddAsync(QuestionBase entity) + public async Task AddAsync(QuestionBase entity) { - throw new NotImplementedException(); + await _context.Questions.AddAsync(entity); + await _context.SaveChangesAsync(); } - public Task UpdateAsync(QuestionBase entity) + public async Task UpdateAsync(QuestionBase entity) { - throw new NotImplementedException(); + _context.Questions.Update(entity); + await _context.SaveChangesAsync(); } - public Task DeleteAsync(QuestionBase entity) + public async Task DeleteAsync(QuestionBase entity) { - throw new NotImplementedException(); + _context.Questions.Remove(entity); + await _context.SaveChangesAsync(); } - public Task GetWithAnswersAsync(int questionId) + public async Task GetWithAnswersAsync(int questionId) { - throw new NotImplementedException(); + return await _context.Questions.Include(q => q.Answers) + .FirstOrDefaultAsync(q => q.Id == questionId); } - public Task> GetBySurveyIdAsync(int surveyId) + public async Task> GetBySurveyIdAsync(int surveyId) { - throw new NotImplementedException(); + return await _context.Questions.Where(q => q.SurveyId == surveyId).ToListAsync(); } } \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs index c36e455..b655a9e 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs @@ -44,12 +44,14 @@ public class SurveyRepository : ISurveyRepository public async Task GetWithQuestionsAsync(int surveyId) { - return await _context.Surveys.Include(survey => survey.Questions).FirstOrDefaultAsync(s => s.Id == surveyId); + return await _context.Surveys.Include(survey => survey.Questions) + .FirstOrDefaultAsync(s => s.Id == surveyId); } public async Task GetWithCompletionsAsync(int surveyId) { - return await _context.Surveys.Include(survey => survey.Completions).FirstOrDefaultAsync(s => s.Id == surveyId); + return await _context.Surveys.Include(survey => survey.Completions) + .FirstOrDefaultAsync(s => s.Id == surveyId); } public async Task> FindByTitleAsync(string title)