diff --git a/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs b/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs index e76d044..a42137a 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshop/Program.cs @@ -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; diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs index 98fe0e9..124313c 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs @@ -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); diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/WorkPieceStorage.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/WorkPieceStorage.cs new file mode 100644 index 0000000..fe53ba7 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Implements/WorkPieceStorage.cs @@ -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 GetFullList() + { + using var context = new BlacksmithWorkshopDatabase(); + + return context.WorkPieces + .Select(x => x.GetViewModel) + .ToList(); + } + + public List 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; + } + } +}