модели для базы данных работника
This commit is contained in:
parent
f2041b026e
commit
5e0f6fd0be
@ -13,7 +13,7 @@ namespace HospitalContracts.BindingModels
|
||||
|
||||
public string FIO { get; set; } = string.Empty;
|
||||
|
||||
public string Adress { get; set; } = string.Empty;
|
||||
public string Address { get; set; } = string.Empty;
|
||||
|
||||
public DateOnly BirthDate { get; set; }
|
||||
|
||||
|
@ -10,7 +10,7 @@ namespace HospitalDataModels.Models
|
||||
{
|
||||
string FIO { get; }
|
||||
DateOnly BirthDate { get; }
|
||||
string Adress { get; }
|
||||
string Address { get; }
|
||||
int DoctorId { get; }
|
||||
Dictionary<int, IProcedureModel> PatientProcedures { get; }
|
||||
|
||||
|
77
Hospital/HospitalDatabaseImplement/Models/Disease.cs
Normal file
77
Hospital/HospitalDatabaseImplement/Models/Disease.cs
Normal file
@ -0,0 +1,77 @@
|
||||
using HospitalContracts.BindingModels;
|
||||
using HospitalContracts.ViewModels;
|
||||
using HospitalDatabaseImplement.Modelss;
|
||||
using HospitalDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HospitalDatabaseImplement.Models
|
||||
{
|
||||
public class Disease : IDiseaseModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public int DoctorId { get; private set; }
|
||||
|
||||
public virtual Doctor Doctor { get; set; }
|
||||
|
||||
[Required]
|
||||
[MaxLength(50)]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
[MaxLength(150)]
|
||||
public string Description { get; set; } = string.Empty;
|
||||
|
||||
[ForeignKey("DiseaseId")]
|
||||
public virtual List<Recipe> Recipes { get; set; } = new();
|
||||
|
||||
public static Disease? Create(DiseaseBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Disease()
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name,
|
||||
Description = model.Description,
|
||||
DoctorId = model.DoctorId
|
||||
};
|
||||
}
|
||||
|
||||
public static Disease Create(DiseaseViewModel model)
|
||||
{
|
||||
return new Disease
|
||||
{
|
||||
Id = model.Id,
|
||||
Name = model.Name,
|
||||
Description = model.Description
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(DiseaseBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Name = model.Name;
|
||||
Description = model.Description;
|
||||
}
|
||||
|
||||
public DiseaseViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Name = Name,
|
||||
Description = Description
|
||||
};
|
||||
}
|
||||
}
|
98
Hospital/HospitalDatabaseImplement/Models/Doctor.cs
Normal file
98
Hospital/HospitalDatabaseImplement/Models/Doctor.cs
Normal file
@ -0,0 +1,98 @@
|
||||
using HospitalContracts.BindingModels;
|
||||
using HospitalContracts.ViewModels;
|
||||
using HospitalDatabaseImplement.Modelss;
|
||||
using HospitalDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HospitalDatabaseImplement.Models
|
||||
{
|
||||
public class Doctor : IDoctorModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
[MaxLength(25)]
|
||||
public string Login { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
[MaxLength(11)]
|
||||
public string PhoneNumber { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
[MaxLength(30)]
|
||||
public string Password { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
[MaxLength(30)]
|
||||
public string FIO { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
[MaxLength(30)]
|
||||
public string MailAddress { get; set; } = string.Empty;
|
||||
|
||||
[ForeignKey("DoctorId")]
|
||||
public virtual List<Patient> Patients { get; set; } = new();
|
||||
|
||||
[ForeignKey("DoctorId")]
|
||||
public virtual List<Recipe> Recipes { get; set; } = new();
|
||||
|
||||
[ForeignKey("DoctorId")]
|
||||
public virtual List<Disease> Diseases { get; set; } = new();
|
||||
|
||||
public static Doctor? Create(DoctorBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Doctor()
|
||||
{
|
||||
Id = model.Id,
|
||||
Login = model.Login,
|
||||
PhoneNumber = model.PhoneNumber,
|
||||
Password = model.Password,
|
||||
FIO = model.FIO,
|
||||
MailAddress = model.MailAddress
|
||||
};
|
||||
}
|
||||
public static Doctor Create(DoctorViewModel model)
|
||||
{
|
||||
return new Doctor
|
||||
{
|
||||
Id = model.Id,
|
||||
Login = model.Login,
|
||||
PhoneNumber = model.PhoneNumber,
|
||||
Password = model.Password,
|
||||
FIO = model.FIO,
|
||||
MailAddress = model.MailAddress
|
||||
};
|
||||
}
|
||||
public void Update(DoctorBindingModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Login = model.Login;
|
||||
PhoneNumber = model.PhoneNumber;
|
||||
Password = model.Password;
|
||||
FIO = model.FIO;
|
||||
MailAddress = model.MailAddress;
|
||||
}
|
||||
public DoctorViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Login = Login,
|
||||
PhoneNumber = PhoneNumber,
|
||||
Password = Password,
|
||||
FIO = FIO,
|
||||
MailAddress = MailAddress
|
||||
};
|
||||
}
|
||||
}
|
128
Hospital/HospitalDatabaseImplement/Models/Patient.cs
Normal file
128
Hospital/HospitalDatabaseImplement/Models/Patient.cs
Normal file
@ -0,0 +1,128 @@
|
||||
using HospitalContracts.BindingModels;
|
||||
using HospitalContracts.ViewModels;
|
||||
using HospitalDataModels.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace HospitalDatabaseImplement.Models
|
||||
{
|
||||
public class Patient : IPatientModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
public int DoctorId { get; private set; }
|
||||
|
||||
public virtual Doctor Doctor { get; set; }
|
||||
|
||||
[Required]
|
||||
[MaxLength(50)]
|
||||
public string FIO { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
[MaxLength(50)]
|
||||
public string Address { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public DateOnly BirthDate { get; set; }
|
||||
|
||||
private Dictionary<int, IRecipeModel>? _patientRecipes = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, IRecipeModel> PatientRecipes
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_patientRecipes == null)
|
||||
{
|
||||
_patientRecipes = Recipes.ToDictionary(recPR => recPR.RecipeId, recPR => (recPR.Recipe as IRecipeModel));
|
||||
}
|
||||
return _patientRecipes;
|
||||
}
|
||||
}
|
||||
|
||||
[ForeignKey("PatientId")]
|
||||
public virtual List<PatientRecipe> Recipes { get; set; } = new();
|
||||
|
||||
private Dictionary<int, IProcedureModel>? _patientProcedures = null;
|
||||
|
||||
[NotMapped]
|
||||
public Dictionary<int, IProcedureModel> PatientProcedures
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_patientProcedures == null)
|
||||
{
|
||||
_patientProcedures = Procedures.ToDictionary(recPP => recPP.ProcedureId, recPP => (recPP.Procedure as IProcedureModel));
|
||||
}
|
||||
return _patientProcedures;
|
||||
}
|
||||
}
|
||||
|
||||
[ForeignKey("PatientId")]
|
||||
public virtual List<PatientProcedure> Procedures { get; set; } = new();
|
||||
|
||||
public static Patient Create(HospitalDatabase context, PatientBindingModel model)
|
||||
{
|
||||
return new Patient()
|
||||
{
|
||||
Id = model.Id,
|
||||
FIO = model.FIO,
|
||||
Address = model.Address,
|
||||
|
||||
BirthDate = model.BirthDate,
|
||||
DoctorId = model.DoctorId,
|
||||
Doctor = context.Doctors.First(x => x.Id == model.DoctorId),
|
||||
Recipes = model.PatientRecipes.Select(x => new PatientRecipe
|
||||
{
|
||||
Recipe = context.Recipes.First(y => y.Id == x.Key),
|
||||
}).ToList(),
|
||||
Procedures = model.PatientProcedures.Select(x => new PatientProcedure
|
||||
{
|
||||
Procedure = context.Procedures.First(y => y.Id == x.Key),
|
||||
}).ToList()
|
||||
};
|
||||
}
|
||||
|
||||
public void Update(PatientBindingModel model)
|
||||
{
|
||||
FIO = model.FIO;
|
||||
Address = model.Address;
|
||||
BirthDate = model.BirthDate;
|
||||
}
|
||||
|
||||
public PatientViewModel GetViewModel => new()
|
||||
{
|
||||
FIO = FIO,
|
||||
Address = Address,
|
||||
BirthDate = BirthDate,
|
||||
PatientRecipes = PatientRecipes,
|
||||
PatientProcedures = PatientProcedures
|
||||
};
|
||||
|
||||
public void UpdateRecipes(HospitalDatabase context, PatientBindingModel model)
|
||||
{
|
||||
var patientRecipes = context.PatientRecipes.Where(rec => rec.PatientId == model.Id).ToList();
|
||||
if (patientRecipes != null)
|
||||
{ // удалили те, которых нет в модели
|
||||
context.PatientRecipes.RemoveRange(patientRecipes.Where(rec => !model.PatientRecipes.ContainsKey(rec.RecipeId)));
|
||||
context.SaveChanges();
|
||||
}
|
||||
var patient = context.Patients.First(x => x.Id == Id);
|
||||
foreach (var pr in model.PatientRecipes)
|
||||
{
|
||||
context.PatientRecipes.Add(new PatientRecipe
|
||||
{
|
||||
Patient = patient,
|
||||
Recipe = context.Recipes.First(x => x.Id == pr.Key),
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_patientRecipes = null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user