PIbd-23_Firsov_KA_SUBD/Hotel/HotelMongoDB/MongoDBImplement.cs
2024-05-29 17:06:07 +04:00

241 lines
8.3 KiB
C#

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<MongoHotel> _hotelCollection;
private IMongoCollection<MongoRoom> _roomCollection;
private IMongoCollection<MongoGuest> _guestCollection;
private IMongoCollection<MongoService> _serviceCollection;
private IMongoCollection<MongoReservation> _reservationCollection;
private IMongoCollection<MongoServiceCheck> _serviceCheckCollection;
private IMongoCollection<Sequence> _sequenceCollection;
public MongoDBImplement()
{
var client = new MongoClient("mongodb://localhost:27017");
_database = client.GetDatabase("hoteldb");
_hotelCollection = _database.GetCollection<MongoHotel>("hotel");
_roomCollection = _database.GetCollection<MongoRoom>("room");
_guestCollection = _database.GetCollection<MongoGuest>("guest");
_serviceCollection = _database.GetCollection<MongoService>("service");
_reservationCollection = _database.GetCollection<MongoReservation>("reservation");
_serviceCheckCollection = _database.GetCollection<MongoServiceCheck>("service_check");
_sequenceCollection = _database.GetCollection<Sequence>("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<MongoHotel> 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<MongoRoom> 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<MongoGuest> 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<MongoService> 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<MongoReservation> 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<MongoServiceCheck> 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<Sequence> 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);
}
}
}