107 lines
3.2 KiB
C#
107 lines
3.2 KiB
C#
|
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;
|
|||
|
using TravelAgencyContracts.BindingModels;
|
|||
|
using TravelAgencyContracts.ViewModels;
|
|||
|
using TravelAgencyDataModels.Models;
|
|||
|
|
|||
|
namespace TravelAgencyDatabaseImplement.Models
|
|||
|
{
|
|||
|
public class Trip : ITripModel
|
|||
|
{
|
|||
|
public int Id { get; set; }
|
|||
|
|
|||
|
[Required]
|
|||
|
public string TripName { get; set; } = string.Empty;
|
|||
|
|
|||
|
[Required]
|
|||
|
public DateTime TripDate { get; set; }
|
|||
|
|
|||
|
[Required]
|
|||
|
public int GuideId { get; set; }
|
|||
|
|
|||
|
public virtual Guide Guide { get; set; }
|
|||
|
|
|||
|
private Dictionary<int, IPlaceModel>? _tripPlaces = null;
|
|||
|
|
|||
|
[NotMapped]
|
|||
|
public Dictionary<int, IPlaceModel> TripPlaces
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
if (_tripPlaces == null)
|
|||
|
{
|
|||
|
_tripPlaces = Places
|
|||
|
.ToDictionary(recPC => recPC.PlaceId, recPC => recPC.Place as IPlaceModel);
|
|||
|
}
|
|||
|
return _tripPlaces;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
[ForeignKey("TripId")]
|
|||
|
public virtual List<TripPlace> Places { get; set; } = new();
|
|||
|
|
|||
|
public static Trip? Create(TravelAgencyDatabase context, TripBindingModel? model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
return new Trip()
|
|||
|
{
|
|||
|
Id = model.Id,
|
|||
|
TripName = model.TripName,
|
|||
|
TripDate = model.TripDate,
|
|||
|
GuideId = model.GuideId,
|
|||
|
Places = model.TripPlaces.Select(x => new TripPlace
|
|||
|
{
|
|||
|
Place = context.Places.First(y => y.Id == x.Key)
|
|||
|
}).ToList()
|
|||
|
};
|
|||
|
}
|
|||
|
public void Update(TripBindingModel? model)
|
|||
|
{
|
|||
|
if (model == null)
|
|||
|
{
|
|||
|
return;
|
|||
|
}
|
|||
|
TripName = model.TripName;
|
|||
|
TripDate = model.TripDate;
|
|||
|
}
|
|||
|
|
|||
|
public TripViewModel GetViewModel => new()
|
|||
|
{
|
|||
|
Id = Id,
|
|||
|
TripName = TripName,
|
|||
|
TripDate = TripDate,
|
|||
|
GuideId = GuideId,
|
|||
|
TripPlaces = TripPlaces
|
|||
|
};
|
|||
|
|
|||
|
public void UpdatePlaces(TravelAgencyDatabase context, TripBindingModel model)
|
|||
|
{
|
|||
|
var tripPlaces = context.TripPlaces.Where(rec => rec.TripId == model.Id).ToList();
|
|||
|
if (tripPlaces != null && tripPlaces.Count > 0)
|
|||
|
{ // удалили те, которых нет в модели
|
|||
|
context.TripPlaces.RemoveRange(tripPlaces.Where(rec => !model.TripPlaces.ContainsKey(rec.PlaceId)));
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
var trip = context.Trips.First(x => x.Id == Id);
|
|||
|
foreach (var et in model.TripPlaces)
|
|||
|
{
|
|||
|
context.TripPlaces.Add(new TripPlace
|
|||
|
{
|
|||
|
Trip = trip,
|
|||
|
Place = context.Places.First(x => x.Id == et.Key)
|
|||
|
});
|
|||
|
context.SaveChanges();
|
|||
|
}
|
|||
|
_tripPlaces = null;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|