Добавление клиента(нужно пофиксить ошибки)

This commit is contained in:
Екатерина Рогашова 2023-04-06 21:56:38 +04:00
parent 167747ecae
commit 1edcb20fce
32 changed files with 873 additions and 103 deletions

View File

@ -0,0 +1,121 @@
using HospitalContracts.BindingModels;
using HospitalContracts.BusinessLogicsContracts;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalBusinessLogic.BusinessLogics
{
public class ClientLogic : IClientLogic
{
private readonly ILogger _logger;
private readonly IClientStorage _clientStorage;
public ClientLogic(ILogger<ClientLogic> logger, IClientStorage clientStorage)
{
_logger = logger;
_clientStorage = clientStorage;
}
public bool Create(ClientBindingModel model)
{
CheckModel(model);
if (_clientStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ClientBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_clientStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ClientViewModel? ReadElement(ClientSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ClientFIO: {ClientFIO}. Email: {Email}. Id: {Id}.", model.ClientFIO, model.Email, model.Id);
var element = _clientStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id: {Id}", element.Id);
return element;
}
public List<ClientViewModel>? ReadList(ClientSearchModel? model)
{
_logger.LogInformation("ReadList. ClientFIO: {ClientName}. Email: {Email}. Id: {Id}.", model?.ClientFIO, model?.Email, model?.Id);
var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public bool Update(ClientBindingModel model)
{
CheckModel(model);
if (_clientStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(ClientBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ClientFIO))
{
throw new ArgumentNullException("Нет ФИО клиента", nameof(model.ClientFIO));
}
if (string.IsNullOrEmpty(model.Email))
{
throw new ArgumentNullException("Нет почты клиента", nameof(model.Email));
}
if (string.IsNullOrEmpty(model.Password))
{
throw new ArgumentNullException("Нет пароля клиента", nameof(model.Password));
}
_logger.LogInformation("Client. ClientFIO: {ClientFIO}. Email: {Email}. Id: {Id}", model.ClientFIO, model.Email, model.Id);
var element = _clientStorage.GetElement(new ClientSearchModel
{
Email = model.Email
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Клиент с такой почтой уже есть");
}
}
}
}

View File

@ -0,0 +1,17 @@
using HospitalDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalContracts.BindingModels
{
public class ClientBindingModel : IClientModel
{
public int Id { get; set; }
public string ClientFIO { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Password { get; set; } = string.Empty;
}
}

View File

@ -12,12 +12,12 @@ namespace HospitalContracts.BindingModels
public int Id { get; set; }
public string IllnessName { get; set; } = string.Empty;
public string Form { get; set; } = string.Empty;
public Dictionary<int, (ISymptomsModel, int)> IllnessSymptoms
public Dictionary<int, ISymptomsModel> IllnessSymptoms
{
get;
set;
} = new();
public Dictionary<int, (IKurseModel, int)> IllnessKurse
public Dictionary<int, IKurseModel> IllnessKurse
{
get;
set;

View File

@ -14,13 +14,13 @@ namespace HospitalContracts.BindingModels
public DateTime Date { get; set; } = DateTime.Now;
public string ModeOfApplication { get; set; } = string.Empty;
public int MedicinesId { get; set; }
public Dictionary<int, (ISymptomsModel, int)> RecipeSymptoms
public Dictionary<int, ISymptomsModel> RecipeSymptoms
{
get;
set;
} = new();
public Dictionary<int, (IProceduresModel, int)> RecipeProcedures
public Dictionary<int, IProceduresModel> RecipeProcedures
{
get;
set;

View File

@ -0,0 +1,20 @@
using HospitalContracts.BindingModels;
using HospitalContracts.SearchModels;
using HospitalContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalContracts.BusinessLogicsContracts
{
public interface IClientLogic
{
List<ClientViewModel>? ReadList(ClientSearchModel? model);
ClientViewModel? ReadElement(ClientSearchModel model);
bool Create(ClientBindingModel model);
bool Update(ClientBindingModel model);
bool Delete(ClientBindingModel model);
}
}

View File

@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalContracts.SearchModels
{
public class ClientSearchModel
{
public int? Id { get; set; }
public string? ClientFIO { get; set; }
public string? Email { get; set; }
public string? Password { get; set; }
}
}

View File

@ -8,7 +8,7 @@ namespace HospitalContracts.SearchModels
{
public class SymptomsSearchModel
{
public int Id { get; set; }
public int? Id { get; set; }
public string? SymptomName { get; set; }
}
}

View File

@ -0,0 +1,21 @@
using HospitalContracts.BindingModels;
using HospitalContracts.SearchModels;
using HospitalContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalContracts.StoragesContracts
{
public interface IClientStorage
{
List<ClientViewModel> GetFullList();
List<ClientViewModel> GetFilteredList(ClientSearchModel model);
ClientViewModel? GetElement(ClientSearchModel model);
ClientViewModel? Insert(ClientBindingModel model);
ClientViewModel? Update(ClientBindingModel model);
ClientViewModel? Delete(ClientBindingModel model);
}
}

View File

@ -0,0 +1,21 @@
using HospitalDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalContracts.ViewModels
{
public class ClientViewModel : IClientModel
{
public int Id { get; set; }
[DisplayName("ФИО клиента")]
public string ClientFIO { get; set; } = string.Empty;
[DisplayName("Логин (эл. почта)")]
public string Email { get; set; } = string.Empty;
[DisplayName("Пароль")]
public string Password { get; set; } = string.Empty;
}
}

View File

@ -12,15 +12,15 @@ namespace HospitalContracts.ViewModels
{
public int Id { get; set; }
[DisplayName("Название болезни")]
public string IllnessName { get; set; }
public string IllnessName { get; set; } = string.Empty;
[DisplayName("Форма болезни")]
public string Form { get; set; }
public Dictionary<int, (ISymptomsModel, int)> IllnessSymptoms
public string Form { get; set; } = string.Empty;
public Dictionary<int, ISymptomsModel> IllnessSymptoms
{
get;
set;
} = new();
public Dictionary<int, (IKurseModel, int)> IllnessKurse
public Dictionary<int, IKurseModel> IllnessKurse
{
get;
set;

View File

@ -12,11 +12,11 @@ namespace HospitalContracts.ViewModels
{
public int Id { get; set; }
[DisplayName("Продолжительность курса")]
public string Duration { get; set; }
public string Duration { get; set; } = string.Empty;
[DisplayName("Срок приема")]
public int CountInDay { get; set; }
public int MedicinesId { get; set; }
[DisplayName("Название лекарства")]
public string MedicinesName { get; set; }
public string MedicinesName { get; set; } = string.Empty;
}
}

View File

@ -12,8 +12,8 @@ namespace HospitalContracts.ViewModels
{
public int Id { get; set; }
[DisplayName("Название лекарства")]
public string MedicinesName { get; set; }
public string MedicinesName { get; set; } = string.Empty;
[DisplayName("Группа")]
public string Group { get; set; }
public string Group { get; set; } = string.Empty;
}
}

View File

@ -12,8 +12,8 @@ namespace HospitalContracts.ViewModels
{
public int Id { get; set; }
[DisplayName("Название процедуры")]
public string ProceduresName { get; set; }
public string ProceduresName { get; set; } = string.Empty;
[DisplayName("Тип процедуры")]
public string Type { get; set; }
public string Type { get; set; } = string.Empty;
}
}

View File

@ -12,21 +12,21 @@ namespace HospitalContracts.ViewModels
{
public int Id { get; set; }
[DisplayName("Доза")]
public string Dose { get; set; }
public string Dose { get; set; } = string.Empty;
[DisplayName("Дата выписки")]
public DateTime Date { get; set; }
[DisplayName("Способ приготовления")]
public string ModeOfApplication { get; set; }
public string ModeOfApplication { get; set; } = string.Empty;
[DisplayName("Лекарство")]
public int MedicinesId { get; set; }
[DisplayName("Симптом")]
public Dictionary<int, (ISymptomsModel, int)> RecipeSymptoms
public Dictionary<int, ISymptomsModel> RecipeSymptoms
{
get;
set;
} = new();
[DisplayName("Процедура")]
public Dictionary<int, (IProceduresModel, int)> RecipeProcedures
public Dictionary<int, IProceduresModel> RecipeProcedures
{
get;
set;

View File

@ -12,8 +12,8 @@ namespace HospitalContracts.ViewModels
{
public int Id { get; set; }
[DisplayName("Симптом")]
public string SymptomName { get; set; }
public string SymptomName { get; set; } = string.Empty;
[DisplayName("Описание")]
public string Description { get; set; }
public string Description { get; set; } = string.Empty;
}
}

View File

@ -27,5 +27,8 @@ namespace HospitalDataBaseImplements
public virtual DbSet<Symptoms> Symptomses { set; get; }
public virtual DbSet<Recipes> Recipes { set; get; }
public virtual DbSet<Medicines> Medicines { set; get; }
public virtual DbSet<Procedures> Procedures { set; get; }
public virtual DbSet<Client> Clients { set; get; }
}
}

View File

@ -0,0 +1,83 @@
using HospitalContracts.BindingModels;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using HospitalDataBaseImplements.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalDataBaseImplements.Implements
{
public class ClientStorage : IClientStorage
{
public ClientViewModel? Delete(ClientBindingModel model)
{
using var context = new HospitalDatabase();
var element = context.Clients.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Clients.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public ClientViewModel? GetElement(ClientSearchModel model)
{
using var context = new HospitalDatabase();
if (model.Id.HasValue)
return context.Clients.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password))
return context.Clients.FirstOrDefault(x => x.Email.Equals(model.Email) && x.Password.Equals(model.Password))?.GetViewModel;
if (!string.IsNullOrEmpty(model.Email))
return context.Clients.FirstOrDefault(x => x.Email.Equals(model.Email))?.GetViewModel;
return null;
}
public List<ClientViewModel> GetFilteredList(ClientSearchModel model)
{
if (string.IsNullOrEmpty(model.ClientFIO))
{
return new();
}
using var context = new HospitalDatabase();
return context.Clients.Where(x => x.ClientFIO.Contains(model.ClientFIO)).Select(x => x.GetViewModel).ToList();
}
public List<ClientViewModel> GetFullList()
{
using var context = new HospitalDatabase();
return context.Clients.Select(x => x.GetViewModel).ToList();
}
public ClientViewModel? Insert(ClientBindingModel model)
{
var newClient = Client.Create(model);
if (newClient == null)
{
return null;
}
using var context = new HospitalDatabase();
context.Clients.Add(newClient);
context.SaveChanges();
return newClient.GetViewModel;
}
public ClientViewModel? Update(ClientBindingModel model)
{
using var context = new HospitalDatabase();
var client = context.Clients.FirstOrDefault(x => x.Id == model.Id);
if (client == null)
{
return null;
}
client.Update(model);
context.SaveChanges();
return client.GetViewModel;
}
}
}

View File

@ -1,4 +1,9 @@
using System;
using HospitalContracts.BindingModels;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using HospitalDataBaseImplements.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -6,7 +11,71 @@ using System.Threading.Tasks;
namespace HospitalDataBaseImplements.Implements
{
public class KurseStorage
public class KurseStorage : IKurseStorage
{
public KurseViewModel? Delete(KurseBindingModel model)
{
using var context = new HospitalDatabase();
var element = context.Kurse.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Kurse.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public KurseViewModel? GetElement(KurseSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new HospitalDatabase();
return context.Kurse.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<KurseViewModel> GetFilteredList(KurseSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new HospitalDatabase();
return context.Kurse.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
}
public List<KurseViewModel> GetFullList()
{
using var context = new HospitalDatabase();
return context.Kurse.Select(x => x.GetViewModel).ToList();
}
public KurseViewModel? Insert(KurseBindingModel model)
{
var newKurse = Kurses.Create(model);
if (newKurse == null)
{
return null;
}
using var context = new HospitalDatabase();
context.Kurse.Add(newKurse);
context.SaveChanges();
return newKurse.GetViewModel;
}
public KurseViewModel? Update(KurseBindingModel model)
{
using var context = new HospitalDatabase();
var kurse = context.Kurse.FirstOrDefault(x => x.Id == model.Id);
if (kurse == null)
{
return null;
}
kurse.Update(model);
context.SaveChanges();
return kurse.GetViewModel;
}
}
}

View File

@ -1,4 +1,9 @@
using System;
using HospitalContracts.BindingModels;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using HospitalDataBaseImplements.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -6,7 +11,76 @@ using System.Threading.Tasks;
namespace HospitalDataBaseImplements.Implements
{
public class MedicinesStorage
public class MedicinesStorage : IMedicinesStorage
{
public List<MedicinesViewModel> GetFullList()
{
using var context = new HospitalDatabase();
return context.Medicines
.Select(x => x.GetViewModel)
.ToList();
}
public List<MedicinesViewModel> GetFilteredList(MedicinesSearchModel model)
{
if (string.IsNullOrEmpty(model.MedicinesName))
{
return new();
}
using var context = new HospitalDatabase();
return context.Medicines
.Where(x => x.MedicinesName.Contains(model.MedicinesName))
.Select(x => x.GetViewModel)
.ToList();
}
public MedicinesViewModel? GetElement(MedicinesSearchModel model)
{
if (string.IsNullOrEmpty(model.MedicinesName) && !model.Id.HasValue)
{
return null;
}
using var context = new HospitalDatabase();
return context.Medicines
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.MedicinesName) && x.MedicinesName ==
model.MedicinesName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public MedicinesViewModel? Insert(MedicinesBindingModel model)
{
var newMedicine = Medicines.Create(model);
if (newMedicine == null)
{
return null;
}
using var context = new HospitalDatabase();
context.Medicines.Add(newMedicine);
context.SaveChanges();
return newMedicine.GetViewModel;
}
public MedicinesViewModel? Update(MedicinesBindingModel model)
{
using var context = new HospitalDatabase();
var medicine = context.Medicines.FirstOrDefault(x => x.Id == model.Id);
if (medicine == null)
{
return null;
}
medicine.Update(model);
context.SaveChanges();
return medicine.GetViewModel;
}
public MedicinesViewModel? Delete(MedicinesBindingModel model)
{
using var context = new HospitalDatabase();
var element = context.Medicines.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Medicines.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -1,4 +1,9 @@
using System;
using HospitalContracts.BindingModels;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using HospitalDataBaseImplements.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -6,7 +11,76 @@ using System.Threading.Tasks;
namespace HospitalDataBaseImplements.Implements
{
public class ProceduresStorage
public class ProceduresStorage : IProceduresStorage
{
public List<ProceduresViewModel> GetFullList()
{
using var context = new HospitalDatabase();
return context.Procedures
.Select(x => x.GetViewModel)
.ToList();
}
public List<ProceduresViewModel> GetFilteredList(ProceduresSearchModel model)
{
if (string.IsNullOrEmpty(model.ProceduresName))
{
return new();
}
using var context = new HospitalDatabase();
return context.Procedures
.Where(x => x.ProceduresName.Contains(model.ProceduresName))
.Select(x => x.GetViewModel)
.ToList();
}
public ProceduresViewModel? GetElement(ProceduresSearchModel model)
{
if (string.IsNullOrEmpty(model.ProceduresName) && !model.Id.HasValue)
{
return null;
}
using var context = new HospitalDatabase();
return context.Procedures
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.ProceduresName) && x.ProceduresName ==
model.ProceduresName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ProceduresViewModel? Insert(ProceduresBindingModel model)
{
var newProcedures = Procedures.Create(model);
if (newProcedures == null)
{
return null;
}
using var context = new HospitalDatabase();
context.Procedures.Add(newProcedures);
context.SaveChanges();
return newProcedures.GetViewModel;
}
public ProceduresViewModel? Update(ProceduresBindingModel model)
{
using var context = new HospitalDatabase();
var procedure = context.Procedures.FirstOrDefault(x => x.Id == model.Id);
if (procedure == null)
{
return null;
}
procedure.Update(model);
context.SaveChanges();
return procedure.GetViewModel;
}
public ProceduresViewModel? Delete(ProceduresBindingModel model)
{
using var context = new HospitalDatabase();
var element = context.Procedures.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Procedures.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -1,4 +1,9 @@
using System;
using HospitalContracts.BindingModels;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using HospitalDataBaseImplements.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -6,7 +11,71 @@ using System.Threading.Tasks;
namespace HospitalDataBaseImplements.Implements
{
public class RecipesStorage
public class RecipesStorage : IRecipesStorage
{
public RecipesViewModel? Delete(RecipesBindingModel model)
{
using var context = new HospitalDatabase();
var element = context.Recipes.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Recipes.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public RecipesViewModel? GetElement(RecipesSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new HospitalDatabase();
return context.Recipes.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<RecipesViewModel> GetFilteredList(RecipesSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new HospitalDatabase();
return context.Recipes.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList();
}
public List<RecipesViewModel> GetFullList()
{
using var context = new HospitalDatabase();
return context.Recipes.Select(x => x.GetViewModel).ToList();
}
public RecipesViewModel? Insert(RecipesBindingModel model)
{
using var context = new HospitalDatabase();
var newRecipe = Recipes.Create(context, model);
if (newRecipe == null)
{
return null;
}
context.Recipes.Add(newRecipe);
context.SaveChanges();
return newRecipe.GetViewModel;
}
public RecipesViewModel? Update(RecipesBindingModel model)
{
using var context = new HospitalDatabase();
var recipe = context.Recipes.FirstOrDefault(x => x.Id == model.Id);
if (recipe == null)
{
return null;
}
recipe.Update(model);
context.SaveChanges();
return recipe.GetViewModel;
}
}
}

View File

@ -1,4 +1,9 @@
using System;
using HospitalContracts.BindingModels;
using HospitalContracts.SearchModels;
using HospitalContracts.StoragesContracts;
using HospitalContracts.ViewModels;
using HospitalDataBaseImplements.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@ -6,7 +11,76 @@ using System.Threading.Tasks;
namespace HospitalDataBaseImplements.Implements
{
public class SymptomsStorage
public class SymptomsStorage : ISymptomsStorage
{
public List<SymptomsViewModel> GetFullList()
{
using var context = new HospitalDatabase();
return context.Symptomses
.Select(x => x.GetViewModel)
.ToList();
}
public List<SymptomsViewModel> GetFilteredList(SymptomsSearchModel model)
{
if (string.IsNullOrEmpty(model.SymptomName))
{
return new();
}
using var context = new HospitalDatabase();
return context.Symptomses
.Where(x => x.SymptomName.Contains(model.SymptomName))
.Select(x => x.GetViewModel)
.ToList();
}
public SymptomsViewModel? GetElement(SymptomsSearchModel model)
{
if (string.IsNullOrEmpty(model.SymptomName) && !model.Id.HasValue)
{
return null;
}
using var context = new HospitalDatabase();
return context.Symptomses
.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.SymptomName) && x.SymptomName ==
model.SymptomName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public SymptomsViewModel? Insert(SymptomsBindingModel model)
{
var newSymptoms = Symptoms.Create(model);
if (newSymptoms == null)
{
return null;
}
using var context = new HospitalDatabase();
context.Symptomses.Add(newSymptoms);
context.SaveChanges();
return newSymptoms.GetViewModel;
}
public SymptomsViewModel? Update(SymptomsBindingModel model)
{
using var context = new HospitalDatabase();
var symptom = context.Symptomses.FirstOrDefault(x => x.Id == model.Id);
if (symptom == null)
{
return null;
}
symptom.Update(model);
context.SaveChanges();
return symptom.GetViewModel;
}
public SymptomsViewModel? Delete(SymptomsBindingModel model)
{
using var context = new HospitalDatabase();
var element = context.Symptomses.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Symptomses.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,71 @@
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 HospitalDataBaseImplements.Models
{
public class Client : IClientModel
{
public int Id { get; private set; }
[Required]
public string ClientFIO { get; set; } = string.Empty;
[Required]
public string Email { get; set; } = string.Empty;
[Required]
public string Password { get; set; } = string.Empty;
[ForeignKey("ClientId")]
//public virtual List<Medicines> Medicines { get; set; } = new();
//public virtual List<Procedures> Procedures { get; set; } = new();
//public virtual List<Recipes> Recipes { get; set; } = new();
public static Client? Create(ClientBindingModel model)
{
if (model == null)
{
return null;
}
return new Client()
{
Id = model.Id,
ClientFIO = model.ClientFIO,
Email = model.Email,
Password = model.Password
};
}
public static Client Create(ClientViewModel model)
{
return new Client
{
Id = model.Id,
ClientFIO = model.ClientFIO,
Email = model.Email,
Password = model.Password
};
}
public void Update(ClientBindingModel model)
{
if (model == null)
{
return;
}
ClientFIO = model.ClientFIO;
Email = model.Email;
Password = model.Password;
}
public ClientViewModel GetViewModel => new()
{
Id = Id,
ClientFIO = ClientFIO,
Email = Email,
Password = Password
};
}
}

View File

@ -18,16 +18,15 @@ namespace HospitalDataBaseImplements.Models
public string IllnessName { get; private set; } = string.Empty;
[Required]
public string Form { get; private set; } = string.Empty;
private Dictionary<int, (IKurseModel, int)>? _illnessKurses = null;
private Dictionary<int, IKurseModel>? _illnessKurses = null;
[NotMapped]
public Dictionary<int, (IKurseModel, int)> IllnessKurse
public Dictionary<int, IKurseModel> IllnessKurse
{
get
{
if (_illnessKurses == null)
{
_illnessKurses = Kurses.ToDictionary(recPC => recPC.KurseId, recPC =>
(recPC.Kurse as IKurseModel, recPC.Count));
_illnessKurses = Kurses.ToDictionary(recPC => recPC.KurseId, recPC => (recPC.Kurse as IKurseModel));
}
return _illnessKurses;
}
@ -35,15 +34,15 @@ namespace HospitalDataBaseImplements.Models
[ForeignKey("IllnessId")]
public virtual List<IllnessKurse> Kurses { get; set; } = new();
private Dictionary<int, (ISymptomsModel, int)>? _illnessSymptomses = null;
private Dictionary<int, ISymptomsModel>? _illnessSymptomses = null;
[NotMapped]
public Dictionary<int, (ISymptomsModel, int)> IllnessSymptoms
public Dictionary<int, ISymptomsModel> IllnessSymptoms
{
get
{
if (_illnessSymptomses == null)
{
_illnessSymptomses = Symptomses.ToDictionary(recPC => recPC.SymptomsId, recPC =>(recPC.Symptoms as ISymptomsModel, recPC.Count));
_illnessSymptomses = Symptomses.ToDictionary(recPC => recPC.SymptomsId, recPC => (recPC.Symptoms as ISymptomsModel));
}
return _illnessSymptomses;
}
@ -61,12 +60,10 @@ namespace HospitalDataBaseImplements.Models
Kurses = model.IllnessKurse.Select(x => new IllnessKurse
{
Kurse = context.Kurse.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList(),
Symptomses = model.IllnessSymptoms.Select(x => new IllnessSymptoms
{
Symptoms = context.Symptomses.First(y => y.Id == x.Key),
Count = x.Value.Item2
}).ToList()
};
}
@ -91,14 +88,6 @@ namespace HospitalDataBaseImplements.Models
context.IllnessKurse.RemoveRange(illnessKurses.Where(rec
=> !model.IllnessKurse.ContainsKey(rec.KurseId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateDrugCourse in illnessKurses)
{
updateDrugCourse.Count =
model.IllnessKurse[updateDrugCourse.KurseId].Item2;
model.IllnessKurse.Remove(updateDrugCourse.KurseId);
}
context.SaveChanges();
}
var illness = context.Illnesses.First(x => x.Id == Id);
foreach (var pc in model.IllnessKurse)
@ -106,8 +95,7 @@ namespace HospitalDataBaseImplements.Models
context.IllnessKurse.Add(new IllnessKurse
{
Illness = illness,
Kurse = context.Kurse.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
Kurse = context.Kurse.First(x => x.Id == pc.Key)
});
context.SaveChanges();
}
@ -121,14 +109,6 @@ namespace HospitalDataBaseImplements.Models
context.IllnessSymptomses.RemoveRange(illnessSymptomses.Where(rec
=> !model.IllnessSymptoms.ContainsKey(rec.SymptomsId)));
context.SaveChanges();
// обновили количество у существующих записей
foreach (var updateSymptoms in illnessSymptomses)
{
updateSymptoms.Count =
model.IllnessSymptoms[updateSymptoms.SymptomsId].Item2;
model.IllnessSymptoms.Remove(updateSymptoms.SymptomsId);
}
context.SaveChanges();
}
var illness = context.Illnesses.First(x => x.Id == Id);
foreach (var pc in model.IllnessSymptoms)
@ -136,8 +116,7 @@ namespace HospitalDataBaseImplements.Models
context.IllnessSymptomses.Add(new IllnessSymptoms
{
Illness = illness,
Symptoms = context.Symptomses.First(x => x.Id == pc.Key),
Count = pc.Value.Item2
Symptoms = context.Symptomses.First(x => x.Id == pc.Key)
});
context.SaveChanges();
}

View File

@ -15,7 +15,6 @@ namespace HospitalDataBaseImplements.Models
[Required]
public int KurseId { get; set; }
[Required]
public int Count { get; set; }
public virtual Illness Illness { get; set; } = new();
public virtual Kurses Kurse { get; set; } = new();
}

View File

@ -15,7 +15,6 @@ namespace HospitalDataBaseImplements.Models
[Required]
public int SymptomsId { get; set; }
[Required]
public int Count { get; set; }
public virtual Illness Illness { get; set; } = new();
public virtual Symptoms Symptoms { get; set; } = new();
}

View File

@ -21,80 +21,109 @@ namespace HospitalDataBaseImplements.Models
public int MedicinesId { get; private set; }
[Required]
public string ModeOfApplication { get; private set; } = string.Empty;
private Dictionary<int, (IProceduresModel, int)>? _recipeProcedures = null;
private Dictionary<int, IProceduresModel>? _recipeProcedures = null;
[NotMapped]
public Dictionary<int, (IProceduresModel, int)> RecipeProcedures
public Dictionary<int, IProceduresModel> RecipeProcedures
{
get
{
if (_recipeProcedures == null)
{
// _illnessProcedures = Procedures.ToDictionary(recPC => recPC.KurseId, recPC =>
//(recPC.Kurse as IProceduresModel, recPC.Count));
_recipeProcedures = Procedures.ToDictionary(recPC => recPC.ProcedureId, recPC =>
recPC.Procedures as IProceduresModel);
}
return _recipeProcedures;
}
}
[ForeignKey("ProceduresId")] /////////////////
[ForeignKey("ProceduresId")]
public virtual List<RecipesProcedures> Procedures { get; set; } = new();
private Dictionary<int, (ISymptomsModel, int)>? _recipeSymptoms = null;
private Dictionary<int, ISymptomsModel>? _recipeSymptoms = null;
[NotMapped]
public Dictionary<int, (ISymptomsModel, int)> RecipeSymptoms
public Dictionary<int, ISymptomsModel> RecipeSymptoms
{
get
{
if (_recipeSymptoms == null)
{
// _recipeSymptoms = Symptoms.ToDictionary(recPC => recPC.SymptomsId, recPC => (recPC.Symptoms as ISymptomsModel, recPC.Count));
_recipeSymptoms = Symptoms.ToDictionary(recPC => recPC.SymptomsId, recPC => (recPC.Symptoms as ISymptomsModel));
}
return _recipeSymptoms;
}
}
[ForeignKey("SymptomsId")] /////////////////
[ForeignKey("SymptomsId")]
public virtual List<RecipesSymptoms> Symptoms { get; set; } = new();
//[Required]
//public int SymptomsId { get; private set; }
//public string SymptomsName { get; private set; } = string.Empty;
//public virtual Symptoms Symptoms { get; set; } = new();
public static Recipes? Create(RecipesBindingModel? model)
{
if (model == null)
{
return null;
}
public static Recipes Create(HospitalDatabase context, RecipesBindingModel model)
{
return new Recipes()
{
Id = model.Id,
Dose = model.Dose,
Date = model.Date,
ModeOfApplication = model.ModeOfApplication,
//SymptomsId = model.SymptomsId,
//SymptomsName = model.SymptomsName
MedicinesId = model.MedicinesId,
Procedures = model.RecipeProcedures.Select(x => new RecipesProcedures
{
Procedures = context.Procedures.First(y => y.Id == x.Key),
}).ToList(),
Symptoms = model.RecipeSymptoms.Select(x => new RecipesSymptoms
{
Symptoms = context.Symptomses.First(y => y.Id == x.Key),
}).ToList()
};
}
public void Update(RecipesBindingModel? model)
public void Update(RecipesBindingModel model)
{
if (model == null)
{
return;
}
Dose = model.Dose;
Date = model.Date;
ModeOfApplication = model.ModeOfApplication;
//SymptomsName = model.SymptomsName;
MedicinesId = model.MedicinesId;
}
public RecipesViewModel GetViewModel => new()
{
Id = Id,
Dose = Dose,
Date = Date,
ModeOfApplication = ModeOfApplication,
//SymptomsId = SymptomsId,
//SymptomsName = SymptomsName
RecipeProcedures = RecipeProcedures,
RecipeSymptoms = RecipeSymptoms
};
public void UpdateProcedures(HospitalDatabase context, RecipesBindingModel model)
{
var recipeProcedures = context.RecipesProcedures.Where(rec => rec.RecipeId == model.Id).ToList();
if (recipeProcedures != null && recipeProcedures.Count > 0)
{ // удалили те, которых нет в модели
context.RecipesProcedures.RemoveRange(recipeProcedures.Where(rec
=> !model.RecipeProcedures.ContainsKey(rec.ProcedureId)));
context.SaveChanges();
}
var recipe = context.Recipes.First(x => x.Id == Id);
foreach (var pc in model.RecipeProcedures)
{
context.RecipesProcedures.Add(new RecipesProcedures
{
Recipes = recipe,
Procedures = context.Procedures.First(x => x.Id == pc.Key)
});
context.SaveChanges();
}
_recipeProcedures = null;
}
public void UpdateSymptomses(HospitalDatabase context, RecipesBindingModel model)
{
var recipeSymptomses = context.RecipesSymptoms.Where(rec => rec.RecipeId == model.Id).ToList();
if (recipeSymptomses != null && recipeSymptomses.Count > 0)
{ // удалили те, которых нет в модели
context.RecipesSymptoms.RemoveRange(recipeSymptomses.Where(rec
=> !model.RecipeSymptoms.ContainsKey(rec.SymptomsId)));
context.SaveChanges();
}
var recipe = context.Recipes.First(x => x.Id == Id);
foreach (var pc in model.RecipeSymptoms)
{
context.RecipesSymptoms.Add(new RecipesSymptoms
{
Recipes = recipe,
Symptoms = context.Symptomses.First(x => x.Id == pc.Key)
});
context.SaveChanges();
}
_recipeSymptoms = null;
}
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -8,5 +9,12 @@ namespace HospitalDataBaseImplements.Models
{
public class RecipesProcedures
{
public int Id { get; set; }
[Required]
public int RecipeId { get; set; }
[Required]
public int ProcedureId { get; set; }
public virtual Recipes Recipes { get; set; } = new();
public virtual Procedures Procedures { get; set; } = new();
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@ -8,5 +9,12 @@ namespace HospitalDataBaseImplements.Models
{
public class RecipesSymptoms
{
public int Id { get; set; }
[Required]
public int RecipeId { get; set; }
[Required]
public int SymptomsId { get; set; }
public virtual Recipes Recipes { get; set; } = new();
public virtual Symptoms Symptoms { get; set; } = new();
}
}

View File

@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HospitalDataModels.Models
{
public interface IClientModel : IId
{
string ClientFIO { get; }
string Email { get; }
string Password { get; }
}
}

View File

@ -10,7 +10,7 @@ namespace HospitalDataModels.Models
{
string IllnessName { get; }
string Form { get; }
Dictionary<int, (ISymptomsModel, int)> IllnessSymptoms { get; }
Dictionary<int, (IKurseModel, int)> IllnessKurse { get; }
Dictionary<int, ISymptomsModel> IllnessSymptoms { get; }
Dictionary<int, IKurseModel> IllnessKurse { get; }
}
}

View File

@ -12,7 +12,7 @@ namespace HospitalDataModels.Models
DateTime Date { get; }
string ModeOfApplication { get; }
int MedicinesId { get; }
Dictionary<int, (IProceduresModel, int)> RecipeProcedures { get; }
Dictionary<int, (ISymptomsModel, int)> RecipeSymptoms { get; }
Dictionary<int, IProceduresModel> RecipeProcedures { get; }
Dictionary<int, ISymptomsModel> RecipeSymptoms { get; }
}
}