Merge branch 'tests_merging' of codefirst.iut.uca.fr:HeartDev/API into tests_merging

WORK-KMO2
Antoine PEREDERII 1 year ago
commit d66febf72d

@ -87,7 +87,7 @@ namespace Entities
/// </summary>
public bool IsCoach { get; set; }
// [TODO] [DAVE] Pourquoi c'est un byte[] ? et pas un string ? it's me so should change it to string
// [TODO] [DAVE] Check Image
public string? ProfilPicture { get; set; }
public LargeImageEntity? Image { get; set; }
@ -106,7 +106,6 @@ namespace Entities
public ICollection<NotificationEntity> NotificationsSent { get; set; } = new List<NotificationEntity>();
public int? DataSourceId { get; set; }
// [TODO] [DAVE] Pourquoi c'est un one to one ? et pas un one to many ?
public DataSourceEntity? DataSource { get; set; }
public ICollection<FriendshipEntity> Followers { get; set; } = [];

@ -334,8 +334,6 @@ public class UsersController : Controller
}
}
// #[TODO] [Dave] ou faire un get qui si le role est coach resend les athletes et si le role est athlete resend les coach
/// <summary>
/// Obtient la liste des athlètes d'un coach spécifique.
/// </summary>
@ -414,13 +412,6 @@ public class UsersController : Controller
return Problem();
}
}
/* [TODO] [Dave]
[HttpGet("{userId}/trainings")]
[ProducesResponseType(typeof(PageResponse<TrainingDto>), 200)]
[ProducesResponseType(404)]
[ProducesResponseType(500)]
public async Task<ActionResult<PageResponse<TrainingDto>> GetTrainings(int userId, [FromQuery] PageRequest request)
*/
/// <summary>
/// Déconnecte l'utilisateur actuel.

@ -1,4 +1,4 @@
using HeartTrackAPI;
using HeartTrackAPI.Utils;
var builder = WebApplication.CreateBuilder(args);

@ -14,7 +14,7 @@ using StubAPI;
using StubbedContextLib;
using Swashbuckle.AspNetCore.SwaggerGen;
namespace HeartTrackAPI;
namespace HeartTrackAPI.Utils;
public class AppBootstrap(IConfiguration configuration)
{

@ -1,9 +0,0 @@
namespace Model.Manager;
public class ActivityManager : IActivityManager
{
public void AddActivityFromFitFile(byte filePath)
{
throw new NotImplementedException();
}
}

@ -1,6 +0,0 @@
namespace Model.Manager;
public interface IActivityManager
{
public void AddActivityFromFitFile(byte filePath);
}

@ -1,6 +0,0 @@
namespace Model.Manager;
public class UserManager
{
}

@ -5,13 +5,11 @@ namespace Model.Repository;
public interface IUserRepository : IGenericRepository<User>
{
public Task<IEnumerable<User>?> GetUsers(int index, int count, AthleteOrderCriteria? criteria , bool descending = false);
public Task<bool> AddFriend(User user, User friend);
public Task<bool> RemoveFriend(User user, User friend);
// should be removed cause i just have to call the GetItem then get the friends
public Task<IEnumerable<User>?> GetFriends(User user, int index, int count, AthleteOrderCriteria? criteria, bool descending = false);
public Task<int> GetNbFriends(User user);
public Task<IEnumerable<User>?> GetAllAthletes(int index, int count, AthleteOrderCriteria? criteria, bool descending = false);
public Task<IEnumerable<User>?> GetAllCoaches(int index, int count, AthleteOrderCriteria? criteria, bool descending = false);
}

@ -99,6 +99,12 @@ public static class Extensions
.Include(a => a.Athlete)
.Include(a => a.DataSource);
}
public static IQueryable<AthleteEntity> IncludeStandardProperties(this IQueryable<AthleteEntity> query)
{
return query.Include(a => a.DataSource)
.Include(a => a.Activities)
.Include(a => a.Image);
}
// public static Activity ToModel(this ActivityEntity entity)
// {
// return new Activity (

@ -4,6 +4,7 @@ using Model.Repository;
using Shared;
using EFMappers;
using Entities;
using Microsoft.EntityFrameworkCore;
namespace Model2Entities;
@ -16,66 +17,71 @@ public partial class DbDataManager
public UserRepository(DbDataManager dbDataManager, ILogger<DbDataManager> logger)
{
this._dataManager = dbDataManager;
this._logger = logger;
_dataManager = dbDataManager;
_logger = logger;
}
public async Task<IEnumerable<User>> GetItems(int index, int count, AthleteOrderCriteria? orderingProperty = null, bool descending = false)
public async Task<IEnumerable<User>> GetItems(int index, int count, string? orderingProperty = null,
bool descending = false)
=> await GetUsers(index, count, this.ToEnum(orderingProperty), descending);
public async Task<IEnumerable<User>> GetUsers(int index, int count,
AthleteOrderCriteria? orderingProperty = null, 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($"GetItems with orderingProperty {orderingProperty} and descending {descending}",
orderingProperty, descending);
var users = _dataManager.DbContext.AthletesSet.IncludeStandardProperties().GetItemsWithFilterAndOrdering(b => true, index, count,
orderingProperty != AthleteOrderCriteria.None ? orderingProperty : null, 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)
{
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();
var userEntity = await _dataManager.DbContext.AthletesSet.IncludeStandardProperties()
.SingleOrDefaultAsync(a => a.IdAthlete == id);
var user = userEntity != null ? userEntity.ToModel() : null;
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)
{
// 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();
_logger.LogInformation($"UpdateItem with id {oldItem}", oldItem);
var originalEntity = _dataManager.DbContext.AthletesSet.Find(oldItem);
if (originalEntity == null)
{
_logger.LogWarning($"No user found with ID {oldItem}");
return await Task.FromResult<User?>(null);
}
var originalEntry = _dataManager.DbContext.Entry(originalEntity);
var values = typeof(AthleteEntity).GetProperties().Where(ppty => ppty.Name != "IdAthlete")
.ToDictionary(ppty => ppty.Name, ppty => ppty.GetValue(newItem.ToEntity()));
originalEntry.CurrentValues.SetValues(values);
_dataManager.DbContext.AthletesSet.Attach(originalEntity);
_dataManager.DbContext.Entry(originalEntity).State = EntityState.Modified;
_dataManager.DbContext.SaveChanges();
var updatedUser = originalEntity.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);
}
public async Task<User?> AddItem(User item)
{
try {
_logger.LogInformation("Adding new user");
var addedUser = (await _dataManager.DbContext.AddItem(item.ToEntity()))?.ToModel();
if (addedUser != null)
@ -83,17 +89,12 @@ public partial class DbDataManager
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)
{
try {
_logger.LogInformation($"DeleteItem with id {item}", item);
var deleted = await _dataManager.DbContext.DeleteItem<AthleteEntity>(item);
if (deleted)
@ -101,94 +102,109 @@ public partial class DbDataManager
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()
{
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)
{
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<IEnumerable<User>> GetAllAthletes(int index, int count, AthleteOrderCriteria? criteria,
bool descending = false)
{
throw new NotImplementedException();
_logger.LogInformation($"GetAllAthletes with index {index} and count {count}", index, count);
_logger.LogInformation($"GetAllAthletes with criteria {criteria} and descending {descending}", criteria,
descending);
var athletes = _dataManager.DbContext.AthletesSet.IncludeStandardProperties()
.GetItemsWithFilterAndOrdering(a => a.IsCoach == false, index, count,
criteria != AthleteOrderCriteria.None ? criteria : null, descending).ToModels();
_logger.LogInformation($"Retrieved {athletes.Count()} athletes");
return await Task.FromResult(athletes);
}
public async Task<bool> RemoveFriend(User user, User friend)
public async Task<IEnumerable<User>> GetAllCoaches(int index, int count, AthleteOrderCriteria? criteria,
bool descending = false)
{
throw new NotImplementedException();
_logger.LogInformation($"GetAllCoaches with index {index} and count {count}", index, count);
_logger.LogInformation($"GetAllCoaches with criteria {criteria} and descending {descending}", criteria,
descending);
var coaches = _dataManager.DbContext.AthletesSet.IncludeStandardProperties()
.GetItemsWithFilterAndOrdering(a => a.IsCoach, index, count,
criteria != AthleteOrderCriteria.None ? criteria : null, descending).ToModels();
_logger.LogInformation($"Retrieved {coaches.Count()} coaches");
return await Task.FromResult(coaches);
}
public Task<IEnumerable<User>> GetFriends(User user, int index, int count, AthleteOrderCriteria? criteria, bool descending = false)
public async Task<bool> AddFriend(User user, User friend)
{
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);
_logger.LogInformation($"Attempting to add friend: User {user.Id} adding Friend {friend.Id}");
var userEntity = _dataManager.DbContext.AthletesSet.IncludeStandardProperties().FirstOrDefault(a => a.IdAthlete == user.Id);
var friendEntity = _dataManager.DbContext.AthletesSet.IncludeStandardProperties().FirstOrDefault(a => a.IdAthlete == friend.Id);
if (userEntity == null || friendEntity == null)
{
_logger.LogWarning($"User or friend not found: User {user.Id}, Friend {friend.Id}");
return false;
}
catch (Exception ex)
if (userEntity.Followings.All(f => f.FollowingId != friend.Id))
{
_logger.LogError(ex.Message, ex.InnerException, ex.StackTrace);
return Task.FromResult<IEnumerable<User>>(new List<User>());
userEntity.Followings.Add(new FriendshipEntity
{ FollowingId = friend.Id, FollowerId = user.Id, StartDate = DateTime.Now });
await _dataManager.DbContext.SaveChangesAsync();
_logger.LogInformation($"Successfully added friend: User {user.Id} added Friend {friend.Id}");
return true;
}
_logger.LogInformation($"Friendship already exists: User {user.Id} and Friend {friend.Id}");
return false;
}
public Task<int> GetNbFriends(User user)
public async Task<bool> RemoveFriend(User user, User friend)
{
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);
_logger.LogInformation($"Attempting to remove friend: User {user.Id} removing Friend {friend.Id}");
var userEntity = user.ToEntity();
var friendEntity = friend.ToEntity();
if (userEntity == null || friendEntity == null)
{
_logger.LogWarning($"User or friend not found: User {user.Id}, Friend {friend.Id}");
return false;
}
catch (Exception ex)
var friendship = userEntity.Followings.FirstOrDefault(f => f.FollowingId == friend.Id);
if (friendship != null)
{
_logger.LogError(ex.Message, ex.InnerException, ex.StackTrace);
return Task.FromResult(0);
userEntity.Followings.Remove(friendship);
await _dataManager.DbContext.SaveChangesAsync();
_logger.LogInformation($"Successfully removed friend: User {user.Id} removed Friend {friend.Id}");
return true;
}
_logger.LogInformation($"Friendship does not exist: User {user.Id} and Friend {friend.Id}");
return false;
}
public Task<IEnumerable<User>> GetItems(int index, int count, AthleteOrderCriteria orderingProperty, bool descending = false)
public Task<IEnumerable<User>> GetFriends(User user, int index, int count, AthleteOrderCriteria? criteria,
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);
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.IncludeStandardProperties()
.GetItemsWithFilterAndOrdering(a => a.Followers.Any(f => f.FollowingId == user.Id), index, count,
criteria, descending).ToModels();
_logger.LogInformation($"Retrieved {friends.Count()} friends");
return Task.FromResult(friends);
}
catch (Exception ex)
{
@ -197,9 +213,15 @@ public partial class DbDataManager
}
}
public Task<IEnumerable<User>> GetItems(int index, int count, string? orderingProperty = null, bool descending = false)
public Task<int> GetNbFriends(User user)
{
throw new NotImplementedException();
_logger.LogInformation($"GetNbFriends with user {user}", user);
var nbFriends = _dataManager.DbContext.AthletesSet
.GetItemsWithFilterAndOrdering(a => a.IdAthlete == user.Id, 0, int.MaxValue,
AthleteOrderCriteria.None, false).First().Followings.Count();
_logger.LogInformation($"Retrieved {nbFriends} friends");
return Task.FromResult(nbFriends);
}
}
}

@ -108,4 +108,14 @@ public class UserService : IUserRepository
{
return await Task.FromResult(athletes.Count);
}
public async Task<IEnumerable<User>> GetAllAthletes(int index, int count, AthleteOrderCriteria? criteria, bool descending = false)
{
return await Task.FromResult(athletes.GetItemsWithFilterAndOrdering(c=>c.Role is Athlete,index, count,criteria, descending));
}
public async Task<IEnumerable<User>> GetAllCoaches(int index, int count, AthleteOrderCriteria? criteria, bool descending = false)
{
return await Task.FromResult(athletes.GetItemsWithFilterAndOrdering(c=>c.Role is Coach,index, count,criteria, descending));
}
}
Loading…
Cancel
Save