2021-08-09 15:41:42 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
|
|
|
|
using AsbCloudDb.Model;
|
2021-08-09 15:41:42 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services
|
|
|
|
|
{
|
|
|
|
|
public class FileService : IFileService
|
|
|
|
|
{
|
|
|
|
|
public string RootPath { get; private set; }
|
|
|
|
|
private readonly IAsbCloudDbContext db;
|
|
|
|
|
|
2021-08-09 15:41:42 +05:00
|
|
|
|
public FileService(IAsbCloudDbContext db)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
|
|
|
|
RootPath = "files";
|
|
|
|
|
this.db = db;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-09 15:41:42 +05:00
|
|
|
|
public IDictionary<string, int> SaveFilesPropertiesToDb(int idWell, int idCategory,
|
2021-07-26 11:54:50 +05:00
|
|
|
|
IEnumerable<(string fileName, int idWell, int idCategory, DateTime date, int idUser)> filesInfo)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
|
|
|
|
var fileIdsToNames = new Dictionary<string, int>();
|
|
|
|
|
|
2021-08-09 15:41:42 +05:00
|
|
|
|
foreach (var fileInfo in filesInfo)
|
|
|
|
|
{
|
2021-07-23 17:40:31 +05:00
|
|
|
|
var file = new File()
|
|
|
|
|
{
|
|
|
|
|
Name = fileInfo.fileName,
|
2021-07-26 11:54:50 +05:00
|
|
|
|
IdWell = fileInfo.idWell,
|
2021-07-23 17:40:31 +05:00
|
|
|
|
IdCategory = fileInfo.idCategory,
|
|
|
|
|
Date = fileInfo.date,
|
2021-07-27 16:55:32 +05:00
|
|
|
|
IdAuthor = fileInfo.idUser
|
2021-07-23 17:40:31 +05:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
db.Files.Add(file);
|
|
|
|
|
db.SaveChanges();
|
|
|
|
|
fileIdsToNames.Add(file.Name, file.Id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return fileIdsToNames;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-27 14:43:30 +05:00
|
|
|
|
public PaginationContainer<FilePropertiesDto> GetFilesInfo(int idWell,
|
2021-08-09 15:41:42 +05:00
|
|
|
|
int idCategory, DateTime begin = default, DateTime end = default,
|
2021-07-26 11:54:50 +05:00
|
|
|
|
int skip = 0, int take = 32)
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
2021-07-26 17:26:07 +05:00
|
|
|
|
var filesInfoQuery = db.Files.Include(f => f.User)
|
2021-07-27 14:43:30 +05:00
|
|
|
|
.Where(f => f.IdWell == idWell &&
|
2021-08-11 10:16:01 +05:00
|
|
|
|
f.IdCategory == idCategory).AsNoTracking();
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
2021-07-26 17:26:07 +05:00
|
|
|
|
if (!filesInfoQuery.Any())
|
2021-07-26 11:54:50 +05:00
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var result = new PaginationContainer<FilePropertiesDto>() { Skip = skip, Take = take };
|
|
|
|
|
|
|
|
|
|
if (begin != default)
|
2021-08-11 10:16:01 +05:00
|
|
|
|
filesInfoQuery = filesInfoQuery.Where(m => m.Date >= begin).AsNoTracking();
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
|
|
|
|
if (end != default)
|
2021-08-11 10:16:01 +05:00
|
|
|
|
filesInfoQuery = filesInfoQuery.Where(m => m.Date <= end).AsNoTracking();
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
2021-07-26 17:26:07 +05:00
|
|
|
|
result.Count = filesInfoQuery.Count();
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
|
|
|
|
if (skip > 0)
|
2021-08-11 10:16:01 +05:00
|
|
|
|
filesInfoQuery = filesInfoQuery.Skip(skip).AsNoTracking();
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
2021-07-27 16:55:32 +05:00
|
|
|
|
var filesInfoList = filesInfoQuery.OrderBy(f => f.Date).Take(take).ToList();
|
2021-07-26 11:54:50 +05:00
|
|
|
|
|
|
|
|
|
if (filesInfoList.Count == 0)
|
|
|
|
|
return result;
|
|
|
|
|
|
|
|
|
|
foreach (var fileInfo in filesInfoList)
|
|
|
|
|
{
|
|
|
|
|
var messageDto = new FilePropertiesDto
|
2021-07-23 17:40:31 +05:00
|
|
|
|
{
|
2021-07-26 11:54:50 +05:00
|
|
|
|
Id = fileInfo.Id,
|
|
|
|
|
Name = fileInfo.Name,
|
|
|
|
|
IdCategory = fileInfo.IdCategory,
|
|
|
|
|
UploadDate = fileInfo.Date,
|
|
|
|
|
UserName = fileInfo.User.Name
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
result.Items.Add(messageDto);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
2021-07-23 17:40:31 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public (int Id, string Name, int IdCategory)? GetFileInfo(int fileId)
|
|
|
|
|
{
|
2021-08-11 10:16:01 +05:00
|
|
|
|
var fileInfo = db.Files.AsNoTracking().FirstOrDefault(f => f.Id == fileId);
|
2021-07-23 17:40:31 +05:00
|
|
|
|
|
|
|
|
|
if (fileInfo is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
return (fileInfo.Id, fileInfo.Name, fileInfo.IdCategory);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|