From 83c9b7fe6e46fd2c3723ee9bd04f4495094939e4 Mon Sep 17 00:00:00 2001 From: Nikolai Papin Date: Sat, 7 Sep 2024 12:18:30 +0300 Subject: [PATCH 1/3] verified field in car, fixed missing public declarations there as well --- Database/Models/Car.cs | 9 ++++--- Repositories/PolisRepository.cs | 48 +++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/Database/Models/Car.cs b/Database/Models/Car.cs index ba77df7..66a5cca 100644 --- a/Database/Models/Car.cs +++ b/Database/Models/Car.cs @@ -9,11 +9,12 @@ public class Car [Required] public string Number { get; set; } = null!; [Required] - Polis Polis { get; set; } = null!; + public Polis Polis { get; set; } = null!; [Required] - long PolisId { get; set; } + public long PolisId { get; set; } [Required] - User Resp { get; set; } = null!; + public User Resp { get; set; } = null!; [Required] - long RespId { get; set; } + public long RespId { get; set; } + public bool Verified { get; set; } } \ No newline at end of file diff --git a/Repositories/PolisRepository.cs b/Repositories/PolisRepository.cs index e69de29..00e8a4f 100644 --- a/Repositories/PolisRepository.cs +++ b/Repositories/PolisRepository.cs @@ -0,0 +1,48 @@ +using Microsoft.EntityFrameworkCore; +using Renis.Database; +using Renis.Database.Models; + +namespace Renis.Repositories; + +public class PolisRepository(ApplicationContext db) : IPolisRepository +{ + private readonly ApplicationContext _db = db; + + public async Task AddPolis(Polis polis) + { + _db.Polises.Add(polis); + return await Save(); + } + + public async Task DeletePolis(Polis polis) + { + _db.Polises.Remove(polis); + return await Save(); + } + + public async Task GetPolisById(long id) + { + return await _db.Polises.FirstOrDefaultAsync(x => x.Id == id); + } + + public async Task GetPolisByNumber(string number) + { + return await _db.Polises.FirstOrDefaultAsync(x => x.Number == number); + } + + public IQueryable GetPolises() + { + return _db.Polises.AsQueryable(); + } + + public async Task Save() + { + return await _db.SaveChangesAsync() > 0; + } + + public async Task UpdatePolis(Polis polis) + { + _db.Polises.Update(polis); + return await Save(); + } +} From d75f179ad4431bfece6731dbc0e820cabec9e213 Mon Sep 17 00:00:00 2001 From: Nikolai Papin Date: Sat, 7 Sep 2024 12:34:55 +0300 Subject: [PATCH 2/3] Car, Polis & User repos + moved to dedicated folders --- Repositories/Cars/CarRepository.cs | 48 +++++++++++++++++++ Repositories/Cars/ICarRepository.cs | 14 ++++++ .../{ => Polises}/IPolisRepository.cs | 0 Repositories/{ => Polises}/PolisRepository.cs | 0 Repositories/{ => Users}/IUserRepository.cs | 0 Repositories/{ => Users}/UserRepository.cs | 0 6 files changed, 62 insertions(+) create mode 100644 Repositories/Cars/CarRepository.cs create mode 100644 Repositories/Cars/ICarRepository.cs rename Repositories/{ => Polises}/IPolisRepository.cs (100%) rename Repositories/{ => Polises}/PolisRepository.cs (100%) rename Repositories/{ => Users}/IUserRepository.cs (100%) rename Repositories/{ => Users}/UserRepository.cs (100%) diff --git a/Repositories/Cars/CarRepository.cs b/Repositories/Cars/CarRepository.cs new file mode 100644 index 0000000..bb03135 --- /dev/null +++ b/Repositories/Cars/CarRepository.cs @@ -0,0 +1,48 @@ +using Microsoft.EntityFrameworkCore; +using Renis.Database; +using Renis.Database.Models; + +namespace Renis.Repositories; + +public class CarRepository(ApplicationContext db) : ICarRepository +{ + private readonly ApplicationContext _db = db; + + public async Task AddCar(Car car) + { + _db.Cars.Add(car); + return await Save(); + } + + public async Task DeleteCar(Car car) + { + _db.Cars.Remove(car); + return await Save(); + } + + public async Task GetCarById(long id) + { + return await _db.Cars.FirstOrDefaultAsync(x => x.Id == id); + } + + public async Task GetCarByNumber(string number) + { + return await _db.Cars.FirstOrDefaultAsync(x => x.Number == number); + } + + public IQueryable GetCars() + { + return _db.Cars.AsQueryable(); + } + + public async Task Save() + { + return await _db.SaveChangesAsync() > 0; + } + + public async Task UpdateCar(Car car) + { + _db.Cars.Update(car); + return await Save(); + } +} diff --git a/Repositories/Cars/ICarRepository.cs b/Repositories/Cars/ICarRepository.cs new file mode 100644 index 0000000..06e55b3 --- /dev/null +++ b/Repositories/Cars/ICarRepository.cs @@ -0,0 +1,14 @@ +using Renis.Database.Models; + +namespace Renis.Repositories; + +public interface ICarRepository +{ + public Task AddCar(Car car); + public Task UpdateCar(Car car); + public Task DeleteCar(Car car); + public Task GetCarById(long id); + public Task GetCarByNumber(string number); + public IQueryable GetCars(); + public Task Save(); +} diff --git a/Repositories/IPolisRepository.cs b/Repositories/Polises/IPolisRepository.cs similarity index 100% rename from Repositories/IPolisRepository.cs rename to Repositories/Polises/IPolisRepository.cs diff --git a/Repositories/PolisRepository.cs b/Repositories/Polises/PolisRepository.cs similarity index 100% rename from Repositories/PolisRepository.cs rename to Repositories/Polises/PolisRepository.cs diff --git a/Repositories/IUserRepository.cs b/Repositories/Users/IUserRepository.cs similarity index 100% rename from Repositories/IUserRepository.cs rename to Repositories/Users/IUserRepository.cs diff --git a/Repositories/UserRepository.cs b/Repositories/Users/UserRepository.cs similarity index 100% rename from Repositories/UserRepository.cs rename to Repositories/Users/UserRepository.cs From e45c143afc8b010987d7af7f9c1bb5b8e95ffbec Mon Sep 17 00:00:00 2001 From: Nikolai Papin Date: Sat, 7 Sep 2024 12:42:31 +0300 Subject: [PATCH 3/3] 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; + } +}