diff --git a/SUBD_Car_rent/database/ImplementationMongoDB.cs b/SUBD_Car_rent/database/ImplementationMongoDB.cs new file mode 100644 index 0000000..2d8a380 --- /dev/null +++ b/SUBD_Car_rent/database/ImplementationMongoDB.cs @@ -0,0 +1,214 @@ +using MongoDB.Driver; + +namespace database +{ + public class ImplementationMongoDB : Abstractions + { + private IMongoDatabase _database; + private IMongoCollection _carCollection; + private IMongoCollection _clientCollection; + private IMongoCollection _rentalCollection; + private IMongoCollection _carModelCollection; + private IMongoCollection _branchCollection; + private IMongoCollection _statusCollection; + private IMongoCollection _bodyTypeCollection; + + public ImplementationMongoDB() + { + var client = new MongoClient("mongodb://localhost:27017"); + _database = client.GetDatabase("car_rent"); + + _carCollection = _database.GetCollection("car"); + _clientCollection = _database.GetCollection("client"); + _rentalCollection = _database.GetCollection("rental"); + _carModelCollection = _database.GetCollection("car_model"); + _branchCollection = _database.GetCollection("branch"); + _statusCollection = _database.GetCollection("status"); + _bodyTypeCollection = _database.GetCollection("body_type"); + } + + // CDUD операции для каждой сущности + + // Car + public override void AddCar(Car car) + { + _carCollection.InsertOne(car); + } + + public override List GetCars() + { + return _carCollection.Find(_ => true).ToList(); + } + + public override Car GetCarById(int id) + { + return _carCollection.Find(car => car.Id == id).FirstOrDefault(); + } + + public override void UpdateCar(Car car) + { + _carCollection.ReplaceOne(c => c.Id == car.Id, car); + } + + public override void DeleteCar(int id) + { + _carCollection.DeleteOne(car => car.Id == id); + } + + // Client + public override void AddClient(Client client) + { + _clientCollection.InsertOne(client); + } + + public override List GetClients() + { + return _clientCollection.Find(_ => true).ToList(); + } + + public override Client GetClientById(int id) + { + return _clientCollection.Find(client => client.Id == id).FirstOrDefault(); + } + + public override void UpdateClient(Client client) + { + _clientCollection.ReplaceOne(c => c.Id == client.Id, client); + } + + public override void DeleteClient(int id) + { + _clientCollection.DeleteOne(client => client.Id == id); + } + + // Rental + public override void AddRental(Rental rental) + { + _rentalCollection.InsertOne(rental); + } + + public override List GetRentals() + { + return _rentalCollection.Find(_ => true).ToList(); + } + + public override Rental GetRentalById(int id) + { + return _rentalCollection.Find(rental => rental.Id == id).FirstOrDefault(); + } + + public override void UpdateRental(Rental rental) + { + _rentalCollection.ReplaceOne(r => r.Id == rental.Id, rental); + } + + public override void DeleteRental(int id) + { + _rentalCollection.DeleteOne(rental => rental.Id == id); + } + + // CarModel + public override void AddCarModel(CarModel carModel) + { + _carModelCollection.InsertOne(carModel); + } + + public override List GetCarModels() + { + return _carModelCollection.Find(_ => true).ToList(); + } + + public override CarModel GetCarModelById(int id) + { + return _carModelCollection.Find(model => model.Id == id).FirstOrDefault(); + } + + public override void UpdateCarModel(CarModel carModel) + { + _carModelCollection.ReplaceOne(m => m.Id == carModel.Id, carModel); + } + + public override void DeleteCarModel(int id) + { + _carModelCollection.DeleteOne(model => model.Id == id); + } + + // Branch + public override void AddBranch(Branch branch) + { + _branchCollection.InsertOne(branch); + } + + public override List GetBranches() + { + return _branchCollection.Find(_ => true).ToList(); + } + + public override Branch GetBranchById(int id) + { + return _branchCollection.Find(branch => branch.Id == id).FirstOrDefault(); + } + + public override void UpdateBranch(Branch branch) + { + _branchCollection.ReplaceOne(b => b.Id == branch.Id, branch); + } + + public override void DeleteBranch(int id) + { + _branchCollection.DeleteOne(branch => branch.Id == id); + } + + // Status + public override void AddStatus(Status status) + { + _statusCollection.InsertOne(status); + } + + public override List GetStatuses() + { + return _statusCollection.Find(_ => true).ToList(); + } + + public override Status GetStatusById(int id) + { + return _statusCollection.Find(status => status.Id == id).FirstOrDefault(); + } + + public override void UpdateStatus(Status status) + { + _statusCollection.ReplaceOne(s => s.Id == status.Id, status); + } + + public override void DeleteStatus(int id) + { + _statusCollection.DeleteOne(status => status.Id == id); + } + + // BodyType + public override void AddBodyType(BodyType bodyType) + { + _bodyTypeCollection.InsertOne(bodyType); + } + + public override List GetBodyTypes() + { + return _bodyTypeCollection.Find(_ => true).ToList(); + } + + public override BodyType GetBodyTypeById(int id) + { + return _bodyTypeCollection.Find(bodyType => bodyType.Id == id).FirstOrDefault(); + } + + public override void UpdateBodyType(BodyType bodyType) + { + _bodyTypeCollection.ReplaceOne(b => b.Id == bodyType.Id, bodyType); + } + + public override void DeleteBodyType(int id) + { + _bodyTypeCollection.DeleteOne(bodyType => bodyType.Id == id); + } + } +} \ No newline at end of file diff --git a/SUBD_Car_rent/database/Implementation.cs b/SUBD_Car_rent/database/ImplementationPostgres.cs similarity index 99% rename from SUBD_Car_rent/database/Implementation.cs rename to SUBD_Car_rent/database/ImplementationPostgres.cs index 94bbe47..0604499 100644 --- a/SUBD_Car_rent/database/Implementation.cs +++ b/SUBD_Car_rent/database/ImplementationPostgres.cs @@ -2,7 +2,7 @@ namespace database { - public class Implementation : Abstractions + public class ImplementationPostgres : Abstractions { private NpgsqlConnection GetConnection() { diff --git a/SUBD_Car_rent/database/database.csproj b/SUBD_Car_rent/database/database.csproj index 715d83b..cf10113 100644 --- a/SUBD_Car_rent/database/database.csproj +++ b/SUBD_Car_rent/database/database.csproj @@ -7,6 +7,7 @@ +