using skydiveLogs_api.Domain; using skydiveLogs_api.DomainBusiness.Interfaces; using skydiveLogs_api.DomainService.Repositories; using System; using System.Collections.Generic; using System.Linq; namespace skydiveLogs_api.DomainBusiness { public class GearService : IGearService { #region Public Constructors public GearService(IGearRepository gearRepository, ICacheService cacheService, IIdentityService identityService) { _gearRepository = gearRepository; _cacheService = cacheService; _identityService = identityService; } #endregion Public Constructors #region Public Methods /// /// Adds a new gear item to the system. /// /// The Gear entity containing the new gear data. public void AddNewGear(Gear newGear) { newGear.User = _identityService.ConnectedUser; _gearRepository.Add(newGear); var userId = _identityService.ConnectedUser.Id; _cacheService.Delete(CacheType.Gear, userId: userId); } /// /// Adds rental gear to the system for a new user. /// /// The new user for whom rental gear should be added. public void AddRentalGear(User newUser) { var rentalGear = new Gear { Name = "Rental gear", Manufacturer = "?", MainCanopy = "?", Aad = "Cypress/Vigil", MaxSize = 280, MinSize = 190, ReserveCanopy = "?", Equipment = "RAS", User = newUser }; _gearRepository.Add(rentalGear); } /// /// Deletes a gear item by its ID. /// /// The gear ID to delete. public void DeleteGearById(int id) { throw new NotImplementedException(); } /// /// Retrieves all gear items. /// /// A collection of Gear entities containing all gear data. public IEnumerable GetAllGears() { var userId = _identityService.ConnectedUser.Id; if (!_cacheService.Contains(CacheType.Gear, userId: userId)) _cacheService.Put(CacheType.Gear, _gearRepository.GetAll(_identityService.ConnectedUser), 5 * 60 * 1000, userId: userId); return _cacheService.Get>(CacheType.Gear, userId: userId); } /// /// Retrieves a gear item by its ID. /// /// The gear ID to retrieve. /// A Gear entity containing the gear details. public Gear GetGearById(int id) { var allGears = GetAllGears(); return allGears.Single(g => g.Id == id); } /// /// Updates an existing gear item. /// /// The gear ID to update. /// Gear entity containing the updated gear data. /// True if the update was successful, false otherwise. public bool UpdateGear(int id, Gear gear) { gear.Id = id; gear.User = _identityService.ConnectedUser; var tmp = _gearRepository.Update(gear); var userId = _identityService.ConnectedUser.Id; _cacheService.Delete(CacheType.Gear, userId: userId); return tmp; } #endregion Public Methods #region Private Fields private readonly ICacheService _cacheService; private readonly IGearRepository _gearRepository; private readonly IIdentityService _identityService; #endregion Private Fields } }