parent
a258313f37
commit
70137ee08d
@ -1,13 +0,0 @@
|
|||||||
using Dto;
|
|
||||||
using Model;
|
|
||||||
|
|
||||||
namespace ApiMappeur;
|
|
||||||
|
|
||||||
public static class ActivityMappeur
|
|
||||||
{
|
|
||||||
public static ActivityDto ToDto(this Activity activity)
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,80 @@
|
|||||||
|
using Entities;
|
||||||
|
using Model;
|
||||||
|
using Shared;
|
||||||
|
|
||||||
|
namespace EFMappers;
|
||||||
|
|
||||||
|
public static class ActivityMapper
|
||||||
|
{
|
||||||
|
private static GenericMapper<Activity, ActivityEntity> _mapper = new GenericMapper<Activity, ActivityEntity>();
|
||||||
|
public static Activity? GetModel(ActivityEntity entity) =>
|
||||||
|
_mapper.GetT(entity);
|
||||||
|
public static ActivityEntity? GetEntity(Activity model) =>
|
||||||
|
_mapper.GetU(model);
|
||||||
|
public static void Add(Activity model, ActivityEntity entity)
|
||||||
|
{
|
||||||
|
var tuple = new Tuple<Activity, ActivityEntity>(model, entity);
|
||||||
|
_mapper.Add(model, entity);
|
||||||
|
}
|
||||||
|
// ! RESET
|
||||||
|
// ? Quand on fait appel au reset ?
|
||||||
|
// * Apres des saves changing ou rollback.
|
||||||
|
public static void Reset()
|
||||||
|
{
|
||||||
|
_mapper.Reset();
|
||||||
|
}
|
||||||
|
public static Activity? ToModel(this ActivityEntity entity)
|
||||||
|
{
|
||||||
|
// return entity.ToModel();
|
||||||
|
return entity.ToT(_mapper, activity => new Activity (
|
||||||
|
entity.IdActivity,
|
||||||
|
entity.Type,
|
||||||
|
new DateTime(entity.Date.Year, entity.Date.Month, entity.Date.Day),
|
||||||
|
new DateTime().Add(entity.StartTime.ToTimeSpan()),
|
||||||
|
new DateTime().Add(entity.EndTime.ToTimeSpan()),
|
||||||
|
entity.EffortFelt,
|
||||||
|
entity.Variability,
|
||||||
|
entity.Variance,
|
||||||
|
entity.StandardDeviation,
|
||||||
|
entity.Average,
|
||||||
|
entity.Maximum,
|
||||||
|
entity.Minimum,
|
||||||
|
entity.AverageTemperature,
|
||||||
|
entity.HasAutoPause));
|
||||||
|
// ! regarder a ce que le model est bien les relation comme l'EF
|
||||||
|
// ), (activity, entity) => activity.Id = entity.IdActivity);
|
||||||
|
|
||||||
|
}
|
||||||
|
// dictionnaire;
|
||||||
|
|
||||||
|
|
||||||
|
public static ActivityEntity? ToEntity(this Activity model)
|
||||||
|
{
|
||||||
|
// return model.ToEntity();
|
||||||
|
return model.ToU(_mapper, activityEntity => new ActivityEntity
|
||||||
|
{
|
||||||
|
IdActivity = model.Id,
|
||||||
|
Type = model.Type,
|
||||||
|
Date = DateOnly.FromDateTime(model.Date),
|
||||||
|
StartTime = TimeOnly.FromDateTime(model.StartTime),
|
||||||
|
EndTime = TimeOnly.FromDateTime(model.EndTime),
|
||||||
|
EffortFelt = model.Effort,
|
||||||
|
Variability = model.Variability,
|
||||||
|
Variance = model.Variance,
|
||||||
|
StandardDeviation = model.StandardDeviation,
|
||||||
|
Average = model.Average,
|
||||||
|
Maximum = model.Maximum,
|
||||||
|
Minimum = model.Minimum,
|
||||||
|
AverageTemperature = model.AverageTemperature,
|
||||||
|
HasAutoPause = model.HasAutoPause
|
||||||
|
}
|
||||||
|
// ! regarder a ce que le model est bien les relation comme l'EF
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IEnumerable<Activity> ToModels(this IEnumerable<ActivityEntity> entities)
|
||||||
|
=> entities.Select(a => a.ToModel());
|
||||||
|
|
||||||
|
public static IEnumerable<ActivityEntity> ToEntities(this IEnumerable<Activity> models)
|
||||||
|
=> models.Select(a => a.ToEntity());
|
||||||
|
}
|
@ -1,49 +1,125 @@
|
|||||||
using Model;
|
using Model;
|
||||||
using Model.Repository;
|
using Model.Repository;
|
||||||
using Shared;
|
using Shared;
|
||||||
|
using Model.Manager;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Entities;
|
||||||
|
using EFMappers;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
|
||||||
namespace Model2Entities;
|
namespace Model2Entities;
|
||||||
|
|
||||||
public partial class DbDataManager
|
public partial class DbDataManager : IDataManager
|
||||||
{
|
{
|
||||||
public class ActivityRepository : IActivityRepository
|
public class ActivityRepository : IActivityRepository
|
||||||
{
|
{
|
||||||
private readonly DbDataManager _dataManager;
|
private readonly DbDataManager _dataManager;
|
||||||
|
private readonly ILogger<ActivityRepository> _logger = new Logger<ActivityRepository>(new LoggerFactory());
|
||||||
|
|
||||||
|
// logger et tests sur tout et mapper
|
||||||
public ActivityRepository(DbDataManager dataManager)
|
public ActivityRepository(DbDataManager dataManager)
|
||||||
{
|
{
|
||||||
_dataManager = dataManager;
|
_dataManager = dataManager;
|
||||||
|
// _logger = logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public async Task<IEnumerable<Activity>> GetActivities(int index, int count, ActivityOrderCriteria criteria, bool descending = false)
|
public async Task<IEnumerable<Activity>> GetActivities(int index, int count, ActivityOrderCriteria criteria, bool descending = false)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
_logger.LogInformation($"GetActivities with index {index} and count {count}", index, count);
|
||||||
|
_logger.LogInformation($"GetActivities with criteria {criteria} and descending {descending}", criteria, descending);
|
||||||
|
|
||||||
|
var activities = _dataManager.DbContext.ActivitiesSet.GetItemsWithFilterAndOrdering(b => true, index, count, criteria, descending).ToModels();
|
||||||
|
|
||||||
|
_logger.LogInformation($"Retrieved {activities.Count()} activities");
|
||||||
|
return await Task.FromResult(activities);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Activity?> GetActivityByIdAsync(int id)
|
public async Task<Activity?> GetActivityByIdAsync(int id)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
_logger.LogInformation($"GetActivityByIdAsync with id {id}", id);
|
||||||
|
// ! By None don't pass the filter
|
||||||
|
var activity = _dataManager.DbContext.ActivitiesSet.GetItemsWithFilterAndOrdering(b => b.IdActivity == id, 0, 1, ActivityOrderCriteria.ByType, false).First().ToModel();
|
||||||
|
|
||||||
|
if (activity != null)
|
||||||
|
_logger.LogInformation($"Retrieved activity with ID {id}");
|
||||||
|
else
|
||||||
|
_logger.LogWarning($"No activity found with ID {id}");
|
||||||
|
|
||||||
|
return await Task.FromResult(activity);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Activity?> AddActivity(Activity activity)
|
public async Task<Activity?> AddActivity(Activity activity)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
try
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Adding new activity");
|
||||||
|
var addedActivity = (await _dataManager.DbContext.AddItem(activity.ToEntity())).ToModel();
|
||||||
|
if (addedActivity != null)
|
||||||
|
_logger.LogInformation($"Added activity with ID {addedActivity.Id}");
|
||||||
|
else
|
||||||
|
_logger.LogError("Failed to add activity");
|
||||||
|
return await Task.FromResult(addedActivity);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error occurred while adding activity");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<Activity?> UpdateActivity(int id, Activity activity)
|
public async Task<Activity?> UpdateActivity(int id, Activity activity)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
try
|
||||||
|
{
|
||||||
|
return activity;
|
||||||
|
// _logger.LogInformation($"Updating activity with ID {id}");
|
||||||
|
// var updatedActivity = await _dataManager.DbContext.ActivitiesSet.UpdateItem<ActivityEntity>(_dataManager.DbContext,(await GetActivityByIdAsync(id)).ToEntity(), activity.ToEntity());
|
||||||
|
// if (updatedActivity != null)
|
||||||
|
// _logger.LogInformation($"Updated activity with ID {id}");
|
||||||
|
// else
|
||||||
|
// _logger.LogError($"Failed to update activity with ID {id}");
|
||||||
|
// return await Task.FromResult(updatedActivity.ToModel());
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, $"Error occurred while updating activity with ID {id}");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<bool> DeleteActivity(int id)
|
public async Task<bool> DeleteActivity(int id)
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
try
|
||||||
|
{
|
||||||
|
_logger.LogInformation($"Deleting activity with ID {id}");
|
||||||
|
var isDeleted = await _dataManager.DbContext.DeleteItem<ActivityEntity>(id);
|
||||||
|
if (isDeleted)
|
||||||
|
_logger.LogInformation($"Deleted activity with ID {id}");
|
||||||
|
else
|
||||||
|
_logger.LogWarning($"No activity found with ID {id}");
|
||||||
|
return await Task.FromResult(isDeleted);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, $"Error occurred while deleting activity with ID {id}");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<int> GetNbItems()
|
public async Task<int> GetNbItems()
|
||||||
{
|
{
|
||||||
throw new NotImplementedException();
|
try
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Getting the total number of activities");
|
||||||
|
var count = await _dataManager.DbContext.ActivitiesSet.CountAsync();
|
||||||
|
_logger.LogInformation($"Total number of activities: {count}");
|
||||||
|
return await Task.FromResult(count);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error occurred while getting the total number of activities");
|
||||||
|
throw;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
namespace Shared;
|
||||||
|
|
||||||
|
public enum DataSourceOrderCriteria
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
ByName,
|
||||||
|
ByDate,
|
||||||
|
ByType,
|
||||||
|
ByContent
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
namespace Shared;
|
||||||
|
|
||||||
|
public static class Extensions
|
||||||
|
{
|
||||||
|
public static U? ToU<T, U>(this T t, GenericMapper<T, U> mapper, Func<T, U> func) where U :class where T :class
|
||||||
|
{
|
||||||
|
var u = mapper.GetU(t);
|
||||||
|
if (u != null) {
|
||||||
|
return u;
|
||||||
|
}
|
||||||
|
u = func(t);
|
||||||
|
mapper.Add(t, u);
|
||||||
|
// action(t, u);
|
||||||
|
return u;
|
||||||
|
}
|
||||||
|
// , Action<T, U> action
|
||||||
|
public static T? ToT<T,U>(this U u, GenericMapper<T, U> mapper, Func<U, T> func) where U :class where T :class
|
||||||
|
{
|
||||||
|
var t = mapper.GetT(u);
|
||||||
|
if (t != null) {
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
t = func(u);
|
||||||
|
mapper.Add(t, u);
|
||||||
|
// action(t, u);
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,34 @@
|
|||||||
|
namespace Shared;
|
||||||
|
|
||||||
|
public class GenericMapper<T,U> where T : class where U : class
|
||||||
|
{
|
||||||
|
private HashSet<Tuple<T,U>> mapper = new HashSet<Tuple<T,U>>();
|
||||||
|
public T? GetT(U u)
|
||||||
|
{
|
||||||
|
var found = mapper.Where(t => ReferenceEquals(t.Item2, u));
|
||||||
|
if (found.Count() != 1)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return found.First().Item1;
|
||||||
|
}
|
||||||
|
public U? GetU(T t)
|
||||||
|
{
|
||||||
|
var found = mapper.Where(t => ReferenceEquals(t.Item1, t));
|
||||||
|
if (found.Count() != 1)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return found.First().Item2;
|
||||||
|
}
|
||||||
|
public void Add(T model, U entity)
|
||||||
|
{
|
||||||
|
var tuple = new Tuple<T, U>(model, entity);
|
||||||
|
mapper.Add(tuple);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void Reset()
|
||||||
|
{
|
||||||
|
mapper.Clear();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
namespace Shared;
|
||||||
|
|
||||||
|
public enum HeartRateOrderCriteria
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
ByDate,
|
||||||
|
ByValue,
|
||||||
|
ByActivity,
|
||||||
|
ByUser
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
namespace Shared;
|
||||||
|
|
||||||
|
public enum NotificationOrderCriteria
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
ByDate,
|
||||||
|
ByType,
|
||||||
|
BySender,
|
||||||
|
ByReceiver,
|
||||||
|
ByContent
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
namespace Shared;
|
||||||
|
|
||||||
|
public enum StatisticOrderCriteria
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
ByDate,
|
||||||
|
ByType,
|
||||||
|
ByValue,
|
||||||
|
ByUser
|
||||||
|
}
|
@ -0,0 +1,12 @@
|
|||||||
|
namespace Shared;
|
||||||
|
|
||||||
|
public enum TrainingOrderCriteria
|
||||||
|
{
|
||||||
|
None,
|
||||||
|
ByDate,
|
||||||
|
ByType,
|
||||||
|
ByDuration,
|
||||||
|
ByDistance,
|
||||||
|
ByCalories,
|
||||||
|
ByUser
|
||||||
|
}
|
@ -1,6 +0,0 @@
|
|||||||
namespace SharedEF;
|
|
||||||
|
|
||||||
public class Activity
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
namespace SharedEF;
|
|
||||||
|
|
||||||
public interface IActivityRepository : IGenericRepository<Activity>
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
@ -1,12 +0,0 @@
|
|||||||
namespace SharedEF;
|
|
||||||
|
|
||||||
public interface IGenericRepository<T>
|
|
||||||
{
|
|
||||||
Task<IEnumerable<T>> GetItems(int index, int count, string? orderingProperty = null, bool descending = false);
|
|
||||||
Task<T?> GetItemById(int id);
|
|
||||||
Task<T?> UpdateItem(int oldItem, T newItem);
|
|
||||||
Task<T?> AddItem(T item);
|
|
||||||
Task<bool> DeleteItem(int item);
|
|
||||||
Task<int> GetNbItems();
|
|
||||||
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
namespace SharedEF;
|
|
||||||
|
|
||||||
public interface IUserRepository: IGenericRepository<User>
|
|
||||||
{
|
|
||||||
public Task<IEnumerable<User>> GetUsers(int index, int count, AthleteOrderCriteria? criteria , bool descending = false);
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
namespace SharedEF;
|
|
||||||
|
|
||||||
public class User
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue