Eliseev E.E. LabWork03_Hard #8

Closed
ElEgEv wants to merge 23 commits from LabWork03_Hard into LabWork02_Hard
3 changed files with 105 additions and 2 deletions
Showing only changes of commit 0ce85a004c - Show all commits

View File

@ -1,7 +1,7 @@
using BlacksmithWorkshopBusinessLogic.BusinessLogic;
using BlacksmithWorkshopContracts.BusinessLogicsContracts;
using BlacksmithWorkshopContracts.StoragesContracts;
using BlacksmithWorkshopFileImplement.Implements;
using BlacksmithWorkshopDatabaseImplement.Implements;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NLog.Extensions.Logging;

View File

@ -15,7 +15,7 @@ namespace BlacksmithWorkshopDatabaseImplement
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseSqlServer(@"DataSource=CHESHIR\SQLEXPRESS;Initial Catalog=AbstractShopDatabaseFull;IntegratedSecurity=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
optionsBuilder.UseSqlServer(@"DataSource=LAPTOP-CFLH20EE\SQLEXPRESS;Initial Catalog=BlacksmithWorkshopDatabase;IntegratedSecurity=True;MultipleActiveResultSets=True;;TrustServerCertificate=True");
}
base.OnConfiguring(optionsBuilder);

View File

@ -0,0 +1,103 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.SearchModels;
using BlacksmithWorkshopContracts.StoragesContracts;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlacksmithWorkshopDatabaseImplement.Implements
{
internal class WorkPieceStorage : IWorkPieceStorage
{
public List<WorkPieceViewModel> GetFullList()
{
using var context = new BlacksmithWorkshopDatabase();
return context.WorkPieces
.Select(x => x.GetViewModel)
.ToList();
}
public List<WorkPieceViewModel> GetFilteredList(WorkPieceSearchModel model)
{
if (string.IsNullOrEmpty(model.WorkPieceName))
{
return new();
}
using var context = new BlacksmithWorkshopDatabase();
return context.WorkPieces
.Where(x => x.WorkPieceName.Contains(model.WorkPieceName))
.Select(x => x.GetViewModel)
.ToList();
}
public WorkPieceViewModel? GetElement(WorkPieceSearchModel model)
{
if (string.IsNullOrEmpty(model.WorkPieceName) && !model.Id.HasValue)
{
return null;
}
using var context = new BlacksmithWorkshopDatabase();
return context.WorkPieces
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.WorkPieceName) && x.WorkPieceName == model.WorkPieceName) ||
(model.Id.HasValue && x.Id == model.Id))
?.GetViewModel;
}
public WorkPieceViewModel? Insert(WorkPieceBindingModel model)
{
var newWorkPiece = WorkPiece.Create(model);
if (newWorkPiece == null)
{
return null;
}
using var context = new BlacksmithWorkshopDatabase();
context.WorkPieces.Add(newWorkPiece);
context.SaveChanges();
return newWorkPiece.GetViewModel;
}
public WorkPieceViewModel? Update(WorkPieceBindingModel model)
{
using var context = new BlacksmithWorkshopDatabase();
var workPiece = context.WorkPieces.FirstOrDefault(x => x.Id == model.Id);
if (workPiece == null)
{
return null;
}
workPiece.Update(model);
context.SaveChanges();
return workPiece.GetViewModel;
}
public WorkPieceViewModel? Delete(WorkPieceBindingModel model)
{
using var context = new BlacksmithWorkshopDatabase();
var element = context.WorkPieces.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.WorkPieces.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}