Created UserService
This commit is contained in:
56
FabApprovalWorkerService/Services/DalService.cs
Normal file
56
FabApprovalWorkerService/Services/DalService.cs
Normal file
@ -0,0 +1,56 @@
|
||||
using Dapper;
|
||||
using Dapper.Contrib.Extensions;
|
||||
|
||||
using System.Data;
|
||||
|
||||
namespace FabApprovalWorkerService.Services;
|
||||
public interface IDalService {
|
||||
Task<IEnumerable<T>> QueryAsync<T>(string sql);
|
||||
Task<bool> UpdateAsync<T>(ICollection<T> collection);
|
||||
}
|
||||
|
||||
public class DalService : IDalService {
|
||||
private readonly IDbConnection _dbConnection;
|
||||
private readonly ILogger<DalService> _logger;
|
||||
|
||||
public DalService(IDbConnection dbConnection, ILogger<DalService> logger) {
|
||||
_dbConnection = dbConnection;
|
||||
if (_dbConnection is null) throw new ArgumentNullException("IDbConnection not injected");
|
||||
_logger = logger;
|
||||
if (_logger is null) throw new ArgumentNullException("ILogger not injected");
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<T>> QueryAsync<T>(string sql) {
|
||||
if (sql is null) throw new ArgumentNullException("sql cannot be null");
|
||||
try {
|
||||
_logger.LogInformation("Attempting to perform query with {sql}", sql);
|
||||
|
||||
_dbConnection.Open();
|
||||
return await _dbConnection.QueryAsync<T>(sql);
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError("An exception occurred while attempting to perform a query. Exception: {ex}",
|
||||
ex.Message);
|
||||
|
||||
throw;
|
||||
} finally {
|
||||
_dbConnection.Close();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateAsync<T>(ICollection<T> collection) {
|
||||
if (collection is null) throw new ArgumentNullException("collection cannot be null");
|
||||
try {
|
||||
_logger.LogInformation("Attempting to perform a bulk update");
|
||||
|
||||
_dbConnection.Open();
|
||||
return await _dbConnection.UpdateAsync<ICollection<T>>(collection);
|
||||
} catch (Exception ex) {
|
||||
_logger.LogError("An exception occurred while attempting to perform a bulk update. Exception: {ex}",
|
||||
ex.Message);
|
||||
|
||||
throw;
|
||||
} finally {
|
||||
_dbConnection.Close();
|
||||
}
|
||||
}
|
||||
}
|
65
FabApprovalWorkerService/Services/UserService.cs
Normal file
65
FabApprovalWorkerService/Services/UserService.cs
Normal file
@ -0,0 +1,65 @@
|
||||
using Dapper;
|
||||
using Dapper.Contrib.Extensions;
|
||||
|
||||
using FabApprovalWorkerService.Models;
|
||||
|
||||
using System.Data;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace FabApprovalWorkerService.Services;
|
||||
|
||||
public interface IUserService {
|
||||
Task<Dictionary<string, User>> GetAllUsers();
|
||||
Task<bool> UpdateUserCertificationData([DisallowNull] ICollection<User> users);
|
||||
}
|
||||
|
||||
public class UserService : IUserService {
|
||||
private readonly ILogger<UserService> _logger;
|
||||
private IDalService _dalService;
|
||||
|
||||
public UserService(ILogger<UserService> logger, IDalService dalService) {
|
||||
_logger = logger;
|
||||
_dalService = dalService;
|
||||
}
|
||||
|
||||
public async Task<Dictionary<string, User>> GetAllUsers() {
|
||||
Dictionary<string, User> users = new Dictionary<string, User>();
|
||||
|
||||
try {
|
||||
_logger.LogInformation("Attempting to get all users");
|
||||
|
||||
string sql = "select * from users;";
|
||||
|
||||
IEnumerable<User> enumerableUsers = await _dalService.QueryAsync<User>(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<bool> UpdateUserCertificationData([DisallowNull] ICollection<User> 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;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user