using System.ComponentModel.DataAnnotations.Schema; using System.ComponentModel.DataAnnotations; using TravelAgencyContracts.BindingModels; using TravelAgencyContracts.ViewModels; using TravelAgencyDataModels.Models; using Microsoft.EntityFrameworkCore; namespace TravelAgencyDatabaseImplement.Models { public class ExcursionGroup : IExcursionGroupModel { public int Id { get; set; } [Required] public string ExcursionGroupName { get; set; } = string.Empty; [Required] public int ParticipantsAmount { get; set; } [Required] public int UserId { get; set; } [DeleteBehavior(DeleteBehavior.Restrict)] public virtual User User { get; set; } [Required] public int GuideId { get; set; } public virtual Guide Guide { get; set; } private Dictionary? _excursionGroupTours = null; [NotMapped] public Dictionary ExcursionGroupTours { get { if (_excursionGroupTours == null) { _excursionGroupTours = Tours .ToDictionary(recPC => recPC.TourId, recPC => recPC.Tour as ITourModel); } return _excursionGroupTours; } } [ForeignKey("ExcursionGroupId")] public virtual List Tours { get; set; } = new(); private Dictionary? _excursionGroupPlaces = null; [NotMapped] public Dictionary ExcursionGroupPlaces { get { if (_excursionGroupPlaces == null) { _excursionGroupPlaces = Places .ToDictionary(recPC => recPC.PlaceId, recPC => recPC.Place as IPlaceModel); } return _excursionGroupPlaces; } } [ForeignKey("ExcursionGroupId")] public virtual List Places { get; set; } = new(); public static ExcursionGroup? Create(TravelAgencyDatabase context, ExcursionGroupBindingModel? model) { if (model == null) { return null; } return new ExcursionGroup() { Id = model.Id, ExcursionGroupName = model.ExcursionGroupName, ParticipantsAmount = model.ParticipantsAmount, UserId = model.UserId, GuideId = model.GuideId, Guide = context.Guides .First(x => x.Id == model.GuideId), Tours = model.ExcursionGroupTours.Select(x => new ExcursionGroupTour { Tour = context.Tours.First(y => y.Id == x.Key) }).ToList(), Places = model.ExcursionGroupPlaces.Select(x => new ExcursionGroupPlace { Place = context.Places.First(y => y.Id == x.Key) }).ToList() }; } public void Update(ExcursionGroupBindingModel? model) { if (model == null) { return; } using var context = new TravelAgencyDatabase(); ExcursionGroupName = model.ExcursionGroupName; ParticipantsAmount = model.ParticipantsAmount; GuideId = model.GuideId; Guide = context.Guides .First(x => x.Id == model.GuideId); } public ExcursionGroupViewModel GetViewModel => new() { Id = Id, ExcursionGroupName = ExcursionGroupName, ParticipantsAmount = ParticipantsAmount, UserId = UserId, GuideId = GuideId, GuideFIO = Guide?.GuideFIO, ExcursionGroupTours = ExcursionGroupTours, ExcursionGroupPlaces = ExcursionGroupPlaces }; public void UpdateTours(TravelAgencyDatabase context, ExcursionGroupBindingModel model) { var excursionGroupTours = context.ExcursionGroupTours.Where(rec => rec.ExcursionGroupId == model.Id).ToList(); if (excursionGroupTours != null && excursionGroupTours.Count > 0) { // удалили те, которых нет в модели context.ExcursionGroupTours.RemoveRange(excursionGroupTours.Where(rec => !model.ExcursionGroupTours.ContainsKey(rec.TourId))); context.SaveChanges(); } var excursionGroup = context.ExcursionGroups.First(x => x.Id == Id); foreach (var et in model.ExcursionGroupTours) { if (excursionGroupTours!.Any(x => x.TourId == et.Key)) { continue; } context.ExcursionGroupTours.Add(new ExcursionGroupTour { ExcursionGroup = excursionGroup, Tour = context.Tours.First(x => x.Id == et.Key) }); context.SaveChanges(); } _excursionGroupTours = null; } public void UpdatePlaces(TravelAgencyDatabase context, ExcursionGroupBindingModel model) { var excursionGroupPlaces = context.ExcursionGroupPlaces.Where(rec => rec.ExcursionGroupId == model.Id).ToList(); if (excursionGroupPlaces != null && excursionGroupPlaces.Count > 0) { // удалили те, которых нет в модели context.ExcursionGroupPlaces.RemoveRange(excursionGroupPlaces.Where(rec => !model.ExcursionGroupPlaces.ContainsKey(rec.PlaceId))); context.SaveChanges(); } var excursionGroup = context.ExcursionGroups.First(x => x.Id == Id); foreach (var et in model.ExcursionGroupPlaces) { if (excursionGroupPlaces!.Any(x => x.PlaceId == et.Key)) { continue; } context.ExcursionGroupPlaces.Add(new ExcursionGroupPlace { ExcursionGroup = excursionGroup, Place = context.Places.First(x => x.Id == et.Key) }); context.SaveChanges(); } _excursionGroupPlaces = null; } } }