126 lines
4.2 KiB
C#
Raw Normal View History

2024-08-10 18:43:15 +04:00
using Microsoft.EntityFrameworkCore;
using ServiceStationContracts.BindingModels;
2024-04-30 19:55:37 +03:00
using ServiceStationContracts.SearchModels;
using ServiceStationContracts.ViewModels;
2024-08-19 17:49:44 +04:00
using ServiceStationContracts.StorageContracts;
2024-04-30 19:55:37 +03:00
using ServiceStationsDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ServiceStationsDataBaseImplement.Implements
{
public class WorkStorage : IWorkStorage
{
2024-08-10 18:43:15 +04:00
public WorkViewModel? Insert(WorkBindingModel model) {
2024-04-30 19:55:37 +03:00
using var context = new Database();
2024-08-10 18:43:15 +04:00
var newRec = Work.Create(context,model);
if (newRec == null) {
return null;
2024-04-30 19:55:37 +03:00
}
2024-08-10 18:43:15 +04:00
context.Works.Add(newRec);
context.SaveChanges();
return newRec.GetViewModel;
2024-04-30 19:55:37 +03:00
}
2024-08-10 18:43:15 +04:00
public WorkViewModel? Update(WorkBindingModel model) {
2024-04-30 19:55:37 +03:00
using var context = new Database();
2024-08-10 18:43:15 +04:00
using var transaction = context.Database.BeginTransaction();
try {
var recUp = context.Works.FirstOrDefault(x => x.Id == model.Id);
if (recUp == null) {
return null;
}
recUp.Update(model);
context.SaveChanges();
recUp.UpdateClients(context, model);
transaction.Commit();
return recUp.GetViewModel;
}
catch {
transaction.Rollback();
throw;
}
2024-04-30 19:55:37 +03:00
}
2024-08-10 18:43:15 +04:00
public WorkViewModel? Delete(WorkBindingModel model)
2024-04-30 19:55:37 +03:00
{
2024-08-10 18:43:15 +04:00
using var context = new Database();
var recDel = context.Works
.Include(x => x.WorkClients)
.FirstOrDefault(rec => rec.Id == model.Id);
if (recDel != null)
2024-04-30 19:55:37 +03:00
{
2024-08-10 18:43:15 +04:00
context.Works.Remove(recDel);
context.SaveChanges();
return recDel.GetViewModel;
2024-04-30 19:55:37 +03:00
}
2024-08-10 18:43:15 +04:00
return null;
2024-04-30 19:55:37 +03:00
}
2024-08-10 18:43:15 +04:00
public WorkViewModel? GetElement(WorkSearchModel model)
2024-04-30 19:55:37 +03:00
{
using var context = new Database();
2024-08-10 18:43:15 +04:00
if (model.Id.HasValue) {
return context.Works
2024-08-23 19:44:26 +04:00
.Include(x => x.WorkClients)
.ThenInclude(x => x._client)
2024-08-10 18:43:15 +04:00
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
else if (model.ExecutorId.HasValue) {
return context.Works
2024-08-22 20:40:32 +04:00
.Include(x => x.WorkClients)
2024-08-23 19:44:26 +04:00
.ThenInclude(x => x._client)
2024-08-10 18:43:15 +04:00
.FirstOrDefault(x => x.ExecutorId == model.ExecutorId)
?.GetViewModel;
}
return context.Works
2024-08-22 20:40:32 +04:00
.Include(x => x.WorkClients)
2024-08-23 19:44:26 +04:00
.ThenInclude(x => x._client)
.FirstOrDefault(x => x.TaskByWorkId == model.TaskId)
2024-08-10 18:43:15 +04:00
?.GetViewModel;
2024-04-30 19:55:37 +03:00
}
2024-08-10 18:43:15 +04:00
public List<WorkViewModel> GetFilteredList(WorkSearchModel model)
2024-04-30 19:55:37 +03:00
{
2024-08-10 18:43:15 +04:00
using var context = new Database();
if (model.DateFrom.HasValue && model.DateTo.HasValue) {
return context.Works
2024-08-22 20:40:32 +04:00
.Include(x => x.WorkClients)
.ThenInclude(x => x._client)
2024-08-10 18:43:15 +04:00
.Where(x => x.Date >= model.DateFrom && x.Date <= model.DateTo)
.Select(x => x.GetViewModel)
.ToList();
2024-04-30 19:55:37 +03:00
}
2024-08-10 18:43:15 +04:00
else if (model.ExecutorId.HasValue) {
2024-08-23 19:44:26 +04:00
return context.Works
2024-08-22 20:40:32 +04:00
.Where(x => x.ExecutorId == model.ExecutorId)
2024-08-23 19:44:26 +04:00
.Include(x => x._task)
.Select(x => x.GetViewModel)
2024-08-10 18:43:15 +04:00
.ToList();
}
return context.Works
2024-08-22 20:40:32 +04:00
.Include(x => x.WorkClients)
.ThenInclude(x => x._client)
2024-08-23 19:44:26 +04:00
.Where(x => x.TaskByWorkId == model.TaskId)
2024-08-10 18:43:15 +04:00
.Select(x => x.GetViewModel)
.ToList();
2024-04-30 19:55:37 +03:00
}
2024-08-10 18:43:15 +04:00
public List<WorkViewModel> GetFullList()
2024-04-30 19:55:37 +03:00
{
using var context = new Database();
2024-08-10 18:43:15 +04:00
return context.Works
2024-08-22 20:40:32 +04:00
.Include(x => x.WorkClients)
.ThenInclude(x => x._client)
2024-08-10 18:43:15 +04:00
.Select(x => x.GetViewModel)
.ToList();
2024-04-30 19:55:37 +03:00
}
}
}