Upload files to 'STODataBaseImplement'

This commit is contained in:
Ivan_Starostin 2024-05-01 14:33:58 +04:00
parent 6855bcd115
commit 03eb00a7e9
2 changed files with 127 additions and 0 deletions

View File

@ -0,0 +1,105 @@
using Microsoft.EntityFrameworkCore;
using STOContracts.BindingModels;
using STOContracts.SearchModels;
using STOContracts.StorageContracts;
using STOContracts.ViewModels;
using STODatabaseImplement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement
{
public class WorkStorage : IWorkStorage
{
public List<WorkViewModel> GetFullList()
{
using var context = new STODatabase();
return context.Works
.Include(x => x.CarPartCar)
.ThenInclude(x => x.CarPart)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public List<WorkViewModel> GetFilteredList(WorkSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new STODatabase();
return context.Works.Include(x => x.CarPartCar)
.ThenInclude(x => x.CarPart)
.Where(x => x.Id == model.Id)
.ToList()
.Select(x => x.GetViewModel)
.ToList();
}
public WorkViewModel? GetElement(WorkSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new STODatabase();
return context.Works
.Include(x => x.CarPartCar)
.ThenInclude(x => x.CarPart)
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
public WorkViewModel? Insert(WorkBindingModel model)
{
using var context = new STODatabase();
var newWork = Work.Create(context, model);
if (newWork == null)
{
return null;
}
context.Works.Add(newWork);
context.SaveChanges();
return newWork.GetViewModel;
}
public WorkViewModel? Update(WorkBindingModel model)
{
using var context = new STODatabase();
using var transaction = context.Database.BeginTransaction();
try
{
var car = context.Works.FirstOrDefault(rec =>
rec.Id == model.Id);
if (car == null)
{
return null;
}
car.Update(model);
context.SaveChanges();
car.UpdateCarParts(context, model);
transaction.Commit();
return car.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public WorkViewModel? Delete(WorkBindingModel model)
{
using var context = new STODatabase();
var element = context.Works
.Include(x => x.CarPartCar)
.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Works.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DatabaseImplement
{
public class WorkTechnicalWork
{
public int Id { get; set; }
[Required]
public int TechnicalWorkId { get; set; }
[Required]
public int WorkId { get; set; }
[Required]
public int Count { get; set; }
public virtual TechnicalWork TechnicalWork { get; set; } = new();
public virtual Work Work { get; set; } = new();
}
}