112 lines
3.4 KiB
C#
112 lines
3.4 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 CheckerStorage : ICheckerStorage
|
|
{
|
|
public CheckerViewModel? Delete(CheckerBindingModel model)
|
|
{
|
|
using var context = new UchetLabDataBase();
|
|
var element = context.Checkers.FirstOrDefault(rec => rec.Id == model.Id);
|
|
if (element != null)
|
|
{
|
|
context.Checkers.Remove(element);
|
|
context.SaveChanges();
|
|
return element.GetViewModel;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public CheckerViewModel? GetElement(CheckerSearchModel model)
|
|
{
|
|
if (!model.Id.HasValue)
|
|
{
|
|
return null;
|
|
}
|
|
using var context = new UchetLabDataBase();
|
|
return context.Checkers.FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
|
|
}
|
|
|
|
public List<CheckerViewModel> GetFilteredList(CheckerSearchModel model)
|
|
{
|
|
using var context = new UchetLabDataBase();
|
|
return context.Checkers
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public List<CheckerViewModel> GetFullList()
|
|
{
|
|
using var context = new UchetLabDataBase();
|
|
return context.Checkers
|
|
.Select(x => x.GetViewModel)
|
|
.ToList();
|
|
}
|
|
|
|
public CheckerViewModel? Insert(CheckerBindingModel model)
|
|
{
|
|
using var context = new UchetLabDataBase();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
{
|
|
try
|
|
{
|
|
var newEl = Checker.Create(model);
|
|
if (newEl == null)
|
|
{
|
|
transaction.Rollback();
|
|
return null;
|
|
}
|
|
context.Checkers.Add(newEl);
|
|
|
|
context.SaveChanges();
|
|
context.Database.CommitTransaction();
|
|
|
|
return newEl.GetViewModel;
|
|
}
|
|
catch (Exception)
|
|
{
|
|
transaction.Rollback();
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
public CheckerViewModel? Update(CheckerBindingModel model)
|
|
{
|
|
using var context = new UchetLabDataBase();
|
|
using var transaction = context.Database.BeginTransaction();
|
|
{
|
|
try
|
|
{
|
|
var el = context.Checkers.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;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|