2023-02-11 17:28:24 +04:00
|
|
|
|
using LawFirmContracts.BindingModels;
|
|
|
|
|
using LawFirmContracts.SearchModels;
|
|
|
|
|
using LawFirmContracts.StorageContracts;
|
|
|
|
|
using LawFirmContracts.ViewModels;
|
|
|
|
|
using LawFirmFileImplement.Models;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2023-02-26 23:20:43 +04:00
|
|
|
|
using System.Xml.Linq;
|
2023-02-11 17:28:24 +04:00
|
|
|
|
|
|
|
|
|
namespace LawFirmFileImplement.Implements
|
|
|
|
|
{
|
2023-02-11 17:41:53 +04:00
|
|
|
|
public class DocumentStorage : IDocumentStorage
|
2023-02-11 17:28:24 +04:00
|
|
|
|
{
|
|
|
|
|
private readonly DataFileSingleton source;
|
|
|
|
|
|
|
|
|
|
public DocumentStorage()
|
|
|
|
|
{
|
|
|
|
|
source = DataFileSingleton.GetInstance();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DocumentViewModel? GetElement(DocumentSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.DocumentName) && !model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return source.Documents
|
|
|
|
|
.FirstOrDefault(x => (!string.IsNullOrEmpty(model.DocumentName) && x.DocumentName == model.DocumentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<DocumentViewModel> GetFilteredList(DocumentSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(model.DocumentName))
|
|
|
|
|
{
|
|
|
|
|
return new();
|
|
|
|
|
}
|
|
|
|
|
return source.Documents
|
|
|
|
|
.Where(x => x.DocumentName.Contains(model.DocumentName))
|
|
|
|
|
.Select(x => x.GetViewModel)
|
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<DocumentViewModel> GetFullList()
|
|
|
|
|
{
|
|
|
|
|
return source.Documents.Select(x => x.GetViewModel).ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DocumentViewModel? Insert(DocumentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
model.Id = source.Documents.Count > 0 ? source.Documents.Max(x => x.Id) + 1 : 1;
|
|
|
|
|
var newDoc = Document.Create(model);
|
|
|
|
|
if (newDoc == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
source.Documents.Add(newDoc);
|
|
|
|
|
source.SaveDocuments();
|
|
|
|
|
return newDoc.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public DocumentViewModel? Update(DocumentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var document = source.Documents.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (document == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
document.Update(model);
|
|
|
|
|
source.SaveDocuments();
|
|
|
|
|
return document.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
public DocumentViewModel? Delete(DocumentBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var document = source.Documents.FirstOrDefault(x => x.Id == model.Id);
|
|
|
|
|
if (document == null)
|
|
|
|
|
{
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2023-02-26 23:20:43 +04:00
|
|
|
|
source.Documents.Remove(document);
|
2023-02-11 17:28:24 +04:00
|
|
|
|
source.SaveDocuments();
|
|
|
|
|
return document.GetViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|