using Dapper; using Dapper.Contrib.Extensions; using FabApprovalWorkerService.Models; using System.Data; using System.Diagnostics.CodeAnalysis; namespace FabApprovalWorkerService.Services; public interface IUserService { Task> GetAllUsers(); Task UpdateUserCertificationData([DisallowNull] ICollection users); } public class UserService : IUserService { private readonly ILogger _logger; private IDalService _dalService; public UserService(ILogger logger, IDalService dalService) { _logger = logger; _dalService = dalService; } public async Task> GetAllUsers() { Dictionary users = new Dictionary(); try { _logger.LogInformation("Attempting to get all users"); string sql = "select * from users;"; IEnumerable enumerableUsers = await _dalService.QueryAsync(sql); users = enumerableUsers.ToDictionary(u => u.FirstName + u.LastName, u => u); if (!users.Any()) { throw new Exception("No users returned from the database"); } else { _logger.LogInformation("Successfully retrieved the users"); } return users; } catch (Exception ex) { _logger.LogError("An exception occurred when attempting to get all users. Exception: {ex}", ex.Message); return users; } } public async Task UpdateUserCertificationData([DisallowNull] ICollection users) { if (users is null) throw new ArgumentNullException("users cannot be null"); try { _logger.LogInformation("Attempting to update user cert data"); bool result = await _dalService.UpdateAsync(users); _logger.LogInformation("User cert data updated successfully: {result}", result); return result; } catch (Exception ex) { _logger.LogError("An exception occurred when attempting to update user cert data. Exception: {ex}", ex.Message); return false; } } }