Add database

This commit is contained in:
Viltskaa 2023-04-07 13:15:55 +04:00
parent 329720bce7
commit 8b20de0f78
10 changed files with 450 additions and 0 deletions

View File

@ -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<Room> Rooms { get; set; }
public virtual DbSet<Reservation> Reservations { get; set; }
public virtual DbSet<ReservationRoom> ReservationRooms { get; set; }
public virtual DbSet<Maitre> Maitres { get; set; }
public virtual DbSet<Guest> Guests { get; set; }
public virtual DbSet<Cleaning> Cleanings { get; set; }
public virtual DbSet<CleaningInstruments> CleaningInstruments { get; set; }
public virtual DbSet<CleaningInstrument> CleaningInstrument { get; set; }
}

View File

@ -6,4 +6,18 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Folder Include="Implements" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.0-preview.2.23128.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="8.0.0-preview.2.23128.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\HotelContracts\HotelContracts.csproj" />
<ProjectReference Include="..\HotelDataModels\HotelDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -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> CleaningInstrument { get; set; } = new();
private Dictionary<int, ICleaningInstrumentsModel>? _cleaningInstruments = null;
[NotMapped]
public Dictionary<int, ICleaningInstrumentsModel> 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
};
}

View File

@ -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();
}

View File

@ -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
};
}

View File

@ -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
};
}

View File

@ -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
};
}

View File

@ -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<ReservationRoom> Rooms { get; set; } = new();
private Dictionary<int, IRoomModel>? _reservationsRooms = null;
public Dictionary<int, IRoomModel> 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
};
}

View File

@ -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();
}

View File

@ -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
};
}