Merge branch 'database' of https://git.is.ulstu.ru/Marselchii/PIbd-21_Sagirov_M.M._Malafeev_L.S._CourseWork_CarCenter into database
This commit is contained in:
commit
62d4b6aebc
@ -0,0 +1,99 @@
|
||||
using CarCenterContracts.BindingModels;
|
||||
using CarCenterContracts.SearchModels;
|
||||
using CarCenterContracts.StoragesContracts;
|
||||
using CarCenterContracts.ViewModels;
|
||||
using CarCenterDatabaseImplement.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace CarCenterDatabaseImplement.Implements
|
||||
{
|
||||
public class StorekeeperStorage : IStorekeeperStorage
|
||||
{
|
||||
public List<StorekeeperViewModel> GetFullList()
|
||||
{
|
||||
using var context = new CarCenterDatabase();
|
||||
|
||||
return context.Storekeepers
|
||||
.Select(x => x.GetViewModel)
|
||||
.ToList();
|
||||
}
|
||||
public List<StorekeeperViewModel> GetFilteredList(StorekeeperSearchModel model)
|
||||
{
|
||||
if (model == null)
|
||||
{
|
||||
return new();
|
||||
}
|
||||
|
||||
if (model.Id.HasValue)
|
||||
{
|
||||
var res = GetElement(model);
|
||||
|
||||
return res != null ? new() { res } : new();
|
||||
}
|
||||
return new();
|
||||
}
|
||||
|
||||
public StorekeeperViewModel? GetElement(StorekeeperSearchModel model)
|
||||
{
|
||||
using var context = new CarCenterDatabase();
|
||||
|
||||
if (model.Id.HasValue)
|
||||
return context.Storekeepers
|
||||
.FirstOrDefault(x => x.Id == model.Id)
|
||||
?.GetViewModel;
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public StorekeeperViewModel? Delete(StorekeeperBindingModel model)
|
||||
{
|
||||
using var context = new CarCenterDatabase();
|
||||
|
||||
var res = context.Storekeepers
|
||||
.FirstOrDefault(x => x.Id == model.Id);
|
||||
|
||||
if (res != null)
|
||||
{
|
||||
context.Storekeepers.Remove(res);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
return res?.GetViewModel;
|
||||
}
|
||||
|
||||
public StorekeeperViewModel? Insert(StorekeeperBindingModel model)
|
||||
{
|
||||
using var context = new CarCenterDatabase();
|
||||
|
||||
var res = Storekeeper.Create(model);
|
||||
|
||||
if (res != null)
|
||||
{
|
||||
context.Storekeepers.Add(res);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
return res?.GetViewModel;
|
||||
}
|
||||
|
||||
public StorekeeperViewModel? Update(StorekeeperBindingModel model)
|
||||
{
|
||||
using var context = new CarCenterDatabase();
|
||||
|
||||
var res = context.Storekeepers.FirstOrDefault(x => x.Id == model.Id);
|
||||
|
||||
if (res != null)
|
||||
{
|
||||
res.Update(model);
|
||||
context.SaveChanges();
|
||||
}
|
||||
|
||||
return res?.GetViewModel;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user