Add sample in mongo and sql layers

This commit is contained in:
Sergio Matias Urquin
2025-05-18 14:38:54 -06:00
parent ca35645488
commit 10f2083590
18 changed files with 248 additions and 250 deletions

View File

@@ -1,18 +0,0 @@
using Core.Blueprint.DAL.Mongo.Entities.Collections;
using Core.Blueprint.DAL.Mongo.Entities.Requests;
namespace Core.Blueprint.DAL.Mongo.Contracts
{
public interface IBlueprintService
{
ValueTask<BlueprintCollection> CreateBlueprint(BlueprintRequest newBlueprint, CancellationToken cancellationToken);
ValueTask<BlueprintCollection> GetBlueprintById(string _id, CancellationToken cancellationToken);
ValueTask<IEnumerable<BlueprintCollection>> GetAllBlueprints(CancellationToken cancellationToken);
ValueTask<BlueprintCollection> UpdateBlueprint(string _id, BlueprintCollection entity, CancellationToken cancellationToken);
ValueTask<BlueprintCollection> DeleteBlueprint(string _id, CancellationToken cancellationToken);
}
}

View File

@@ -0,0 +1,18 @@
using Core.Blueprint.DAL.Mongo.Entities.Collections;
using Core.Blueprint.DAL.Mongo.Entities.Requests;
namespace Core.Blueprint.DAL.Mongo.Contracts
{
public interface IMongoSampleService
{
ValueTask<SampleCollection> CreateSample(SampleRequest newSample, CancellationToken cancellationToken);
ValueTask<SampleCollection> GetSampleById(string _id, CancellationToken cancellationToken);
ValueTask<IEnumerable<SampleCollection>> GetAllSamples(CancellationToken cancellationToken);
ValueTask<SampleCollection> UpdateSample(string _id, SampleCollection entity, CancellationToken cancellationToken);
ValueTask<SampleCollection> DeleteSample(string _id, CancellationToken cancellationToken);
}
}

View File

@@ -2,8 +2,8 @@
namespace Core.Blueprint.DAL.Mongo.Entities.Collections
{
[CollectionAttributeName("Blueprints")]
public class BlueprintCollection : Document
[CollectionAttributeName("Samples")]
public class SampleCollection : Document
{
public string Name { get; set; } = null!;
public string? Description { get; set; }

View File

@@ -1,6 +1,6 @@
namespace Core.Blueprint.DAL.Mongo.Entities.Requests
{
public class BlueprintRequest
public class SampleRequest
{
public string Name { get; set; } = null!;

View File

@@ -1,80 +0,0 @@
using Core.Blueprint.DAL.Mongo.Contracts;
using Core.Blueprint.DAL.Mongo.Entities.Collections;
using Core.Blueprint.DAL.Mongo.Entities.Requests;
using Core.Blueprint.Mongo;
using Core.Blueprint.Redis;
using Core.Blueprint.Redis.Helpers;
using Mapster;
using Microsoft.Extensions.Options;
namespace Core.Blueprint.DAL.Mongo.Service
{
public class BlueprintService : IBlueprintService
{
private readonly CollectionRepository<BlueprintCollection> repository;
private readonly CacheSettings cacheSettings;
private readonly IRedisCacheProvider cacheProvider;
public BlueprintService(CollectionRepository<BlueprintCollection> repository,
IRedisCacheProvider cacheProvider, IOptions<CacheSettings> cacheSettings)
{
this.repository = repository;
this.repository.CollectionInitialization();
this.cacheSettings = cacheSettings.Value;
this.cacheProvider = cacheProvider;
}
public async ValueTask<BlueprintCollection> CreateBlueprint(BlueprintRequest newBlueprint, CancellationToken cancellationToken)
{
var blueprintCollection = newBlueprint.Adapt<BlueprintCollection>();
await this.repository.InsertOneAsync(blueprintCollection);
return blueprintCollection;
}
public async ValueTask<BlueprintCollection> GetBlueprintById(string _id, CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetBlueprintById", _id);
var cachedData = await cacheProvider.GetAsync<BlueprintCollection>(cacheKey);
if (cachedData is not null) { return cachedData; }
var blueprint = await this.repository.FindByIdAsync(_id);
await cacheProvider.SetAsync(cacheKey, blueprint);
return blueprint;
}
public async ValueTask<IEnumerable<BlueprintCollection>> GetAllBlueprints(CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllBlueprints");
var cachedData = await cacheProvider.GetAsync<IEnumerable<BlueprintCollection>>(cacheKey) ?? [];
if (cachedData.Any()) return cachedData;
var blueprints = await this.repository.AsQueryable();
await cacheProvider.SetAsync(cacheKey, blueprints);
return blueprints;
}
public async ValueTask<BlueprintCollection> UpdateBlueprint(string _id, BlueprintCollection entity, CancellationToken cancellationToken)
{
await this.repository.ReplaceOneAsync(entity);
return entity;
}
public async ValueTask<BlueprintCollection> DeleteBlueprint(string _id, CancellationToken cancellationToken)
{
var entity = await this.repository.DeleteOneAsync(doc => doc._Id == _id);
return entity;
}
}
}

View File

@@ -0,0 +1,80 @@
using Core.Blueprint.DAL.Mongo.Contracts;
using Core.Blueprint.DAL.Mongo.Entities.Collections;
using Core.Blueprint.DAL.Mongo.Entities.Requests;
using Core.Blueprint.Mongo;
using Core.Blueprint.Redis;
using Core.Blueprint.Redis.Helpers;
using Mapster;
using Microsoft.Extensions.Options;
namespace Core.Blueprint.DAL.Mongo.Service
{
public class MongoSampleService : IMongoSampleService
{
private readonly CollectionRepository<SampleCollection> repository;
private readonly CacheSettings cacheSettings;
private readonly IRedisCacheProvider cacheProvider;
public MongoSampleService(CollectionRepository<SampleCollection> repository,
IRedisCacheProvider cacheProvider, IOptions<CacheSettings> cacheSettings)
{
this.repository = repository;
this.repository.CollectionInitialization();
this.cacheSettings = cacheSettings.Value;
this.cacheProvider = cacheProvider;
}
public async ValueTask<SampleCollection> CreateSample(SampleRequest newSample, CancellationToken cancellationToken)
{
var sampleCollection = newSample.Adapt<SampleCollection>();
await this.repository.InsertOneAsync(sampleCollection);
return sampleCollection;
}
public async ValueTask<SampleCollection> GetSampleById(string _id, CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetSampleById", _id);
var cachedData = await cacheProvider.GetAsync<SampleCollection>(cacheKey);
if (cachedData is not null) { return cachedData; }
var sample = await this.repository.FindByIdAsync(_id);
await cacheProvider.SetAsync(cacheKey, sample);
return sample;
}
public async ValueTask<IEnumerable<SampleCollection>> GetAllSamples(CancellationToken cancellationToken)
{
var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllSamples");
var cachedData = await cacheProvider.GetAsync<IEnumerable<SampleCollection>>(cacheKey) ?? [];
if (cachedData.Any()) return cachedData;
var samples = await this.repository.AsQueryable();
await cacheProvider.SetAsync(cacheKey, samples);
return samples;
}
public async ValueTask<SampleCollection> UpdateSample(string _id, SampleCollection entity, CancellationToken cancellationToken)
{
await this.repository.ReplaceOneAsync(entity);
return entity;
}
public async ValueTask<SampleCollection> DeleteSample(string _id, CancellationToken cancellationToken)
{
var entity = await this.repository.DeleteOneAsync(doc => doc._Id == _id);
return entity;
}
}
}