CourseWork_Bank/Bank/BankDataBaseImplement/Implemets/CreditStorage.cs
2023-05-21 17:01:50 +04:00

121 lines
3.5 KiB
C#

using BankContracts.BindingModels;
using BankContracts.SearchModels;
using BankContracts.StoragesContracts;
using BankContracts.ViewModels;
using BankDataBaseImplement.Models;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BankDataBaseImplement.Implemets
{
public class CreditStorage : ICreditStorage
{
public CreditViewModel? Delete(CreditBindingModel model)
{
using var context = new BankDataBase();
var element = context.Credits.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Credits.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
public CreditViewModel? GetElement(CreditSearchModel model)
{
if (string.IsNullOrEmpty(model.CreditName) && !model.Id.HasValue)
{
return null;
}
using var context = new BankDataBase();
return context.Credits
.Include(x => x.RoomCredits)
.ThenInclude(x => x.Room)
.Include(x => x.ConferenceBookingCredits)
.ThenInclude(x => x.ConferenceBooking)
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.CreditName) && x.CreditName == model.CreditName) || (model.Id.HasValue && x.Id == model.Id))?
.GetViewModel;
}
public List<CreditViewModel> GetFilteredList(CreditSearchModel model)
{
if (string.IsNullOrEmpty(model.CreditName))
{
return new();
}
using var context = new BankDataBase();
return context.Credits
.Include(x => x.RoomCredits)
.ThenInclude(x => x.Room)
.Include(x => x.ConferenceBookingCredits)
.ThenInclude(x => x.ConferenceBooking)
.Where(x => x.CreditName.Contains(model.CreditName))
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<CreditViewModel> GetFullList()
{
using var context = new BankDataBase();
return context.Credits
.Include(x => x.RoomCredits)
.ThenInclude(x => x.Room)
.Include(x => x.ConferenceBookingCredits)
.ThenInclude(x => x.ConferenceBooking)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public CreditViewModel? Insert(CreditBindingModel model)
{
using var context = new BankDataBase();
var newDinner = Credit.Create(model);
if (newDinner == null)
{
return null;
}
context.Credits.Add(newDinner);
context.SaveChanges();
return newDinner.GetViewModel;
}
public CreditViewModel? Update(CreditBindingModel model)
{
using var context = new BankDataBase();
var dinner = context.Credits.FirstOrDefault(x => x.Id == model.Id);
if (dinner == null)
{
return null;
}
dinner.Update(model);
context.SaveChanges();
return dinner.GetViewModel;
}
}
}