37 lines
988 B
C#
37 lines
988 B
C#
using MongoDB.Driver;
|
|
using MongoDB.Bson;
|
|
using QuestionsApi.Models;
|
|
|
|
namespace QuestionsApi.Services;
|
|
|
|
public class QuestionMediaStore
|
|
{
|
|
private readonly IMongoCollection<QuestionMediaAsset> _col;
|
|
|
|
public QuestionMediaStore(MongoDatabaseProvider mongo)
|
|
{
|
|
_col = mongo.Database.GetCollection<QuestionMediaAsset>("QuestionMediaAssets");
|
|
}
|
|
|
|
public async Task<QuestionMediaAsset> CreateAsync(string fileName, string contentType, byte[] bytes)
|
|
{
|
|
var asset = new QuestionMediaAsset
|
|
{
|
|
FileName = fileName.Trim(),
|
|
ContentType = contentType.Trim(),
|
|
Bytes = bytes,
|
|
CreatedUtc = DateTime.UtcNow
|
|
};
|
|
await _col.InsertOneAsync(asset);
|
|
return asset;
|
|
}
|
|
|
|
public async Task<QuestionMediaAsset?> GetAsync(string id)
|
|
{
|
|
if (!ObjectId.TryParse(id, out _))
|
|
return null;
|
|
|
|
return await _col.Find(asset => asset.Id == id).FirstOrDefaultAsync();
|
|
}
|
|
}
|