DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/FileService.cs

70 lines
2.1 KiB
C#

using System;
using System.Linq;
using System.Collections.Generic;
using AsbCloudApp.Data;
using AsbCloudApp.Services;
using AsbCloudDb.Model;
namespace AsbCloudInfrastructure.Services
{
public class FileService : IFileService
{
public string RootPath { get; private set; }
private readonly IAsbCloudDbContext db;
public FileService(IAsbCloudDbContext db)
{
RootPath = "files";
this.db = db;
}
public IDictionary<string, int> SaveFilesPropertiesToDb(int wellId, int idCategory,
IEnumerable<(string fileName, int idCategory, DateTime date, int idUser)> filesInfo)
{
var fileIdsToNames = new Dictionary<string, int>();
foreach(var fileInfo in filesInfo)
{
var file = new File()
{
Name = fileInfo.fileName,
IdCategory = fileInfo.idCategory,
Date = fileInfo.date,
IdUser = fileInfo.idUser
};
db.Files.Add(file);
db.SaveChanges();
fileIdsToNames.Add(file.Name, file.Id);
}
return fileIdsToNames;
}
public IEnumerable<FilePropertiesDto> GetFilesInfo(int wellId, int idCategory)
{
var fileInfoFromDb = db.Files.Where(f => f.IdWell == wellId &&
f.IdCategory == idCategory).Select(file => new FilePropertiesDto
{
Id = file.Id,
Name = file.Name,
IdCategory = file.IdCategory,
UploadDate = file.Date,
UserName = file.IdUser.ToString()
}).ToList();
return fileInfoFromDb;
}
public (int Id, string Name, int IdCategory)? GetFileInfo(int fileId)
{
var fileInfo = db.Files.FirstOrDefault(f => f.Id == fileId);
if (fileInfo is null)
return null;
return (fileInfo.Id, fileInfo.Name, fileInfo.IdCategory);
}
}
}