112 lines
3.3 KiB
C#
112 lines
3.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using UchetLabContracts.BindingModels;
|
|
using UchetLabContracts.SearchModels;
|
|
using UchetLabContracts.StorageContracts;
|
|
using UchetLabContracts.ViewModels;
|
|
using UchetLabDatabaseImplement.Models;
|
|
|
|
namespace UchetLabDatabaseImplement.Implements
|
|
{
|
|
public class LabStorage : ILabStorage
|
|
{
|
|
public LabViewModel? Delete(LabBindingModel model)
|
|
{
|
|
using var context = new UchetLabDataBase();
|
|
var element = context.Labs.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Labs.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public LabViewModel? GetElement(LabSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new UchetLabDataBase();
|
|
return context.Labs.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
|
|
}
|
|
|
|
public List<LabViewModel> GetFilteredList(LabSearchModel model)
|
|
{
|
|
using var context = new UchetLabDataBase();
|
|
return context.Labs
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<LabViewModel> GetFullList()
|
|
{
|
|
using var context = new UchetLabDataBase();
|
|
return context.Labs
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public LabViewModel? Insert(LabBindingModel model)
|
|
{
|
|
using var context = new UchetLabDataBase();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
{
|
|
try
|
|
{
|
|
var newEl = Lab.Create(model);
|
|
if (newEl == null)
|
|
{
|
|
transaction.Rollback();
|
|
return null;
|
|
}
|
|
context.Labs.Add(newEl);
|
|
|
|
context.SaveChanges();
|
|
context.Database.CommitTransaction();
|
|
|
|
return newEl.GetViewModel;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
transaction.Rollback();
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
public LabViewModel? Update(LabBindingModel model)
|
|
{
|
|
using var context = new UchetLabDataBase();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
{
|
|
try
|
|
{
|
|
var el = context.Labs.FirstOrDefault(x => x.Id == model.Id);
|
|
if (el == null)
|
|
{
|
|
transaction.Rollback();
|
|
return null;
|
|
}
|
|
el.Update(model);
|
|
|
|
context.SaveChanges();
|
|
context.Database.CommitTransaction();
|
|
|
|
return el.GetViewModel;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
transaction.Rollback();
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|