/
rumina-v
/
database
Обзор
Документация
Войти
/
rumina-v
/
database
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
LibraryApp/Repositories/ReaderProfileRepository.cs
100 строк
3 KB
Viktoria Rumina
Add solution files
04 июн 2026, 12:13
04 июн 2026, 12:13
73cfef0
Код
Авторство
О чём код?
using LibraryApp.Abstractions; using LibraryApp.Data; using LibraryApp.Entities; using Microsoft.EntityFrameworkCore; namespace LibraryApp.Repositories; public class ReaderProfileRepository : IReaderProfileRepository { private readonly AppDbContext _context; public ReaderProfileRepository(AppDbContext context) { _context = context; } public async Task<List<ReaderProfile>> GetAllAsync() { return await _context.ReaderProfiles .Include(x => x.Reader) .ToListAsync(); } public async Task<ReaderProfile?> GetByIdAsync(int id) { return await _context.ReaderProfiles .Include(x => x.Reader) .FirstOrDefaultAsync(x => x.Id == id); } public async Task<ReaderProfile> AddAsync(ReaderProfile profile) { _context.ReaderProfiles.Add(profile); await _context.SaveChangesAsync(); return profile; } public async Task<ReaderProfile?> GetByReaderIdAsync(int readerId) { return await _context.ReaderProfiles .Include(x => x.Reader) .FirstOrDefaultAsync(x => x.ReaderId == readerId); } public async Task<ReaderProfile?> UpdateAsync(int id, ReaderProfile profile) { var existingProfile = await _context.ReaderProfiles .FirstOrDefaultAsync(x => x.Id == id); if (existingProfile is null) { return null; } existingProfile.ReaderId = profile.ReaderId; existingProfile.Phone = profile.Phone; existingProfile.Address = profile.Address; existingProfile.BirthDate = profile.BirthDate; await _context.SaveChangesAsync(); return existingProfile; } public async Task<bool> DeleteAsync(int id) { var profile = await _context.ReaderProfiles.FirstOrDefaultAsync(x => x.Id == id); if (profile is null) { return false; } _context.ReaderProfiles.Remove(profile); await _context.SaveChangesAsync(); return true; } public async Task<List<ReaderProfile>> GetProfilesWithoutPhoneAsync() { return await _context.ReaderProfiles .Include(x => x.Reader) .Where(x => string.IsNullOrWhiteSpace(x.Phone)) .ToListAsync(); } public async Task<List<ReaderProfile>> GetProfilesByCityAsync(string city) { if (string.IsNullOrWhiteSpace(city)) { return new List<ReaderProfile>(); } var normalizedCity = city.Trim().ToLower(); return await _context.ReaderProfiles .Include(x => x.Reader) .Where(x => x.Address != null && x.Address.ToLower().Contains(normalizedCity)) .ToListAsync(); } }