using HotelMongoDB.StorageContracts; using System.Xml.Linq; using MongoDB.Driver; using HotelMongoDB.Models; using HotelAbstractions.Models; namespace HotelMongoDB { public class MongoDBImplement : StorageModel { private IMongoDatabase _database; private IMongoCollection _hotelCollection; private IMongoCollection _roomCollection; private IMongoCollection _guestCollection; private IMongoCollection _serviceCollection; private IMongoCollection _reservationCollection; private IMongoCollection _serviceCheckCollection; private IMongoCollection _sequenceCollection; public MongoDBImplement() { var client = new MongoClient("mongodb://localhost:27017"); _database = client.GetDatabase("hoteldb"); _hotelCollection = _database.GetCollection("hotel"); _roomCollection = _database.GetCollection("room"); _guestCollection = _database.GetCollection("guest"); _serviceCollection = _database.GetCollection("service"); _reservationCollection = _database.GetCollection("reservation"); _serviceCheckCollection = _database.GetCollection("service_check"); _sequenceCollection = _database.GetCollection("sequence"); } // hotel public override void AddHotel(Hotel hotel) { var art = new MongoHotel(hotel); _hotelCollection.InsertOne(art); var seq = new Sequence { Sql_id = hotel.Id.ToString(), Mongo_id = art.Id, Table_name = "hotel" }; _sequenceCollection.InsertOne(seq); } public override List GetHotels() { return _hotelCollection.Find(_ => true).ToList(); } public override MongoHotel GetHotelById(string id) { return _hotelCollection.Find(hotel => hotel.Id == id).FirstOrDefault(); } public override void UpdateHotel(MongoHotel hotel) { _hotelCollection.ReplaceOne(c => c.Id == hotel.Id, hotel); } public override void DeleteHotel(string id) { _hotelCollection.DeleteOne(hotel => hotel.Id == id); } // Room public override void AddRoom(Room room) { string hotel_id = GetMongoId("hotel", room.HotelId.ToString()); var art = new MongoRoom(room, hotel_id); _roomCollection.InsertOne(art); var seq = new Sequence { Sql_id = room.Id.ToString(), Mongo_id = art.Id, Table_name = "room" }; _sequenceCollection.InsertOne(seq); } public override List GetRooms() { return _roomCollection.Find(_ => true).ToList(); } public override MongoRoom GetRoomById(string id) { return _roomCollection.Find(room => room.Id == id).FirstOrDefault(); } public override void UpdateRoom(MongoRoom room) { _roomCollection.ReplaceOne(c => c.Id == room.Id, room); } public override void DeleteRoom(string id) { _roomCollection.DeleteOne(room => room.Id == id); } // Guest public override void AddGuest(Guest guest) { var cat = new MongoGuest(guest); _guestCollection.InsertOne(cat); var seq = new Sequence { Sql_id = guest.Id.ToString(), Mongo_id = cat.Id, Table_name = "guest" }; _sequenceCollection.InsertOne(seq); } public override List GetGuests() { return _guestCollection.Find(_ => true).ToList(); } public override MongoGuest GetGuestById(string id) { return _guestCollection.Find(guest => guest.Id == id).FirstOrDefault(); } public override void UpdateGuest(MongoGuest guest) { _guestCollection.ReplaceOne(r => r.Id == guest.Id, guest); } public override void DeleteGuest(string id) { _guestCollection.DeleteOne(guest => guest.Id == id); } // Service public override void AddService(Service service) { var cat = new MongoService(service); _serviceCollection.InsertOne(cat); var seq = new Sequence { Sql_id = service.Id.ToString(), Mongo_id = cat.Id, Table_name = "service" }; _sequenceCollection.InsertOne(seq); } public override List GetServices() { return _serviceCollection.Find(_ => true).ToList(); } public override MongoService GetServiceById(string id) { return _serviceCollection.Find(model => model.Id == id).FirstOrDefault(); } public override void UpdateService(MongoService service) { _serviceCollection.ReplaceOne(m => m.Id == service.Id, service); } public override void DeleteService(string id) { _serviceCollection.DeleteOne(model => model.Id == id); } // Reservation public override void AddReservation(Reservation reservation) { string room_id = GetMongoId("room", reservation.RoomId.ToString()); string guest_id = GetMongoId("guest", reservation.GuestId.ToString()); var art = new MongoReservation(reservation, guest_id, room_id); _reservationCollection.InsertOne(art); var seq = new Sequence { Sql_id = reservation.Id.ToString(), Mongo_id = art.Id, Table_name = "reservation" }; _sequenceCollection.InsertOne(seq); } public override List GetReservations() { return _reservationCollection.Find(_ => true).ToList(); } public override MongoReservation GetReservationById(string id) { return _reservationCollection.Find(Reservation => Reservation.Id == id).FirstOrDefault(); } public override void UpdateReservation(MongoReservation Reservation) { _reservationCollection.ReplaceOne(b => b.Id == Reservation.Id, Reservation); } public override void DeleteReservation(string id) { _reservationCollection.DeleteOne(Reservation => Reservation.Id == id); } // ServiceCheck public override void AddServiceCheck(ServiceCheck serviceCheck) { string reservation_id = GetMongoId("reservation", serviceCheck.ReservationId.ToString()); string service_id = GetMongoId("service", serviceCheck.ServiceId.ToString()); var art = new MongoServiceCheck(serviceCheck, reservation_id, service_id); _serviceCheckCollection.InsertOne(art); var seq = new Sequence { Sql_id = serviceCheck.Id.ToString(), Mongo_id = art.Id, Table_name = "service_check" }; _sequenceCollection.InsertOne(seq); } public override List GetServiceChecks() { return _serviceCheckCollection.Find(_ => true).ToList(); } public override MongoServiceCheck GetServiceCheckById(string id) { return _serviceCheckCollection.Find(ServiceCheck => ServiceCheck.Id == id).FirstOrDefault(); } public override void UpdateServiceCheck(MongoServiceCheck ServiceCheck) { _serviceCheckCollection.ReplaceOne(b => b.Id == ServiceCheck.Id, ServiceCheck); } public override void DeleteServiceCheck(string id) { _serviceCheckCollection.DeleteOne(ServiceCheck => ServiceCheck.Id == id); } public List GetSequence() { return _sequenceCollection.Find(_ => true).ToList(); } public override string GetMongoId(string tableName, string sqlId) { var sequence = _sequenceCollection.Find(s => s.Table_name == tableName && s.Sql_id == sqlId).FirstOrDefault(); return sequence?.Mongo_id ?? string.Empty; } public void DeleteSequence(string id) { _sequenceCollection.DeleteOne(Sequence => Sequence.Id == id); } } }