using BankSampahApp.Models;
namespace BankSampahApp.Services;
///
/// Service implementation untuk menangani logic bisnis halaman Home
/// Menggunakan base service untuk common functionality
///
public class HomeService : BaseService, IHomeService
{
private readonly IStatisticsService _statisticsService;
private readonly ICacheService _cacheService;
private readonly IAppConfigurationService _appConfig;
///
/// Cache keys untuk optimasi performa
///
private static class CacheKeys
{
public const string Features = "home_features";
public const string Statistics = "home_statistics";
}
///
/// Constructor dengan dependency injection
///
/// Logger untuk logging
/// Memory cache untuk caching
/// Configuration service
/// Service untuk statistik
/// Cache service
/// Application configuration service
public HomeService(
ILogger logger,
Microsoft.Extensions.Caching.Memory.IMemoryCache cache,
IConfiguration configuration,
IStatisticsService statisticsService,
ICacheService cacheService,
IAppConfigurationService appConfig) : base(logger, cache, configuration)
{
_statisticsService = statisticsService ?? throw new ArgumentNullException(nameof(statisticsService));
_cacheService = cacheService ?? throw new ArgumentNullException(nameof(cacheService));
_appConfig = appConfig ?? throw new ArgumentNullException(nameof(appConfig));
}
///
public async Task GetHomeViewModelAsync()
{
return await ExecuteWithErrorHandlingAsync(
async () =>
{
var features = await GetFeaturesAsync();
var statistics = await GetStatisticsAsync();
return new HomeViewModel
{
Title = _appConfig.ApplicationName,
Subtitle = "Kelola sampah Anda dengan mudah dan dapatkan reward!",
Features = features.ToList(),
Statistics = statistics
};
},
"Creating home view model");
}
///
public async Task> GetFeaturesAsync()
{
return await _cacheService.GetOrSetAsync(
CacheKeys.Features,
LoadFeaturesFromSourceAsync,
_appConfig.CacheSettings.FeaturesExpiration);
}
///
/// Loads features from source
///
/// List of features
private async Task> LoadFeaturesFromSourceAsync()
{
using var scope = CreateLogScope("LoadFeaturesFromSource");
// Simulate async data loading
if (_appConfig.DevelopmentSettings.SimulatedDelayMs > 0)
{
await Task.Delay(_appConfig.DevelopmentSettings.SimulatedDelayMs);
}
else
{
await Task.Delay(1);
}
var features = new List
{
CreateFeature("♻️", "Kelola Sampah",
"Catat dan kelola sampah Anda dengan sistem digital yang mudah"),
CreateFeature("💰", "Dapatkan Reward",
"Tukar poin sampah Anda dengan berbagai hadiah menarik"),
CreateFeature("🌱", "Go Green",
"Berkontribusi untuk lingkungan yang lebih bersih dan sehat"),
CreateFeature("📊", "Tracking Real-time",
"Monitor progress dan statistik sampah Anda secara real-time")
};
_logger.LogInformation("Successfully loaded {Count} features", features.Count);
return features;
}
///
public async Task GetStatisticsAsync()
{
return await _cacheService.GetOrSetWithSlidingAsync(
CacheKeys.Statistics,
() => _statisticsService.GetCurrentStatisticsAsync(),
_appConfig.CacheSettings.StatisticsSlidingExpiration,
_appConfig.CacheSettings.StatisticsExpiration,
Microsoft.Extensions.Caching.Memory.CacheItemPriority.High);
}
///
/// Helper method untuk membuat FeatureModel dengan DRY principle
///
/// Icon fitur
/// Judul fitur
/// Deskripsi fitur
/// FeatureModel instance
private static FeatureModel CreateFeature(string icon, string title, string description)
{
return new FeatureModel
{
Icon = icon,
Title = title,
Description = description
};
}
}