DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/HelpPageService.cs
Дмитрий Степанов cd279b925f Справки по страницам
1. Добавил модель данных
2. Добавил Dto для справки
3. Добавил доменный сервис + сделал покрытие тестами
4. Добавил репозиторий для справки
5. Сделал регистрацию зависимостей
6. Добавил контроллер содержащий методы: создания, редактирования, получения файла справки
2023-06-28 16:33:27 +05:00

169 lines
5.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using AsbCloudApp.Data;
using AsbCloudApp.Repositories;
using AsbCloudApp.Services;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Exceptions;
namespace AsbCloudInfrastructure.Services;
/// <summary>
/// Реализация сервиса справок страниц
/// </summary>
public class HelpPageService : IHelpPageService
{
private readonly string directoryNameHelpPageFiles;
private readonly IHelpPageRepository helpPageRepository;
private readonly IFileStorageRepository fileStorageRepository;
/// <summary>
/// Конструктор класса
/// </summary>
/// <param name="helpPageRepository"></param>
/// <param name="fileStorageRepository"></param>
/// <param name="directoryNameHelpPageFiles"></param>
public HelpPageService(IHelpPageRepository helpPageRepository,
IFileStorageRepository fileStorageRepository,
string directoryNameHelpPageFiles)
{
if (string.IsNullOrWhiteSpace(directoryNameHelpPageFiles))
throw new ArgumentException("Value cannot be null or whitespace", nameof(this.directoryNameHelpPageFiles));
this.helpPageRepository = helpPageRepository;
this.fileStorageRepository = fileStorageRepository;
this.directoryNameHelpPageFiles = directoryNameHelpPageFiles;
}
/// <summary>
/// Создание справки страницы
/// </summary>
/// <param name="urlPage"></param>
/// <param name="idCategory"></param>
/// <param name="fileName"></param>
/// <param name="fileStream"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task<int> CreateAsync(string urlPage,
int idCategory,
string fileName,
Stream fileStream,
CancellationToken cancellationToken)
{
if (await helpPageRepository.IsCheckHelpPageWithUrlPageAndIdCategoryAsync(urlPage,
idCategory,
cancellationToken))
{
throw new ArgumentsInvalidException("Справка с такой категории файла для данной страницы уже существует",
new[] { nameof(urlPage), nameof(idCategory) });
}
HelpPageDto helpPage = new()
{
UrlPage = urlPage,
IdCategory = idCategory,
Name = Path.GetFileName(fileName),
Size = fileStream.Length,
};
int idFile = await helpPageRepository.InsertAsync(helpPage,
cancellationToken);
await SaveFileAsync(idCategory,
fileName,
fileStream,
idFile,
cancellationToken);
return idFile;
}
/// <summary>
/// Обновление справки страницы
/// </summary>
/// <param name="helpPage"></param>
/// <param name="idCategory"></param>
/// <param name="fileName"></param>
/// <param name="fileStream"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public async Task UpdateAsync(HelpPageDto helpPage,
int idCategory,
string fileName,
Stream fileStream,
CancellationToken cancellationToken)
{
helpPage.Name = Path.GetFileName(fileName);
helpPage.IdCategory = idCategory;
helpPage.Size = fileStream.Length;
string fileFullName = fileStorageRepository.GetFilePath(directoryNameHelpPageFiles,
idCategory.ToString(),
helpPage.Id,
Path.GetExtension(helpPage.Name));
await helpPageRepository.UpdateAsync(helpPage,
cancellationToken);
fileStorageRepository.DeleteFile(fileFullName);
await SaveFileAsync(helpPage.IdCategory,
fileName,
fileStream,
helpPage.Id,
cancellationToken);
}
/// <summary>
/// Получение справки по url страницы и id категории
/// </summary>
/// <param name="urlPage"></param>
/// <param name="idCategory"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Task<HelpPageDto?> GetOrDefaultByUrlPageAndIdCategoryAsync(string urlPage,
int idCategory,
CancellationToken cancellationToken) =>
helpPageRepository.GetOrDefaultByUrlPageAndIdCategoryAsync(urlPage,
idCategory,
cancellationToken);
public Task<HelpPageDto?> GetOrDefaultByIdAsync(int id, CancellationToken cancellationToken) =>
helpPageRepository.GetOrDefaultAsync(id,
cancellationToken);
/// <summary>
/// Получение файлового потока для файла справки
/// </summary>
/// <param name="helpPage"></param>
/// <param name="cancellationToken"></param>
/// <returns></returns>
public Stream GetFileStream(HelpPageDto helpPage)
{
string filePath = fileStorageRepository.GetFilePath(directoryNameHelpPageFiles,
helpPage.IdCategory.ToString(),
helpPage.Id,
Path.GetExtension(helpPage.Name));;
var fileStream = new FileStream(Path.GetFullPath(filePath), FileMode.Open);
return fileStream;
}
private async Task SaveFileAsync(int idCategory,
string fileName,
Stream fileStream,
int fileId,
CancellationToken cancellationToken = default)
{
string filePath = fileStorageRepository.MakeFilePath(directoryNameHelpPageFiles,
idCategory.ToString(),
$"{fileId}" + $"{Path.GetExtension(fileName)}");
await fileStorageRepository.SaveFileAsync(filePath,
fileStream,
cancellationToken);
}
}