добавлена реализация для MongoDB

This commit is contained in:
Timourka 2024-05-07 22:34:17 +04:00
parent 3e837d8492
commit 07f6a73f21
3 changed files with 216 additions and 1 deletions

View File

@ -0,0 +1,214 @@
using MongoDB.Driver;
namespace database
{
public class ImplementationMongoDB : Abstractions
{
private IMongoDatabase _database;
private IMongoCollection<Car> _carCollection;
private IMongoCollection<Client> _clientCollection;
private IMongoCollection<Rental> _rentalCollection;
private IMongoCollection<CarModel> _carModelCollection;
private IMongoCollection<Branch> _branchCollection;
private IMongoCollection<Status> _statusCollection;
private IMongoCollection<BodyType> _bodyTypeCollection;
public ImplementationMongoDB()
{
var client = new MongoClient("mongodb://localhost:27017");
_database = client.GetDatabase("car_rent");
_carCollection = _database.GetCollection<Car>("car");
_clientCollection = _database.GetCollection<Client>("client");
_rentalCollection = _database.GetCollection<Rental>("rental");
_carModelCollection = _database.GetCollection<CarModel>("car_model");
_branchCollection = _database.GetCollection<Branch>("branch");
_statusCollection = _database.GetCollection<Status>("status");
_bodyTypeCollection = _database.GetCollection<BodyType>("body_type");
}
// CDUD îïåðàöèè äëÿ êàæäîé ñóùíîñòè
// Car
public override void AddCar(Car car)
{
_carCollection.InsertOne(car);
}
public override List<Car> 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<Client> 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<Rental> 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<CarModel> 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<Branch> 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<Status> 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<BodyType> 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);
}
}
}

View File

@ -2,7 +2,7 @@
namespace database
{
public class Implementation : Abstractions
public class ImplementationPostgres : Abstractions
{
private NpgsqlConnection GetConnection()
{

View File

@ -7,6 +7,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="MongoDB.Driver" Version="2.25.0" />
<PackageReference Include="Npgsql" Version="8.0.2" />
</ItemGroup>