From 960a6ce191fbe03aa98d55fba600d5b9756c1aa1 Mon Sep 17 00:00:00 2001 From: shept Date: Fri, 14 Mar 2025 23:07:06 +0500 Subject: [PATCH] completed SurveyRepository.cs --- .../Repositories/IGenericRepository.cs | 4 ++- .../Repositories/ISurveyRepository.cs | 2 +- .../Repositories/AnswerRepository.cs | 2 +- .../Repositories/CompletionRepository.cs | 2 +- .../Repositories/QuestionRepository.cs | 2 +- .../Repositories/SurveyRepository.cs | 36 ++++++++++--------- 6 files changed, 27 insertions(+), 21 deletions(-) diff --git a/SurveyLib.Core/Repositories/IGenericRepository.cs b/SurveyLib.Core/Repositories/IGenericRepository.cs index 071416f..b13c749 100644 --- a/SurveyLib.Core/Repositories/IGenericRepository.cs +++ b/SurveyLib.Core/Repositories/IGenericRepository.cs @@ -1,8 +1,10 @@ +using SurveyLib.Core.Models; + namespace SurveyLib.Core.Repositories; public interface IGenericRepository where T : class { - Task? GetByIdAsync(int id); + Task GetByIdAsync(int id); Task> GetAllAsync(); Task AddAsync(T entity); Task UpdateAsync(T entity); diff --git a/SurveyLib.Core/Repositories/ISurveyRepository.cs b/SurveyLib.Core/Repositories/ISurveyRepository.cs index 795c26d..a918cd3 100644 --- a/SurveyLib.Core/Repositories/ISurveyRepository.cs +++ b/SurveyLib.Core/Repositories/ISurveyRepository.cs @@ -6,5 +6,5 @@ public interface ISurveyRepository : IGenericRepository { Task GetWithQuestionsAsync(int surveyId); Task GetWithCompletionsAsync(int surveyId); - Task FindByTitleAsync(string title); + Task> FindByTitleAsync(string title); } \ No newline at end of file diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs index d9a78c6..fb77da5 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/AnswerRepository.cs @@ -14,7 +14,7 @@ public class AnswerRepository : IAnswerRepository _context = context; } - public Task? GetByIdAsync(int id) + public Task GetByIdAsync(int id) { throw new NotImplementedException(); } diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs index 83c2705..e33eba6 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/CompletionRepository.cs @@ -13,7 +13,7 @@ public class CompletionRepository : ICompletionRepository _context = context; } - public Task? GetByIdAsync(int id) + public Task GetByIdAsync(int id) { throw new NotImplementedException(); } diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs index eb3403b..ce448a6 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/QuestionRepository.cs @@ -13,7 +13,7 @@ public class QuestionRepository : IQuestionRepository _context = context; } - public Task? GetByIdAsync(int id) + public Task GetByIdAsync(int id) { throw new NotImplementedException(); } diff --git a/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs b/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs index 4aeb17e..c36e455 100644 --- a/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs +++ b/SurveyLib.Infrastructure.EFCore/Repositories/SurveyRepository.cs @@ -1,3 +1,4 @@ +using Microsoft.EntityFrameworkCore; using SurveyLib.Core.Models; using SurveyLib.Core.Repositories; using SurveyLib.Infrastructure.EFCore.Data; @@ -13,43 +14,46 @@ public class SurveyRepository : ISurveyRepository _context = context; } - public Task? GetByIdAsync(int id) + public async Task GetByIdAsync(int id) { - throw new NotImplementedException(); + return await _context.Surveys.FirstOrDefaultAsync(s => s.Id == id); } - public Task> GetAllAsync() + public async Task> GetAllAsync() { - throw new NotImplementedException(); + return await _context.Surveys.ToListAsync(); } - public Task AddAsync(Survey entity) + public async Task AddAsync(Survey entity) { - throw new NotImplementedException(); + await _context.Surveys.AddAsync(entity); + await _context.SaveChangesAsync(); } - public Task UpdateAsync(Survey entity) + public async Task UpdateAsync(Survey entity) { - throw new NotImplementedException(); + _context.Surveys.Update(entity); + await _context.SaveChangesAsync(); } - public Task DeleteAsync(Survey entity) + public async Task DeleteAsync(Survey entity) { - throw new NotImplementedException(); + _context.Surveys.Remove(entity); + await _context.SaveChangesAsync(); } - public Task GetWithQuestionsAsync(int surveyId) + public async Task GetWithQuestionsAsync(int surveyId) { - throw new NotImplementedException(); + return await _context.Surveys.Include(survey => survey.Questions).FirstOrDefaultAsync(s => s.Id == surveyId); } - public Task GetWithCompletionsAsync(int surveyId) + public async Task GetWithCompletionsAsync(int surveyId) { - throw new NotImplementedException(); + return await _context.Surveys.Include(survey => survey.Completions).FirstOrDefaultAsync(s => s.Id == surveyId); } - public Task FindByTitleAsync(string title) + public async Task> FindByTitleAsync(string title) { - throw new NotImplementedException(); + return await _context.Surveys.Where(s => s.Title.ToLower().Contains(title.ToLower())).ToListAsync(); } } \ No newline at end of file