140 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			C#
		
	
	
			
		
		
	
	
			140 lines
		
	
	
		
			4.9 KiB
		
	
	
	
		
			C#
		
	
	
using BpsRwApp.Models;
 | 
						|
 | 
						|
namespace BpsRwApp.Services;
 | 
						|
 | 
						|
/// <summary>
 | 
						|
/// Service implementation untuk menangani logic bisnis halaman Home
 | 
						|
/// Menggunakan base service untuk common functionality
 | 
						|
/// </summary>
 | 
						|
public class HomeService : BaseService, IHomeService
 | 
						|
{
 | 
						|
    private readonly IStatisticsService _statisticsService;
 | 
						|
    private readonly ICacheService _cacheService;
 | 
						|
    private readonly IAppConfigurationService _appConfig;
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Cache keys untuk optimasi performa
 | 
						|
    /// </summary>
 | 
						|
    private static class CacheKeys
 | 
						|
    {
 | 
						|
        public const string Features = "home_features";
 | 
						|
        public const string Statistics = "home_statistics";
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Constructor dengan dependency injection
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="logger">Logger untuk logging</param>
 | 
						|
    /// <param name="cache">Memory cache untuk caching</param>
 | 
						|
    /// <param name="configuration">Configuration service</param>
 | 
						|
    /// <param name="statisticsService">Service untuk statistik</param>
 | 
						|
    /// <param name="cacheService">Cache service</param>
 | 
						|
    /// <param name="appConfig">Application configuration service</param>
 | 
						|
    public HomeService(
 | 
						|
        ILogger<HomeService> 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));
 | 
						|
    }
 | 
						|
 | 
						|
    /// <inheritdoc/>
 | 
						|
    public async Task<HomeViewModel> 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");
 | 
						|
    }
 | 
						|
 | 
						|
    /// <inheritdoc/>
 | 
						|
    public async Task<IEnumerable<FeatureModel>> GetFeaturesAsync()
 | 
						|
    {
 | 
						|
        return await _cacheService.GetOrSetAsync(
 | 
						|
            CacheKeys.Features,
 | 
						|
            LoadFeaturesFromSourceAsync,
 | 
						|
            _appConfig.CacheSettings.FeaturesExpiration);
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Loads features from source
 | 
						|
    /// </summary>
 | 
						|
    /// <returns>List of features</returns>
 | 
						|
    private async Task<IEnumerable<FeatureModel>> 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<FeatureModel>
 | 
						|
        {
 | 
						|
            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;
 | 
						|
    }
 | 
						|
 | 
						|
    /// <inheritdoc/>
 | 
						|
    public async Task<StatisticsModel> GetStatisticsAsync()
 | 
						|
    {
 | 
						|
        return await _cacheService.GetOrSetWithSlidingAsync(
 | 
						|
            CacheKeys.Statistics,
 | 
						|
            () => _statisticsService.GetCurrentStatisticsAsync(),
 | 
						|
            _appConfig.CacheSettings.StatisticsSlidingExpiration,
 | 
						|
            _appConfig.CacheSettings.StatisticsExpiration,
 | 
						|
            Microsoft.Extensions.Caching.Memory.CacheItemPriority.High);
 | 
						|
    }
 | 
						|
 | 
						|
    /// <summary>
 | 
						|
    /// Helper method untuk membuat FeatureModel dengan DRY principle
 | 
						|
    /// </summary>
 | 
						|
    /// <param name="icon">Icon fitur</param>
 | 
						|
    /// <param name="title">Judul fitur</param>
 | 
						|
    /// <param name="description">Deskripsi fitur</param>
 | 
						|
    /// <returns>FeatureModel instance</returns>
 | 
						|
    private static FeatureModel CreateFeature(string icon, string title, string description)
 | 
						|
    {
 | 
						|
        return new FeatureModel
 | 
						|
        {
 | 
						|
            Icon = icon,
 | 
						|
            Title = title,
 | 
						|
            Description = description
 | 
						|
        };
 | 
						|
    }
 | 
						|
} |