144 lines
4.5 KiB
C#
144 lines
4.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using VeterinaryClinicContracts.BindingModels;
|
|
using VeterinaryClinicContracts.ViewModels;
|
|
using VeterinaryClinicDataModels.Models;
|
|
|
|
namespace VeterinaryClinicDatabaseImplement.Models
|
|
{
|
|
public class Visit: IVisitModel
|
|
{
|
|
public int Id { get; set; }
|
|
|
|
[Required]
|
|
public DateTime Date { get; set; } = DateTime.Now;
|
|
|
|
[ForeignKey("UserId")]
|
|
public int UserId { get; set; }
|
|
|
|
public virtual User User { get; set; } = new();
|
|
|
|
[ForeignKey("VisitId")]
|
|
public virtual List<VisitAnimal> Animals { get; set; } = new();
|
|
|
|
private Dictionary<int, IAnimalModel>? _visitAnimals = null;
|
|
|
|
public Dictionary<int, IAnimalModel> VisitAnimals {
|
|
get
|
|
{
|
|
if (_visitAnimals == null)
|
|
{
|
|
_visitAnimals = Animals
|
|
.ToDictionary(recVA => recVA.AnimalId, recVA => (recVA.Animal as IAnimalModel));
|
|
}
|
|
return _visitAnimals;
|
|
}
|
|
}
|
|
|
|
[ForeignKey("VisitId")]
|
|
public virtual List<VisitService> Services { get; set; } = new();
|
|
|
|
private Dictionary<int, IServiceModel>? _visitServices = null;
|
|
|
|
public Dictionary<int, IServiceModel> VisitServices {
|
|
get
|
|
{
|
|
if (_visitServices == null)
|
|
{
|
|
_visitServices = Services
|
|
.ToDictionary(recVS => recVS.ServiceId, recVS => (recVS.Service as IServiceModel));
|
|
}
|
|
return _visitServices;
|
|
}
|
|
}
|
|
|
|
public static Visit? Create(VeterinaryClinicDatabase context, VisitBindingModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return null;
|
|
}
|
|
|
|
return new Visit()
|
|
{
|
|
Id = model.Id,
|
|
Date = model.Date,
|
|
UserId = model.UserId,
|
|
User = context.Users
|
|
.First(x => x.Id == model.UserId),
|
|
Animals = model.VisitAnimals.Select(x => new VisitAnimal
|
|
{
|
|
Animal = context.Animals.First(y => y.Id == x.Key)
|
|
}).ToList(),
|
|
Services = model.VisitServices.Select(x => new VisitService
|
|
{
|
|
Service = context.Services.First(y => y.Id == x.Key)
|
|
}).ToList()
|
|
};
|
|
}
|
|
|
|
public void Update(VisitBindingModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
Date = model.Date;
|
|
}
|
|
|
|
public VisitViewModel GetViewModel => new()
|
|
{
|
|
Id = Id,
|
|
Date = Date,
|
|
UserId = UserId,
|
|
UserFullName = User.FullName,
|
|
VisitAnimals = VisitAnimals,
|
|
VisitServices = VisitServices
|
|
};
|
|
|
|
public void UpdateAnimals(VeterinaryClinicDatabase context, VisitBindingModel model)
|
|
{
|
|
var visitAnimals = context.VisitAnimals.Where(rec => rec.VisitId == model.Id).ToList();
|
|
context.VisitAnimals.RemoveRange(visitAnimals);
|
|
context.SaveChanges();
|
|
|
|
var visit = context.Visits.First(x => x.Id == Id);
|
|
foreach (var rm in model.VisitAnimals)
|
|
{
|
|
context.VisitAnimals.Add(new VisitAnimal
|
|
{
|
|
Visit = visit,
|
|
Animal = context.Animals.First(x => x.Id == rm.Key)
|
|
});
|
|
context.SaveChanges();
|
|
}
|
|
_visitAnimals = null;
|
|
}
|
|
|
|
public void UpdateServices(VeterinaryClinicDatabase context, VisitBindingModel model)
|
|
{
|
|
var visitServices = context.VisitServices.Where(rec => rec.VisitId == model.Id).ToList();
|
|
context.VisitServices.RemoveRange(visitServices);
|
|
context.SaveChanges();
|
|
|
|
var visit = context.Visits.First(x => x.Id == Id);
|
|
foreach (var rm in model.VisitServices)
|
|
{
|
|
context.VisitServices.Add(new VisitService
|
|
{
|
|
Visit = visit,
|
|
Service = context.Services.First(x => x.Id == rm.Key)
|
|
});
|
|
context.SaveChanges();
|
|
}
|
|
_visitServices = null;
|
|
}
|
|
}
|
|
}
|