From e45c143afc8b010987d7af7f9c1bb5b8e95ffbec Mon Sep 17 00:00:00 2001 From: Nikolai Papin Date: Sat, 7 Sep 2024 12:42:31 +0300 Subject: [PATCH] UserPolis repo --- .../UserPolises/IUserPolisRepository.cs | 14 ++++++ .../UserPolises/UserPolisRepository.cs | 48 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 Repositories/UserPolises/IUserPolisRepository.cs create mode 100644 Repositories/UserPolises/UserPolisRepository.cs diff --git a/Repositories/UserPolises/IUserPolisRepository.cs b/Repositories/UserPolises/IUserPolisRepository.cs new file mode 100644 index 0000000..b222989 --- /dev/null +++ b/Repositories/UserPolises/IUserPolisRepository.cs @@ -0,0 +1,14 @@ +using Renis.Database.Models; + +namespace Renis.Repositories; + +public interface IUserPolisRepository +{ + public Task AddUserPolis(UserPolis userPolis); + public Task UpdateUserPolis(UserPolis userPolis); + public Task DeleteUserPolis(UserPolis userPolis); + public Task GetUserPolisById(long id); + public Task GetUserPolisByUserIdAndPolisId(long userId, long polisId); + public IQueryable GetUserPolises(); + public Task Save(); +} \ No newline at end of file diff --git a/Repositories/UserPolises/UserPolisRepository.cs b/Repositories/UserPolises/UserPolisRepository.cs new file mode 100644 index 0000000..0848763 --- /dev/null +++ b/Repositories/UserPolises/UserPolisRepository.cs @@ -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 AddUserPolis(UserPolis userPolis) + { + _db.UserPolises.Add(userPolis); + return await Save(); + } + + public async Task UpdateUserPolis(UserPolis userPolis) + { + _db.UserPolises.Update(userPolis); + return await Save(); + } + + public async Task DeleteUserPolis(UserPolis userPolis) + { + _db.UserPolises.Remove(userPolis); + return await Save(); + } + + public async Task GetUserPolisById(long id) + { + return await _db.UserPolises.FirstOrDefaultAsync(x => x.Id == id); + } + + public async Task GetUserPolisByUserIdAndPolisId(long userId, long polisId) + { + return await _db.UserPolises.FirstOrDefaultAsync(x => x.UserId == userId && x.PolisId == polisId); + } + + public IQueryable GetUserPolises() + { + return _db.UserPolises.AsQueryable(); + } + + public async Task Save() + { + return await _db.SaveChangesAsync() > 0; + } +}