105 lines
2.6 KiB
C#
105 lines
2.6 KiB
C#
|
using ComputerShopContracts.BindingModels;
|
|||
|
using ComputerShopContracts.SearchModels;
|
|||
|
using ComputerShopContracts.StorageContracts;
|
|||
|
using ComputerShopContracts.ViewModels;
|
|||
|
using ComputerShopDatabaseImplement.Models;
|
|||
|
using Microsoft.EntityFrameworkCore;
|
|||
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading.Tasks;
|
|||
|
|
|||
|
namespace ComputerShopDatabaseImplement.Implements
|
|||
|
{
|
|||
|
public class RequestStorage : IRequestStorage
|
|||
|
{
|
|||
|
public List<RequestViewModel> GetFullList()
|
|||
|
{
|
|||
|
using var context = new ComputerShopDatabase();
|
|||
|
return context.Requests
|
|||
|
.Include(x => x.Assembly)
|
|||
|
.ToList()
|
|||
|
.Select(x => x.GetViewModel)
|
|||
|
.ToList();
|
|||
|
}
|
|||
|
|
|||
|
public RequestViewModel? GetElement(RequestSearchModel model)
|
|||
|
{
|
|||
|
if (!model.Id.HasValue)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
using var context = new ComputerShopDatabase();
|
|||
|
return context.Requests
|
|||
|
.Include(x => x.Assembly)
|
|||
|
.FirstOrDefault(x => x.Id == model.Id)
|
|||
|
?.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public RequestViewModel? Insert(RequestBindingModel model)
|
|||
|
{
|
|||
|
using var context = new ComputerShopDatabase();
|
|||
|
var newRequest = Request.Create(context, model);
|
|||
|
if (newRequest == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
context.Requests.Add(newRequest);
|
|||
|
context.SaveChanges();
|
|||
|
return newRequest.GetViewModel;
|
|||
|
}
|
|||
|
|
|||
|
public RequestViewModel? Update(RequestBindingModel model)
|
|||
|
{
|
|||
|
using var context = new ComputerShopDatabase();
|
|||
|
using var transaction = context.Database.BeginTransaction();
|
|||
|
try
|
|||
|
{
|
|||
|
var request = context.Requests.FirstOrDefault(x => x.Id == model.Id);
|
|||
|
if (request == null)
|
|||
|
{
|
|||
|
return null;
|
|||
|
}
|
|||
|
request.Update(model);
|
|||
|
context.SaveChanges();
|
|||
|
transaction.Commit();
|
|||
|
return request.GetViewModel;
|
|||
|
}
|
|||
|
catch
|
|||
|
{
|
|||
|
transaction.Rollback();
|
|||
|
throw;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public RequestViewModel? Delete(RequestBindingModel model)
|
|||
|
{
|
|||
|
using var context = new ComputerShopDatabase();
|
|||
|
var request = context.Requests
|
|||
|
.FirstOrDefault(y => y.Id == model.Id);
|
|||
|
if (request != null)
|
|||
|
{
|
|||
|
context.Requests.Remove(request);
|
|||
|
context.SaveChanges();
|
|||
|
return request.GetViewModel;
|
|||
|
}
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
public bool ConnectRequestAssembly(int requestId, int assemblyId)
|
|||
|
{
|
|||
|
using var context = new ComputerShopDatabase();
|
|||
|
var request = context.Requests.FirstOrDefault(x => x.Id == requestId);
|
|||
|
var assembly = context.Assemblies.FirstOrDefault(x => x.Id == assemblyId);
|
|||
|
if (request == null || assembly == null)
|
|||
|
{
|
|||
|
return false;
|
|||
|
}
|
|||
|
request.ConnectAssembly(context, assemblyId);
|
|||
|
context.SaveChanges();
|
|||
|
return true;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|