83 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			83 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using Core.Blueprint.DAL.SQLServer;
 | |
| using Core.Blueprint.DAL.SQLServer.Context;
 | |
| using Core.Blueprint.DAL.SQLServer.Contracts;
 | |
| using Core.Blueprint.DAL.SQLServer.Entities;
 | |
| using Core.Blueprint.DAL.SQLServer.Entities.Request;
 | |
| using Core.Blueprint.Redis;
 | |
| using Core.Blueprint.Redis.Helpers;
 | |
| using Mapster;
 | |
| using Microsoft.Extensions.Options;
 | |
| 
 | |
| public class SqlSampleService : ISqlSampleService
 | |
| {
 | |
|     private readonly IEntityRepository<Sample, SqlServerContext> _sqlSampleRepository;
 | |
|     private readonly CacheSettings cacheSettings;
 | |
|     private readonly IRedisCacheProvider cacheProvider;
 | |
| 
 | |
|     public SqlSampleService(IEntityRepository<Sample, SqlServerContext> sqlSampleRepository, IRedisCacheProvider cacheProvider, IOptions<CacheSettings> cacheSettings)
 | |
|     {
 | |
|         _sqlSampleRepository = sqlSampleRepository;
 | |
|         this.cacheSettings = cacheSettings.Value;
 | |
|         this.cacheProvider = cacheProvider;
 | |
|     }
 | |
| 
 | |
|     public async Task<IEnumerable<Sample>> GetAllSamples()
 | |
|     {
 | |
|         var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetAllSamples");
 | |
|         var cachedData = await cacheProvider.GetAsync<IEnumerable<Sample>>(cacheKey) ?? [];
 | |
| 
 | |
|         if (cachedData.Any()) return cachedData;
 | |
| 
 | |
|         var samples = await _sqlSampleRepository.GetAllAsync();
 | |
| 
 | |
|         await cacheProvider.SetAsync(cacheKey, samples, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes));
 | |
| 
 | |
|         return samples;
 | |
|     }
 | |
| 
 | |
|     public async Task<Sample?> GetSampleById(int id)
 | |
|     {
 | |
|         var cacheKey = CacheKeyHelper.GenerateCacheKey(this, "GetSampleById", id);
 | |
|         var cachedData = await cacheProvider.GetAsync<Sample>(cacheKey);
 | |
| 
 | |
|         if (cachedData is not null) { return cachedData; }
 | |
| 
 | |
|         var sample = await _sqlSampleRepository.GetByIdAsync(id);
 | |
| 
 | |
|         await cacheProvider.SetAsync(cacheKey, sample, TimeSpan.FromMinutes(cacheSettings.DefaultCacheDurationInMinutes));
 | |
| 
 | |
|         return sample;
 | |
|     }
 | |
| 
 | |
|     public async Task<Sample> AddSample(SampleRequest newSample)
 | |
|     {
 | |
|         var sample = newSample.Adapt<Sample>();
 | |
| 
 | |
|         await _sqlSampleRepository.AddAsync(sample);
 | |
|         await _sqlSampleRepository.SaveAsync();
 | |
|         return sample;
 | |
|     }
 | |
| 
 | |
|     public async Task<Sample?> UpdateSample(Sample sample)
 | |
|     {
 | |
|         var existingEntity = await _sqlSampleRepository.GetByIdAsync(sample.Id);
 | |
| 
 | |
|         if (existingEntity is null) return existingEntity;
 | |
| 
 | |
|         _sqlSampleRepository.Update(sample);
 | |
|         await _sqlSampleRepository.SaveAsync();
 | |
|         return sample;
 | |
|     }
 | |
| 
 | |
|     public async Task<Sample?> DeleteSample(int id)
 | |
|     {
 | |
|         var sample = await _sqlSampleRepository.GetByIdAsync(id);
 | |
|         if (sample == null)
 | |
|             return null;
 | |
| 
 | |
|         _sqlSampleRepository.Delete(sample);
 | |
|         await _sqlSampleRepository.SaveAsync();
 | |
|         return sample;
 | |
|     }
 | |
| }
 | 
