UserPolis repo

This commit is contained in:
Nikolai Papin 2024-09-07 12:42:31 +03:00
parent d75f179ad4
commit e45c143afc
2 changed files with 62 additions and 0 deletions

View File

@ -0,0 +1,14 @@
using Renis.Database.Models;
namespace Renis.Repositories;
public interface IUserPolisRepository
{
public Task<bool> AddUserPolis(UserPolis userPolis);
public Task<bool> UpdateUserPolis(UserPolis userPolis);
public Task<bool> DeleteUserPolis(UserPolis userPolis);
public Task<UserPolis?> GetUserPolisById(long id);
public Task<UserPolis?> GetUserPolisByUserIdAndPolisId(long userId, long polisId);
public IQueryable<UserPolis> GetUserPolises();
public Task<bool> Save();
}

View File

@ -0,0 +1,48 @@
using Microsoft.EntityFrameworkCore;
using Renis.Database;
using Renis.Database.Models;
namespace Renis.Repositories;
public class UserPolisRepository(ApplicationContext db) : IUserPolisRepository
{
private readonly ApplicationContext _db = db;
public async Task<bool> AddUserPolis(UserPolis userPolis)
{
_db.UserPolises.Add(userPolis);
return await Save();
}
public async Task<bool> UpdateUserPolis(UserPolis userPolis)
{
_db.UserPolises.Update(userPolis);
return await Save();
}
public async Task<bool> DeleteUserPolis(UserPolis userPolis)
{
_db.UserPolises.Remove(userPolis);
return await Save();
}
public async Task<UserPolis?> GetUserPolisById(long id)
{
return await _db.UserPolises.FirstOrDefaultAsync(x => x.Id == id);
}
public async Task<UserPolis?> GetUserPolisByUserIdAndPolisId(long userId, long polisId)
{
return await _db.UserPolises.FirstOrDefaultAsync(x => x.UserId == userId && x.PolisId == polisId);
}
public IQueryable<UserPolis> GetUserPolises()
{
return _db.UserPolises.AsQueryable();
}
public async Task<bool> Save()
{
return await _db.SaveChangesAsync() > 0;
}
}