From 16b7c3051eff221022f339b9f1815f229a7ccfc4 Mon Sep 17 00:00:00 2001 From: dave Date: Sat, 16 Mar 2024 08:13:39 +0100 Subject: [PATCH] OK seems to be good but the DB is fucked up i cannot access to property by incluing them => it's null and throw error no Collum "nameOfProperty" --- src/APIMappers/ActivityMapper.cs | 71 +++++--- src/APIMappers/DataSourceMapper.cs | 51 ++++++ src/APIMappers/HeartRateMapper.cs | 45 +++++ src/APIMappers/LargeImageMapper.cs | 12 ++ src/APIMappers/UserMappeur.cs | 6 + src/DbContextLib/HeartTrackContext.cs | 13 +- src/Dto/ActivityDto.cs | 10 +- src/Dto/AthleteDto.cs | 4 +- src/Dto/DataSourceDto.cs | 16 ++ src/Dto/HeartRateDto.cs | 2 + src/Dto/LargeImageDto.cs | 6 + src/Dto/NotificationDto.cs | 1 + src/Dto/StatisticDto.cs | 1 + src/Dto/TrainingDto.cs | 1 + src/EFMappers/ActivityMapper.cs | 99 ++++++----- src/EFMappers/AthleteMappeur.cs | 167 +++++++----------- src/EFMappers/DataSourceMapper.cs | 51 ++++++ src/EFMappers/HeartRateMapper.cs | 56 ++++++ src/EFMappers/LargeImageMapper.cs | 12 ++ src/Entities/ActivityEntity.cs | 6 +- src/Entities/AthleteEntity.cs | 12 +- src/Entities/HeartRateEntity.cs | 28 ++- src/Entities/LargeImageEntity.cs | 11 ++ src/Model/Activity.cs | 32 +++- src/Model/DataSource.cs | 29 +++ src/Model/HeartRate.cs | 44 +++++ src/Model/LargeImage.cs | 25 +++ src/Model/User.cs | 12 +- src/Model2Entities/ActivityRepository.cs | 10 +- src/Model2Entities/DbDataManager.cs | 9 +- src/Model2Entities/Extension.cs | 11 +- src/Shared/Extension.cs | 35 ++-- src/StubAPI/ActivityService.cs | 8 +- .../AthleteStubbedContext.cs | 18 +- src/Tests/ConsoleTestEFMapper/Program.cs | 23 ++- src/Tests/ConsoleTestEntities/Program.cs | 4 +- .../Controllers/UsersControllerTest.cs | 2 +- 37 files changed, 702 insertions(+), 241 deletions(-) create mode 100644 src/APIMappers/DataSourceMapper.cs create mode 100644 src/APIMappers/HeartRateMapper.cs create mode 100644 src/APIMappers/LargeImageMapper.cs create mode 100644 src/Dto/LargeImageDto.cs create mode 100644 src/EFMappers/DataSourceMapper.cs create mode 100644 src/EFMappers/HeartRateMapper.cs create mode 100644 src/EFMappers/LargeImageMapper.cs create mode 100644 src/Entities/LargeImageEntity.cs create mode 100644 src/Model/DataSource.cs create mode 100644 src/Model/HeartRate.cs create mode 100644 src/Model/LargeImage.cs diff --git a/src/APIMappers/ActivityMapper.cs b/src/APIMappers/ActivityMapper.cs index 6013e57..4f61896 100644 --- a/src/APIMappers/ActivityMapper.cs +++ b/src/APIMappers/ActivityMapper.cs @@ -6,18 +6,18 @@ namespace APIMappers; public static class ActivityMapper { - private static GenericMapper _mapper = new GenericMapper(); + private static GenericMapper _mapper = new(); - public static ActivityDto ToDto(this Activity activity) + public static Activity ToModel(this ActivityDto activityDto) { - return activity.ToU(_mapper, activityDto => new ActivityDto + Func create = activity => new Activity { Id = activity.Id, Type = activity.Type, Date = activity.Date, StartTime = activity.StartTime, EndTime = activity.EndTime, - EffortFelt = activity.Effort, + Effort = activity.EffortFelt, Variability = activity.Variability, Variance = activity.Variance, StandardDeviation = activity.StandardDeviation, @@ -26,28 +26,55 @@ public static class ActivityMapper Minimum = activity.Minimum, AverageTemperature = activity.AverageTemperature, HasAutoPause = activity.HasAutoPause - }); + }; + + Action link = (activity, model) => + { + model.HeartRates.AddRange(activity.HeartRates.ToModels(activityDto).ToArray()); + if (activity.DataSource != null) model.DataSource = activity.DataSource.ToModel(); + model.Athlete = activity.Athlete.ToModel(); + }; + + return activityDto.ToT(_mapper, create, link); } - public static Activity ToModel(this ActivityDto activityDto) + public static ActivityDto ToDto(this Activity model) { - return activityDto.ToT(_mapper, activity => new Activity + Func create = activity => new ActivityDto + { + Id = activity.Id, + Type = activity.Type, + Date = activity.Date, + StartTime = activity.StartTime, + EndTime = activity.EndTime, + EffortFelt = activity.Effort, + Variability = activity.Variability, + Variance = activity.Variance, + StandardDeviation = activity.StandardDeviation, + Average = activity.Average, + Maximum = activity.Maximum, + Minimum = activity.Minimum, + AverageTemperature = activity.AverageTemperature, + HasAutoPause = activity.HasAutoPause + }; + + Action link = (activity, dto) => { - Id = activityDto.Id, - Type = activityDto.Type, - Date = activityDto.Date, - StartTime = activityDto.StartTime, - EndTime = activityDto.EndTime, - Effort = activityDto.EffortFelt, - Variability = activityDto.Variability, - Variance = activityDto.Variance, - StandardDeviation = activityDto.StandardDeviation, - Average = activityDto.Average, - Maximum = activityDto.Maximum, - Minimum = activityDto.Minimum, - AverageTemperature = activityDto.AverageTemperature, - HasAutoPause = activityDto.HasAutoPause - }); + dto.HeartRates = activity.HeartRates.ToDtos(model).ToArray(); + dto.DataSource = activity.DataSource.ToDto(); + dto.Athlete = activity.Athlete.ToDto(); + }; + + return model.ToU(_mapper, create, link); } + + public static IEnumerable ToModels(this IEnumerable dtos) + => dtos.Select(dto => dto.ToModel()); + + public static IEnumerable ToDtos(this IEnumerable models) + => models.Select(model => model.ToDto()); + + + } \ No newline at end of file diff --git a/src/APIMappers/DataSourceMapper.cs b/src/APIMappers/DataSourceMapper.cs new file mode 100644 index 0000000..130e0e2 --- /dev/null +++ b/src/APIMappers/DataSourceMapper.cs @@ -0,0 +1,51 @@ +using Dto; +using Model; +using Shared; + +namespace APIMappers; + +public static class DataSourceMapper +{ + + private static GenericMapper _mapper = new (); + + public static DataSource ToModel(this DataSourceDto dto) + { + Func create = dataSourceDto => + new DataSource( dataSourceDto.Id, dataSourceDto.Type, dataSourceDto.Model, dataSourceDto.Precision, dataSourceDto.Athletes.ToModels().ToList(), dataSourceDto.Activities.ToModels().ToList()); + /* + Action link = (dataSourceDto, model) => + { + model.Activities.AddRange(dataSourceDto.Activities.ToModels()); + model.Athletes.AddRange(dataSourceDto.Athletes.ToModels()); + };*/ + + return dto.ToT(_mapper, create); + } + + public static DataSourceDto ToDto(this DataSource model) + { + Func create = dataSource => + new DataSourceDto + { + Id = dataSource.Id, + Type = dataSource.Type, + Model = dataSource.Model, + Precision = dataSource.Precision, + }; + Action link = (dataSource, dto) => + { + dto.Activities = dataSource.Activities.ToDtos().ToArray(); + dto.Athletes = dataSource.Athletes.ToDtos().ToArray(); + }; + return model.ToU(_mapper, create, link); + } + + public static IEnumerable ToModels(this IEnumerable dtos) + => dtos.Select(d => d.ToModel()); + + public static IEnumerable ToDtos(this IEnumerable models) + => models.Select(m => m.ToDto()); + + +} \ No newline at end of file diff --git a/src/APIMappers/HeartRateMapper.cs b/src/APIMappers/HeartRateMapper.cs new file mode 100644 index 0000000..6e3925d --- /dev/null +++ b/src/APIMappers/HeartRateMapper.cs @@ -0,0 +1,45 @@ +using Dto; +using Model; +using Shared; + +namespace APIMappers; + +public static class HeartRateMapper +{ + private static GenericMapper _mapper = new(); + + public static HeartRate ToModel(this HeartRateDto dto, ActivityDto activityDto) + { + Func create = heartRateDto => + new HeartRate(heartRateDto.HeartRate, TimeOnly.FromDateTime(heartRateDto.Timestamp), activityDto.ToModel(), heartRateDto.Latitude, heartRateDto.Longitude, heartRateDto.Altitude, heartRateDto.Cadence, heartRateDto.Distance, heartRateDto.Speed, heartRateDto.Power, heartRateDto.Temperature); + + return dto.ToT(_mapper, create); + } + + public static HeartRateDto ToDto(this HeartRate model, Activity activity) + { + Func create = heartRate => + new HeartRateDto + { + Timestamp = new DateTime(activity.Date.Year, activity.Date.Month, activity.Date.Day, heartRate.Timestamp.Hour, heartRate.Timestamp.Minute, heartRate.Timestamp.Second), + Latitude = heartRate.Latitude, + Longitude = heartRate.Longitude, + Altitude = heartRate.Altitude, + HeartRate = heartRate.Bpm, + Cadence = heartRate.Cadence, + Distance = heartRate.Distance, + Speed = heartRate.Speed, + Power = heartRate.Power, + Temperature = heartRate.Temperature + }; + return model.ToU(_mapper, create); + } + + public static IEnumerable ToModels(this IEnumerable dtos, ActivityDto activityDto) + => dtos.Select(d => d.ToModel(activityDto)); + + public static IEnumerable ToDtos(this IEnumerable models, Activity activity) + => models.Select(m => m.ToDto(activity)); + + +} \ No newline at end of file diff --git a/src/APIMappers/LargeImageMapper.cs b/src/APIMappers/LargeImageMapper.cs new file mode 100644 index 0000000..f198415 --- /dev/null +++ b/src/APIMappers/LargeImageMapper.cs @@ -0,0 +1,12 @@ +using Dto; +using Model; + +namespace APIMappers; + +public static class LargeImageMapper +{ + public static LargeImageDto ToDto(this LargeImage largeImage) + => new() { Base64 = largeImage.Base64 }; + + public static LargeImage ToModel(this LargeImageDto largeImageDto) => new(largeImageDto.Base64); +} \ No newline at end of file diff --git a/src/APIMappers/UserMappeur.cs b/src/APIMappers/UserMappeur.cs index 48a8cda..6dbd1fc 100644 --- a/src/APIMappers/UserMappeur.cs +++ b/src/APIMappers/UserMappeur.cs @@ -47,5 +47,11 @@ public static class UserMappeur }); } + + public static IEnumerable ToModels(this IEnumerable dtos) + => dtos.Select(dto => dto.ToModel()); + + public static IEnumerable ToDtos(this IEnumerable models) + => models.Select(model => model.ToDto()); } \ No newline at end of file diff --git a/src/DbContextLib/HeartTrackContext.cs b/src/DbContextLib/HeartTrackContext.cs index bf16d85..dba0072 100644 --- a/src/DbContextLib/HeartTrackContext.cs +++ b/src/DbContextLib/HeartTrackContext.cs @@ -9,7 +9,6 @@ using Entities; using Microsoft.EntityFrameworkCore; - namespace DbContextLib { /// @@ -51,6 +50,12 @@ namespace DbContextLib /// Gets or sets the set of trainings. /// public DbSet TrainingsSet { get; set; } + + /// + /// Gets or sets the set of large images. + /// + public DbSet LargeImages { get; set; } + /// /// Initializes a new instance of the class. @@ -219,13 +224,13 @@ namespace DbContextLib .HasMany(d => d.Activities) .WithOne(a => a.DataSource) .HasForeignKey(a => a.DataSourceId) - .IsRequired(); + .IsRequired(false); modelBuilder.Entity() - .HasMany(ds => ds.Activities) + .HasMany(ds => ds.Athletes) .WithOne(at => at.DataSource) .HasForeignKey(at => at.DataSourceId) - .IsRequired(false); + .IsRequired(); // modelBuilder.Entity() // .HasMany(fer => fer.Followers) diff --git a/src/Dto/ActivityDto.cs b/src/Dto/ActivityDto.cs index bff6ae6..003bdcc 100644 --- a/src/Dto/ActivityDto.cs +++ b/src/Dto/ActivityDto.cs @@ -16,12 +16,10 @@ public class ActivityDto public int Minimum { get; set; } public float AverageTemperature { get; set; } public bool HasAutoPause { get; set; } + public DataSourceDto? DataSource { get; set; } - public int AthleteId { get; set; } + public UserDto? Athlete { get; set; } - public int DataSourceId { get; set; } - - public int? TrainingId { get; set; } - - public IEnumerable HeartRates { get; set; } // = new List(); + // public int? TrainingId { get; set; } + public IEnumerable HeartRates { get; set; } } \ No newline at end of file diff --git a/src/Dto/AthleteDto.cs b/src/Dto/AthleteDto.cs index 9ee369d..bb4b73e 100644 --- a/src/Dto/AthleteDto.cs +++ b/src/Dto/AthleteDto.cs @@ -17,7 +17,9 @@ public class UserDto public float Weight { get; set; } public string? Password { get; set; } public DateTime DateOfBirth { get; set; } + public string ProfilePicture { get; set; } = "https://davidalmeida.site/assets/me_avatar.f77af006.png"; - public string ProfilePicture { get; set; } = "default.jpg"; + public LargeImageDto Image { get; set; } + public bool IsCoach { get; set; } } diff --git a/src/Dto/DataSourceDto.cs b/src/Dto/DataSourceDto.cs index e69de29..f30de8f 100644 --- a/src/Dto/DataSourceDto.cs +++ b/src/Dto/DataSourceDto.cs @@ -0,0 +1,16 @@ +namespace Dto; + +public class DataSourceDto +{ + public int Id { get; set; } + + public string? Type { get; set; } + + public string Model { get; set; } + + public float Precision { get; set; } + + public IEnumerable? Athletes { get; set; } + + public IEnumerable? Activities { get; set; } +} \ No newline at end of file diff --git a/src/Dto/HeartRateDto.cs b/src/Dto/HeartRateDto.cs index 12d815e..a0d3a40 100644 --- a/src/Dto/HeartRateDto.cs +++ b/src/Dto/HeartRateDto.cs @@ -1,3 +1,5 @@ +namespace Dto; + public class HeartRateDto { public DateTime Timestamp { get; set; } diff --git a/src/Dto/LargeImageDto.cs b/src/Dto/LargeImageDto.cs new file mode 100644 index 0000000..a7e6783 --- /dev/null +++ b/src/Dto/LargeImageDto.cs @@ -0,0 +1,6 @@ +namespace Dto; + +public class LargeImageDto +{ + public string Base64 { get; set; } +} \ No newline at end of file diff --git a/src/Dto/NotificationDto.cs b/src/Dto/NotificationDto.cs index e69de29..25a4442 100644 --- a/src/Dto/NotificationDto.cs +++ b/src/Dto/NotificationDto.cs @@ -0,0 +1 @@ +namespace Dto; \ No newline at end of file diff --git a/src/Dto/StatisticDto.cs b/src/Dto/StatisticDto.cs index e69de29..25a4442 100644 --- a/src/Dto/StatisticDto.cs +++ b/src/Dto/StatisticDto.cs @@ -0,0 +1 @@ +namespace Dto; \ No newline at end of file diff --git a/src/Dto/TrainingDto.cs b/src/Dto/TrainingDto.cs index e69de29..25a4442 100644 --- a/src/Dto/TrainingDto.cs +++ b/src/Dto/TrainingDto.cs @@ -0,0 +1 @@ +namespace Dto; \ No newline at end of file diff --git a/src/EFMappers/ActivityMapper.cs b/src/EFMappers/ActivityMapper.cs index 81c94c5..efa6468 100644 --- a/src/EFMappers/ActivityMapper.cs +++ b/src/EFMappers/ActivityMapper.cs @@ -6,61 +6,70 @@ namespace EFMappers; public static class ActivityMapper { - private static GenericMapper _mapper = new GenericMapper(); - // ! RESET - // ? Quand on fait appel au reset ? - // * Apres des saves changing ou rollback. + private static GenericMapper _mapper = new (); 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); + public static Activity ToModel(this ActivityEntity entity) + { + Func create = activityEntity => new Activity + { + Id = activityEntity.IdActivity, + Type = activityEntity.Type, + Date = activityEntity.Date.ToDateTime(TimeOnly.MinValue), + StartTime = activityEntity.Date.ToDateTime(activityEntity.StartTime), + EndTime = activityEntity.Date.ToDateTime(activityEntity.EndTime), + Effort = activityEntity.EffortFelt, + Variability = activityEntity.Variability, + Variance = activityEntity.Variance, + StandardDeviation = activityEntity.StandardDeviation, + Average = activityEntity.Average, + Maximum = activityEntity.Maximum, + Minimum = activityEntity.Minimum, + AverageTemperature = activityEntity.AverageTemperature, + HasAutoPause = activityEntity.HasAutoPause + }; + Console.WriteLine("ActivityMapper.ToModel"); + // here + + Action link = (activityEntity, model) => + { + model.HeartRates.AddRange(activityEntity.HeartRates?.ToModels()); + model.DataSource = activityEntity.DataSource.ToModel(); + model.Athlete = activityEntity.Athlete.ToModel(); + }; + return entity.ToT(_mapper, create, link); } - // dictionnaire; - public static ActivityEntity? ToEntity(this Activity model) + public static ActivityEntity ToEntity(this Activity model) { - // return model.ToEntity(); - return model.ToU(_mapper, activityEntity => new ActivityEntity + Func create = activity => new ActivityEntity + { + IdActivity = activity.Id, + Type = activity.Type, + Date = DateOnly.FromDateTime(activity.Date), + StartTime = TimeOnly.FromDateTime(activity.StartTime), + EndTime = TimeOnly.FromDateTime(activity.EndTime), + EffortFelt = activity.Effort, + Variability = activity.Variability, + Variance = activity.Variance, + StandardDeviation = activity.StandardDeviation, + Average = activity.Average, + Maximum = activity.Maximum, + Minimum = activity.Minimum, + AverageTemperature = activity.AverageTemperature, + HasAutoPause = activity.HasAutoPause + }; + Action link = (activity, entity) => { - 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 - ); + entity.HeartRates = activity.HeartRates.ToEntities().ToList(); + entity.DataSource = activity.DataSource.ToEntity(); + entity.Athlete = activity.Athlete.ToEntity(); + }; + return model.ToU(_mapper, create, link); } public static IEnumerable ToModels(this IEnumerable entities) diff --git a/src/EFMappers/AthleteMappeur.cs b/src/EFMappers/AthleteMappeur.cs index 48dea10..185ae3c 100644 --- a/src/EFMappers/AthleteMappeur.cs +++ b/src/EFMappers/AthleteMappeur.cs @@ -1,117 +1,86 @@ using System.Buffers; using Dto; +using Entities; using Model; +using Shared; -namespace ApiMappeur; +namespace EFMappers; public static class UserMappeur { - private static readonly ArrayPool UserDtoPool = ArrayPool.Create(); - private static readonly Dictionary userToDtoMap = new Dictionary(); - private static readonly Dictionary dtoToUserMap = new Dictionary(); - public static UserDto ToDto(this User user) + private static GenericMapper _mapper = new (); + + public static User ToModel(this AthleteEntity entity) { - return new UserDto + Func create = athleteEntity => new User { - Id = user.Id, - Username = user.Username, - ProfilePicture = user.ProfilePicture, - LastName = user.LastName, - FirstName = user.FirstName, - Email = user.Email, - Password = user.MotDePasse, - Sexe = user.Sexe, - Lenght = user.Lenght, - Weight = user.Weight, - DateOfBirth = user.DateOfBirth, - IsCoach = user.Role is Coach + Id = athleteEntity.IdAthlete, + FirstName = athleteEntity.FirstName, + LastName = athleteEntity.LastName, + Email = athleteEntity.Email, + MotDePasse = athleteEntity.Password, + DateOfBirth = athleteEntity.DateOfBirth.ToDateTime(TimeOnly.MinValue), + Sexe = athleteEntity.Sexe, + Username = athleteEntity.Username, + Weight = athleteEntity.Weight, + Lenght = (float)athleteEntity.Length, + ProfilePicture = athleteEntity.ProfilPicture, + // Role = athleteEntity.IsCoach ? new Coach() : new Athlete(), }; - } - - public static User ToModel(this UserDto userDto) - { - return new User + + Action link = (athleteEntity, model) => { - Username = userDto.Username, - ProfilePicture = userDto.ProfilePicture, - LastName = userDto.LastName, - FirstName = userDto.FirstName, - Email = userDto.Email, - MotDePasse = userDto.Password, - Sexe = userDto.Sexe, - Lenght = userDto.Lenght, - Weight = userDto.Weight, - DateOfBirth = userDto.DateOfBirth, - Role = userDto.IsCoach ? new Coach() : new Athlete() - + model.Role = athleteEntity.IsCoach ? new Coach() : new Athlete(); + model.DataSources.Add(athleteEntity.DataSource.ToModel()); + model.Activities.AddRange(athleteEntity.Activities.ToModels()); + model.Image = athleteEntity.Image.ToModel(); }; + + return entity.ToT(_mapper, create, link); } -} -/* -using Dto; -using Model; -using System.Buffers; -namespace ApiMappeur -{ - // anotine - public static class UserMappeur + public static AthleteEntity ToEntity(this User model) { - private static readonly ArrayPool UserDtoPool = ArrayPool.Create(); - - public static UserDto ToDto(this User user) + Func create = user => new AthleteEntity { - UserDto userDto = UserDtoPool.Rent(); - userDto.Id = user.Id; - userDto.Username = user.Username; - userDto.ProfilePicture = user.ProfilePicture; - userDto.LastName = user.LastName; - userDto.FirstName = user.FirstName; - userDto.Email = user.Email; - userDto.Password = user.MotDePasse; - userDto.Sexe = user.Sexe; - userDto.Lenght = user.Lenght; - userDto.Weight = user.Weight; - userDto.DateOfBirth = user.DateOfBirth; - userDto.IsCoach = user.Role is Coach; - return userDto; - } - - public static User ToModel(this UserDto userDto) + IdAthlete = user.Id, + Username = user.Username, + Sexe = user.Sexe, + FirstName = user.FirstName, + LastName = user.LastName, + Email = user.Email, + Password = user.MotDePasse, + DateOfBirth = DateOnly.FromDateTime(user.DateOfBirth), + IsCoach = user.Role is Coach, + Weight = user.Weight, + Length = user.Lenght, + ProfilPicture = user.ProfilePicture, + }; + + Action link = (user, entity) => { - if (userDto.IsCoach) - { - return new User( - userDto.Username, - userDto.ProfilePicture, - userDto.LastName, - userDto.FirstName, - userDto.Email, - userDto.Password, - userDto.Sexe, - userDto.Lenght, - userDto.Weight, - userDto.DateOfBirth, - new Coach() - ); - } - return new User( - userDto.Username, - userDto.ProfilePicture, - userDto.LastName, - userDto.FirstName, - userDto.Email, - userDto.Password, - userDto.Sexe, - userDto.Lenght, - userDto.Weight, - userDto.DateOfBirth, - new Athlete()); - } + entity.DataSource = user.DataSources.ToEntities().First(); + entity.Activities = user.Activities.ToEntities().ToList(); + entity.IsCoach = user.Role is Coach; + entity.Image = user.Image.ToEntity(); + /*if (user.Role is Coach) + entity.TrainingsCoach = user.Traning.ToEntities().ToList(); + else + entity.TrainingsAthlete = user.Traning.ToEntities().ToList(); + */ + // entity.NotificationsReceived = user.Notifications.ToEntities().ToList(); - public static void ReturnToPool(this UserDto userDto) - { - UserDtoPool.Return(userDto); - } + // entity.DataSource = user.DataSources.ToEntities().ToList(); + + // [TODO] [DAVE] : Add the link to the friendship + + }; + + return model.ToU(_mapper, create, link); } -} -*/ \ No newline at end of file + + public static IEnumerable ToModels(this IEnumerable entities) + => entities.Select(e => e.ToModel()); + + public static IEnumerable ToEntities(this IEnumerable models) + => models.Select(m => m.ToEntity()); +} \ No newline at end of file diff --git a/src/EFMappers/DataSourceMapper.cs b/src/EFMappers/DataSourceMapper.cs new file mode 100644 index 0000000..2295d1a --- /dev/null +++ b/src/EFMappers/DataSourceMapper.cs @@ -0,0 +1,51 @@ +using Entities; +using Model; +using Shared; + +namespace EFMappers; + +public static class DataSourceMapper +{ + + private static GenericMapper _mapper = new (); + + public static DataSource ToModel(this DataSourceEntity entity) + { + Func create = dataSourceEntity => + new DataSource( dataSourceEntity.IdSource, dataSourceEntity.Type, dataSourceEntity.Model, dataSourceEntity.Precision, dataSourceEntity.Athletes.ToModels().ToList(), dataSourceEntity.Activities.ToModels().ToList()); + /* + Action link = (dataSourceEntity, model) => + { + model.Activities.AddRange(dataSourceEntity.Activities.ToModels()); + model.Athletes.AddRange(dataSourceEntity.Athletes.ToModels()); + };*/ + + return entity.ToT(_mapper, create); + } + + public static DataSourceEntity ToEntity(this DataSource model) + { + Func create = dataSource => + new DataSourceEntity + { + IdSource = dataSource.Id, + Type = dataSource.Type, + Model = dataSource.Model, + Precision = dataSource.Precision + }; + + Action link = (dataSource, entity) => + { + entity.Activities = dataSource.Activities.ToEntities().ToList(); + entity.Athletes = dataSource.Athletes.ToEntities().ToList(); + }; + + return model.ToU(_mapper, create, link); + } + + public static IEnumerable ToModels(this IEnumerable entities) + => entities.Select(e => e.ToModel()); + + public static IEnumerable ToEntities(this IEnumerable models) + => models.Select(m => m.ToEntity()); +} \ No newline at end of file diff --git a/src/EFMappers/HeartRateMapper.cs b/src/EFMappers/HeartRateMapper.cs new file mode 100644 index 0000000..0f30d36 --- /dev/null +++ b/src/EFMappers/HeartRateMapper.cs @@ -0,0 +1,56 @@ +using Entities; +using Model; +using Shared; + +namespace EFMappers; + +public static class HeartRateMapper +{ + private static GenericMapper _mapper = new (); + + public static HeartRate ToModel(this HeartRateEntity entity) + { + Func create = heartRateEntity => + new HeartRate(heartRateEntity.IdHeartRate, heartRateEntity.Bpm, heartRateEntity.Time, heartRateEntity.Activity.ToModel(),heartRateEntity.Latitude, heartRateEntity.Longitude, heartRateEntity.Altitude, heartRateEntity.Cadence, heartRateEntity.Distance, heartRateEntity.Speed, heartRateEntity.Power, heartRateEntity.Temperature); + + Action link = (heartRateEntity, model) => + { + model.Activity = heartRateEntity.Activity.ToModel(); + }; + + return entity.ToT(_mapper, create, link); + } + + public static HeartRateEntity ToEntity(this HeartRate model) + { + Func create = heartRate => + new HeartRateEntity + { + IdHeartRate = heartRate.Id, + Bpm = heartRate.Bpm, + Time = heartRate.Timestamp, + Latitude = heartRate.Latitude, + Longitude = heartRate.Longitude, + Altitude = heartRate.Altitude, + Cadence = heartRate.Cadence??0, + Distance = heartRate.Distance, + Speed = heartRate.Speed, + Power = heartRate.Power, + Temperature = heartRate.Temperature + }; + + Action link = (heartRate, entity) => + { + entity.Activity = heartRate.Activity.ToEntity(); + }; + + return model.ToU(_mapper, create, link); + } + + public static IEnumerable ToModels(this IEnumerable entities) + => entities.Select(h => h.ToModel()); + + public static IEnumerable ToEntities(this IEnumerable models) + => models.Select(h => h.ToEntity()); + +} \ No newline at end of file diff --git a/src/EFMappers/LargeImageMapper.cs b/src/EFMappers/LargeImageMapper.cs new file mode 100644 index 0000000..644351b --- /dev/null +++ b/src/EFMappers/LargeImageMapper.cs @@ -0,0 +1,12 @@ +using Entities; +using Model; + +namespace EFMappers; + +public static class LargeImageMapper +{ + public static LargeImage ToModel(this LargeImageEntity largeImage) => new(largeImage.Base64); + + public static LargeImageEntity ToEntity(this LargeImage largeImage) => new() { Base64 = largeImage.Base64 }; + +} \ No newline at end of file diff --git a/src/Entities/ActivityEntity.cs b/src/Entities/ActivityEntity.cs index 5dc7761..351ef5a 100644 --- a/src/Entities/ActivityEntity.cs +++ b/src/Entities/ActivityEntity.cs @@ -97,11 +97,11 @@ namespace Entities /// public bool HasAutoPause { get; set; } - public ICollection HeartRates { get; set; } = new List(); + public ICollection? HeartRates { get; set; } = new List(); - public int DataSourceId { get; set; } + public int? DataSourceId { get; set; } - public DataSourceEntity DataSource { get; set; } = null!; + public DataSourceEntity? DataSource { get; set; } = null!; public int AthleteId { get; set; } diff --git a/src/Entities/AthleteEntity.cs b/src/Entities/AthleteEntity.cs index 6d6f351..89b184a 100644 --- a/src/Entities/AthleteEntity.cs +++ b/src/Entities/AthleteEntity.cs @@ -17,8 +17,6 @@ namespace Entities [Table("Athlete")] public class AthleteEntity { - public AthleteEntity() : base() { } - /// /// Gets or sets the unique identifier of the athlete. /// @@ -89,9 +87,13 @@ namespace Entities /// public bool IsCoach { get; set; } - public byte[]? ProfilPicture { get; set; } + // [TODO] [DAVE] Pourquoi c'est un byte[] ? et pas un string ? it's me so should change it to string + public string? ProfilPicture { get; set; } - + public LargeImageEntity? Image { get; set; } + + [ForeignKey("Image")] + public Guid? ImageId { get; set; } public ICollection Activities { get; set; } = new List(); @@ -104,7 +106,7 @@ namespace Entities public ICollection NotificationsSent { get; set; } = new List(); 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 Followers { get; set; } = []; diff --git a/src/Entities/HeartRateEntity.cs b/src/Entities/HeartRateEntity.cs index 8104a99..21c0c70 100644 --- a/src/Entities/HeartRateEntity.cs +++ b/src/Entities/HeartRateEntity.cs @@ -27,7 +27,7 @@ namespace Entities /// /// Gets or sets the altitude. /// - public double Altitude { get; set; } + public double? Altitude { get; set; } /// /// Gets or sets the time of the heart rate measurement. @@ -39,7 +39,7 @@ namespace Entities /// /// Gets or sets the temperature. /// - public float Temperature { get; set; } + public double? Temperature { get; set; } /// /// Gets or sets the heart rate in beats per minute (bpm). @@ -49,12 +49,32 @@ namespace Entities /// /// Gets or sets the longitude. /// - public float Longitude { get; set; } + public double? Longitude { get; set; } /// /// Gets or sets the latitude. /// - public float Latitude { get; set; } + public double? Latitude { get; set; } + + /// + /// Gets or sets the cadence. + /// + public int Cadence { get; set; } + + /// + /// Gets or sets the distance. + /// + public double? Distance { get; set; } + + /// + /// Gets or sets the speed. + /// + public double? Speed { get; set; } + + /// + /// Gets or sets the power. + /// + public int? Power { get; set; } public int ActivityId { get; set; } diff --git a/src/Entities/LargeImageEntity.cs b/src/Entities/LargeImageEntity.cs new file mode 100644 index 0000000..dfe917e --- /dev/null +++ b/src/Entities/LargeImageEntity.cs @@ -0,0 +1,11 @@ +using System.ComponentModel.DataAnnotations; + +namespace Entities; + +public class LargeImageEntity +{ + [Key] + public Guid Id { get; set; } + public string Base64 { get; set; } + +} \ No newline at end of file diff --git a/src/Model/Activity.cs b/src/Model/Activity.cs index cef25b7..bdb3e79 100644 --- a/src/Model/Activity.cs +++ b/src/Model/Activity.cs @@ -10,6 +10,7 @@ public class Activity public DateTime EndTime { get; set; } private int _effort; + [Range(0, 5)] public int Effort { @@ -32,12 +33,32 @@ public class Activity public float AverageTemperature { get; set; } public bool HasAutoPause { get; set; } - public HashSet Users { get; private set; } = new HashSet(); - public Activity(int idActivity ,string? type, DateTime date, DateTime startTime, DateTime endTime, - int effort, float variability, float variance, float standardDeviation, - float average, int maximum, int minimum, float averageTemperature, bool hasAutoPause) + public User Athlete { get; set; } + public DataSource? DataSource { get; set; } + public List HeartRates { get; set; } = new List(); + + public Activity(int id, string type, DateTime date, DateTime startTime, DateTime endTime, int effort, float variability, float variance, float standardDeviation, float average, int maximum, int minimum, float averageTemperature, bool hasAutoPause, User user, DataSource dataSource, List heartRates) { - Id = idActivity; + Id = id; + Type = type; + Date = date; + StartTime = startTime; + EndTime = endTime; + Effort = effort; + Variability = variability; + Variance = variance; + StandardDeviation = standardDeviation; + Average = average; + Maximum = maximum; + Minimum = minimum; + AverageTemperature = averageTemperature; + HasAutoPause = hasAutoPause; + Athlete = user; + DataSource = dataSource; + HeartRates = heartRates; + } + public Activity(int id, string type, DateTime date, DateTime startTime, DateTime endTime, int effort, float variability, float variance, float standardDeviation, float average, int maximum, int minimum, float averageTemperature, bool hasAutoPause, User user){ + Id = id; Type = type; Date = date; StartTime = startTime; @@ -51,6 +72,7 @@ public class Activity Minimum = minimum; AverageTemperature = averageTemperature; HasAutoPause = hasAutoPause; + Athlete = user; } public Activity(){} diff --git a/src/Model/DataSource.cs b/src/Model/DataSource.cs new file mode 100644 index 0000000..e073dba --- /dev/null +++ b/src/Model/DataSource.cs @@ -0,0 +1,29 @@ +namespace Model; + +public class DataSource +{ + public int Id { get; set; } + public string Type { get; set; } + public string Model { get; set; } + public float Precision { get; set; } + public ICollection Activities { get; set; } = new List(); + public ICollection Athletes { get; set; } + + public DataSource(int id, string type, string model, float precision, ICollection athletes, ICollection? activities) + { + Id = id; + Type = type; + Model = model; + Precision = precision; + Athletes = athletes; + Activities = activities; + } + public DataSource(string type, string model, float precision, ICollection athletes, ICollection? activities) + : this(0, type, model, precision, athletes, activities) + {} + + public override string ToString() + { + return $"DataSource #{Id}: {Type} {Model} with a precision of {Precision}"; + } +} \ No newline at end of file diff --git a/src/Model/HeartRate.cs b/src/Model/HeartRate.cs new file mode 100644 index 0000000..1742023 --- /dev/null +++ b/src/Model/HeartRate.cs @@ -0,0 +1,44 @@ +namespace Model; + +public class HeartRate +{ + public int Id { get; set; } + public int Bpm { get; set; } + public TimeOnly Timestamp { get; set; } + public Activity Activity { get; set; } + public double? Latitude { get; set; } + public double? Longitude { get; set; } + public double? Altitude { get; set; } + public int? Cadence { get; set; } + public double? Distance { get; set; } + public double? Speed { get; set; } + public int? Power { get; set; } + public double? Temperature { get; set; } + + public HeartRate(int id, int bpm, TimeOnly timestamp, Activity activity, double? latitude, double? longitude, double? altitude, int? cadence, double? distance, double? speed, int? power, double? temperature) + { + Id = id; + Bpm = bpm; + Timestamp = timestamp; + Activity = activity; + Latitude = latitude; + Longitude = longitude; + Altitude = altitude; + Cadence = cadence; + Distance = distance; + Speed = speed; + Power = power; + Temperature = temperature; + } + + + public HeartRate(int bpm, TimeOnly timestamp, Activity activity, double? latitude, double? longitude, double? altitude, int? cadence, double? distance, double? speed, int? power, double? temperature) + : this(0, bpm, timestamp, activity, latitude, longitude, altitude, cadence, distance, speed, power, temperature) + {} + + public override string ToString() + { + return $"HeartRate #{Id}: {Bpm} bpm at {Timestamp:HH:mm:ss} with a temperature of {Temperature}°C" + + $" and an altitude of {Altitude}m at {Longitude}°E and {Latitude}°N"; + } +} \ No newline at end of file diff --git a/src/Model/LargeImage.cs b/src/Model/LargeImage.cs new file mode 100644 index 0000000..9f28c59 --- /dev/null +++ b/src/Model/LargeImage.cs @@ -0,0 +1,25 @@ +namespace Model; + +public class LargeImage : IEquatable +{ + public string Base64 { get; set; } + + public LargeImage(string base64) + { + Base64 = base64; + } + + public bool Equals(LargeImage? other) + => other != null && other.Base64.Equals(Base64); + + public override bool Equals(object? obj) + { + if(ReferenceEquals(obj, null)) return false; + if(ReferenceEquals(obj!, this)) return true; + if(GetType() != obj!.GetType()) return false; + return Equals(obj! as LargeImage); + } + + public override int GetHashCode() + => Base64.Substring(0, 10).GetHashCode(); +} diff --git a/src/Model/User.cs b/src/Model/User.cs index 0991ca5..f2a6e28 100644 --- a/src/Model/User.cs +++ b/src/Model/User.cs @@ -4,7 +4,7 @@ public class User { public int Id { get; set; } public string Username { get; set; } - public string ProfilePicture { get; set; } + public string ProfilePicture { get; set; } = ""; public string LastName { get; set; } public string FirstName { get; set; } public string Email { get; set; } @@ -15,9 +15,12 @@ public class User public DateTime DateOfBirth { get; set; } public Role Role { get; set; } - protected List Notifications { get; set; } = new List(); + public LargeImage Image { get; set; } = new LargeImage(""); + public List Notifications { get; set; } = new List(); + public List Activities { get; set; } = new List(); public List Users { get; set; } = new List(); + public List DataSources { get; set; } = new List(); public User( string username, string profilePicture, string nom, string prenom, string email, string motDePasse, string sexe, float taille, float poids, DateTime dateNaissance, Role role) { @@ -35,9 +38,4 @@ public class User } public User(){} - - - - - } \ No newline at end of file diff --git a/src/Model2Entities/ActivityRepository.cs b/src/Model2Entities/ActivityRepository.cs index a120374..88de3af 100644 --- a/src/Model2Entities/ActivityRepository.cs +++ b/src/Model2Entities/ActivityRepository.cs @@ -28,7 +28,8 @@ public partial class DbDataManager : IDataManager _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(); + var activities = _dataManager.DbContext.ActivitiesSet + .Include(a => a.DataSource).GetItemsWithFilterAndOrdering(b => true, index, count, criteria, descending).ToModels(); _logger.LogInformation($"Retrieved {activities.Count()} activities"); return await Task.FromResult(activities); @@ -89,10 +90,15 @@ public partial class DbDataManager : IDataManager entity.HasAutoPause = activity.HasAutoPause; }); if (updatedActivity != null) + { _logger.LogInformation($"Updated activity with ID {id}"); + return await Task.FromResult(updatedActivity.ToModel()); + } else + { _logger.LogError($"Failed to update activity with ID {id}"); - return await Task.FromResult(updatedActivity!.ToModel()); + return await Task.FromResult(null); + } } catch (Exception ex) { diff --git a/src/Model2Entities/DbDataManager.cs b/src/Model2Entities/DbDataManager.cs index 6384b43..56431f1 100644 --- a/src/Model2Entities/DbDataManager.cs +++ b/src/Model2Entities/DbDataManager.cs @@ -16,8 +16,6 @@ public partial class DbDataManager: IDataManager public DbDataManager(HeartTrackContext dbContext) { DbContext = dbContext; - Console.WriteLine("Contexttttttttt"); - Console.WriteLine($"Database created Context: {DbContext.Database.EnsureCreated()}"); ActivityRepo = new ActivityRepository(this); UserRepo = new UserRepository(this); ActivityMapper.Reset(); @@ -26,14 +24,11 @@ public partial class DbDataManager: IDataManager public DbDataManager(string dbPlatformPath) : this(new HeartTrackContext(dbPlatformPath)) - { - Console.WriteLine($"Database created String: {DbContext.Database.EnsureCreated()}"); } - - + {} + public DbDataManager() { DbContext = new HeartTrackContext(); - Console.WriteLine($"Database created None: {DbContext.Database.EnsureCreated()}"); ActivityRepo = new ActivityRepository(this); UserRepo= new UserRepository(this); } diff --git a/src/Model2Entities/Extension.cs b/src/Model2Entities/Extension.cs index d1e33cc..21fa02e 100644 --- a/src/Model2Entities/Extension.cs +++ b/src/Model2Entities/Extension.cs @@ -14,7 +14,13 @@ public static class Extensions return await Task.FromResult(null); } var entry = context.Set().Add(item); - await context.SaveChangesAsync(); + try { + await context.SaveChangesAsync(); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message, ex.InnerException, ex.StackTrace); + } return await Task.FromResult(entry.Entity); } @@ -41,9 +47,10 @@ public static class Extensions // Enregistrer les modifications dans la base de données await context.SaveChangesAsync(); + return existingT; } + return Task.FromResult(null).Result; - return existingT; } diff --git a/src/Shared/Extension.cs b/src/Shared/Extension.cs index 6301ad7..7682ebd 100644 --- a/src/Shared/Extension.cs +++ b/src/Shared/Extension.cs @@ -2,27 +2,36 @@ namespace Shared; public static class Extensions { - public static U ToU(this T t, GenericMapper mapper, Func func) where U :class where T :class + public static U ToU(this T t, GenericMapper mapper, Func func,Action? action = null) where U :class where T :class { - var u = mapper.GetU(t); - if (u != null) { - return u; + var res = mapper.GetU(t); + if (res != null) { + return res; } - u = func(t); + U u = func(t); mapper.Add(t, u); - // action(t, u); + if(action != null) action(t, u); return u; } // , Action action - public static T ToT(this U u, GenericMapper mapper, Func func) where U :class where T :class + public static T ToT(this U u, GenericMapper mapper, Func func,Action? action = null) where U :class where T :class { - var t = mapper.GetT(u); - if (t != null) { - return t; - } - t = func(u); + var result = mapper.GetT(u); + + if(result != null) return result; + + T t = func(u); mapper.Add(t, u); - // action(t, u); + if(action != null) action(u, t); + return t; } + + public static void AddRange(this ICollection set, IEnumerable ts) + { + foreach(var t in ts) + { + set.Add(t); + } + } } \ No newline at end of file diff --git a/src/StubAPI/ActivityService.cs b/src/StubAPI/ActivityService.cs index 2b274ae..fe08608 100644 --- a/src/StubAPI/ActivityService.cs +++ b/src/StubAPI/ActivityService.cs @@ -25,12 +25,12 @@ public class ActivityService: IActivityRepository Minimum = 0, AverageTemperature = 20.0f, HasAutoPause = false, - Users = {new User + Athlete = new User { Id = 3, Username = "Athlete3", ProfilePicture = "https://plus.unsplash.com/premium_photo-1705091981693-6006f8a20479?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D", FirstName = "First3", LastName = "Last3", Sexe = "M", Lenght = 190, Weight = 80, DateOfBirth = new DateTime(1994, 3, 3), Email = "ath@ex.fr", Role = new Athlete() - }} + } }, } ); @@ -66,13 +66,13 @@ public class ActivityService: IActivityRepository public async Task?> GetActivitiesByUser(int userId, int index, int count, ActivityOrderCriteria criteria, bool descending = false) { - var activities = _activities.GetItemsWithFilterAndOrdering(c => c.Users.Any(u => u.Id == userId), index, count, + var activities = _activities.GetItemsWithFilterAndOrdering(a => a.Athlete.Id == userId, index, count, criteria != ActivityOrderCriteria.None ? criteria : null, descending); return await Task.FromResult(activities); } public Task GetNbActivitiesByUser(int userId) { - return Task.FromResult(_activities.Count(a => a.Users.Any(u => u.Id == userId))); + return Task.FromResult(_activities.Count(a => a.Athlete.Id == userId)); } } \ No newline at end of file diff --git a/src/StubbedContextLib/AthleteStubbedContext.cs b/src/StubbedContextLib/AthleteStubbedContext.cs index c7890bb..f45140f 100644 --- a/src/StubbedContextLib/AthleteStubbedContext.cs +++ b/src/StubbedContextLib/AthleteStubbedContext.cs @@ -35,14 +35,18 @@ namespace StubbedContextLib protected override void OnModelCreating(ModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); - var picture = System.Text.Encoding.UTF8.GetBytes( - "\"UklGRtwDAABXRUJQVlA4INADAAAwEACdASoqACoAAMASJZgCdMoSCz655ndU4XXAP2yXIge5neM/Qd6WCfO8evoj2S0A/p7+f0An85cBxlLDgPC8jO/0nsl/13/O8vvzj7Af8s/p3/H4FU6td4MCwq23z1H2uzoKIXaqJniPI/bRMf8qzv0Zp+HE1RCBw5WQ1j/JovdM1FS52+QcaAAA/v/+NxU4DpPk3+xQPW7tcmURSo9vC4qc+XMxNVBzEM5E8actDz98gmwTXgD62e9EmG/ervdd2ovFFSuxYppWl/wtaX3rkn0xrt8qOql/5I2jfLOnCU0kALLcW4F/wTjU10qsxZXW9fxauC6OPVRF28sc94V9ocmoSWy+sf6jW3vYkVOh+gE/RE0L6b2d3oFyHmkRJnfYwG8o3p6fv9pivNF5aopIBzFnjzwb/VqSq3/b+MWKFmjr8T1qe4/fITo2vBWEqDyogV3ZVGnDVi2DbiEFVSUr2eXTNZQ9V/D9QC/+vCR5TGyX9QOVBgtAYtm/ZTIwzPEYB9NrV1NeO1/sAz78u0tW59r0I+SO5Jgm3B9i1toRurzHv9EZJ9yZL8nafb/T1FaoPDkuJfM+iPs0j8xnS7TaU/gEK0wCxeDYRYtJx9j4hUQq7pAu/T2yWy0vjcUHki952ZNbXnXxB8m8pV5x9E1sfLj5MZEgpU2XV8RHrVvWniCjsf6vgxmR7+KtwIbMjahitUGtHet1WdL+8MmdL29iQJC37pDXirir1NibxKKhFYRuJ3xW9O0r9+Vnh8diqbBuXqDbYR/MSoHvscOCm2t95dN5WBdRUoD7YCG/ZHWc7Ypv/x/al4fkB2lZlYhVWHxjaoeF9jEPI0gAN5XsvUI6hbzEzWMsNW/1orkNOnlskalgmpI4B2rm4Gc7LNui+MuMBrpnBvLkbYX9exe9g8tu7wLt7ScOjDcL99oOyR89Mh9L8rd4+43+JQyR6tsIfcPJo6T6FxHf11d/MGayJi+SWct/uhvvua0oOh+zXNIaUzgoBmu1XULjkpuA0Ghzctf30jbY1AOM49qbMZRYS9A+0S1HrHPnwRvpQY/Sj4xKPn0gdpv/+iTbKJb8zkPC4/9af0Jvesa+GDG0/iw3TswenMhqlh7BM9MW5txpeblsByx4WnJ/oHv6cc0dmM7tsV36lYkCTUXEf/0eKlnfivnN0g1g+j/Lk9et/uoa6TFCW0HgwFOIVFumEYdT675PfuTrYO5o8ZrWEIHtv2Ctlrv9J3TrslD/iKEwtipGHtn0Vak8B9wLL+kz+CIQ/VG4KJpXjx88CeCC4XaGitEdjAAA\""); + var picture2 = + "https://davidalmeida.site/assets/me_avatar.f77af006.png"; + LargeImageEntity picture = new LargeImageEntity { Id = Guid.Parse("{8d121cdc-6787-4738-8edd-9e026ac16b65}"), Base64 = "UklGRtwDAABXRUJQVlA4INADAAAwEACdASoqACoAAMASJZgCdMoSCz655ndU4XXAP2yXIge5neM/Qd6WCfO8evoj2S0A/p7+f0An85cBxlLDgPC8jO/0nsl/13/O8vvzj7Af8s/p3/H4FU6td4MCwq23z1H2uzoKIXaqJniPI/bRMf8qzv0Zp+HE1RCBw5WQ1j/JovdM1FS52+QcaAAA/v/+NxU4DpPk3+xQPW7tcmURSo9vC4qc+XMxNVBzEM5E8actDz98gmwTXgD62e9EmG/ervdd2ovFFSuxYppWl/wtaX3rkn0xrt8qOql/5I2jfLOnCU0kALLcW4F/wTjU10qsxZXW9fxauC6OPVRF28sc94V9ocmoSWy+sf6jW3vYkVOh+gE/RE0L6b2d3oFyHmkRJnfYwG8o3p6fv9pivNF5aopIBzFnjzwb/VqSq3/b+MWKFmjr8T1qe4/fITo2vBWEqDyogV3ZVGnDVi2DbiEFVSUr2eXTNZQ9V/D9QC/+vCR5TGyX9QOVBgtAYtm/ZTIwzPEYB9NrV1NeO1/sAz78u0tW59r0I+SO5Jgm3B9i1toRurzHv9EZJ9yZL8nafb/T1FaoPDkuJfM+iPs0j8xnS7TaU/gEK0wCxeDYRYtJx9j4hUQq7pAu/T2yWy0vjcUHki952ZNbXnXxB8m8pV5x9E1sfLj5MZEgpU2XV8RHrVvWniCjsf6vgxmR7+KtwIbMjahitUGtHet1WdL+8MmdL29iQJC37pDXirir1NibxKKhFYRuJ3xW9O0r9+Vnh8diqbBuXqDbYR/MSoHvscOCm2t95dN5WBdRUoD7YCG/ZHWc7Ypv/x/al4fkB2lZlYhVWHxjaoeF9jEPI0gAN5XsvUI6hbzEzWMsNW/1orkNOnlskalgmpI4B2rm4Gc7LNui+MuMBrpnBvLkbYX9exe9g8tu7wLt7ScOjDcL99oOyR89Mh9L8rd4+43+JQyR6tsIfcPJo6T6FxHf11d/MGayJi+SWct/uhvvua0oOh+zXNIaUzgoBmu1XULjkpuA0Ghzctf30jbY1AOM49qbMZRYS9A+0S1HrHPnwRvpQY/Sj4xKPn0gdpv/+iTbKJb8zkPC4/9af0Jvesa+GDG0/iw3TswenMhqlh7BM9MW5txpeblsByx4WnJ/oHv6cc0dmM7tsV36lYkCTUXEf/0eKlnfivnN0g1g+j/Lk9et/uoa6TFCW0HgwFOIVFumEYdT675PfuTrYO5o8ZrWEIHtv2Ctlrv9J3TrslD/iKEwtipGHtn0Vak8B9wLL+kz+CIQ/VG4KJpXjx88CeCC4XaGitEdjAAA" }; + + modelBuilder.Entity().HasData(picture); + modelBuilder.Entity().HasData( - new AthleteEntity { IdAthlete = 1, Username = "Doe",ProfilPicture = picture, LastName = "Doe", FirstName = "John", Email = "john.doe@example.com", Password = "password123", Sexe = "M", Length = 1.80, Weight = 75, DateOfBirth = new DateOnly(1990, 01, 01), IsCoach = true }, - new AthleteEntity { IdAthlete = 2, Username = "Smith",ProfilPicture = picture, LastName = "Smith", FirstName = "Jane", Email = "jane.smith@exemple.com", Password = "secure456", Sexe = "F", Length = 1.65, Weight = 60, DateOfBirth = new DateOnly(1995, 01, 01), IsCoach = false, DataSourceId = 1 }, - new AthleteEntity { IdAthlete = 3, Username = "Martin",ProfilPicture = picture, LastName = "Martin", FirstName = "Paul", Email = "paul.martin@example.com", Password = "super789", Sexe = "M", Length = 1.75, Weight = 68, DateOfBirth = new DateOnly(1992, 01, 01), IsCoach = true }, - new AthleteEntity { IdAthlete = 4, Username = "Brown",ProfilPicture = picture, LastName = "Brown", FirstName = "Anna", Email = "anna.brown@example.com", Password = "test000", Sexe = "F", Length = 1.70, Weight = 58, DateOfBirth = new DateOnly(1993, 01, 01), IsCoach = false }, - new AthleteEntity { IdAthlete = 5, Username = "Lee", ProfilPicture = picture,LastName = "Lee", FirstName = "Bruce", Email = "bruce.lee@example.com", Password = "hello321", Sexe = "M", Length = 2.0, Weight = 90, DateOfBirth = new DateOnly(1991, 01, 01), IsCoach = false, DataSourceId = 3 } + new AthleteEntity { IdAthlete = 1, Username = "Doe",ProfilPicture = picture2, ImageId = Guid.Parse("{8d121cdc-6787-4738-8edd-9e026ac16b65}") ,LastName = "Doe", FirstName = "John", Email = "john.doe@example.com", Password = "password123", Sexe = "M", Length = 1.80, Weight = 75, DateOfBirth = new DateOnly(1990, 01, 01), IsCoach = true , DataSourceId = 1}, + new AthleteEntity { IdAthlete = 2, Username = "Smith",ProfilPicture = picture2,ImageId = Guid.Parse("{8d121cdc-6787-4738-8edd-9e026ac16b65}") ,LastName = "Smith", FirstName = "Jane", Email = "jane.smith@exemple.com", Password = "secure456", Sexe = "F", Length = 1.65, Weight = 60, DateOfBirth = new DateOnly(1995, 01, 01), IsCoach = false, DataSourceId = 1 }, + new AthleteEntity { IdAthlete = 3, Username = "Martin",ProfilPicture = picture2,ImageId = Guid.Parse("{8d121cdc-6787-4738-8edd-9e026ac16b65}") ,LastName = "Martin", FirstName = "Paul", Email = "paul.martin@example.com", Password = "super789", Sexe = "M", Length = 1.75, Weight = 68, DateOfBirth = new DateOnly(1992, 01, 01), IsCoach = true, DataSourceId = 1}, + new AthleteEntity { IdAthlete = 4, Username = "Brown",ProfilPicture = picture2, ImageId = Guid.Parse("{8d121cdc-6787-4738-8edd-9e026ac16b65}"),LastName = "Brown", FirstName = "Anna", Email = "anna.brown@example.com", Password = "test000", Sexe = "F", Length = 1.70, Weight = 58, DateOfBirth = new DateOnly(1993, 01, 01), IsCoach = false, DataSourceId = 2 }, + new AthleteEntity { IdAthlete = 5, Username = "Lee", ProfilPicture = picture2, ImageId = Guid.Parse("{8d121cdc-6787-4738-8edd-9e026ac16b65}"),LastName = "Lee", FirstName = "Bruce", Email = "bruce.lee@example.com", Password = "hello321", Sexe = "M", Length = 2.0, Weight = 90, DateOfBirth = new DateOnly(1991, 01, 01), IsCoach = false, DataSourceId = 3 } ); } } diff --git a/src/Tests/ConsoleTestEFMapper/Program.cs b/src/Tests/ConsoleTestEFMapper/Program.cs index fcf59ac..3218198 100644 --- a/src/Tests/ConsoleTestEFMapper/Program.cs +++ b/src/Tests/ConsoleTestEFMapper/Program.cs @@ -43,7 +43,28 @@ namespace Model2Entities // // Test de la méthode AddActivity Console.WriteLine("Testing AddActivity method..."); - var newActivity = new Activity(10, "New Activity", new DateTime(2021, 10, 10), new DateTime(10, 10, 10, 10, 10, 10), new DateTime(10, 10, 10, 12, 12, 12), 5, 5, 5, 5, 5, 5, 5, 5, false); + var user = new User + { + Id = 1, Username = "DoeDoe", + ProfilePicture = + "https://images.unsplash.com/photo-1682687982134-2ac563b2228b?q=80&w=2070&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D", + FirstName = "John", LastName = "Doe", + Sexe = "M", Lenght = 180, Weight = 70, DateOfBirth = new DateTime(1990, 1, 1), + Email = "john.doe@example.com", Role = new Athlete() + }; + var dataSource = new DataSource(1, "Polar", "Vantage V2", 0.5f, new List {user}, new List()); + + var newActivity = new Activity(10, "New Activity", new DateTime(2021, 10, 10), + new DateTime(10, 10, 10, 10, 10, 10), new DateTime(10, 10, 10, 12, 12, 12), 5, 5, 5, 5, 5, 5, 5, 5, + false, user) + { + DataSource = dataSource + }; + var HeartRates = new List + { + new HeartRate(1, 60, new TimeOnly(10, 10, 10), newActivity, 5, 5, 5, 5, 5, 5, 5, 5), + }; + newActivity.HeartRates = HeartRates; var addedActivity = await activityRepository.AddActivity(newActivity); if (addedActivity != null) { diff --git a/src/Tests/ConsoleTestEntities/Program.cs b/src/Tests/ConsoleTestEntities/Program.cs index e5a8286..826f345 100644 --- a/src/Tests/ConsoleTestEntities/Program.cs +++ b/src/Tests/ConsoleTestEntities/Program.cs @@ -753,9 +753,7 @@ class Program static void AddUpdateDeleteAthlete(HeartTrackContext db) { Console.WriteLine("Test d'ajout, de modification et de suppression des athletes :"); - var picture = System.Text.Encoding.UTF8.GetBytes( - "\"UklGRtwDAABXRUJQVlA4INADAAAwEACdASoqACoAAMASJZgCdMoSCz655ndU4XXAP2yXIge5neM/Qd6WCfO8evoj2S0A/p7+f0An85cBxlLDgPC8jO/0nsl/13/O8vvzj7Af8s/p3/H4FU6td4MCwq23z1H2uzoKIXaqJniPI/bRMf8qzv0Zp+HE1RCBw5WQ1j/JovdM1FS52+QcaAAA/v/+NxU4DpPk3+xQPW7tcmURSo9vC4qc+XMxNVBzEM5E8actDz98gmwTXgD62e9EmG/ervdd2ovFFSuxYppWl/wtaX3rkn0xrt8qOql/5I2jfLOnCU0kALLcW4F/wTjU10qsxZXW9fxauC6OPVRF28sc94V9ocmoSWy+sf6jW3vYkVOh+gE/RE0L6b2d3oFyHmkRJnfYwG8o3p6fv9pivNF5aopIBzFnjzwb/VqSq3/b+MWKFmjr8T1qe4/fITo2vBWEqDyogV3ZVGnDVi2DbiEFVSUr2eXTNZQ9V/D9QC/+vCR5TGyX9QOVBgtAYtm/ZTIwzPEYB9NrV1NeO1/sAz78u0tW59r0I+SO5Jgm3B9i1toRurzHv9EZJ9yZL8nafb/T1FaoPDkuJfM+iPs0j8xnS7TaU/gEK0wCxeDYRYtJx9j4hUQq7pAu/T2yWy0vjcUHki952ZNbXnXxB8m8pV5x9E1sfLj5MZEgpU2XV8RHrVvWniCjsf6vgxmR7+KtwIbMjahitUGtHet1WdL+8MmdL29iQJC37pDXirir1NibxKKhFYRuJ3xW9O0r9+Vnh8diqbBuXqDbYR/MSoHvscOCm2t95dN5WBdRUoD7YCG/ZHWc7Ypv/x/al4fkB2lZlYhVWHxjaoeF9jEPI0gAN5XsvUI6hbzEzWMsNW/1orkNOnlskalgmpI4B2rm4Gc7LNui+MuMBrpnBvLkbYX9exe9g8tu7wLt7ScOjDcL99oOyR89Mh9L8rd4+43+JQyR6tsIfcPJo6T6FxHf11d/MGayJi+SWct/uhvvua0oOh+zXNIaUzgoBmu1XULjkpuA0Ghzctf30jbY1AOM49qbMZRYS9A+0S1HrHPnwRvpQY/Sj4xKPn0gdpv/+iTbKJb8zkPC4/9af0Jvesa+GDG0/iw3TswenMhqlh7BM9MW5txpeblsByx4WnJ/oHv6cc0dmM7tsV36lYkCTUXEf/0eKlnfivnN0g1g+j/Lk9et/uoa6TFCW0HgwFOIVFumEYdT675PfuTrYO5o8ZrWEIHtv2Ctlrv9J3TrslD/iKEwtipGHtn0Vak8B9wLL+kz+CIQ/VG4KJpXjx88CeCC4XaGitEdjAAA\""); - + var picture = "https://davidalmeida.site/assets/me_avatar.f77af006.png"; // Ajout d'un nouveau livre var newAthlete = new AthleteEntity { Username = "Doe", LastName = "Doe",ProfilPicture = picture,FirstName = "John", Email = "essaie.example.com", Password = "TheNewPassword", Sexe = "M", Length = 1.80, Weight = 90, DateOfBirth = new DateOnly(2024, 02, 22), IsCoach = false }; db.AthletesSet.Add(newAthlete); diff --git a/src/Tests/TestsAPI/UnitTestApi/Controllers/UsersControllerTest.cs b/src/Tests/TestsAPI/UnitTestApi/Controllers/UsersControllerTest.cs index 1d1500c..5d69fb4 100644 --- a/src/Tests/TestsAPI/UnitTestApi/Controllers/UsersControllerTest.cs +++ b/src/Tests/TestsAPI/UnitTestApi/Controllers/UsersControllerTest.cs @@ -1,4 +1,4 @@ -using ApiMappeur; +using APIMappers; using Dto; using HeartTrackAPI.Controllers; using HeartTrackAPI.Request;