Базовая лаб3 вроде работает

This commit is contained in:
Данила Мочалов 2023-02-24 19:23:26 +04:00
parent ceec6e155c
commit 0e351cdabe
4 changed files with 303 additions and 1 deletions

View File

@ -1,7 +1,7 @@
using LawFirmBusinessLogic.BusinessLogics; using LawFirmBusinessLogic.BusinessLogics;
using LawFirmContracts.BusinessLogicContracts; using LawFirmContracts.BusinessLogicContracts;
using LawFirmContracts.StorageContracts; using LawFirmContracts.StorageContracts;
using LawFirmFileImplement.Implements; using LawFirmDatabaseImplement.Implements;
using LawFirmView; using LawFirmView;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;

View File

@ -0,0 +1,91 @@
using LawFirmContracts.BindingModels;
using LawFirmContracts.SearchModels;
using LawFirmContracts.StorageContracts;
using LawFirmContracts.ViewModels;
using LawFirmDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LawFirmDatabaseImplement.Implements
{
public class BlankStorage : IBlankStorage
{
public BlankViewModel? GetElement(BlankSearchModel model)
{
if (string.IsNullOrEmpty(model.BlankName) && !model.Id.HasValue)
{
return null;
}
using var context = new LawFirmDatabase();
return context.Blanks.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.BlankName) && x.BlankName == model.BlankName)
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<BlankViewModel> GetFilteredList(BlankSearchModel model)
{
if (string.IsNullOrEmpty(model.BlankName))
{
return new();
}
using var context = new LawFirmDatabase();
return context.Blanks
.Where(x => x.BlankName.Contains(model.BlankName))
.Select(x => x.GetViewModel)
.ToList();
}
public List<BlankViewModel> GetFullList()
{
using var context = new LawFirmDatabase();
return context.Blanks.Select(x => x.GetViewModel).ToList();
}
public BlankViewModel? Insert(BlankBindingModel model)
{
var newBlank = Blank.Create(model);
if (newBlank == null)
{
return null;
}
using var context = new LawFirmDatabase();
context.Blanks.Add(newBlank);
context.SaveChanges();
return newBlank.GetViewModel;
}
public BlankViewModel? Update(BlankBindingModel model)
{
using var context = new LawFirmDatabase();
var blank = context.Blanks.FirstOrDefault(x => x.Id == model.Id);
if (blank == null)
{
return null;
}
blank.Update(model);
context.SaveChanges();
return blank.GetViewModel;
}
public BlankViewModel? Delete(BlankBindingModel model)
{
using var context = new LawFirmDatabase();
var element = context.Blanks.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Blanks.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,128 @@
using LawFirmContracts.BindingModels;
using LawFirmContracts.SearchModels;
using LawFirmContracts.StorageContracts;
using LawFirmContracts.ViewModels;
using LawFirmDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading.Tasks;
namespace LawFirmDatabaseImplement.Implements
{
public class DocumentStorage : IDocumentStorage
{
public DocumentViewModel? GetElement(DocumentSearchModel model)
{
if (string.IsNullOrEmpty(model.DocumentName) && !model.Id.HasValue)
{
return null;
}
using var context = new LawFirmDatabase();
/*return context.Documents.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == model.DocumentName)
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;*/
var element = context.Documents.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == model.DocumentName)
|| (model.Id.HasValue && x.Id == model.Id));
return element?.GetViewModel;
}
public List<DocumentViewModel> GetFilteredList(DocumentSearchModel model)
{
if (string.IsNullOrEmpty(model.DocumentName))
{
return new();
}
using var context = new LawFirmDatabase();
/*return context.Documents
.Where(x => x.DocumentName.Contains(model.DocumentName))
.Select(x => x.GetViewModel)
.ToList();*/
var filteredList = context.Documents.Where(x => x.DocumentName.Contains(model.DocumentName)).ToList();
return filteredList.Select(x=> x.GetViewModel).ToList();
}
public List<DocumentViewModel> GetFullList()
{
using var context = new LawFirmDatabase();
//return context.Documents.Select(x => x.GetViewModel).ToList();
var fullList = context.Documents.ToList();
return fullList.Select(x => x.GetViewModel).ToList();
}
public DocumentViewModel? Insert(DocumentBindingModel model)
{
using var context = new LawFirmDatabase();
using var transaction = context.Database.BeginTransaction();
{
try
{
var newDoc = Document.Create(context, model);
if (newDoc == null)
{
transaction.Rollback();
return null;
}
context.Documents.Add(newDoc);
context.SaveChanges();
context.Database.CommitTransaction();
return newDoc.GetViewModel;
}
catch (Exception)
{
transaction.Rollback();
return null;
}
}
}
public DocumentViewModel? Update(DocumentBindingModel model)
{
using var context = new LawFirmDatabase();
using var transaction = context.Database.BeginTransaction();
{
try
{
var document = context.Documents.FirstOrDefault(x => x.Id == model.Id);
if (document == null)
{
transaction.Rollback();
return null;
}
document.Update(model);
context.SaveChanges();
context.Database.CommitTransaction();
return document.GetViewModel;
}
catch (Exception)
{
transaction.Rollback();
return null;
}
}
}
public DocumentViewModel? Delete(DocumentBindingModel model)
{
using var context = new LawFirmDatabase();
var element = context.Documents.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Documents.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}

View File

@ -0,0 +1,83 @@
using LawFirmContracts.BindingModels;
using LawFirmContracts.SearchModels;
using LawFirmContracts.StorageContracts;
using LawFirmContracts.ViewModels;
using LawFirmDatabaseImplement.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LawFirmDatabaseImplement.Implements
{
public class OrderStorage : IOrderStorage
{
public OrderViewModel? GetElement(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new LawFirmDatabase();
return context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
if (!model.Id.HasValue)
{
return new();
}
using var context = new LawFirmDatabase();
return context.Orders
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
public List<OrderViewModel> GetFullList()
{
using var context = new LawFirmDatabase();
return context.Orders.Select(x => x.GetViewModel).ToList();
}
public OrderViewModel? Insert(OrderBindingModel model)
{
var newOrder = Order.Create(model);
if (newOrder == null)
{
return null;
}
using var context = new LawFirmDatabase();
context.Orders.Add(newOrder);
context.SaveChanges();
return newOrder.GetViewModel;
}
public OrderViewModel? Update(OrderBindingModel model)
{
using var context = new LawFirmDatabase();
var order = context.Orders.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return order.GetViewModel;
}
public OrderViewModel? Delete(OrderBindingModel model)
{
using var context = new LawFirmDatabase();
var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Orders.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}