2021-08-09 15:41:42 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2022-01-18 11:04:15 +05:00
|
|
|
|
using AsbCloudApp.Exceptions;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
2021-08-13 17:26:19 +05:00
|
|
|
|
using Mapster;
|
2021-08-09 15:41:42 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
using System.IO;
|
2021-08-09 15:41:42 +05:00
|
|
|
|
using System.Linq;
|
2021-08-11 16:54:42 +05:00
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
|
{
|
|
|
|
|
public class FileService : IFileService
|
|
|
|
|
{
|
|
|
|
|
public string RootPath { get; private set; }
|
2021-08-31 18:01:26 +05:00
|
|
|
|
|
|
|
|
|
private readonly IQueryable<AsbCloudDb.Model.FileInfo> dbSetConfigured;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
|
|
2021-11-17 13:06:48 +05:00
|
|
|
|
public FileService(IAsbCloudDbContext db)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
|
|
|
|
RootPath = "files";
|
|
|
|
|
this.db = db;
|
2021-08-31 18:01:26 +05:00
|
|
|
|
dbSetConfigured = db.Files
|
|
|
|
|
.Include(f => f.Author)
|
|
|
|
|
.ThenInclude(u => u.Company)
|
2021-11-09 17:36:44 +05:00
|
|
|
|
.ThenInclude(c => c.CompanyType)
|
|
|
|
|
.Include(f => f.FileMarks)
|
2022-01-05 17:50:45 +05:00
|
|
|
|
.ThenInclude(m => m.User)
|
|
|
|
|
.Include(f=>f.Well);
|
2021-11-09 17:36:44 +05:00
|
|
|
|
}
|
2021-10-27 17:00:27 +05:00
|
|
|
|
|
2021-11-17 13:06:48 +05:00
|
|
|
|
public async Task<string> GetSharedUrlAsync(int idFileInfo, int idUser, IFileShareService fileShareService,
|
2021-11-09 17:36:44 +05:00
|
|
|
|
CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var fileInfo = await GetInfoAsync(idFileInfo, token);
|
|
|
|
|
if (fileInfo is null)
|
|
|
|
|
return null;
|
2021-11-17 13:06:48 +05:00
|
|
|
|
var sharedUrl = await GetSharedUrlAsync(fileInfo, idUser, fileShareService, token);
|
2021-11-09 17:36:44 +05:00
|
|
|
|
return sharedUrl;
|
2021-10-27 17:00:27 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-17 13:06:48 +05:00
|
|
|
|
public async Task<string> GetSharedUrlAsync( FileInfoDto fileInfo, int idUser, IFileShareService fileShareService,
|
2021-11-03 15:53:31 +05:00
|
|
|
|
CancellationToken token)
|
2021-10-28 15:08:06 +05:00
|
|
|
|
{
|
2021-10-29 16:03:24 +05:00
|
|
|
|
var fileWebUrl = fileInfo.PublishInfo?.WebStorageFileUrl;
|
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(fileWebUrl))
|
|
|
|
|
return fileWebUrl;
|
|
|
|
|
|
|
|
|
|
var relativePath = GetUrl(fileInfo);
|
2021-11-09 17:36:44 +05:00
|
|
|
|
var sharedUrl = await fileShareService.PublishFileToCloudAsync(relativePath,
|
2021-10-29 16:03:24 +05:00
|
|
|
|
fileInfo.Name, token);
|
2021-10-28 15:08:06 +05:00
|
|
|
|
|
2021-11-09 17:36:44 +05:00
|
|
|
|
await SaveWeblinkToFileInfo(fileInfo.Id, idUser, sharedUrl, token);
|
|
|
|
|
|
|
|
|
|
return sharedUrl;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-03 14:12:39 +05:00
|
|
|
|
public async Task<FileInfoDto> MoveAsync(int idWell, int? idUser, int idCategory,
|
2021-11-09 17:36:44 +05:00
|
|
|
|
string destinationFileName, string srcFilePath, CancellationToken token = default)
|
2021-09-23 11:55:25 +05:00
|
|
|
|
{
|
2021-09-23 14:36:05 +05:00
|
|
|
|
destinationFileName = Path.GetFileName(destinationFileName);
|
|
|
|
|
srcFilePath = Path.GetFullPath(srcFilePath);
|
|
|
|
|
if (!File.Exists(srcFilePath))
|
2022-01-18 11:04:15 +05:00
|
|
|
|
throw new ArgumentInvalidException($"file {srcFilePath} doesn't exist", nameof(srcFilePath));
|
2021-09-23 11:55:25 +05:00
|
|
|
|
|
2021-09-23 14:36:05 +05:00
|
|
|
|
var sysFileInfo = new System.IO.FileInfo(srcFilePath);
|
2021-09-23 11:55:25 +05:00
|
|
|
|
|
|
|
|
|
//save info to db
|
|
|
|
|
var fileInfo = new AsbCloudDb.Model.FileInfo()
|
|
|
|
|
{
|
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IdAuthor = idUser,
|
|
|
|
|
IdCategory = idCategory,
|
2021-09-23 14:36:05 +05:00
|
|
|
|
Name = destinationFileName,
|
2022-01-05 17:50:45 +05:00
|
|
|
|
UploadDate = DateTime.UtcNow,
|
2021-09-23 11:55:25 +05:00
|
|
|
|
IsDeleted = false,
|
|
|
|
|
Size = sysFileInfo.Length,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var entry = db.Files.Add(fileInfo);
|
|
|
|
|
await db.SaveChangesAsync(token).ConfigureAwait(false);
|
|
|
|
|
var fileId = entry.Entity.Id;
|
|
|
|
|
string filePath = MakeFilePath(idWell, idCategory, destinationFileName, fileId);
|
2021-09-23 14:36:05 +05:00
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
|
|
|
|
|
File.Move(srcFilePath, filePath);
|
2021-09-23 11:55:25 +05:00
|
|
|
|
|
2022-01-05 17:50:45 +05:00
|
|
|
|
return await GetInfoAsync(entry.Entity.Id, token);
|
2021-09-23 11:55:25 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-03 14:12:39 +05:00
|
|
|
|
public async Task<FileInfoDto> SaveAsync(int idWell, int? idUser, int idCategory,
|
2021-11-03 15:53:31 +05:00
|
|
|
|
string fileFullName, Stream fileStream, CancellationToken token)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
2021-08-29 17:25:16 +05:00
|
|
|
|
//save info to db
|
|
|
|
|
var fileInfo = new AsbCloudDb.Model.FileInfo()
|
2021-08-09 15:41:42 +05:00
|
|
|
|
{
|
2021-08-29 17:25:16 +05:00
|
|
|
|
IdWell = idWell,
|
|
|
|
|
IdAuthor = idUser,
|
|
|
|
|
IdCategory = idCategory,
|
|
|
|
|
Name = Path.GetFileName(fileFullName),
|
2022-01-05 17:50:45 +05:00
|
|
|
|
UploadDate = DateTime.UtcNow,
|
2021-08-29 17:25:16 +05:00
|
|
|
|
IsDeleted = false,
|
2021-09-23 11:37:57 +05:00
|
|
|
|
Size = fileStream?.Length ?? 0
|
2021-08-29 17:25:16 +05:00
|
|
|
|
};
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2021-08-29 17:25:16 +05:00
|
|
|
|
var entry = db.Files.Add(fileInfo);
|
2021-09-23 11:55:25 +05:00
|
|
|
|
await db.SaveChangesAsync(token).ConfigureAwait(false);
|
2021-08-29 17:25:16 +05:00
|
|
|
|
var fileId = entry.Entity.Id;
|
|
|
|
|
//save stream to disk
|
2021-09-23 11:55:25 +05:00
|
|
|
|
string filePath = MakeFilePath(idWell, idCategory, fileFullName, fileId);
|
2021-08-19 16:32:04 +05:00
|
|
|
|
|
2021-09-23 11:55:25 +05:00
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(filePath));
|
2021-08-19 16:32:04 +05:00
|
|
|
|
|
2021-09-23 11:55:25 +05:00
|
|
|
|
using var newfileStream = new FileStream(filePath, FileMode.Create);
|
|
|
|
|
await fileStream.CopyToAsync(newfileStream, token).ConfigureAwait(false);
|
2022-01-05 17:50:45 +05:00
|
|
|
|
|
|
|
|
|
return await GetInfoAsync(entry.Entity.Id, token);
|
2021-08-29 17:25:16 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-23 11:55:25 +05:00
|
|
|
|
private string MakeFilePath(int idWell, int idCategory, string fileFullName, int fileId)
|
|
|
|
|
{
|
|
|
|
|
return Path.Combine(RootPath, $"{idWell}",
|
|
|
|
|
$"{idCategory}", $"{fileId}" + $"{Path.GetExtension(fileFullName)}");
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-29 17:25:16 +05:00
|
|
|
|
public async Task<IEnumerable<FileInfoDto>> GetInfosByCategoryAsync(int idWell,
|
2021-11-03 15:53:31 +05:00
|
|
|
|
int idCategory, CancellationToken token)
|
2021-08-29 17:25:16 +05:00
|
|
|
|
{
|
2021-08-31 18:01:26 +05:00
|
|
|
|
var entities = await dbSetConfigured
|
2021-10-18 17:38:07 +05:00
|
|
|
|
.Where(e => e.IdWell == idWell && e.IdCategory == idCategory && e.IsDeleted == false)
|
2021-08-29 17:25:16 +05:00
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2022-01-05 17:50:45 +05:00
|
|
|
|
var dtos = entities.Select(e => Convert(e));
|
2021-08-29 17:25:16 +05:00
|
|
|
|
return dtos;
|
2021-08-19 16:32:04 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-29 17:25:16 +05:00
|
|
|
|
public async Task<PaginationContainer<FileInfoDto>> GetInfosAsync(int idWell,
|
2021-08-31 18:01:26 +05:00
|
|
|
|
int idCategory, string companyName = default, string fileName = default, DateTime begin = default,
|
2021-08-20 11:20:24 +05:00
|
|
|
|
DateTime end = default, int skip = 0, int take = 32, CancellationToken token = default)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
2021-08-31 18:01:26 +05:00
|
|
|
|
var query = dbSetConfigured
|
2021-11-02 13:44:45 +05:00
|
|
|
|
.Where(e => e.IdWell == idWell &&
|
2021-11-03 15:53:31 +05:00
|
|
|
|
e.IdCategory == idCategory &&
|
|
|
|
|
!e.IsDeleted);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
2021-08-31 18:01:26 +05:00
|
|
|
|
if (!string.IsNullOrEmpty(companyName))
|
2021-11-09 17:36:44 +05:00
|
|
|
|
query = query.Where(e => (e.Author == null) ||
|
2021-11-03 15:53:31 +05:00
|
|
|
|
(e.Author.Company == null) ||
|
|
|
|
|
e.Author.Company.Caption.Contains(companyName));
|
2021-08-20 11:20:24 +05:00
|
|
|
|
|
2021-08-31 09:59:23 +05:00
|
|
|
|
if (!string.IsNullOrEmpty(fileName))
|
2021-08-31 12:29:27 +05:00
|
|
|
|
query = query.Where(e => e.Name.ToLower().Contains(fileName.ToLower()));
|
2021-08-31 09:59:23 +05:00
|
|
|
|
|
2022-01-05 17:50:45 +05:00
|
|
|
|
var firstFile = await query.FirstOrDefaultAsync(token);
|
|
|
|
|
if (firstFile is null)
|
|
|
|
|
return new PaginationContainer<FileInfoDto>()
|
|
|
|
|
{
|
|
|
|
|
Skip = skip,
|
|
|
|
|
Take = take,
|
|
|
|
|
Count = 0,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var timezoneOffset = firstFile.Well.Timezone?.Hours ?? 5;
|
|
|
|
|
|
2021-07-26 11:54:50 +05:00
|
|
|
|
if (begin != default)
|
2022-01-05 17:50:45 +05:00
|
|
|
|
{
|
|
|
|
|
var beginUtc = begin.ToUtcDateTimeOffset(timezoneOffset);
|
|
|
|
|
query = query.Where(e => e.UploadDate >= beginUtc);
|
|
|
|
|
}
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
|
|
|
|
if (end != default)
|
2022-01-05 17:50:45 +05:00
|
|
|
|
{
|
|
|
|
|
var endUtc = end.ToUtcDateTimeOffset(timezoneOffset);
|
|
|
|
|
query = query.Where(e => e.UploadDate <= endUtc);
|
|
|
|
|
}
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
2021-08-13 17:26:19 +05:00
|
|
|
|
var count = await query.CountAsync(token).ConfigureAwait(false);
|
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
var result = new PaginationContainer<FileInfoDto>(count)
|
|
|
|
|
{
|
|
|
|
|
Skip = skip,
|
2021-08-13 17:26:19 +05:00
|
|
|
|
Take = take,
|
|
|
|
|
Count = count,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (count <= skip)
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
query = query.OrderBy(e => e.UploadDate);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
|
|
|
|
if (skip > 0)
|
2021-08-13 17:26:19 +05:00
|
|
|
|
query = query.Skip(skip);
|
|
|
|
|
query = query.Take(take);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
2021-08-13 17:26:19 +05:00
|
|
|
|
var entities = await query
|
|
|
|
|
.Take(take).AsNoTracking().ToListAsync(token)
|
2021-08-11 17:26:02 +05:00
|
|
|
|
.ConfigureAwait(false);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
2022-01-05 17:50:45 +05:00
|
|
|
|
var dtos = entities.Select(e => Convert(e, timezoneOffset));
|
2021-08-31 12:29:27 +05:00
|
|
|
|
result.Items.AddRange(dtos);
|
2021-07-26 11:54:50 +05:00
|
|
|
|
return result;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-09 17:36:44 +05:00
|
|
|
|
public async Task<FileInfoDto> GetInfoAsync(int idFile,
|
2021-11-03 15:53:31 +05:00
|
|
|
|
CancellationToken token)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
2021-08-31 18:01:26 +05:00
|
|
|
|
var entity = await dbSetConfigured
|
2021-08-13 17:26:19 +05:00
|
|
|
|
.AsNoTracking()
|
2021-11-09 17:36:44 +05:00
|
|
|
|
.FirstOrDefaultAsync(f => f.Id == idFile, token)
|
2021-08-11 17:26:02 +05:00
|
|
|
|
.ConfigureAwait(false);
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2021-08-13 17:26:19 +05:00
|
|
|
|
if (entity is null)
|
2022-02-08 10:25:05 +05:00
|
|
|
|
{
|
|
|
|
|
throw new FileNotFoundException($"fileId:{idFile} not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var ext = Path.GetExtension(entity.Name);
|
|
|
|
|
|
|
|
|
|
var relativePath = GetUrl(entity.IdWell, entity.IdCategory, entity.Id, ext);
|
|
|
|
|
var fullPath = Path.GetFullPath(relativePath);
|
|
|
|
|
if (! File.Exists(fullPath))
|
|
|
|
|
{
|
|
|
|
|
throw new FileNotFoundException("not found", relativePath);
|
|
|
|
|
}
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
2022-01-05 17:50:45 +05:00
|
|
|
|
var dto = Convert(entity);
|
2021-08-13 17:26:19 +05:00
|
|
|
|
return dto;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
2021-08-19 16:58:26 +05:00
|
|
|
|
|
2021-08-29 17:25:16 +05:00
|
|
|
|
public async Task<int> MarkAsDeletedAsync(int idFile,
|
2021-08-19 16:58:26 +05:00
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
2021-08-29 17:25:16 +05:00
|
|
|
|
var fileInfo = await db.Files.FirstOrDefaultAsync(f => f.Id == idFile, token).ConfigureAwait(false);
|
2021-08-19 16:58:26 +05:00
|
|
|
|
|
|
|
|
|
if (fileInfo is null)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
fileInfo.IsDeleted = true;
|
|
|
|
|
|
2021-08-29 17:25:16 +05:00
|
|
|
|
return await db.SaveChangesAsync(token).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-06 18:07:35 +05:00
|
|
|
|
public async Task<int> DeleteAsync(int idFile, CancellationToken token)
|
2021-08-29 17:25:16 +05:00
|
|
|
|
{
|
|
|
|
|
var fileInfo = await db.Files
|
|
|
|
|
.FirstOrDefaultAsync(f => f.Id == idFile, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (fileInfo is null)
|
|
|
|
|
return 0;
|
|
|
|
|
|
2021-09-23 10:53:48 +05:00
|
|
|
|
var fileName = GetUrl(fileInfo.Adapt<FileInfoDto>());
|
2021-08-29 17:25:16 +05:00
|
|
|
|
if (File.Exists(fileName))
|
|
|
|
|
File.Delete(fileName);
|
|
|
|
|
|
|
|
|
|
db.Files.Remove(fileInfo);
|
|
|
|
|
return await db.SaveChangesAsync(token).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-23 10:53:48 +05:00
|
|
|
|
public string GetUrl(int idFile)
|
2021-08-29 17:25:16 +05:00
|
|
|
|
{
|
2021-09-23 10:53:48 +05:00
|
|
|
|
var fileInfo = db.Files
|
|
|
|
|
.FirstOrDefault(f => f.Id == idFile);
|
|
|
|
|
|
|
|
|
|
if (fileInfo is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return GetUrl(fileInfo.IdWell, fileInfo.IdCategory, fileInfo.Id, Path.GetExtension(fileInfo.Name));
|
2021-08-19 16:58:26 +05:00
|
|
|
|
}
|
2021-09-23 10:53:48 +05:00
|
|
|
|
|
2021-11-09 17:36:44 +05:00
|
|
|
|
public string GetUrl(FileInfoDto fileInfo) =>
|
|
|
|
|
GetUrl(fileInfo.IdWell, fileInfo.IdCategory, fileInfo.Id, Path.GetExtension(fileInfo.Name));
|
|
|
|
|
|
2021-09-23 10:53:48 +05:00
|
|
|
|
public string GetUrl(int idWell, int idCategory, int idFile, string dotExtention) =>
|
|
|
|
|
Path.Combine(RootPath, idWell.ToString(), idCategory.ToString(), $"{idFile}{dotExtention}");
|
2021-11-01 16:41:25 +05:00
|
|
|
|
|
2021-11-09 17:36:44 +05:00
|
|
|
|
public async Task<FileInfoDto> GetByMarkId(int idMark,
|
|
|
|
|
CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var entity = await dbSetConfigured
|
|
|
|
|
.FirstOrDefaultAsync(f => f.FileMarks.Any(m => m.Id == idMark), token)
|
|
|
|
|
.ConfigureAwait(false);
|
2022-01-05 17:50:45 +05:00
|
|
|
|
|
|
|
|
|
FileInfoDto dto = Convert(entity);
|
2021-11-09 17:36:44 +05:00
|
|
|
|
return dto;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 17:50:45 +05:00
|
|
|
|
private static FileInfoDto Convert(AsbCloudDb.Model.FileInfo entity)
|
|
|
|
|
{
|
|
|
|
|
var timezoneOffset = entity.Well.Timezone?.Hours ?? 5;
|
|
|
|
|
return Convert(entity, timezoneOffset);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static FileInfoDto Convert(AsbCloudDb.Model.FileInfo entity, double timezoneOffset)
|
|
|
|
|
{
|
|
|
|
|
var dto = entity.Adapt<FileInfoDto>();
|
|
|
|
|
dto.UploadDate = entity.UploadDate.ToRemoteDateTime(timezoneOffset);
|
|
|
|
|
dto.FileMarks = entity.FileMarks.Select(m =>
|
|
|
|
|
{
|
|
|
|
|
var mark = m.Adapt<FileMarkDto>();
|
|
|
|
|
mark.DateCreated = m.DateCreated.ToRemoteDateTime(timezoneOffset);
|
|
|
|
|
return mark;
|
|
|
|
|
});
|
|
|
|
|
return dto;
|
|
|
|
|
}
|
2021-11-09 17:36:44 +05:00
|
|
|
|
public async Task<int> CreateFileMarkAsync(FileMarkDto fileMarkDto, int idUser, CancellationToken token)
|
2021-11-01 16:41:25 +05:00
|
|
|
|
{
|
|
|
|
|
var fileMark = await db.FileMarks
|
2021-11-09 17:36:44 +05:00
|
|
|
|
.FirstOrDefaultAsync(m => m.IdFile == fileMarkDto.IdFile &&
|
|
|
|
|
m.IdMarkType == fileMarkDto.IdMarkType &&
|
|
|
|
|
m.IdUser == idUser &&
|
|
|
|
|
m.IsDeleted == false,
|
|
|
|
|
token)
|
2021-11-01 16:41:25 +05:00
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (fileMark is not null)
|
2021-11-03 14:12:39 +05:00
|
|
|
|
return 0;
|
2021-11-01 16:41:25 +05:00
|
|
|
|
|
2021-11-09 17:36:44 +05:00
|
|
|
|
var newFileMark = fileMarkDto.Adapt<FileMark>();
|
|
|
|
|
newFileMark.Id = default;
|
2022-01-05 17:50:45 +05:00
|
|
|
|
newFileMark.DateCreated = DateTime.UtcNow;
|
2021-11-09 17:36:44 +05:00
|
|
|
|
newFileMark.IdUser = idUser;
|
|
|
|
|
|
2021-11-01 16:41:25 +05:00
|
|
|
|
db.FileMarks.Add(newFileMark);
|
2021-11-03 14:12:39 +05:00
|
|
|
|
return await db.SaveChangesAsync(token);
|
2021-11-01 16:41:25 +05:00
|
|
|
|
}
|
|
|
|
|
|
2021-11-09 17:36:44 +05:00
|
|
|
|
public async Task<int> MarkFileMarkAsDeletedAsync(int idMark,
|
2021-11-03 15:53:31 +05:00
|
|
|
|
CancellationToken token)
|
2021-10-29 12:47:18 +05:00
|
|
|
|
{
|
2021-11-09 17:36:44 +05:00
|
|
|
|
var fileMark = await db.FileMarks
|
|
|
|
|
.FirstOrDefaultAsync(m => m.Id == idMark, token)
|
2021-10-29 12:47:18 +05:00
|
|
|
|
.ConfigureAwait(false);
|
2021-11-09 17:36:44 +05:00
|
|
|
|
fileMark.IsDeleted = true;
|
|
|
|
|
return await db.SaveChangesAsync(token);
|
|
|
|
|
}
|
2021-10-29 12:47:18 +05:00
|
|
|
|
|
2021-11-09 17:36:44 +05:00
|
|
|
|
private async Task<int> SaveWeblinkToFileInfo(int idFileInfo, int idUser, string weblink,
|
2021-11-03 15:53:31 +05:00
|
|
|
|
CancellationToken token)
|
2021-10-29 12:47:18 +05:00
|
|
|
|
{
|
|
|
|
|
var fileInfo = await db.Files.FirstOrDefaultAsync(f => f.Id == idFileInfo, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
fileInfo.PublishInfo = new FilePublishInfo()
|
|
|
|
|
{
|
2021-11-09 17:36:44 +05:00
|
|
|
|
IdPublisher = idUser,
|
2022-01-05 17:50:45 +05:00
|
|
|
|
Date = DateTime.UtcNow,
|
2021-10-29 12:47:18 +05:00
|
|
|
|
WebStorageFileUrl = weblink
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return await db.SaveChangesAsync(token).ConfigureAwait(false);
|
|
|
|
|
}
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
|
|
|
|
}
|