2024-04-28 23:08:12 +04:00
|
|
|
|
using System.ComponentModel.DataAnnotations.Schema;
|
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
|
using TravelAgencyContracts.BindingModels;
|
|
|
|
|
using TravelAgencyContracts.ViewModels;
|
|
|
|
|
using TravelAgencyDataModels.Models;
|
2024-04-29 21:30:41 +04:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-04-28 23:08:12 +04:00
|
|
|
|
|
|
|
|
|
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; }
|
|
|
|
|
|
2024-04-29 21:30:41 +04:00
|
|
|
|
[DeleteBehavior(DeleteBehavior.Restrict)]
|
|
|
|
|
public virtual User User { get; set; }
|
|
|
|
|
|
2024-04-28 23:08:12 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public int GuideId { get; set; }
|
|
|
|
|
|
|
|
|
|
public virtual Guide Guide { get; set; }
|
|
|
|
|
|
|
|
|
|
private Dictionary<int, ITourModel>? _excursionGroupTours = null;
|
|
|
|
|
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, ITourModel> ExcursionGroupTours
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_excursionGroupTours == null)
|
|
|
|
|
{
|
|
|
|
|
_excursionGroupTours = Tours
|
|
|
|
|
.ToDictionary(recPC => recPC.TourId, recPC => recPC.Tour as ITourModel);
|
|
|
|
|
}
|
|
|
|
|
return _excursionGroupTours;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ForeignKey("ExcursionGroupId")]
|
|
|
|
|
public virtual List<ExcursionGroupTour> Tours { get; set; } = new();
|
|
|
|
|
|
2024-05-29 13:06:49 +04:00
|
|
|
|
private Dictionary<int, IPlaceModel>? _excursionGroupPlaces = null;
|
|
|
|
|
|
|
|
|
|
[NotMapped]
|
|
|
|
|
public Dictionary<int, IPlaceModel> ExcursionGroupPlaces
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_excursionGroupPlaces == null)
|
|
|
|
|
{
|
|
|
|
|
_excursionGroupPlaces = Places
|
|
|
|
|
.ToDictionary(recPC => recPC.PlaceId, recPC => recPC.Place as IPlaceModel);
|
|
|
|
|
}
|
|
|
|
|
return _excursionGroupPlaces;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[ForeignKey("ExcursionGroupId")]
|
|
|
|
|
public virtual List<ExcursionGroupPlace> Places { get; set; } = new();
|
|
|
|
|
|
2024-04-28 23:08:12 +04:00
|
|
|
|
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,
|
2024-04-30 18:42:41 +04:00
|
|
|
|
Guide = context.Guides
|
|
|
|
|
.First(x => x.Id == model.GuideId),
|
2024-04-28 23:08:12 +04:00
|
|
|
|
Tours = model.ExcursionGroupTours.Select(x => new ExcursionGroupTour
|
|
|
|
|
{
|
|
|
|
|
Tour = context.Tours.First(y => y.Id == x.Key)
|
2024-05-29 13:06:49 +04:00
|
|
|
|
}).ToList(),
|
|
|
|
|
Places = model.ExcursionGroupPlaces.Select(x => new ExcursionGroupPlace
|
|
|
|
|
{
|
|
|
|
|
Place = context.Places.First(y => y.Id == x.Key)
|
2024-04-28 23:08:12 +04:00
|
|
|
|
}).ToList()
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
public void Update(ExcursionGroupBindingModel? model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-05-28 18:32:51 +04:00
|
|
|
|
using var context = new TravelAgencyDatabase();
|
2024-04-28 23:08:12 +04:00
|
|
|
|
ExcursionGroupName = model.ExcursionGroupName;
|
|
|
|
|
ParticipantsAmount = model.ParticipantsAmount;
|
2024-05-28 18:32:51 +04:00
|
|
|
|
GuideId = model.GuideId;
|
|
|
|
|
Guide = context.Guides
|
|
|
|
|
.First(x => x.Id == model.GuideId);
|
2024-04-28 23:08:12 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ExcursionGroupViewModel GetViewModel => new()
|
|
|
|
|
{
|
|
|
|
|
Id = Id,
|
|
|
|
|
ExcursionGroupName = ExcursionGroupName,
|
|
|
|
|
ParticipantsAmount = ParticipantsAmount,
|
|
|
|
|
UserId = UserId,
|
|
|
|
|
GuideId = GuideId,
|
2024-05-28 18:32:51 +04:00
|
|
|
|
GuideFIO = Guide?.GuideFIO,
|
2024-05-29 13:06:49 +04:00
|
|
|
|
ExcursionGroupTours = ExcursionGroupTours,
|
|
|
|
|
ExcursionGroupPlaces = ExcursionGroupPlaces
|
2024-04-28 23:08:12 +04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2024-05-28 18:32:51 +04:00
|
|
|
|
|
|
|
|
|
if (excursionGroupTours!.Any(x => x.TourId == et.Key))
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-04-28 23:08:12 +04:00
|
|
|
|
context.ExcursionGroupTours.Add(new ExcursionGroupTour
|
|
|
|
|
{
|
|
|
|
|
ExcursionGroup = excursionGroup,
|
|
|
|
|
Tour = context.Tours.First(x => x.Id == et.Key)
|
|
|
|
|
});
|
|
|
|
|
context.SaveChanges();
|
|
|
|
|
}
|
|
|
|
|
_excursionGroupTours = null;
|
|
|
|
|
}
|
2024-05-29 13:06:49 +04:00
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
2024-04-28 23:08:12 +04:00
|
|
|
|
}
|
|
|
|
|
}
|