/
rumina-v
/
database
Обзор
Документация
Войти
/
rumina-v
/
database
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
LibraryApp/Services/ReaderService.cs
60 строк
2 KB
Viktoria Rumina
Add solution files
04 июн 2026, 12:13
04 июн 2026, 12:13
73cfef0
Код
Авторство
О чём код?
using LibraryApp.Abstractions; using LibraryApp.Entities; namespace LibraryApp.Services; public class ReaderService : IReaderService { private readonly IReaderRepository _readerRepository; private readonly ILoanRepository _loanRepository; public ReaderService(IReaderRepository readerRepository, ILoanRepository loanRepository) { _readerRepository = readerRepository; _loanRepository = loanRepository; } public async Task<List<Reader>> GetAllAsync() => await _readerRepository.GetAllAsync(); public async Task<Reader?> GetByIdAsync(int id) => await _readerRepository.GetByIdAsync(id); public async Task<Reader> AddAsync(Reader reader) { if (string.IsNullOrWhiteSpace(reader.FullName)) throw new Exception("FullName is required"); if (string.IsNullOrWhiteSpace(reader.Email)) throw new Exception("Email is required"); reader.RegisteredAt = DateTime.UtcNow; return await _readerRepository.AddAsync(reader); } public async Task<Reader?> UpdateAsync(int id, Reader reader) { var existing = await _readerRepository.GetByIdAsync(id); if (existing is null) return null; existing.FullName = reader.FullName; existing.Email = reader.Email; return await _readerRepository.UpdateAsync(id, existing); } public async Task<bool> DeleteAsync(int id) { var activeLoans = await _loanRepository.GetActiveLoansByReaderIdAsync(id); if (activeLoans.Any()) throw new Exception("Reader has active loans"); return await _readerRepository.DeleteAsync(id); } public async Task<List<Reader>> GetReadersWithOverdueBooksAsync() => await _readerRepository.GetReadersWithOverdueBooksAsync(); public async Task<List<Reader>> GetTopReadersByLoansCountAsync(int count) => await _readerRepository.GetTopReadersByLoansCountAsync(count); }