From d92984f518a10471e1479e55f0e67026c21b9a08 Mon Sep 17 00:00:00 2001 From: AnnZhimol Date: Sun, 2 Apr 2023 16:19:39 +0400 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20BindingModel,=20ViewModel,=20DataMod?= =?UTF-8?q?els,=20=D0=B8=20=D0=B4=D0=BE=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20Models=20=D0=B2=20DataBaseImplement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BindingModels/ConferenceBindingModel.cs | 1 + .../BindingModels/MealPlanBindingModel.cs | 1 + .../ViewModels/ConferenceViewModel.cs | 1 + .../ViewModels/MealPlanViewModel.cs | 2 + Hotel/HotelDataBaseImplement/HotelDataBase.cs | 18 +++- .../Models/Conference.cs | 85 ++++++++++++++++-- .../Models/ConferenceMember.cs | 18 ++++ .../HotelDataBaseImplement/Models/MealPlan.cs | 86 +++++++++++++++++-- .../Models/MealPlanMember.cs | 18 ++++ Hotel/HotelDataBaseImplement/Models/Member.cs | 22 ++++- .../Models/Organiser.cs | 80 +++++++++++++++-- .../Models/IConferenceModel.cs | 1 + .../HotelDataModels/Models/IMealPlanModel.cs | 1 + 13 files changed, 309 insertions(+), 25 deletions(-) create mode 100644 Hotel/HotelDataBaseImplement/Models/ConferenceMember.cs create mode 100644 Hotel/HotelDataBaseImplement/Models/MealPlanMember.cs diff --git a/Hotel/HotelContracts/BindingModels/ConferenceBindingModel.cs b/Hotel/HotelContracts/BindingModels/ConferenceBindingModel.cs index a84e8e4..622fb38 100644 --- a/Hotel/HotelContracts/BindingModels/ConferenceBindingModel.cs +++ b/Hotel/HotelContracts/BindingModels/ConferenceBindingModel.cs @@ -11,5 +11,6 @@ namespace HotelContracts.BindingModels public int Id { get; set; } public int OrganiserId { get; set; } + public Dictionary ConferenceMembers { get; set; } } } diff --git a/Hotel/HotelContracts/BindingModels/MealPlanBindingModel.cs b/Hotel/HotelContracts/BindingModels/MealPlanBindingModel.cs index 3bb29e6..c8147b6 100644 --- a/Hotel/HotelContracts/BindingModels/MealPlanBindingModel.cs +++ b/Hotel/HotelContracts/BindingModels/MealPlanBindingModel.cs @@ -11,5 +11,6 @@ namespace HotelContracts.BindingModels public int OrganiserId { get; set; } public int Id { get; set; } + public Dictionary MealPlanMembers { get; set; } } } diff --git a/Hotel/HotelContracts/ViewModels/ConferenceViewModel.cs b/Hotel/HotelContracts/ViewModels/ConferenceViewModel.cs index b5855d2..4d3a370 100644 --- a/Hotel/HotelContracts/ViewModels/ConferenceViewModel.cs +++ b/Hotel/HotelContracts/ViewModels/ConferenceViewModel.cs @@ -14,5 +14,6 @@ namespace HotelContracts.ViewModels public int OrganiserId { get; set; } public int Id { get; set; } + public Dictionary ConferenceMembers { get; set; } } } diff --git a/Hotel/HotelContracts/ViewModels/MealPlanViewModel.cs b/Hotel/HotelContracts/ViewModels/MealPlanViewModel.cs index 6de6193..fe37310 100644 --- a/Hotel/HotelContracts/ViewModels/MealPlanViewModel.cs +++ b/Hotel/HotelContracts/ViewModels/MealPlanViewModel.cs @@ -14,5 +14,7 @@ namespace HotelContracts.ViewModels public int OrganiserId { get; set; } public int Id { get; set; } + + public Dictionary MealPlanMembers { get; set; } } } diff --git a/Hotel/HotelDataBaseImplement/HotelDataBase.cs b/Hotel/HotelDataBaseImplement/HotelDataBase.cs index 03406d6..cf02a1f 100644 --- a/Hotel/HotelDataBaseImplement/HotelDataBase.cs +++ b/Hotel/HotelDataBaseImplement/HotelDataBase.cs @@ -1,9 +1,23 @@ -using Microsoft.EntityFrameworkCore; +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=ANNZHIMOL\SQLEXPRESS;Initial Catalog=HotelDataBaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Conferences { set; get; } + public virtual DbSet MealPlans { set; get; } + public virtual DbSet Members { set; get; } + public virtual DbSet Organisers { set; get; } + public virtual DbSet ConferenceMembers { set; get; } + public virtual DbSet MealPlanMembers { set; get; } } } \ No newline at end of file diff --git a/Hotel/HotelDataBaseImplement/Models/Conference.cs b/Hotel/HotelDataBaseImplement/Models/Conference.cs index eaad732..b61cd1b 100644 --- a/Hotel/HotelDataBaseImplement/Models/Conference.cs +++ b/Hotel/HotelDataBaseImplement/Models/Conference.cs @@ -1,15 +1,90 @@ -using HotelDataModels.Models; +using HotelContracts.BindingModels; +using HotelContracts.ViewModels; +using HotelDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace HotelDataBaseImplement.Models { public class Conference : IConferenceModel { - public string ConferenceName => throw new NotImplementedException(); + [Required] + public string ConferenceName { get; set; } = string.Empty; + [Required] + public DateTime StartDate { get; set; } = DateTime.Now; - public DateTime StartDate => throw new NotImplementedException(); + public int OrganiserId { get; private set; } - public int OrganiserId => throw new NotImplementedException(); + public int Id { get; private set; } - public int Id => throw new NotImplementedException(); + public virtual Organiser Organiser { get; set; } + + [ForeignKey("ConferenceId")] + public virtual List Members { get; set; } + + private Dictionary _conferenceMembers = null; + public Dictionary ConferenceMembers + { + get + { + if (_conferenceMembers == null) + { + _conferenceMembers = Members.ToDictionary(recPC => recPC.MemberId, recPC => (recPC.Member as IMemberModel)); + } + return _conferenceMembers; + } + } + + public static Conference Create(HotelDataBase context, ConferenceBindingModel model) + { + return new Conference() + { + Id = model.Id, + ConferenceName = model.ConferenceName, + StartDate = model.StartDate, + Members = model.ConferenceMembers.Select(x => new ConferenceMember + { + Member = context.Members.First(y => y.Id == x.Key), + }).ToList() + }; + } + + public void Update(ConferenceBindingModel model) + { + ConferenceName = model.ConferenceName; + StartDate = model.StartDate; + } + + public ConferenceViewModel GetViewModel => new() + { + Id = Id, + ConferenceName = ConferenceName, + StartDate = StartDate, + ConferenceMembers = ConferenceMembers + }; + + public void UpdateMembers(HotelDataBase context, ConferenceBindingModel model) + { + var conferenceMembers = context.ConferenceMembers.Where(rec => rec.ConferenceId == model.Id).ToList(); + + if (conferenceMembers != null) + { // удалили те, которых нет в модели + context.ConferenceMembers.RemoveRange(conferenceMembers.Where(rec => !model.ConferenceMembers.ContainsKey(rec.MemberId))); + context.SaveChanges(); + } + + var conference = context.Conferences.First(x => x.Id == Id); + + foreach (var cm in model.ConferenceMembers) + { + context.ConferenceMembers.Add(new ConferenceMember + { + Conference = conference, + Member = context.Members.First(x => x.Id == cm.Key) + }); + context.SaveChanges(); + } + _conferenceMembers = null; + } } } diff --git a/Hotel/HotelDataBaseImplement/Models/ConferenceMember.cs b/Hotel/HotelDataBaseImplement/Models/ConferenceMember.cs new file mode 100644 index 0000000..1c3d5de --- /dev/null +++ b/Hotel/HotelDataBaseImplement/Models/ConferenceMember.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace HotelDataBaseImplement.Models +{ + public class ConferenceMember + { + public int Id { get; set; } + + [Required] + public int MemberId { get; set; } + + [Required] + public int ConferenceId { get; set; } + + public virtual Conference Conference { get; set; } = new(); + public virtual Member Member { get; set; } = new(); + } +} diff --git a/Hotel/HotelDataBaseImplement/Models/MealPlan.cs b/Hotel/HotelDataBaseImplement/Models/MealPlan.cs index 3ae8b43..1517c3e 100644 --- a/Hotel/HotelDataBaseImplement/Models/MealPlan.cs +++ b/Hotel/HotelDataBaseImplement/Models/MealPlan.cs @@ -1,15 +1,91 @@ -using HotelDataModels.Models; +using HotelContracts.BindingModels; +using HotelContracts.ViewModels; +using HotelDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace HotelDataBaseImplement.Models { public class MealPlan : IMealPlanModel { - public string MealPlanName => throw new NotImplementedException(); + [Required] + public string MealPlanName { get; set; } = string.Empty; - public double MealPlanPrice => throw new NotImplementedException(); + [Required] + public double MealPlanPrice { get; set; } - public int OrganiserId => throw new NotImplementedException(); + public int OrganiserId { get; private set; } - public int Id => throw new NotImplementedException(); + public int Id { get; private set; } + + public virtual Organiser Organiser { get; set; } + + [ForeignKey("MealPlanId")] + public virtual List Members { get; set; } + + private Dictionary _mealPlanMembers = null; + + public Dictionary MealPlanMembers + { + get + { + if (_mealPlanMembers == null) + { + _mealPlanMembers = Members.ToDictionary(recPC => recPC.MemberId, recPC => (recPC.Member as IMemberModel)); + } + return _mealPlanMembers; + } + } + public static MealPlan Create(HotelDataBase context, MealPlanBindingModel model) + { + return new MealPlan() + { + Id = model.Id, + MealPlanName = model.MealPlanName, + MealPlanPrice = model.MealPlanPrice, + Members = model.MealPlanMembers.Select(x => new MealPlanMember + { + Member = context.Members.First(y => y.Id == x.Key), + }).ToList() + }; + } + + public void Update(MealPlanBindingModel model) + { + MealPlanName = model.MealPlanName; + MealPlanPrice = model.MealPlanPrice; + } + + public MealPlanViewModel GetViewModel => new() + { + Id = Id, + MealPlanName = MealPlanName, + MealPlanPrice = MealPlanPrice, + MealPlanMembers = MealPlanMembers + }; + + public void UpdateMembers(HotelDataBase context, MealPlanBindingModel model) + { + var mealPlanMembers = context.MealPlanMembers.Where(rec => rec.MealPlanId == model.Id).ToList(); + + if (mealPlanMembers != null) + { // удалили те, которых нет в модели + context.MealPlanMembers.RemoveRange(mealPlanMembers.Where(rec => !model.MealPlanMembers.ContainsKey(rec.MemberId))); + context.SaveChanges(); + } + + var mealPlan = context.MealPlans.First(x => x.Id == Id); + + foreach (var cm in model.MealPlanMembers) + { + context.MealPlanMembers.Add(new MealPlanMember + { + MealPlan = mealPlan, + Member = context.Members.First(x => x.Id == cm.Key) + }); + context.SaveChanges(); + } + _mealPlanMembers = null; + } } } diff --git a/Hotel/HotelDataBaseImplement/Models/MealPlanMember.cs b/Hotel/HotelDataBaseImplement/Models/MealPlanMember.cs new file mode 100644 index 0000000..4865f6a --- /dev/null +++ b/Hotel/HotelDataBaseImplement/Models/MealPlanMember.cs @@ -0,0 +1,18 @@ +using System.ComponentModel.DataAnnotations; + +namespace HotelDataBaseImplement.Models +{ + public class MealPlanMember + { + public int Id { get; set; } + + [Required] + public int MemberId { get; set; } + + [Required] + public int MealPlanId { get; set; } + + public virtual MealPlan MealPlan { get; set; } = new(); + public virtual Member Member { get; set; } = new(); + } +} diff --git a/Hotel/HotelDataBaseImplement/Models/Member.cs b/Hotel/HotelDataBaseImplement/Models/Member.cs index 7ee5d51..d55e872 100644 --- a/Hotel/HotelDataBaseImplement/Models/Member.cs +++ b/Hotel/HotelDataBaseImplement/Models/Member.cs @@ -1,15 +1,29 @@ using HotelDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace HotelDataBaseImplement.Models { public class Member : IMemberModel { - public string MemberFIO => throw new NotImplementedException(); + [Required] + public string MemberFIO { get; set; } = string.Empty; - public string Citizenship => throw new NotImplementedException(); + [Required] + public string Citizenship { get; set; } = string.Empty; - public int OrganiserId => throw new NotImplementedException(); + public int OrganiserId { get; private set; } - public int Id => throw new NotImplementedException(); + public int Id { get; private set; } + + public virtual Organiser Organiser { get; set; } + + [ForeignKey("MemberId")] + public virtual List MealPlanMember { get; set; } + + [ForeignKey("MemberId")] + public virtual List ConferenceMember { get; set; } + + } } diff --git a/Hotel/HotelDataBaseImplement/Models/Organiser.cs b/Hotel/HotelDataBaseImplement/Models/Organiser.cs index d888514..e2cd78e 100644 --- a/Hotel/HotelDataBaseImplement/Models/Organiser.cs +++ b/Hotel/HotelDataBaseImplement/Models/Organiser.cs @@ -1,19 +1,81 @@ -using HotelDataModels.Models; +using HotelContracts.BindingModels; +using HotelContracts.ViewModels; +using HotelDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace HotelDataBaseImplement.Models { public class Organiser : IOrganiserModel { - public string OrganiserFIO => throw new NotImplementedException(); + [Required] + public string OrganiserFIO { get; set; } = string.Empty; + [Required] + public string OrganiserPassword { get; set; } = string.Empty; + [Required] + public string OrganiserLogin { get; set; } = string.Empty; + [Required] + public string OrganiserEmail { get; set; } = string.Empty; + [Required] + public string OrganiserNumber { get; set; } = string.Empty; - public string OrganiserPassword => throw new NotImplementedException(); + public int Id { get; private set; } - public string OrganiserLogin => throw new NotImplementedException(); + [ForeignKey("OrganiserId")] + public virtual List Conferences { get; set; } = new(); + [ForeignKey("OrganiserId")] + public virtual List MealPlans { get; set; } = new(); + [ForeignKey("OrganiserId")] + public virtual List Members { get; set; } = new(); - public string OrganiserEmail => throw new NotImplementedException(); - - public string OrganiserNumber => throw new NotImplementedException(); - - public int Id => throw new NotImplementedException(); + public static Organiser? Create(OrganiserBindingModel model) + { + if (model == null) + { + return null; + } + return new Organiser() + { + Id = model.Id, + OrganiserFIO = model.OrganiserFIO, + OrganiserEmail = model.OrganiserEmail, + OrganiserPassword = model.OrganiserPassword, + OrganiserLogin = model.OrganiserLogin, + OrganiserNumber = model.OrganiserNumber + }; + } + public static Organiser Create(OrganiserViewModel model) + { + return new Organiser + { + Id = model.Id, + OrganiserFIO = model.OrganiserFIO, + OrganiserEmail = model.OrganiserEmail, + OrganiserPassword = model.OrganiserPassword, + OrganiserLogin= model.OrganiserLogin, + OrganiserNumber= model.OrganiserNumber + }; + } + public void Update(OrganiserBindingModel model) + { + if (model == null) + { + return; + } + OrganiserFIO = model.OrganiserFIO; + OrganiserEmail = model.OrganiserEmail; + OrganiserPassword = model.OrganiserPassword; + OrganiserLogin = model.OrganiserLogin; + OrganiserNumber = model.OrganiserNumber; + } + public OrganiserViewModel GetViewModel => new() + { + Id = Id, + OrganiserFIO = OrganiserFIO, + OrganiserEmail = OrganiserEmail, + OrganiserPassword = OrganiserPassword, + OrganiserNumber = OrganiserNumber, + OrganiserLogin = OrganiserLogin + }; } } diff --git a/Hotel/HotelDataModels/Models/IConferenceModel.cs b/Hotel/HotelDataModels/Models/IConferenceModel.cs index d1ef09d..38a9d14 100644 --- a/Hotel/HotelDataModels/Models/IConferenceModel.cs +++ b/Hotel/HotelDataModels/Models/IConferenceModel.cs @@ -5,5 +5,6 @@ string ConferenceName { get; } DateTime StartDate { get; } int OrganiserId { get; } + public Dictionary ConferenceMembers { get; } } } diff --git a/Hotel/HotelDataModels/Models/IMealPlanModel.cs b/Hotel/HotelDataModels/Models/IMealPlanModel.cs index 9821716..53fc27f 100644 --- a/Hotel/HotelDataModels/Models/IMealPlanModel.cs +++ b/Hotel/HotelDataModels/Models/IMealPlanModel.cs @@ -5,5 +5,6 @@ string MealPlanName { get; } double MealPlanPrice { get; } int OrganiserId { get; } + public Dictionary MealPlanMembers { get; } } }