Исправление BindingModel, ViewModel, DataModels, и дополнение Models в DataBaseImplement

This commit is contained in:
AnnZhimol 2023-04-02 16:19:39 +04:00
parent c0c1c2dad2
commit d92984f518
13 changed files with 309 additions and 25 deletions

View File

@ -11,5 +11,6 @@ namespace HotelContracts.BindingModels
public int Id { get; set; } public int Id { get; set; }
public int OrganiserId { get; set; } public int OrganiserId { get; set; }
public Dictionary<int, IMemberModel> ConferenceMembers { get; set; }
} }
} }

View File

@ -11,5 +11,6 @@ namespace HotelContracts.BindingModels
public int OrganiserId { get; set; } public int OrganiserId { get; set; }
public int Id { get; set; } public int Id { get; set; }
public Dictionary<int, IMemberModel> MealPlanMembers { get; set; }
} }
} }

View File

@ -14,5 +14,6 @@ namespace HotelContracts.ViewModels
public int OrganiserId { get; set; } public int OrganiserId { get; set; }
public int Id { get; set; } public int Id { get; set; }
public Dictionary<int, IMemberModel> ConferenceMembers { get; set; }
} }
} }

View File

@ -14,5 +14,7 @@ namespace HotelContracts.ViewModels
public int OrganiserId { get; set; } public int OrganiserId { get; set; }
public int Id { get; set; } public int Id { get; set; }
public Dictionary<int, IMemberModel> MealPlanMembers { get; set; }
} }
} }

View File

@ -1,9 +1,23 @@
using Microsoft.EntityFrameworkCore; using HotelDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
namespace HotelDataBaseImplement namespace HotelDataBaseImplement
{ {
public class HotelDataBase : DbContext 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<Conference> Conferences { set; get; }
public virtual DbSet<MealPlan> MealPlans { set; get; }
public virtual DbSet<Member> Members { set; get; }
public virtual DbSet<Organiser> Organisers { set; get; }
public virtual DbSet<ConferenceMember> ConferenceMembers { set; get; }
public virtual DbSet<MealPlanMember> MealPlanMembers { set; get; }
} }
} }

View File

@ -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 namespace HotelDataBaseImplement.Models
{ {
public class Conference : IConferenceModel 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<ConferenceMember> Members { get; set; }
private Dictionary<int, IMemberModel> _conferenceMembers = null;
public Dictionary<int, IMemberModel> 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;
}
} }
} }

View File

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

View File

@ -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 namespace HotelDataBaseImplement.Models
{ {
public class MealPlan : IMealPlanModel 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<MealPlanMember> Members { get; set; }
private Dictionary<int, IMemberModel> _mealPlanMembers = null;
public Dictionary<int, IMemberModel> 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;
}
} }
} }

View File

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

View File

@ -1,15 +1,29 @@
using HotelDataModels.Models; using HotelDataModels.Models;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace HotelDataBaseImplement.Models namespace HotelDataBaseImplement.Models
{ {
public class Member : IMemberModel 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 { get; private set; }
public int Id { get; private set; }
public virtual Organiser Organiser { get; set; }
[ForeignKey("MemberId")]
public virtual List<MealPlanMember> MealPlanMember { get; set; }
[ForeignKey("MemberId")]
public virtual List<ConferenceMember> ConferenceMember { get; set; }
public int OrganiserId => throw new NotImplementedException();
public int Id => throw new NotImplementedException();
} }
} }

View File

@ -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 namespace HotelDataBaseImplement.Models
{ {
public class Organiser : IOrganiserModel 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<Conference> Conferences { get; set; } = new();
[ForeignKey("OrganiserId")]
public virtual List<MealPlan> MealPlans { get; set; } = new();
[ForeignKey("OrganiserId")]
public virtual List<Member> Members { get; set; } = new();
public string OrganiserEmail => throw new NotImplementedException(); public static Organiser? Create(OrganiserBindingModel model)
{
public string OrganiserNumber => throw new NotImplementedException(); if (model == null)
{
public int Id => throw new NotImplementedException(); 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
};
} }
} }

View File

@ -5,5 +5,6 @@
string ConferenceName { get; } string ConferenceName { get; }
DateTime StartDate { get; } DateTime StartDate { get; }
int OrganiserId { get; } int OrganiserId { get; }
public Dictionary<int, IMemberModel> ConferenceMembers { get; }
} }
} }

View File

@ -5,5 +5,6 @@
string MealPlanName { get; } string MealPlanName { get; }
double MealPlanPrice { get; } double MealPlanPrice { get; }
int OrganiserId { get; } int OrganiserId { get; }
public Dictionary<int, IMemberModel> MealPlanMembers { get; }
} }
} }