и мы пьем лимонад

This commit is contained in:
ChaZIR 2024-04-30 23:17:38 +04:00
parent 4a2e6ac004
commit 9ffe450b66
5 changed files with 117 additions and 107 deletions

View File

@ -19,9 +19,6 @@ namespace TravelCompanyDatabaseImplement.Implements.ContractorImplements
using var context = new TravelCompanyDatabase();
return context.Excursions
.Include(x => x.Contractor)
.Include(x => x.Tours)
.ThenInclude(x => x.Tour)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
@ -33,10 +30,7 @@ namespace TravelCompanyDatabaseImplement.Implements.ContractorImplements
{
return context.Excursions
.Include(x => x.Contractor)
.Include(x => x.Tours)
.ThenInclude(x => x.Tour)
.Where(x => x.ExcursionName.Contains(model.ExcursionName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
@ -44,10 +38,7 @@ namespace TravelCompanyDatabaseImplement.Implements.ContractorImplements
{
return context.Excursions
.Include(x => x.Contractor)
.Include(x => x.Tours)
.ThenInclude(x => x.Tour)
.Where(x => x.ContractorID.Equals(model.ContractorID))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
@ -61,63 +52,57 @@ namespace TravelCompanyDatabaseImplement.Implements.ContractorImplements
return null;
}
using var context = new TravelCompanyDatabase();
return context.Excursions
.Include(x => x.Contractor)
.Include(x => x.Tours)
.ThenInclude(x => x.Tour)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ExcursionName) && x.ExcursionName == model.ExcursionName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
return context.Excursions.Include(x => x.Contractor)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ExcursionName) && x.ExcursionName == model.ExcursionName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public ExcursionViewModel? Insert(ExcursionBindingModel model)
{
using var context = new TravelCompanyDatabase();
var newExcursion = Excursion.Create(context, model);
var newExcursion = Excursion.Create(model);
if (newExcursion == null)
{
return null;
}
using var context = new TravelCompanyDatabase();
context.Excursions.Add(newExcursion);
context.SaveChanges();
return newExcursion.GetViewModel;
return context.Excursions
.Include(x => x.Contractor)
.FirstOrDefault(x => x.Id == newExcursion.Id)
?.GetViewModel;
}
public ExcursionViewModel? Update(ExcursionBindingModel model)
{
using var context = new TravelCompanyDatabase();
using var transaction = context.Database.BeginTransaction();
try
var excursion = context.Excursions.FirstOrDefault(x => x.Id == model.Id);
if (excursion == null)
{
var excursion = context.Excursions.FirstOrDefault(rec => rec.Id == model.Id);
if (excursion == null)
{
return null;
}
excursion.Update(model);
context.SaveChanges();
excursion.UpdateTours(context, model);
transaction.Commit();
return excursion.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
return null;
}
excursion.Update(model);
context.SaveChanges();
return context.Excursions
.Include(x => x.Contractor)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public ExcursionViewModel? Delete(ExcursionBindingModel model)
{
using var context = new TravelCompanyDatabase();
var element = context.Excursions
.Include(x => x.Tours)
.FirstOrDefault(rec => rec.Id == model.Id);
var element = context.Excursions.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
var deletedElement = context.Excursions
.Include(x => x.Contractor)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
context.Excursions.Remove(element);
context.SaveChanges();
return element.GetViewModel;
return deletedElement;
}
return null;
}

View File

@ -9,7 +9,6 @@ using TravelCompanyContracts.BindingModels.Contractor;
using TravelCompanyContracts.ViewModels.Contractor.ViewModels;
using TravelCompanyDataModels.Models.Contractor;
using Microsoft.EntityFrameworkCore;
using TravelCompanyDatabaseImplement.Models.GuarantorModels;
namespace TravelCompanyDatabaseImplement.Models
{
@ -26,13 +25,16 @@ namespace TravelCompanyDatabaseImplement.Models
[Required]
public int ContractorID { get; set; }
[DeleteBehavior(DeleteBehavior.Restrict)]
public virtual Contractor Contractor { get; set; }
[ForeignKey("ExcursionId")]
public virtual List<ExcursionTour> Tours { get; set; } = new();
[ForeignKey("ExcursionId")]
public virtual List<ExcursionGuide> Guides { get; set; } = new();
public static Excursion? Create(TravelCompanyDatabase context, ExcursionBindingModel? model)
public static Excursion? Create(ExcursionBindingModel? model)
{
if (model == null)
{

View File

@ -9,7 +9,6 @@ using TravelCompanyContracts.BindingModels.Contractor;
using TravelCompanyContracts.ViewModels.Contractor.ViewModels;
using TravelCompanyDataModels.Models.Contractor;
using Microsoft.EntityFrameworkCore;
using TravelCompanyDatabaseImplement.Models.GuarantorModels;
namespace TravelCompanyDatabaseImplement.Models
{
@ -28,83 +27,82 @@ namespace TravelCompanyDatabaseImplement.Models
[DeleteBehavior(DeleteBehavior.Restrict)]
public virtual Contractor Contractor { get; set; }
}
private Dictionary<int, ITourModel>? _excursionGroupTours = null;
private Dictionary<int, ITourModel>? _excursionGroupTours = null;
[NotMapped]
public Dictionary<int, ITourModel> ExcursionGroupTours
{
get
[NotMapped]
public Dictionary<int, ITourModel> ExcursionGroupTours
{
if (_excursionGroupTours == null)
get
{
_excursionGroupTours = Tours
.ToDictionary(recPC => recPC.TourId, recPC => recPC.Tour as ITourModel);
if (_excursionGroupTours == null)
{
_excursionGroupTours = Tours
.ToDictionary(recPC => recPC.TourId, recPC => recPC.Tour as ITourModel);
}
return _excursionGroupTours;
}
return _excursionGroupTours;
}
}
[ForeignKey("ExcursionGroupId")]
public virtual List<ExcursionGroupTour> Tours { get; set; } = new();
[ForeignKey("ExcursionGroupId")]
public virtual List<ExcursionGroupTour> Tours { get; set; } = new();
public static ExcursionGroup? Create(TravelCompanyDatabase context, ExcursionGroupBindingModel? model)
{
if (model == null)
public static ExcursionGroup? Create(TravelCompanyDatabase context, ExcursionGroupBindingModel? model)
{
return null;
}
return new ExcursionGroup()
{
Id = model.Id,
ExcursionGroupName = model.ExcursionGroupName,
PeopleAmount = model.PeopleAmount,
ContractorID = model.ContractorID,
Tours = model.ExcursionGroupTours.Select(x => new ExcursionGroupTour
if (model == null)
{
Tour = context.Tours.First(y => y.Id == x.Key)
}).ToList()
return null;
}
return new ExcursionGroup()
{
Id = model.Id,
ExcursionGroupName = model.ExcursionGroupName,
PeopleAmount = model.PeopleAmount,
ContractorID = model.ContractorID,
Tours = model.ExcursionGroupTours.Select(x => new ExcursionGroupTour
{
Tour = context.Tours.First(y => y.Id == x.Key)
}).ToList()
};
}
public void Update(ExcursionGroupBindingModel? model)
{
if (model == null)
{
return;
}
ExcursionGroupName = model.ExcursionGroupName;
PeopleAmount = model.PeopleAmount;
}
public ExcursionGroupViewModel GetViewModel => new()
{
Id = Id,
ExcursionGroupName = ExcursionGroupName,
PeopleAmount = PeopleAmount,
ContractorID = ContractorID,
ExcursionGroupTours = ExcursionGroupTours
};
}
public void Update(ExcursionGroupBindingModel? model)
{
if (model == null)
{
return;
}
ExcursionGroupName = model.ExcursionGroupName;
PeopleAmount = model.PeopleAmount;
}
public ExcursionGroupViewModel GetViewModel => new()
{
Id = Id,
ExcursionGroupName = ExcursionGroupName,
PeopleAmount = PeopleAmount,
ContractorID = ContractorID,
ExcursionGroupTours = ExcursionGroupTours
};
public void UpdateTours(TravelCompanyDatabase context, ExcursionGroupBindingModel model)
{
var excursionGroupTours = context.ExcursionGroupTours.Where(rec => rec.ExcursionGroupId == model.Id).ToList();
if (excursionGroupTours != null && excursionGroupTours.Count > 0)
public void UpdateTours(TravelCompanyDatabase context, ExcursionGroupBindingModel model)
{
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)
{
context.ExcursionGroupTours.Add(new ExcursionGroupTour
var excursionGroupTours = context.ExcursionGroupTours.Where(rec => rec.ExcursionGroupId == model.Id).ToList();
if (excursionGroupTours != null && excursionGroupTours.Count > 0)
{
ExcursionGroup = excursionGroup,
Tour = context.Tours.First(x => x.Id == et.Key)
});
context.SaveChanges();
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)
{
context.ExcursionGroupTours.Add(new ExcursionGroupTour
{
ExcursionGroup = excursionGroup,
Tour = context.Tours.First(x => x.Id == et.Key)
});
context.SaveChanges();
}
_excursionGroupTours = null;
}
_excursionGroupTours = null;
}
}
}
}

View File

@ -0,0 +1,24 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TravelCompanyDatabaseImplement.Models
{
public class ExcursionGuide
{
public int Id { get; set; }
[Required]
public int ExcursionId { get; set; }
[Required]
public int GuideId { get; set; }
public virtual Excursion Excursion { get; set; } = new();
public virtual Guide Guide { get; set; } = new();
}
}

View File

@ -24,6 +24,7 @@ namespace TravelCompanyDatabaseImplement
public virtual DbSet<Tour> Tours { set; get; }
public virtual DbSet<ExcursionTour> ExcursionTours { set; get; }
public virtual DbSet<ExcursionGroupTour> ExcursionGroupTours { set; get; }
public virtual DbSet<ExcursionGuide> ExcursionGuides { set; get; }
public virtual DbSet<Guide> Guides { set; get; }
public virtual DbSet<Place> Places { set; get; }
public virtual DbSet<Trip> Trips { set; get; }