|
|
@ -1,6 +1,9 @@
|
|
|
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Model;
|
|
|
|
using Model;
|
|
|
|
using Model.Repository;
|
|
|
|
using Model.Repository;
|
|
|
|
using Shared;
|
|
|
|
using Shared;
|
|
|
|
|
|
|
|
using EFMappers;
|
|
|
|
|
|
|
|
using Entities;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Model2Entities;
|
|
|
|
namespace Model2Entities;
|
|
|
|
|
|
|
|
|
|
|
@ -9,45 +12,132 @@ public partial class DbDataManager
|
|
|
|
public class UserRepository : IUserRepository
|
|
|
|
public class UserRepository : IUserRepository
|
|
|
|
{
|
|
|
|
{
|
|
|
|
private readonly DbDataManager _dataManager;
|
|
|
|
private readonly DbDataManager _dataManager;
|
|
|
|
|
|
|
|
private readonly ILogger<DbDataManager> _logger;
|
|
|
|
|
|
|
|
|
|
|
|
public UserRepository(DbDataManager dataManager)
|
|
|
|
public UserRepository(DbDataManager dbDataManager, ILogger<DbDataManager> logger)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
_dataManager = dataManager;
|
|
|
|
this._dataManager = dbDataManager;
|
|
|
|
|
|
|
|
this._logger = logger;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<User>> GetItems(int index, int count, string? orderingProperty = null, bool descending = false)
|
|
|
|
public async Task<IEnumerable<User>> GetItems(int index, int count, AthleteOrderCriteria? orderingProperty = null, bool descending = false)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
try {
|
|
|
|
|
|
|
|
_logger.LogInformation($"GetItems with index {index} and count {count}", index, count);
|
|
|
|
|
|
|
|
_logger.LogInformation($"GetItems with orderingProperty {orderingProperty} and descending {descending}", orderingProperty, descending);
|
|
|
|
|
|
|
|
var users = _dataManager.DbContext.AthletesSet.GetItemsWithFilterAndOrdering(b => true, index, count, orderingProperty, descending).ToModels();
|
|
|
|
|
|
|
|
_logger.LogInformation($"Retrieved {users.Count()} users");
|
|
|
|
|
|
|
|
return await Task.FromResult(users);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.LogError(ex.Message, ex.InnerException, ex.StackTrace);
|
|
|
|
|
|
|
|
return await Task.FromResult<IEnumerable<User>>(new List<User>());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<User?> GetItemById(int id)
|
|
|
|
public async Task<User?> GetItemById(int id)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
try {
|
|
|
|
|
|
|
|
_logger.LogInformation($"GetItemById with id {id}", id);
|
|
|
|
|
|
|
|
var user = _dataManager.DbContext.AthletesSet.GetItemsWithFilterAndOrdering(b => b.IdAthlete == id, 0, 1, AthleteOrderCriteria.None, false).First().ToModel();
|
|
|
|
|
|
|
|
if (user != null)
|
|
|
|
|
|
|
|
_logger.LogInformation($"Retrieved user with ID {id}");
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
_logger.LogWarning($"No user found with ID {id}");
|
|
|
|
|
|
|
|
return await Task.FromResult(user);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.LogError(ex.Message, ex.InnerException, ex.StackTrace);
|
|
|
|
|
|
|
|
return await Task.FromResult<User?>(null);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<User?> UpdateItem(int oldItem, User newItem)
|
|
|
|
public async Task<User?> UpdateItem(int oldItem, User newItem)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|
|
|
|
// try {
|
|
|
|
|
|
|
|
// _logger.LogInformation($"UpdateItem with id {oldItem}", oldItem);
|
|
|
|
|
|
|
|
// var updatedUser = (await _dataManager.DbContext.UpdateItem<AthleteEntity, User>(oldItem, newItem, (user, entity) => user.ToEntity(entity)))?.ToModel();
|
|
|
|
|
|
|
|
// if (updatedUser != null)
|
|
|
|
|
|
|
|
// _logger.LogInformation($"Updated user with ID {oldItem}");
|
|
|
|
|
|
|
|
// else
|
|
|
|
|
|
|
|
// _logger.LogWarning($"No user found with ID {oldItem}");
|
|
|
|
|
|
|
|
// return await Task.FromResult(updatedUser);
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// catch (Exception ex)
|
|
|
|
|
|
|
|
// {
|
|
|
|
|
|
|
|
// _logger.LogError(ex.Message, ex.InnerException, ex.StackTrace);
|
|
|
|
|
|
|
|
// return await Task.FromResult<User?>(null);
|
|
|
|
|
|
|
|
// }
|
|
|
|
throw new NotImplementedException();
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<User?> AddItem(User item)
|
|
|
|
public async Task<User?> AddItem(User item)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
try {
|
|
|
|
|
|
|
|
_logger.LogInformation("Adding new user");
|
|
|
|
|
|
|
|
var addedUser = (await _dataManager.DbContext.AddItem(item.ToEntity()))?.ToModel();
|
|
|
|
|
|
|
|
if (addedUser != null)
|
|
|
|
|
|
|
|
_logger.LogInformation($"Added user with ID {addedUser.Id}");
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
_logger.LogError("Failed to add user");
|
|
|
|
|
|
|
|
return await Task.FromResult(addedUser);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.LogError(ex.Message, ex.InnerException, ex.StackTrace);
|
|
|
|
|
|
|
|
return await Task.FromResult<User?>(null);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<bool> DeleteItem(int item)
|
|
|
|
public async Task<bool> DeleteItem(int item)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
try {
|
|
|
|
|
|
|
|
_logger.LogInformation($"DeleteItem with id {item}", item);
|
|
|
|
|
|
|
|
var deleted = await _dataManager.DbContext.DeleteItem<AthleteEntity>(item);
|
|
|
|
|
|
|
|
if (deleted)
|
|
|
|
|
|
|
|
_logger.LogInformation($"Deleted user with ID {item}");
|
|
|
|
|
|
|
|
else
|
|
|
|
|
|
|
|
_logger.LogWarning($"No user found with ID {item}");
|
|
|
|
|
|
|
|
return await Task.FromResult(deleted);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.LogError(ex.Message, ex.InnerException, ex.StackTrace);
|
|
|
|
|
|
|
|
return await Task.FromResult(false);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<int> GetNbItems()
|
|
|
|
public async Task<int> GetNbItems()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
try {
|
|
|
|
|
|
|
|
_logger.LogInformation("GetNbItems");
|
|
|
|
|
|
|
|
var nbItems = _dataManager.DbContext.AthletesSet.Count();
|
|
|
|
|
|
|
|
_logger.LogInformation($"Retrieved {nbItems} users");
|
|
|
|
|
|
|
|
return await Task.FromResult(nbItems);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.LogError(ex.Message, ex.InnerException, ex.StackTrace);
|
|
|
|
|
|
|
|
return await Task.FromResult(0);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<User>> GetUsers(int index, int count, AthleteOrderCriteria? criteria, bool descending = false)
|
|
|
|
public async Task<IEnumerable<User>> GetUsers(int index, int count, AthleteOrderCriteria? criteria, bool descending = false)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
try {
|
|
|
|
|
|
|
|
_logger.LogInformation($"GetUsers with index {index} and count {count}", index, count);
|
|
|
|
|
|
|
|
_logger.LogInformation($"GetUsers with criteria {criteria} and descending {descending}", criteria, descending);
|
|
|
|
|
|
|
|
var users = _dataManager.DbContext.AthletesSet.GetItemsWithFilterAndOrdering(a => a.IsCoach == false, index, count, criteria, descending).ToModels();
|
|
|
|
|
|
|
|
_logger.LogInformation($"Retrieved {users.Count()} users");
|
|
|
|
|
|
|
|
return await Task.FromResult(users);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.LogError(ex.Message, ex.InnerException, ex.StackTrace);
|
|
|
|
|
|
|
|
return await Task.FromResult<IEnumerable<User>>(new List<User>());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task<bool> AddFriend(User user, User friend)
|
|
|
|
public async Task<bool> AddFriend(User user, User friend)
|
|
|
@ -60,12 +150,54 @@ public partial class DbDataManager
|
|
|
|
throw new NotImplementedException();
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task<IEnumerable<User>?>? GetFriends(User user, int index, int count, AthleteOrderCriteria? criteria, bool descending = false)
|
|
|
|
public Task<IEnumerable<User>> GetFriends(User user, int index, int count, AthleteOrderCriteria? criteria, bool descending = false)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
try {
|
|
|
|
|
|
|
|
_logger.LogInformation($"GetFriends with index {index} and count {count}", index, count);
|
|
|
|
|
|
|
|
_logger.LogInformation($"GetFriends with criteria {criteria} and descending {descending}", criteria, descending);
|
|
|
|
|
|
|
|
var friends = _dataManager.DbContext.AthletesSet.GetItemsWithFilterAndOrdering(a => a.IdAthlete == user.Id, index, count, criteria, descending).ToModels();
|
|
|
|
|
|
|
|
_logger.LogInformation($"Retrieved {friends.Count()} friends");
|
|
|
|
|
|
|
|
return Task.FromResult(friends);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.LogError(ex.Message, ex.InnerException, ex.StackTrace);
|
|
|
|
|
|
|
|
return Task.FromResult<IEnumerable<User>>(new List<User>());
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Task<int> GetNbFriends(User user)
|
|
|
|
public Task<int> GetNbFriends(User user)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
_logger.LogInformation($"GetNbFriends with user {user}", user);
|
|
|
|
|
|
|
|
var nbFriends = _dataManager.DbContext.AthletesSet.Count(a => a.IdAthlete == user.Id);
|
|
|
|
|
|
|
|
_logger.LogInformation($"Retrieved {nbFriends} friends");
|
|
|
|
|
|
|
|
return Task.FromResult(nbFriends);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.LogError(ex.Message, ex.InnerException, ex.StackTrace);
|
|
|
|
|
|
|
|
return Task.FromResult(0);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Task<IEnumerable<User>> GetItems(int index, int count, AthleteOrderCriteria orderingProperty, bool descending = false)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
_logger.LogInformation($"GetItems with index {index} and count {count}", index, count);
|
|
|
|
|
|
|
|
_logger.LogInformation($"GetItems with orderingProperty {orderingProperty} and descending {descending}", orderingProperty, descending);
|
|
|
|
|
|
|
|
var users = _dataManager.DbContext.AthletesSet.GetItemsWithFilterAndOrdering(b => true, index, count, orderingProperty, descending).ToModels();
|
|
|
|
|
|
|
|
_logger.LogInformation($"Retrieved {users.Count()} users");
|
|
|
|
|
|
|
|
return Task.FromResult(users);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
_logger.LogError(ex.Message, ex.InnerException, ex.StackTrace);
|
|
|
|
|
|
|
|
return Task.FromResult<IEnumerable<User>>(new List<User>());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public Task<IEnumerable<User>> GetItems(int index, int count, string? orderingProperty = null, bool descending = false)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|