/
rumina-v
/
database
Обзор
Документация
Войти
/
rumina-v
/
database
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
LibraryApp/Services/ReaderProfileService.cs
65 строк
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 ReaderProfileService : IReaderProfileService { private readonly IReaderProfileRepository _profileRepository; private readonly IReaderRepository _readerRepository; public ReaderProfileService( IReaderProfileRepository profileRepository, IReaderRepository readerRepository) { _profileRepository = profileRepository; _readerRepository = readerRepository; } public async Task<List<ReaderProfile>> GetAllAsync() => await _profileRepository.GetAllAsync(); public async Task<ReaderProfile?> GetByIdAsync(int id) => await _profileRepository.GetByIdAsync(id); public async Task<ReaderProfile> AddAsync(ReaderProfile profile) { var reader = await _readerRepository.GetByIdAsync(profile.ReaderId); if (reader is null) throw new Exception("Reader does not exist"); var existingProfile = await _profileRepository.GetByReaderIdAsync(profile.ReaderId); if (existingProfile != null) throw new Exception("Profile already exists for this reader"); if (profile.BirthDate > DateTime.UtcNow) throw new Exception("BirthDate cannot be in the future"); return await _profileRepository.AddAsync(profile); } public async Task<ReaderProfile?> UpdateAsync(int id, ReaderProfile profile) { var existing = await _profileRepository.GetByIdAsync(id); if (existing is null) return null; var reader = await _readerRepository.GetByIdAsync(profile.ReaderId); if (reader is null) throw new Exception("Reader does not exist"); existing.Phone = profile.Phone; existing.Address = profile.Address; existing.BirthDate = profile.BirthDate; return await _profileRepository.UpdateAsync(id, existing); } public async Task<bool> DeleteAsync(int id) => await _profileRepository.DeleteAsync(id); public async Task<List<ReaderProfile>> GetProfilesWithoutPhoneAsync() => await _profileRepository.GetProfilesWithoutPhoneAsync(); public async Task<List<ReaderProfile>> GetProfilesByCityAsync(string city) => await _profileRepository.GetProfilesByCityAsync(city); }