Gismatullin.ISEbd-21.STO.Co.../ServiceStation/ServiceStationsDataBaseImplement/Implements/TaskStorage.cs

69 lines
2.3 KiB
C#

using ServiceStationContracts.BindingModels;
using ServiceStationContracts.SearchModels;
using ServiceStationContracts.ViewModels;
using ServiceStationsContracts.StorageContracts;
using ServiceStationsDataBaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
namespace ServiceStationsDataBaseImplement.Implements {
public class TaskStorage : ITaskStorage {
public TaskViewModel? Insert(TaskBindingModel model) {
var newRec = TaskByWork.Create(model);
if (newRec == null) {
return null;
}
using var context = new Database();
context.Tasks.Add(newRec);
context.SaveChanges();
return newRec.GetViewModel;
}
public TaskViewModel? Update(TaskBindingModel model) {
using var context = new Database();
var recUp = context.Tasks.FirstOrDefault(x => x.Id == model.Id);
if (recUp == null) {
return null;
}
recUp.Update(model);
context.SaveChanges();
return recUp.GetViewModel;
}
public TaskViewModel? Delete(TaskBindingModel model) {
using var context = new Database();
var recDel = context.Tasks.FirstOrDefault(x => x.Id == model.Id);
if (recDel != null) {
context.Tasks.Remove(recDel);
context.SaveChanges();
return recDel.GetViewModel;
}
return null;
}
public TaskViewModel? GetElement(TaskSearchModel model) {
using var context = new Database();
if (model.Id.HasValue) {
return context.Tasks
.FirstOrDefault(x => x.Id == model.Id)
?.GetViewModel;
}
else {
return context.Tasks
.FirstOrDefault(x => x.Name == model.Name)
?.GetViewModel;
}
}
public List<TaskViewModel> GetFullList() {
using var context = new Database();
return context.Tasks.Select(x => x.GetViewModel).ToList();
}
}
}