бд плаки плаки
This commit is contained in:
parent
bdf6ac388b
commit
c751e67f1b
@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.Contracts;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TravelCompanyContracts.BindingModels.Contractor;
|
||||
using TravelCompanyContracts.SearchModels.Contractor;
|
||||
using TravelCompanyContracts.StoragesModels.Contractor;
|
||||
using TravelCompanyContracts.ViewModels.Contractor.ViewModels;
|
||||
|
||||
namespace TravelCompanyDatabaseImplement.Implements.ContractorImplements
|
||||
{
|
||||
public class ContractorStorage : IContractorStorage
|
||||
{
|
||||
public List<ContractorViewModel> GetFullList()
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
return context.Contractors
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<ContractorViewModel> GetFilteredList(ContractorSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.Surname))
|
||||
{
|
||||
return new();
|
||||
}
|
||||
using var context = new TravelCompanyDatabase();
|
||||
return context.Contractors
|
||||
.Where(x => x.Surname.Contains(model.Surname))
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public ContractorViewModel? GetElement(ContractorSearchModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
if (model.Id.HasValue)
|
||||
{
|
||||
return context.Contractors
|
||||
.FirstOrDefault(x => x.Id.Equals(model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(model.Email) && !string.IsNullOrEmpty(model.Password))
|
||||
{
|
||||
return context.Contractors
|
||||
.FirstOrDefault(x => x.Email.Equals(model.Email) && x.Password.Equals(model.Password))
|
||||
?.GetViewModel;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(model.Email))
|
||||
{
|
||||
return context.Contractors
|
||||
.FirstOrDefault(x => x.Email.Equals(model.Email))
|
||||
?.GetViewModel;
|
||||
}
|
||||
if (!string.IsNullOrEmpty(model.MobilePhone))
|
||||
{
|
||||
return context.Contractors
|
||||
.FirstOrDefault(x => x.MobilePhone.Equals(model.MobilePhone))
|
||||
?.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ContractorViewModel? Insert(ContractorBindingModel model)
|
||||
{
|
||||
var newContractor = Contractor.Create(model);
|
||||
if (newContractor == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new TravelCompanyDatabase();
|
||||
context.Contractors.Add(newContractor);
|
||||
context.SaveChanges();
|
||||
return newContractor.GetViewModel;
|
||||
}
|
||||
|
||||
public ContractorViewModel? Update(ContractorBindingModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
var contractor = context.Contractors.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (contractor == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
contractor.Update(model);
|
||||
context.SaveChanges();
|
||||
return contractor.GetViewModel;
|
||||
}
|
||||
|
||||
public ContractorViewModel? Delete(ContractorBindingModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
var element = context.Contractors.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Contractors.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,112 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TravelCompanyContracts.BindingModels.Contractor;
|
||||
using TravelCompanyContracts.SearchModels.Contractor;
|
||||
using TravelCompanyContracts.StoragesModels.Contractor;
|
||||
using TravelCompanyContracts.ViewModels.Contractor.ViewModels;
|
||||
|
||||
namespace TravelCompanyDatabaseImplement.Implements.ContractorImplements
|
||||
{
|
||||
public class ExcursionGroupStorage : IExcursionGroupStorage
|
||||
{
|
||||
public List<ExcursionGroupViewModel> GetFullList()
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
return context.ExcursionGroups
|
||||
.Include(x => x.Contractor)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<ExcursionGroupViewModel> GetFilteredList(ExcursionGroupSearchModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
if (!string.IsNullOrEmpty(model.ExcursionGroupName))
|
||||
{
|
||||
return context.ExcursionGroups
|
||||
.Include(x => x.Contractor)
|
||||
.Where(x => x.ExcursionGroupName.Contains(model.ExcursionGroupName))
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
if (model.ContractorID.HasValue)
|
||||
{
|
||||
return context.ExcursionGroups
|
||||
.Include(x => x.Contractor)
|
||||
.Where(x => x.UserId.Equals(model.ContractorID))
|
||||
.ToList()
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
return new();
|
||||
}
|
||||
|
||||
public ExcursionGroupViewModel? GetElement(ExcursionGroupSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ExcursionGroupName) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new TravelCompanyDatabase();
|
||||
return context.ExcursionGroups
|
||||
.Include(x => x.Contractor)
|
||||
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ExcursionGroupName) && x.ExcursionGroupName == model.ExcursionGroupName) ||
|
||||
(model.Id.HasValue && x.Id == model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public ExcursionGroupViewModel? Insert(ExcursionGroupBindingModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
var newExcursionGroup = ExcursionGroup.Create(context, model);
|
||||
if (newExcursionGroup == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.ExcursionGroups.Add(newExcursionGroup);
|
||||
context.SaveChanges();
|
||||
return newExcursionGroup.GetViewModel;
|
||||
}
|
||||
|
||||
public ExcursionGroupViewModel? Update(ExcursionGroupBindingModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
var excursionGroup = context.ExcursionGroups.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (excursionGroup == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
excursionGroup.Update(model);
|
||||
context.SaveChanges();
|
||||
transaction.Commit();
|
||||
return excursionGroup.GetViewModel;
|
||||
}
|
||||
catch
|
||||
{
|
||||
transaction.Rollback();
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public ExcursionGroupViewModel? Delete(ExcursionGroupBindingModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
var element = context.ExcursionGroups
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.ExcursionGroups.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,123 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TravelCompanyContracts.BindingModels.Contractor;
|
||||
using TravelCompanyContracts.SearchModels.Contractor;
|
||||
using TravelCompanyContracts.StoragesModels.Contractor;
|
||||
using TravelCompanyContracts.ViewModels.Contractor.ViewModels;
|
||||
|
||||
namespace TravelCompanyDatabaseImplement.Implements.ContractorImplements
|
||||
{
|
||||
public class ExcursionStorage : IExcursionStorage
|
||||
{
|
||||
public List<ExcursionViewModel> GetFullList()
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
public List<ExcursionViewModel> GetFilteredList(ExcursionSearchModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
if (!string.IsNullOrEmpty(model.ExcursionName))
|
||||
{
|
||||
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();
|
||||
}
|
||||
if (model.ContractorID.HasValue)
|
||||
{
|
||||
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();
|
||||
}
|
||||
return new();
|
||||
}
|
||||
|
||||
public ExcursionViewModel? GetElement(ExcursionSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.ExcursionName) && !model.Id.HasValue)
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public ExcursionViewModel? Insert(ExcursionBindingModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
var newExcursion = Excursion.Create(context, model);
|
||||
if (newExcursion == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
context.Excursions.Add(newExcursion);
|
||||
context.SaveChanges();
|
||||
return newExcursion.GetViewModel;
|
||||
}
|
||||
|
||||
public ExcursionViewModel? Update(ExcursionBindingModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
using var transaction = context.Database.BeginTransaction();
|
||||
try
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public ExcursionViewModel? Delete(ExcursionBindingModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
var element = context.Excursions
|
||||
.Include(x => x.Tours)
|
||||
.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
context.Excursions.Remove(element);
|
||||
context.SaveChanges();
|
||||
return element.GetViewModel;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,108 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using TravelCompanyContracts.BindingModels.Contractor;
|
||||
using TravelCompanyContracts.SearchModels.Contractor;
|
||||
using TravelCompanyContracts.StoragesModels.Contractor;
|
||||
using TravelCompanyContracts.ViewModels.Contractor.ViewModels;
|
||||
|
||||
namespace TravelCompanyDatabaseImplement.Implements.ContractorImplements
|
||||
{
|
||||
public class TourStorage : ITourStorage
|
||||
{
|
||||
public List<TourViewModel> GetFullList()
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
return context.Tours
|
||||
.Include(x => x.Contractor)
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<TourViewModel> GetFilteredList(TourSearchModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
if (!string.IsNullOrEmpty(model.TourName))
|
||||
{
|
||||
return context.Tours
|
||||
.Include(x => x.Contractor)
|
||||
.Where(x => x.TourName.Contains(model.TourName))
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
if (model.ContractorID.HasValue)
|
||||
{
|
||||
return context.Tours
|
||||
.Include(x => x.Contractor)
|
||||
.Where(x => x.UserId.Equals(model.ContractorID))
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
return new();
|
||||
}
|
||||
|
||||
public TourViewModel? GetElement(TourSearchModel model)
|
||||
{
|
||||
if (string.IsNullOrEmpty(model.TourName) && !model.Id.HasValue)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new TravelCompanyDatabase();
|
||||
return context.Tours.Include(x => x.Contractor)
|
||||
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.TourName) && x.TourName == model.TourName) ||
|
||||
(model.Id.HasValue && x.Id == model.Id))
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public TourViewModel? Insert(TourBindingModel model)
|
||||
{
|
||||
var newTour = Tour.Create(model);
|
||||
if (newTour == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
using var context = new TravelCompanyDatabase();
|
||||
context.Tours.Add(newTour);
|
||||
context.SaveChanges();
|
||||
return context.Tours
|
||||
.Include(x => x.Contractor)
|
||||
.FirstOrDefault(x => x.Id == newTour.Id)
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public TourViewModel? Update(TourBindingModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
var tour = context.Tours.FirstOrDefault(x => x.Id == model.Id);
|
||||
if (tour == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
tour.Update(model);
|
||||
context.SaveChanges();
|
||||
return context.Tours
|
||||
.Include(x => x.Contractor)
|
||||
.FirstOrDefault(x => x.Id == model.Id)
|
||||
?.GetViewModel;
|
||||
}
|
||||
|
||||
public TourViewModel? Delete(TourBindingModel model)
|
||||
{
|
||||
using var context = new TravelCompanyDatabase();
|
||||
var element = context.Tours.FirstOrDefault(rec => rec.Id == model.Id);
|
||||
if (element != null)
|
||||
{
|
||||
var deletedElement = context.Tours
|
||||
.Include(x => x.Contractor)
|
||||
.FirstOrDefault(x => x.Id == model.Id)
|
||||
?.GetViewModel;
|
||||
context.Tours.Remove(element);
|
||||
context.SaveChanges();
|
||||
return deletedElement;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using TravelCompanyContracts.BindingModels.Contractor;
|
||||
using TravelCompanyContracts.ViewModels.Contractor.ViewModels;
|
||||
using TravelCompanyDataModels.Models.Contractor;
|
||||
|
||||
namespace TravelCompanyDatabaseImplement.Models.ContractorModels
|
||||
{
|
||||
public class Contractor : IContractorModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Surname { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public string Patronymic { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public string Email { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public string MobilePhone { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public string Password { get; set; } = string.Empty;
|
||||
|
||||
[ForeignKey("ContractorID")]
|
||||
public virtual List<Excursion> Excursions { get; set; } = new();
|
||||
|
||||
[ForeignKey("ContractorID")]
|
||||
public virtual List<ExcursionGroup> ExcursionGroups { get; set; } = new();
|
||||
|
||||
[ForeignKey("ContractorID")]
|
||||
public virtual List<Tour> Tours { get; set; } = new();
|
||||
|
||||
public static Contractor? Create(ContractorBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Contractor()
|
||||
{
|
||||
Id = model.Id,
|
||||
Surname = model.Surname,
|
||||
Name = model.Name,
|
||||
Patronymic = model.Patronymic,
|
||||
Email = model.Email,
|
||||
MobilePhone = model.MobilePhone,
|
||||
Password = model.Password,
|
||||
};
|
||||
}
|
||||
public void Update(ContractorBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
Id = model.Id;
|
||||
Surname = model.Surname;
|
||||
Name = model.Name;
|
||||
Patronymic = model.Patronymic;
|
||||
Email = model.Email;
|
||||
MobilePhone = model.MobilePhone;
|
||||
Password = model.Password;
|
||||
}
|
||||
|
||||
public ContractorViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
Surname = Surname,
|
||||
Name = Name,
|
||||
Patronymic = Patronymic,
|
||||
Email = Email,
|
||||
MobilePhone = MobilePhone,
|
||||
Password = Password
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using TravelCompanyContracts.BindingModels.Contractor;
|
||||
using TravelCompanyContracts.ViewModels.Contractor.ViewModels;
|
||||
using TravelCompanyDataModels.Models.Contractor;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TravelCompanyDatabaseImplement.Models.GuarantorModels;
|
||||
|
||||
namespace TravelCompanyDatabaseImplement.Models.ContractorModels
|
||||
{
|
||||
public class Excursion : IExcursionModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public string ExcursionName { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public int ExcursionPrice { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ContractorID { 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)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Excursion()
|
||||
{
|
||||
Id = model.Id,
|
||||
ExcursionName = model.ExcursionName,
|
||||
ExcursionPrice = model.ExcursionPrice,
|
||||
ContractorID = model.ContractorID,
|
||||
};
|
||||
}
|
||||
public void Update(ExcursionBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
ExcursionName = model.ExcursionName;
|
||||
ExcursionPrice = model.ExcursionPrice;
|
||||
}
|
||||
|
||||
public ExcursionViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
ExcursionName = ExcursionName,
|
||||
ExcursionPrice = ExcursionPrice,
|
||||
ContractorID = ContractorID
|
||||
};
|
||||
}
|
||||
}
|
@ -0,0 +1,109 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using TravelCompanyContracts.BindingModels.Contractor;
|
||||
using TravelCompanyContracts.ViewModels.Contractor.ViewModels;
|
||||
using TravelCompanyDataModels.Models.Contractor;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TravelCompanyDatabaseImplement.Models.GuarantorModels;
|
||||
|
||||
namespace TravelCompanyDatabaseImplement.Models.ContractorModels
|
||||
{
|
||||
public class ExcursionGroup : IExcursionGroupModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public string ExcursionGroupName { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public int PeopleAmount { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ContractorID { get; set; }
|
||||
|
||||
[DeleteBehavior(DeleteBehavior.Restrict)]
|
||||
public virtual Contractor Contractor { 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();
|
||||
|
||||
public static ExcursionGroup? Create(TravelCompanyDatabase context, ExcursionGroupBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
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 UpdateTours(TravelCompanyDatabase 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)
|
||||
{
|
||||
context.ExcursionGroupTours.Add(new ExcursionGroupTour
|
||||
{
|
||||
ExcursionGroup = excursionGroup,
|
||||
Tour = context.Tours.First(x => x.Id == et.Key)
|
||||
});
|
||||
context.SaveChanges();
|
||||
}
|
||||
_excursionGroupTours = null;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace TravelCompanyDatabaseImplement.Models.ContractorModels
|
||||
{
|
||||
public class ExcursionGroupTour
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ExcursionGroupId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int TourId { get; set; }
|
||||
|
||||
public virtual ExcursionGroup ExcursionGroup { get; set; } = new();
|
||||
|
||||
public virtual Tour Tour { get; set; } = new();
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace TravelCompanyDatabaseImplement.Models.ContractorModels
|
||||
{
|
||||
public class ExcursionTour
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ExcursionId { get; set; }
|
||||
|
||||
[Required]
|
||||
public int TourId { get; set; }
|
||||
|
||||
public virtual Excursion Excursion { get; set; } = new();
|
||||
|
||||
public virtual Tour Tour { get; set; } = new();
|
||||
}
|
||||
}
|
@ -0,0 +1,69 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using TravelCompanyContracts.BindingModels.Contractor;
|
||||
using TravelCompanyContracts.ViewModels.Contractor.ViewModels;
|
||||
using TravelCompanyDataModels.Models.Contractor;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace TravelCompanyDatabaseImplement.Models.ContractorModels
|
||||
{
|
||||
public class Tour : ITourModel
|
||||
{
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public string TourName { get; set; } = string.Empty;
|
||||
|
||||
[Required]
|
||||
public DateTime TourDate { get; set; }
|
||||
|
||||
[Required]
|
||||
public int ContractorID { get; set; }
|
||||
|
||||
[DeleteBehavior(DeleteBehavior.Restrict)]
|
||||
public virtual Contractor Contractor { get; set; }
|
||||
|
||||
[ForeignKey("TourId")]
|
||||
public virtual List<ExcursionTour> Excursions { get; set; } = new();
|
||||
|
||||
[ForeignKey("TourId")]
|
||||
public virtual List<ExcursionGroupTour> ExcursionGroups { get; set; } = new();
|
||||
|
||||
public static Tour? Create(TourBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
return new Tour()
|
||||
{
|
||||
Id = model.Id,
|
||||
TourName = model.TourName,
|
||||
TourDate = model.TourDate,
|
||||
ContractorID = model.ContractorID,
|
||||
};
|
||||
}
|
||||
public void Update(TourBindingModel? model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
TourName = model.TourName;
|
||||
TourDate = model.TourDate;
|
||||
}
|
||||
|
||||
public TourViewModel GetViewModel => new()
|
||||
{
|
||||
Id = Id,
|
||||
TourName = TourName,
|
||||
TourDate = TourDate,
|
||||
ContractorID = ContractorID
|
||||
};
|
||||
}
|
||||
}
|
@ -21,20 +21,15 @@ namespace TravelCompanyDatabaseImplement
|
||||
}
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
public virtual DbSet<Account> Accounts { set; get; }
|
||||
|
||||
public virtual DbSet<Card> Cards { set; get; }
|
||||
|
||||
public virtual DbSet<Cashier> Cashiers { set; get; }
|
||||
|
||||
public virtual DbSet<CashWithdrawal> CashWithdrawals { set; get; }
|
||||
|
||||
public virtual DbSet<Excursion> Excursions { set; get; }
|
||||
public virtual DbSet<ExcursionGroup> ExcursionGroups { set; get; }
|
||||
public virtual DbSet<Tour> Tours { set; get; }
|
||||
public virtual DbSet<ExcursionTour> ExcursionTours { set; get; }
|
||||
public virtual DbSet<ExcursionGroupTour> ExcursionGroupTours { set; get; }
|
||||
public virtual DbSet<Guide> Guides { set; get; }
|
||||
public virtual DbSet<Place> Places { set; get; }
|
||||
public virtual DbSet<Trip> Trips { set; get; }
|
||||
public virtual DbSet<Guarantor> Guarantors { set; get; }
|
||||
|
||||
public virtual DbSet<Debiting> Debitings { set; get; }
|
||||
|
||||
public virtual DbSet<Crediting> Creditings { set; get; }
|
||||
|
||||
public virtual DbSet<MoneyTransfer> MoneyTransfers { set; get; }
|
||||
public virtual DbSet<Contractor> Contractors { set; get; }
|
||||
}
|
||||
}
|
||||
|
@ -6,11 +6,6 @@
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Folder Include="Implements\ContractorImplements\" />
|
||||
<Folder Include="Models\ContractorModels\" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.17" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.17" />
|
||||
|
Loading…
x
Reference in New Issue
Block a user