diff --git a/SUBD_Car_rent/database/ImplementationMongoDB.cs b/SUBD_Car_rent/database/ImplementationMongoDB.cs index 2d8a380..7b549f6 100644 --- a/SUBD_Car_rent/database/ImplementationMongoDB.cs +++ b/SUBD_Car_rent/database/ImplementationMongoDB.cs @@ -12,6 +12,12 @@ namespace database private IMongoCollection _branchCollection; private IMongoCollection _statusCollection; private IMongoCollection _bodyTypeCollection; + private class Sequence + { + public string Id; + public int count; + } + private IMongoCollection _sequenceCollection; public ImplementationMongoDB() { @@ -32,6 +38,13 @@ namespace database // Car public override void AddCar(Car car) { + if (car.Id == 0) + { + Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "car").FirstOrDefault(); + sequence.count++; + car.Id = sequence.count; + _sequenceCollection.ReplaceOne(seq => seq.Id == "car", sequence); + } _carCollection.InsertOne(car); } @@ -58,6 +71,13 @@ namespace database // Client public override void AddClient(Client client) { + if (client.Id == 0) + { + Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "client").FirstOrDefault(); + sequence.count++; + client.Id = sequence.count; + _sequenceCollection.ReplaceOne(seq => seq.Id == "client", sequence); + } _clientCollection.InsertOne(client); } @@ -84,6 +104,13 @@ namespace database // Rental public override void AddRental(Rental rental) { + if (rental.Id == 0) + { + Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "rental").FirstOrDefault(); + sequence.count++; + rental.Id = sequence.count; + _sequenceCollection.ReplaceOne(seq => seq.Id == "rental", sequence); + } _rentalCollection.InsertOne(rental); } @@ -110,6 +137,13 @@ namespace database // CarModel public override void AddCarModel(CarModel carModel) { + if (carModel.Id == 0) + { + Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "car_model").FirstOrDefault(); + sequence.count++; + carModel.Id = sequence.count; + _sequenceCollection.ReplaceOne(seq => seq.Id == "car_model", sequence); + } _carModelCollection.InsertOne(carModel); } @@ -136,6 +170,13 @@ namespace database // Branch public override void AddBranch(Branch branch) { + if (branch.Id == 0) + { + Sequence sequence = _sequenceCollection.Find(seq => seq.Id == "branch").FirstOrDefault(); + sequence.count++; + branch.Id = sequence.count; + _sequenceCollection.ReplaceOne(seq => seq.Id == "branch", sequence); + } _branchCollection.InsertOne(branch); }