diff --git a/Hotel/HotelDatabaseImplement/HotelDataBase.cs b/Hotel/HotelDatabaseImplement/HotelDataBase.cs new file mode 100644 index 0000000..f2e07b5 --- /dev/null +++ b/Hotel/HotelDatabaseImplement/HotelDataBase.cs @@ -0,0 +1,24 @@ +using HotelDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace HotelDatabaseImplement; + +public class HotelDataBase : DbContext +{ + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=LAPTOP-QKSH4DCA\SQLEXPRESS;Initial Catalog=Hotel;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Rooms { get; set; } + public virtual DbSet Reservations { get; set; } + public virtual DbSet ReservationRooms { get; set; } + public virtual DbSet Maitres { get; set; } + public virtual DbSet Guests { get; set; } + public virtual DbSet Cleanings { get; set; } + public virtual DbSet CleaningInstruments { get; set; } + public virtual DbSet CleaningInstrument { get; set; } +} \ No newline at end of file diff --git a/Hotel/HotelDatabaseImplement/HotelDatabaseImplement.csproj b/Hotel/HotelDatabaseImplement/HotelDatabaseImplement.csproj index 132c02c..0018b83 100644 --- a/Hotel/HotelDatabaseImplement/HotelDatabaseImplement.csproj +++ b/Hotel/HotelDatabaseImplement/HotelDatabaseImplement.csproj @@ -6,4 +6,18 @@ enable + + + + + + + + + + + + + + diff --git a/Hotel/HotelDatabaseImplement/Models/Cleaning.cs b/Hotel/HotelDatabaseImplement/Models/Cleaning.cs new file mode 100644 index 0000000..9814039 --- /dev/null +++ b/Hotel/HotelDatabaseImplement/Models/Cleaning.cs @@ -0,0 +1,87 @@ +using System.ComponentModel.DataAnnotations.Schema; +using HotelContracts.BindingModels; +using HotelContracts.ViewModels; +using HotelDataModels.Models; + +namespace HotelDatabaseImplement.Models; + +public class Cleaning : ICleaningModel +{ + public int Id { get; set; } + public DateTime Date { get; set; } + public int RoomId { get; set; } + + [ForeignKey("CleaningId")] + public virtual List CleaningInstrument { get; set; } = new(); + private Dictionary? _cleaningInstruments = null; + [NotMapped] + public Dictionary CleaningInstruments { + get + { + _cleaningInstruments ??= CleaningInstrument + .ToDictionary( + x => x.CleaningInstrumentsId, + x => x.CleaningInstruments as ICleaningInstrumentsModel + ); + return _cleaningInstruments; + } + } + + public static Cleaning Create(HotelDataBase dataBase, CleaningBindingModel model) + { + return new Cleaning + { + Id = model.Id, + Date = model.Date, + CleaningInstrument = model.CleaningInstruments.Select( + x => new CleaningInstrument + { + CleaningInstruments = dataBase.CleaningInstruments.First(y => y.Id == x.Key) + }).ToList() + }; + } + + public void Update(CleaningBindingModel model) + { + Date = model.Date; + } + + public void UpdateCleaningInstruments(HotelDataBase dataBase, CleaningBindingModel model) + { + var cleaningInstruments = dataBase.CleaningInstrument + .Where(x => x.CleaningId == model.Id) + .ToList(); + + dataBase.CleaningInstrument.RemoveRange( + cleaningInstruments.Where(x => !model.CleaningInstruments.ContainsKey(x.CleaningInstrumentsId)) + ); + dataBase.SaveChanges(); + foreach (var toUpdate in cleaningInstruments) + { + model.CleaningInstruments.Remove(toUpdate.CleaningInstrumentsId); + } + + dataBase.SaveChanges(); + + var cleaning = dataBase.Cleanings.First(x => x.Id == Id); + foreach (var cleaningInstrument in model.CleaningInstruments) + { + dataBase.CleaningInstrument.Add(new CleaningInstrument + { + Cleaning = cleaning, + CleaningInstruments = dataBase.CleaningInstruments + .First(x => x.Id == cleaningInstrument.Key) + }); + dataBase.SaveChanges(); + } + + _cleaningInstruments = null; + } + + public CleaningViewModel GetView => new CleaningViewModel + { + Id = Id, + Date = Date, + CleaningInstruments = CleaningInstruments + }; +} \ No newline at end of file diff --git a/Hotel/HotelDatabaseImplement/Models/CleaningInstrument.cs b/Hotel/HotelDatabaseImplement/Models/CleaningInstrument.cs new file mode 100644 index 0000000..754e12c --- /dev/null +++ b/Hotel/HotelDatabaseImplement/Models/CleaningInstrument.cs @@ -0,0 +1,11 @@ +namespace HotelDatabaseImplement.Models; + +public class CleaningInstrument +{ + public int Id { get; set; } + public int CleaningId { get; set; } + public int CleaningInstrumentsId { get; set; } + + public virtual Cleaning Cleaning { get; set; } = new(); + public virtual CleaningInstruments CleaningInstruments { get; set; } = new(); +} \ No newline at end of file diff --git a/Hotel/HotelDatabaseImplement/Models/CleaningInstruments.cs b/Hotel/HotelDatabaseImplement/Models/CleaningInstruments.cs new file mode 100644 index 0000000..ff2c8d4 --- /dev/null +++ b/Hotel/HotelDatabaseImplement/Models/CleaningInstruments.cs @@ -0,0 +1,41 @@ +using HotelContracts.BindingModels; +using HotelContracts.ViewModels; +using HotelDataModels.Models; + +namespace HotelDatabaseImplement.Models; + +public class CleaningInstruments : ICleaningInstrumentsModel +{ + public int Id { get; set; } + public string Type { get; set; } + + public static CleaningInstruments Create(CleaningInstrumentsBindingModel model) + { + return new CleaningInstruments + { + Id = model.Id, + Type = model.Type + }; + } + + public static CleaningInstruments Create(CleaningInstrumentsViewModel model) + { + return new CleaningInstruments + { + Id = model.Id, + Type = model.Type + }; + } + + public void Update(CleaningInstrumentsBindingModel? model) + { + if (model == null) return; + Type = model.Type; + } + + public CleaningInstrumentsViewModel GetView => new CleaningInstrumentsViewModel + { + Id = Id, + Type = Type + }; +} \ No newline at end of file diff --git a/Hotel/HotelDatabaseImplement/Models/Guest.cs b/Hotel/HotelDatabaseImplement/Models/Guest.cs new file mode 100644 index 0000000..84f74f1 --- /dev/null +++ b/Hotel/HotelDatabaseImplement/Models/Guest.cs @@ -0,0 +1,55 @@ +using System.ComponentModel.DataAnnotations; +using HotelContracts.BindingModels; +using HotelContracts.ViewModels; +using HotelDataModels.Models; + +namespace HotelDatabaseImplement.Models; + +public class Guest : IGuestModel +{ + public int Id { get; set; } + [Required] + public string Name { get; set; } + [Required] + public string SecondName { get; set; } + [Required] + public string LastName { get; set; } + + public static Guest? Create(GuestBindingModel? model) + { + if (model == null) return null; + return new Guest + { + Id = model.Id, + Name = model.Name, + LastName = model.LastName, + SecondName = model.SecondName + }; + } + + public static Guest Create(GuestViewModel model) + { + return new Guest + { + Id = model.Id, + Name = model.Name, + LastName = model.LastName, + SecondName = model.SecondName + }; + } + + public void Update(GuestBindingModel model) + { + Name = model.Name; + LastName = model.LastName; + SecondName = model.SecondName; + } + + public GuestViewModel GetView => new GuestViewModel + { + Id = Id, + Name = Name, + LastName = LastName, + SecondName = SecondName + }; +} \ No newline at end of file diff --git a/Hotel/HotelDatabaseImplement/Models/Maitre.cs b/Hotel/HotelDatabaseImplement/Models/Maitre.cs new file mode 100644 index 0000000..be2b60b --- /dev/null +++ b/Hotel/HotelDatabaseImplement/Models/Maitre.cs @@ -0,0 +1,67 @@ +using System.ComponentModel.DataAnnotations; +using HotelContracts.BindingModels; +using HotelContracts.ViewModels; +using HotelDataModels.Models; + +namespace HotelDatabaseImplement.Models; + +public class Maitre : IMaitreModel +{ + public int Id { get; set; } + [Required] + public string Name { get; set; } + [Required] + public string SecondName { get; set; } + [Required] + public string LastName { get; set; } + [Required] + public string Login { get; set; } + [Required] + public string Password { get; set; } + + public static Maitre Create(MaitreBindingModel model) + { + return new Maitre + { + Id = model.Id, + Name = model.Name, + SecondName = model.SecondName, + LastName = model.LastName, + Login = model.Login, + Password = model.Password + }; + } + + public static Maitre Create(MaitreViewModel model) + { + return new Maitre + { + Id = model.Id, + Name = model.Name, + SecondName = model.SecondName, + LastName = model.LastName, + Login = model.Login, + Password = model.Password + }; + } + + public void Update(MaitreBindingModel? model) + { + if (model == null) return; + Name = model.Name; + SecondName = model.SecondName; + LastName = model.LastName; + Login = model.Login; + Password = model.Password; + } + + public MaitreViewModel GetViewModel => new MaitreViewModel + { + Id = Id, + Name = Name, + SecondName = SecondName, + LastName = LastName, + Login = Login, + Password = Password + }; +} \ No newline at end of file diff --git a/Hotel/HotelDatabaseImplement/Models/Reservation.cs b/Hotel/HotelDatabaseImplement/Models/Reservation.cs new file mode 100644 index 0000000..992288f --- /dev/null +++ b/Hotel/HotelDatabaseImplement/Models/Reservation.cs @@ -0,0 +1,91 @@ +using System.ComponentModel.DataAnnotations.Schema; +using HotelContracts.BindingModels; +using HotelContracts.ViewModels; +using HotelDataModels.Models; + +namespace HotelDatabaseImplement.Models; + +public class Reservation : IReservationModel +{ + public int Id { get; set; } + public DateTime StartDate { get; set; } + public DateTime EndDate { get; set; } + public int GuestId { get; set; } + public int MaitreId { get; set; } + + [ForeignKey("RoomId")] + public virtual List Rooms { get; set; } = new(); + + private Dictionary? _reservationsRooms = null; + public Dictionary ReservationsRooms + { + get + { + _reservationsRooms ??= Rooms + .ToDictionary(record => record.RoomId, room => room.Room as IRoomModel); + return _reservationsRooms; + } + } + + public static Reservation? Create(HotelDataBase dataBase, ReservationBindingModel model) + { + return new Reservation + { + Id = model.Id, + StartDate = model.StartDate, + EndDate = model.EndDate, + GuestId = model.GuestId, + MaitreId = model.MaitreId, + Rooms = model.ReservationsRooms.Select(x => new ReservationRoom + { + Room = dataBase.Rooms.First(y => y.Id == x.Key) + }).ToList() + }; + } + + public void Update(ReservationBindingModel model) + { + StartDate = model.StartDate; + EndDate = model.EndDate; + } + + public void UpdateRooms(HotelDataBase dataBase, ReservationBindingModel model) + { + var rooms = dataBase.ReservationRooms + .Where(x => x.ReservationId == model.Id) + .ToList(); + dataBase.ReservationRooms.RemoveRange( + rooms.Where(x => !model.ReservationsRooms.ContainsKey(x.RoomId)) + ); + + foreach (var toUpdate in rooms) + { + model.ReservationsRooms.Remove(toUpdate.RoomId); + } + + dataBase.SaveChanges(); + + var reservation = dataBase.Reservations.First(x => x.Id == Id); + foreach (var reservationRoom in model.ReservationsRooms) + { + dataBase.ReservationRooms.Add(new ReservationRoom + { + Room = dataBase.Rooms.First(x => x.Id == reservationRoom.Key), + Reservation = reservation + }); + dataBase.SaveChanges(); + } + + _reservationsRooms = null; + } + + public ReservationViewModel GetView => new ReservationViewModel + { + Id = Id, + StartDate = StartDate, + EndDate = EndDate, + GuestId = GuestId, + MaitreId = MaitreId, + ReservationsRooms = ReservationsRooms + }; +} \ No newline at end of file diff --git a/Hotel/HotelDatabaseImplement/Models/ReservationRoom.cs b/Hotel/HotelDatabaseImplement/Models/ReservationRoom.cs new file mode 100644 index 0000000..03e17df --- /dev/null +++ b/Hotel/HotelDatabaseImplement/Models/ReservationRoom.cs @@ -0,0 +1,11 @@ +namespace HotelDatabaseImplement.Models; + +public class ReservationRoom +{ + public int Id { get; set; } + public int ReservationId { get; set; } + public int RoomId { get; set; } + + public virtual Room Room { get; set; } = new(); + public virtual Reservation Reservation { get; set; } = new(); +} \ No newline at end of file diff --git a/Hotel/HotelDatabaseImplement/Models/Room.cs b/Hotel/HotelDatabaseImplement/Models/Room.cs new file mode 100644 index 0000000..bdd6a25 --- /dev/null +++ b/Hotel/HotelDatabaseImplement/Models/Room.cs @@ -0,0 +1,49 @@ +using HotelContracts.BindingModels; +using HotelContracts.ViewModels; +using HotelDataModels.Models; + +namespace HotelDatabaseImplement.Models; + +public class Room : IRoomModel +{ + public int Id { get; set; } + public string Type { get; set; } + public double Cost { get; set; } + + public virtual Reservation Reservation { get; set; } = new(); + + public static Room? Create(RoomBindingModel? model) + { + if (model == null) return null; + return new Room + { + Id = model.Id, + Type = model.Type, + Cost = model.Cost + }; + } + + public static Room Create(RoomViewModel model) + { + return new Room + { + Id = model.Id, + Type = model.Type, + Cost = model.Cost + }; + } + + public void Update(RoomBindingModel? model) + { + if (model == null) return; + Type = model.Type; + Cost = model.Cost; + } + + public RoomViewModel GetView => new RoomViewModel + { + Id = Id, + Type = Type, + Cost = Cost + }; +} \ No newline at end of file