PIbd-23_Mochalov_D.V._LawFirm/LawFirm/LawFirmDatabaseImplement/Implements/BlankStorage.cs

92 lines
2.7 KiB
C#
Raw Normal View History

using LawFirmContracts.BindingModels;
using LawFirmContracts.SearchModels;
using LawFirmContracts.StorageContracts;
using LawFirmContracts.ViewModels;
using LawFirmDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LawFirmDatabaseImplement.Implements
{
public class BlankStorage : IBlankStorage
{
public BlankViewModel? GetElement(BlankSearchModel model)
{
if (string.IsNullOrEmpty(model.BlankName) && !model.Id.HasValue)
{
return null;
}
using var context = new LawFirmDatabase();
return context.Blanks.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.BlankName) && x.BlankName == model.BlankName)
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<BlankViewModel> GetFilteredList(BlankSearchModel model)
{
if (string.IsNullOrEmpty(model.BlankName))
{
return new();
}
using var context = new LawFirmDatabase();
return context.Blanks
.Where(x => x.BlankName.Contains(model.BlankName))
.Select(x => x.GetViewModel)
.ToList();
}
public List<BlankViewModel> GetFullList()
{
using var context = new LawFirmDatabase();
return context.Blanks.Select(x => x.GetViewModel).ToList();
}
public BlankViewModel? Insert(BlankBindingModel model)
{
var newBlank = Blank.Create(model);
if (newBlank == null)
{
return null;
}
using var context = new LawFirmDatabase();
context.Blanks.Add(newBlank);
context.SaveChanges();
return newBlank.GetViewModel;
}
public BlankViewModel? Update(BlankBindingModel model)
{
using var context = new LawFirmDatabase();
var blank = context.Blanks.FirstOrDefault(x => x.Id == model.Id);
if (blank == null)
{
return null;
}
blank.Update(model);
context.SaveChanges();
return blank.GetViewModel;
}
public BlankViewModel? Delete(BlankBindingModel model)
{
using var context = new LawFirmDatabase();
var element = context.Blanks.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Blanks.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}