diff --git a/server/StudySharp.Domain/Repositories/ITheoryBlockRepository.cs b/server/StudySharp.Domain/Repositories/ITheoryBlockRepository.cs new file mode 100644 index 0000000..85d2dc2 --- /dev/null +++ b/server/StudySharp.Domain/Repositories/ITheoryBlockRepository.cs @@ -0,0 +1,16 @@ +using System.Collections.Generic; +using System.Threading.Tasks; +using StudySharp.Domain.General; +using StudySharp.Domain.Models; + +namespace StudySharp.Domain.Repositories +{ + public interface ITheoryBlockRepository : IRepository + { + Task> CreateTheoryBlockAsync(TheoryBlock theoryBlock); + Task> GetTheoryBlockByIdAsync(int id); + Task>> GetTheoryBlocksByCourseIdAsync(int courseId); + Task> UpdateTheoryBlockAsync(TheoryBlock theoryBlock); + Task> RemoveTheoryBlockByIdAsync(int id); + } +} \ No newline at end of file diff --git a/server/StudySharp.DomainServices/Repositories/TheoryBlockRepository.cs b/server/StudySharp.DomainServices/Repositories/TheoryBlockRepository.cs new file mode 100644 index 0000000..442e73b --- /dev/null +++ b/server/StudySharp.DomainServices/Repositories/TheoryBlockRepository.cs @@ -0,0 +1,74 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Microsoft.EntityFrameworkCore; +using StudySharp.Domain.General; +using StudySharp.Domain.Models; +using StudySharp.Domain.Repositories; + +namespace StudySharp.DomainServices.Repositories +{ + public sealed class TheoryBlockRepository : ITheoryBlockRepository + { + private readonly StudySharpDbContext _context; + + public TheoryBlockRepository(StudySharpDbContext context) + { + _context = context; + } + + public async Task> CreateTheoryBlockAsync(TheoryBlock theoryBlock) + { + await _context.TheoryBlocks.AddAsync(theoryBlock); + await _context.SaveChangesAsync(); + return new OperationResult { Result = theoryBlock, IsSucceeded = true }; + } + + public async Task> GetTheoryBlockByIdAsync(int id) + { + var theoryBlock = await _context.TheoryBlocks.FindAsync(id); + if (theoryBlock == null) + { + return BuildErrorResponse("Could not find TheoryBlock`s Id"); + } + return new OperationResult { Result = theoryBlock, IsSucceeded = true }; + } + + public async Task>> GetTheoryBlocksByCourseIdAsync(int courseId) + { + var theoryBlocks = await _context.TheoryBlocks.Where(_ => _.CourseId == courseId).ToListAsync(); + return new OperationResult> { Result = theoryBlocks, IsSucceeded = true }; + } + + public async Task> UpdateTheoryBlockAsync(TheoryBlock theoryBlock) + { + if (theoryBlock == null) + { + return BuildErrorResponse("There`s no TheoryBlock you can modify"); + } + _context.Entry(theoryBlock).State = EntityState.Modified; + await _context.SaveChangesAsync(); + return new OperationResult { Result = theoryBlock, IsSucceeded = true }; + } + + public async Task> RemoveTheoryBlockByIdAsync(int id) + { + var theoryBlock = await _context.TheoryBlocks.FindAsync(id); + if (theoryBlock == null) + { + return BuildErrorResponse("Could not find TheoryBlock`s Id"); + } + _context.TheoryBlocks.Remove(theoryBlock); + await _context.SaveChangesAsync(); + return new OperationResult { Result = theoryBlock, IsSucceeded = true }; + } + + private OperationResult BuildErrorResponse(string errorText) + { + var result = new OperationResult { Result = null, IsSucceeded = false }; + result.Errors.Add(errorText); + return result; + } + } +} \ No newline at end of file