/
renix
/
TravelGuide
Обзор
Документация
Войти
/
renix
/
TravelGuide
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
back/Repositories/CityRepository.cs
53 строки
1 KB
artem777764
CityController PeRFeCtBEsTveRSIonEveR
02 апр 2025, 00:31
02 апр 2025, 00:31
f5a0303
Код
Авторство
О чём код?
using back.Interfaces; using back.Models; using back.Models.Entities; using Microsoft.EntityFrameworkCore; namespace back.Repositories; public class CityRepository : IRepository<CityEntity> { private readonly Context _dbContext; public CityRepository(Context context) { _dbContext = context; } public async Task<int> AddAsync(CityEntity entity) { await _dbContext.Cities.AddAsync(entity); await _dbContext.SaveChangesAsync(); return entity.Id; } public async Task<bool> DeleteAsync(int id) { CityEntity? city = await _dbContext.Cities.FindAsync(id); if (city == null) return false; _dbContext.Cities.Remove(city); await _dbContext.SaveChangesAsync(); return true; } public async Task<List<CityEntity>> GetAllAsync() { return await _dbContext.Cities.AsNoTracking().ToListAsync(); } public async Task<CityEntity?> GetByIdAsync(int id) { return await _dbContext.Cities.FindAsync(id); } public async Task<bool> UpdateAsync(CityEntity entity) { CityEntity? currentCity = await _dbContext.Cities.FindAsync(entity.Id); if (currentCity == null) return false; _dbContext.Entry(currentCity).CurrentValues.SetValues(entity); await _dbContext.SaveChangesAsync(); return true; } }