/// <summary> /// Insert a new Room into the databse. /// </summary> /// <param name="p_room">The new Room.</param> /// <returns>The Newly created Room-Object. NULL, or Exception if an error occurs.</returns> public Room Insert(Room p_room) { using (var context = new FhdwHotelContext()) { using (var transaction = context.Database.BeginTransaction()) { try { var hotel = context.Hotel.SingleOrDefault(h => h.ID == p_room.Hotel.ID); p_room.Hotel = hotel; context.Room.Add(p_room); context.SaveChanges(); transaction.Commit(); return(p_room); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); transaction.Rollback(); return(null); } } } }
/// <summary> /// Get a Collection of all Rooms from one Hotel /// </summary> /// <param name="p_hotel">Hotelobject with an ID set</param> /// <returns>Collection of Rooms.</returns> public ICollection <Room> GetCollectionByHotel(DomainModel.Hotel p_hotel) { using (var context = new FhdwHotelContext()) { return(context.Room.Where(r => r.Hotel.ID == p_hotel.ID).ToList()); } }
/// <summary> /// Delete a room from the database. /// </summary> /// <param name="p_room">Roomobject with the ID set.</param> /// <returns>The deleted Room-Object. NULL, or Exception if an error occurs.</returns> public Room Delete(Room p_room) { using (var context = new FhdwHotelContext()) { using (var transaction = context.Database.BeginTransaction()) { try { var roomToDelete = context.Room.SingleOrDefault(r => r.ID == p_room.ID); context.Room.Remove(roomToDelete); context.SaveChanges(); transaction.Commit(); return(roomToDelete); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); transaction.Rollback(); return(null); } } } }
/// <summary> /// Get a specific Room by ID. /// </summary> /// <param name="p_id">ID of the Room.</param> /// <returns>The requested Hotel. If no Hotel exists, return NULL.</returns> public Room GetById(int p_id) { using (var context = new FhdwHotelContext()) { return(context.Room.FirstOrDefault(r => r.ID == p_id)); } }
/// <summary> /// Get a List of all Hotels in the database. /// </summary> /// <returns>List with all Hotels. If no Hotel exists, return an empty List.</returns> public ICollection <DomainModel.Hotel> GetCollection() { using (var context = new FhdwHotelContext()) { return(context.Hotel.Include(h => h.Address).ToList()); } }
/// <summary> /// Get a specific Hotel. /// </summary> /// <param name="p_id">ID of the Hotel</param> /// <returns>The requested Hotel. If no Hotel exists, return NULL.</returns> public DomainModel.Hotel GetById(int p_id) { using (var context = new FhdwHotelContext()) { return(context.Hotel.Include(a => a.Address).FirstOrDefault(h => h.ID == p_id)); } }
/// <summary> /// Update an existing Booking entry. /// </summary> /// <param name="p_booking">Bookingobject with an ID.</param> /// <returns>Updated Bookingobject.</returns> public Booking Update(Booking p_booking) { using (var context = new FhdwHotelContext()) { using (var transaction = context.Database.BeginTransaction()) { try { p_booking.Hotel = context.Hotel.SingleOrDefault(h => h.ID == p_booking.Hotel.ID); context.Booking.Add(p_booking); context.SaveChanges(); transaction.Commit(); return(p_booking); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); transaction.Rollback(); return(null); } } } }
/// <summary> /// Update an existing Guest in the databse. /// </summary> /// <param name="p_guest">New Guest-Object.</param> /// <returns>The updated Guest-Object. NULL, or Exception if an error occurs.</returns> public Guest Update(Guest p_guest) { using (var context = new FhdwHotelContext()) { using (var transaction = context.Database.BeginTransaction()) { try { p_guest.ContactAddress = context.Address.SingleOrDefault(a => a.ID == p_guest.ContactAddress.ID); p_guest.BillingAddress = context.Address.SingleOrDefault(a => a.ID == p_guest.BillingAddress.ID); context.Guest.Add(p_guest); context.SaveChanges(); transaction.Commit(); return(p_guest); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); transaction.Rollback(); return(null); } } } }
/// <summary> /// Request an Admin-Object from the Database, with the username. /// </summary> /// <param name="p_username">Username of the Administrator.</param> /// <returns>The found Adminobject will be returned. If no Admin was found, NULL will be returned. </returns> public Admin GetByUsername(string p_username) { using (var context = new FhdwHotelContext()) { return(context.Admin.SingleOrDefault(a => a.Username.ToLower() == p_username.ToLower())); } }
/// <summary> /// Get a specific Guest by their Emailaddress. /// </summary> /// <param name="p_email">Emailaddress of the Guest.</param> /// <returns>The requested Guest. If no Guest exists, return NULL.</returns> public Guest GetByEmail(string p_email) { using (var context = new FhdwHotelContext()) { return(context.Guest .Include(g => g.ContactAddress) .Include(g => g.BillingAddress) .SingleOrDefault(g => g.Emailaddress.ToLower() == p_email.ToLower())); } }
/// <summary> /// Get a List of all Rooms, wich are not already booked in a specific timeframe. /// </summary> /// <param name="p_hotelId"></param> /// <param name="p_arrival">The Arrivaldate of the Guest.</param> /// <param name="p_departure">The Departuredate of the Guest.</param> /// <returns>List with all Rooms. If no Room exists, return an empty List.</returns> public ICollection <Room> GetAvailableRooms(int p_hotelId, DateTime p_arrival, DateTime p_departure) { using (var context = new FhdwHotelContext()) { return(context.Room.Include(b => b.Bookings) .Where(r => r.Hotel.ID == p_hotelId) .Where(r => !r.Bookings.Any()) .Where(r => r.Bookings.FirstOrDefault(bo => bo.Arrival > p_arrival) != null) .Where(r => r.Bookings.FirstOrDefault(bo => bo.Departure < p_departure) != null) .ToList()); } }
/// <summary> /// Insert a new booking into the database. /// </summary> /// <param name="p_booking">Bookingobject without an ID.</param> /// <returns>Filled Bookingobject.</returns> public Booking Insert(Booking p_booking) { using (var context = new FhdwHotelContext()) { using (var transaction = context.Database.BeginTransaction()) { try { p_booking.Hotel = context.Hotel.SingleOrDefault(h => h.ID == p_booking.Hotel.ID); if (p_booking.Guest != null && p_booking.Guest.ID > 0) { p_booking.Guest = context.Guest.SingleOrDefault(g => g.ID == p_booking.Guest.ID); if (p_booking.Guest.ContactAddress.ID > 0) { p_booking.Guest.ContactAddress = context.Address.SingleOrDefault(a => a.ID == p_booking.Guest.ContactAddress.ID); } if (p_booking.Guest.BillingAddress.ID > 0) { p_booking.Guest.BillingAddress = context.Address.SingleOrDefault(a => a.ID == p_booking.Guest.BillingAddress.ID); } } var rooms = p_booking.Rooms; p_booking.Rooms = new List <Room>(); foreach (var room in rooms) { p_booking.Rooms.Add(context.Room.FirstOrDefault(r => r.ID == room.ID)); } context.Booking.Add(p_booking); context.SaveChanges(); transaction.Commit(); return(p_booking); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); transaction.Rollback(); return(null); } } } }
/// <summary> /// Insert a wide variety on testdata into the database. /// </summary> public void CreateDatabase() { using (var context = new FhdwHotelContext()) { using (var transaction = context.Database.BeginTransaction()) { try { transaction.Commit(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); transaction.Rollback(); } } context.Database.CreateIfNotExists(); } }
/// <summary> /// Add Testdata to the Database. /// </summary> /// <param name="p_addresses">A Collection with Addressobjects.</param> /// <param name="p_admins">A Collection with Adminobjects</param> /// <param name="p_bookings">A Collection with Bookingobjects</param> /// <param name="p_guests">A Collection with Guestobjects</param> /// <param name="p_hotels">A Collection with Hotelobjects</param> /// <param name="p_rooms">A Collection with Roomobjects</param> /// <param name="p_roomTypes">A Collection of RoomTypes</param> /// <param name="p_roomCategories">A Collection of Roomcategories.</param> public void InsertTestData(ICollection <Address> p_addresses, ICollection <Admin> p_admins, ICollection <Booking> p_bookings, ICollection <Guest> p_guests, ICollection <DomainModel.Hotel> p_hotels, ICollection <Room> p_rooms, ICollection <RoomType> p_roomTypes, ICollection <RoomCategory> p_roomCategories) { using (var context = new FhdwHotelContext()) { using (var transaction = context.Database.BeginTransaction()) { try { if (p_roomTypes != null && !context.RoomType.Any()) { context.RoomType.AddRange(p_roomTypes); context.SaveChanges(); } if (p_roomCategories != null && !context.RoomCategory.Any()) { context.RoomCategory.AddRange(p_roomCategories); context.SaveChanges(); } if (p_admins != null && !context.Admin.Any()) { context.Admin.AddRange(p_admins); context.SaveChanges(); } if (p_addresses != null && !context.Address.Any()) { context.Address.AddRange(p_addresses); context.SaveChanges(); } if (p_hotels != null && !context.Hotel.Any()) { foreach (var hotel in p_hotels) { hotel.Address = context.Address.SingleOrDefault(a => a.ID == hotel.Address.ID); context.Hotel.Add(hotel); } context.SaveChanges(); } if (p_rooms != null && !context.Room.Any()) { foreach (var room in p_rooms) { room.Hotel = context.Hotel.SingleOrDefault(h => h.ID == room.Hotel.ID); context.Room.Add(room); } context.SaveChanges(); } if (p_guests != null && !context.Guest.Any()) { foreach (var guest in p_guests) { if (guest.ContactAddress != null) { guest.ContactAddress = context.Address.SingleOrDefault(a => a.ID == guest.ContactAddress.ID); } if (guest.BillingAddress != null) { guest.BillingAddress = context.Address.SingleOrDefault(a => a.ID == guest.BillingAddress.ID); } context.Guest.Add(guest); } context.SaveChanges(); } if (p_bookings != null && !context.Booking.Any()) { context.Booking.AddRange(p_bookings); } transaction.Commit(); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine(ex.Message); transaction.Rollback(); } } } }