Add question image media support
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
using MongoDB.Driver;
|
||||
using QuestionsApi.Models;
|
||||
|
||||
namespace QuestionsApi.Services;
|
||||
|
||||
public class QuestionMediaStore
|
||||
{
|
||||
private readonly IMongoCollection<QuestionMediaAsset> _col;
|
||||
|
||||
public QuestionMediaStore(IConfiguration cfg)
|
||||
{
|
||||
var cs = cfg["MongoDB:ConnectionString"] ?? "mongodb://127.0.0.1:27017";
|
||||
var dbName = cfg["MongoDB:DatabaseName"] ?? "GameDb";
|
||||
var client = new MongoClient(cs);
|
||||
var db = client.GetDatabase(dbName);
|
||||
_col = db.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) =>
|
||||
await _col.Find(asset => asset.Id == id).FirstOrDefaultAsync();
|
||||
}
|
||||
Reference in New Issue
Block a user