CourseWork_Hotel/Hotel/HotelDataBaseImplement/Implemets/OrganiserStorage.cs

123 lines
3.9 KiB
C#
Raw Normal View History

2023-04-01 20:12:06 +04:00
using HotelContracts.BindingModels;
using HotelContracts.SearchModels;
using HotelContracts.StoragesContracts;
using HotelContracts.ViewModels;
2023-04-05 13:08:35 +04:00
using HotelDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
2023-04-01 20:12:06 +04:00
namespace HotelDataBaseImplement.Implemets
{
public class OrganiserStorage : IOrganiserStorage
{
public OrganiserViewModel? Delete(OrganiserBindingModel model)
{
2023-04-05 13:08:35 +04:00
using var context = new HotelDataBase();
var element = context.Organisers.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Organisers.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
2023-04-01 20:12:06 +04:00
}
public OrganiserViewModel? GetElement(OrganiserSearchModel model)
{
2023-04-05 13:08:35 +04:00
using var context = new HotelDataBase();
if (model.Id.HasValue)
return context.Organisers
.Include(x => x.MealPlans)
.Include(x => x.Members)
.Include(x => x.Conferences)
.FirstOrDefault(x => x.Id == model.Id)?
.GetViewModel;
if (!string.IsNullOrEmpty(model.OrganiserLogin) && !string.IsNullOrEmpty(model.OrganiserPassword))
return context.Organisers
.Include(x => x.MealPlans)
.Include(x => x.Members)
.Include(x => x.Conferences)
.FirstOrDefault(x => x.OrganiserLogin.Equals(model.OrganiserLogin) && x.OrganiserPassword.Equals(model.OrganiserPassword))?
.GetViewModel;
if (!string.IsNullOrEmpty(model.OrganiserLogin))
return context.Organisers
.Include(x => x.MealPlans)
.Include(x => x.Members)
.Include(x => x.Conferences)
.FirstOrDefault(x => x.OrganiserLogin.Equals(model.OrganiserLogin))?
.GetViewModel;
return null;
2023-04-01 20:12:06 +04:00
}
public List<OrganiserViewModel> GetFilteredList(OrganiserSearchModel model)
{
2023-04-05 13:08:35 +04:00
if (string.IsNullOrEmpty(model.OrganiserFIO))
{
return new();
}
using var context = new HotelDataBase();
return context.Organisers
.Include(x => x.MealPlans)
.Include(x => x.Members)
.Include(x => x.Conferences)
.Where(x => x.OrganiserLogin.Contains(model.OrganiserLogin) && x.OrganiserPassword == model.OrganiserPassword)
.Select(x => x.GetViewModel)
.ToList();
2023-04-01 20:12:06 +04:00
}
public List<OrganiserViewModel> GetFullList()
{
2023-04-05 13:08:35 +04:00
using var context = new HotelDataBase();
return context.Organisers
.Select(x => x.GetViewModel)
.ToList();
2023-04-01 20:12:06 +04:00
}
public OrganiserViewModel? Insert(OrganiserBindingModel model)
{
2023-04-05 13:08:35 +04:00
var newOrganiser = Organiser.Create(model);
if (newOrganiser == null)
{
return null;
}
using var context = new HotelDataBase();
context.Organisers.Add(newOrganiser);
context.SaveChanges();
return newOrganiser.GetViewModel;
2023-04-01 20:12:06 +04:00
}
public OrganiserViewModel? Update(OrganiserBindingModel model)
{
2023-04-05 13:08:35 +04:00
using var context = new HotelDataBase();
var organiser = context.Organisers
.FirstOrDefault(x => x.Id == model.Id);
if (organiser == null)
{
return null;
}
organiser.Update(model);
context.SaveChanges();
return organiser.GetViewModel;
2023-04-01 20:12:06 +04:00
}
}
}