diff --git a/src/DbContextLib/HeartTrackContext.cs b/src/DbContextLib/HeartTrackContext.cs index 1cec4ca..a9c6ad7 100644 --- a/src/DbContextLib/HeartTrackContext.cs +++ b/src/DbContextLib/HeartTrackContext.cs @@ -7,6 +7,8 @@ //----------------------------------------------------------------------- using Entities; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; namespace DbContextLib @@ -14,7 +16,7 @@ namespace DbContextLib /// /// Represents the database context for the FitnessApp. /// - public class HeartTrackContext : DbContext + public class HeartTrackContext : IdentityDbContext,int> { /// /// Gets or sets the set of athletes. @@ -132,10 +134,10 @@ namespace DbContextLib //primary key of AthleteEntity modelBuilder.Entity() - .HasKey(at => at.IdAthlete); + .HasKey(at => at.Id); //generation mode (at insertion) modelBuilder.Entity() - .Property(at => at.IdAthlete) + .Property(at => at.Id) .ValueGeneratedOnAdd(); // add image column type // modelBuilder.Entity() @@ -234,6 +236,12 @@ namespace DbContextLib .WithOne(at => at.DataSource) .HasForeignKey(at => at.DataSourceId) .IsRequired(false); + List roles = new List + { + new ("Athlete"), + new ("Coach") + }; + modelBuilder.Entity().HasData(roles); } } } \ No newline at end of file diff --git a/src/DbContextLib/Identity/AuthDbContext.cs b/src/DbContextLib/Identity/AuthDbContext.cs deleted file mode 100644 index 89924a7..0000000 --- a/src/DbContextLib/Identity/AuthDbContext.cs +++ /dev/null @@ -1,25 +0,0 @@ -using Entities; -using Microsoft.AspNetCore.Identity; -using Microsoft.AspNetCore.Identity.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore; - -namespace DbContextLib.Identity; - -public class AuthDbContext: IdentityDbContext -{ - - public AuthDbContext(DbContextOptions options) : base(options) { } - public AuthDbContext() { } - /* - /// - /// Configures the database options if they are not already configured. - /// - /// The options builder instance. - protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - if (!optionsBuilder.IsConfigured) - { - optionsBuilder.UseSqlite($"Data Source=uca.HeartTrack.db"); - } - }*/ -} \ No newline at end of file diff --git a/src/EFMappers/AthleteMappeur.cs b/src/EFMappers/AthleteMappeur.cs index 1cd39a8..f6a2198 100644 --- a/src/EFMappers/AthleteMappeur.cs +++ b/src/EFMappers/AthleteMappeur.cs @@ -13,14 +13,14 @@ public static class UserMappeur { Func create = athleteEntity => new User { - Id = athleteEntity.IdAthlete, + Id = athleteEntity.Id, FirstName = athleteEntity.FirstName, LastName = athleteEntity.LastName, Email = athleteEntity.Email, - MotDePasse = athleteEntity.Password, + MotDePasse = athleteEntity.PasswordHash, DateOfBirth = athleteEntity.DateOfBirth.ToDateTime(TimeOnly.MinValue), Sexe = athleteEntity.Sexe, - Username = athleteEntity.Username, + Username = athleteEntity.UserName, Weight = athleteEntity.Weight, Lenght = (float)athleteEntity.Length, ProfilePicture = athleteEntity.ProfilPicture, @@ -42,13 +42,13 @@ public static class UserMappeur { Func create = user => new AthleteEntity { - IdAthlete = user.Id, - Username = user.Username, + Id = user.Id, + UserName = user.Username, Sexe = user.Sexe, FirstName = user.FirstName, LastName = user.LastName, Email = user.Email, - Password = user.MotDePasse, + PasswordHash = user.MotDePasse, DateOfBirth = DateOnly.FromDateTime(user.DateOfBirth), IsCoach = user.Role is Coach, Weight = user.Weight, diff --git a/src/Entities/AthleteEntity.cs b/src/Entities/AthleteEntity.cs index 3b89fbe..7d1977d 100644 --- a/src/Entities/AthleteEntity.cs +++ b/src/Entities/AthleteEntity.cs @@ -8,56 +8,58 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using Microsoft.AspNetCore.Identity; namespace Entities { + //dentityRole,int /// /// Represents an athlete entity in the database. /// [Table("Athlete")] - public class AthleteEntity + public class AthleteEntity : IdentityUser { /// /// Gets or sets the unique identifier of the athlete. /// [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] - public int IdAthlete { get; set; } + public override int Id { get; set; } /// /// Gets or sets the username of the athlete. /// [MaxLength(100)] [Required(ErrorMessage = "Athlete Username is ")] - public required string Username { get; set; } + public override string UserName { get; set; } /// /// Gets or sets the last name of the athlete. /// [MaxLength(100)] [Required(ErrorMessage = "Athlete Last Name is ")] - public required string LastName { get; set; } + public string LastName { get; set; } /// /// Gets or sets the first name of the athlete. /// [MaxLength(150)] [Required(ErrorMessage = "Athlete First Name is ")] - public required string FirstName { get; set; } + public string FirstName { get; set; } /// /// Gets or sets the email of the athlete. /// [MaxLength(100)] [Required(ErrorMessage = "Athlete Email is ")] - public required string Email { get; set; } + public override string Email { get; set; } /// /// Gets or sets the gender of the athlete. /// [MaxLength(1)] [Required(ErrorMessage = "Athlete Sexe is ")] - public required string Sexe { get; set; } + public string Sexe { get; set; } /// /// Gets or sets the height of the athlete. @@ -68,12 +70,12 @@ namespace Entities /// Gets or sets the weight of the athlete. /// public float Weight { get; set; } - +/* /// /// Gets or sets the password of the athlete. /// - [Required(ErrorMessage = "Athlete Password is ")] - public required string Password { get; set; } + [Required(ErrorMessage = "Athlete PasswordHash is ")] + public string PasswordHash { get; set; }*/ /// /// Gets or sets the date of birth of the athlete. diff --git a/src/Entities/Entities.csproj b/src/Entities/Entities.csproj index b007a4d..7fbb47d 100644 --- a/src/Entities/Entities.csproj +++ b/src/Entities/Entities.csproj @@ -5,4 +5,14 @@ enable enable + + + + + + + + ..\..\..\.nuget\packages\microsoft.extensions.identity.stores\8.0.2\lib\net8.0\Microsoft.Extensions.Identity.Stores.dll + + diff --git a/src/Entities2Dto/Entities2Dto.csproj b/src/Entities2Dto/Entities2Dto.csproj index e3d0baf..e879b8a 100644 --- a/src/Entities2Dto/Entities2Dto.csproj +++ b/src/Entities2Dto/Entities2Dto.csproj @@ -11,4 +11,10 @@ + + + ..\..\..\.nuget\packages\microsoft.extensions.identity.stores\8.0.2\lib\net8.0\Microsoft.Extensions.Identity.Stores.dll + + + diff --git a/src/Entities2Dto/UserMappeur.cs b/src/Entities2Dto/UserMappeur.cs index 71b2dcf..edc6b8c 100644 --- a/src/Entities2Dto/UserMappeur.cs +++ b/src/Entities2Dto/UserMappeur.cs @@ -21,14 +21,14 @@ public static class UserMappeur { Func create = athleteEntity => new UserTinyDto { - Id = athleteEntity.IdAthlete, + Id = athleteEntity.Id, FirstName = athleteEntity.FirstName, LastName = athleteEntity.LastName, Email = athleteEntity.Email, - Password = athleteEntity.Password, + Password = athleteEntity.PasswordHash, DateOfBirth = athleteEntity.DateOfBirth.ToDateTime(TimeOnly.MinValue), Sexe = athleteEntity.Sexe, - Username = athleteEntity.Username, + Username = athleteEntity.UserName, Weight = athleteEntity.Weight, Lenght = (float)athleteEntity.Length, ProfilePicture = athleteEntity.ProfilPicture ?? "", @@ -42,13 +42,13 @@ public static class UserMappeur { Func create = user => new AthleteEntity { - IdAthlete = user.Id, - Username = user.Username, + Id = user.Id, + UserName = user.Username, Sexe = user.Sexe, FirstName = user.FirstName, LastName = user.LastName, Email = user.Email, - Password = user.Password ?? "", + PasswordHash = user.Password ?? "", DateOfBirth = DateOnly.FromDateTime(user.DateOfBirth), IsCoach = user.IsCoach, Weight = user.Weight, @@ -63,14 +63,14 @@ public static class UserMappeur { Func creator = athleteEntity => new ResponseUserDto { - Id = athleteEntity.IdAthlete, + Id = athleteEntity.Id, FirstName = athleteEntity.FirstName, LastName = athleteEntity.LastName, Email = athleteEntity.Email, - Password = athleteEntity.Password, + Password = athleteEntity.PasswordHash, DateOfBirth = athleteEntity.DateOfBirth.ToDateTime(TimeOnly.MinValue), Sexe = athleteEntity.Sexe, - Username = athleteEntity.Username, + Username = athleteEntity.UserName, Weight = athleteEntity.Weight, Lenght = (float)athleteEntity.Length, ProfilePicture = athleteEntity.ProfilPicture ?? "", diff --git a/src/HeartTrackAPI/Controllers/ActivityController.cs b/src/HeartTrackAPI/Controllers/ActivityController.cs index 79f8654..40d5bc4 100644 --- a/src/HeartTrackAPI/Controllers/ActivityController.cs +++ b/src/HeartTrackAPI/Controllers/ActivityController.cs @@ -27,9 +27,9 @@ public class ActivityController : Controller } [HttpGet] - [ProducesResponseType(typeof(PageResponse), 200)] + [ProducesResponseType(typeof(PageResponse), 200)] [ProducesResponseType(400)] - [ProducesResponseType(500)] + [ProducesResponseType(500)] public async Task>> GetActivities([FromQuery] PageRequest pageRequest) { try diff --git a/src/HeartTrackAPI/Controllers/AnalysisController.cs b/src/HeartTrackAPI/Controllers/AnalysisController.cs index e4f9fcb..9a7d433 100644 --- a/src/HeartTrackAPI/Controllers/AnalysisController.cs +++ b/src/HeartTrackAPI/Controllers/AnalysisController.cs @@ -6,7 +6,7 @@ namespace HeartTrackAPI.Controllers; [ApiController] [ApiVersion("1.0")] [Route("api/[controller]")] -public class AnalysisController : ControllerBase +public class AnalysisController : Controller { private readonly List _heartRateZones = new() { @@ -34,8 +34,27 @@ public class AnalysisController : ControllerBase var secondsInZone = heartRates.Count(hr => hr.HeartRate >= zone.MinHeartRate && hr.HeartRate <= zone.MaxHeartRate); return TimeSpan.FromSeconds(secondsInZone); - } + } + /* + [HttpGet("getOptimizedPath")] + public IActionResult GetOptimizedPath(int activityId) + { + var heartRateData = GetMockHeartRateData(); + var sortedData = heartRateData.OrderBy(x => x.Timestamp).ToList(); + + var path = new GeoJsonPath(); + foreach (var item in sortedData) + { + if (item.Latitude.HasValue && item.Longitude.HasValue) + { + path.Coordinates.Add(new GeoJsonCoordinate(item.Longitude.Value, item.Latitude.Value)); + } + } + + return Ok(path.ToGeoJson()); + } +*/ private List GetMockHeartRateData() { var random = new Random(); diff --git a/src/HeartTrackAPI/Controllers/UsersController.cs b/src/HeartTrackAPI/Controllers/UsersController.cs index c7daf65..0463765 100644 --- a/src/HeartTrackAPI/Controllers/UsersController.cs +++ b/src/HeartTrackAPI/Controllers/UsersController.cs @@ -360,25 +360,6 @@ public class UsersController : Controller return Problem(); } }*/ - - /// - /// Déconnecte l'utilisateur actuel. - /// - /// Le gestionnaire de connexion. - /// Paramètre vide utilisé pour s'assurer que la requête provient bien d'un client. - /// Action result. - /// Déconnexion réussie. - /// Déconnexion non autorisée. - /// Erreur interne du serveur. - [HttpPost("logout")] - [ProducesResponseType(200)] - [ProducesResponseType(401)] - [ProducesResponseType(500)] - public async Task Logout(SignInManager signInManager, [FromBody] object? empty) - { - if (empty == null) return Unauthorized(); - await signInManager.SignOutAsync(); - return Ok(); - } + } \ No newline at end of file diff --git a/src/HeartTrackAPI/HeartTrackAPI.csproj b/src/HeartTrackAPI/HeartTrackAPI.csproj index e815c27..ab6b8d3 100644 --- a/src/HeartTrackAPI/HeartTrackAPI.csproj +++ b/src/HeartTrackAPI/HeartTrackAPI.csproj @@ -10,11 +10,13 @@ + + diff --git a/src/HeartTrackAPI/Utils/AppBootstrap.cs b/src/HeartTrackAPI/Utils/AppBootstrap.cs index e45e18f..22c75a5 100644 --- a/src/HeartTrackAPI/Utils/AppBootstrap.cs +++ b/src/HeartTrackAPI/Utils/AppBootstrap.cs @@ -1,11 +1,12 @@ using System.Reflection; using DbContextLib; -using DbContextLib.Identity; +using Entities; +using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Identity; -using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.Mvc.Versioning; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; +using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; using Model.Manager; using Model2Entities; @@ -20,14 +21,17 @@ public class AppBootstrap(IConfiguration configuration) public void ConfigureServices(IServiceCollection services) { + services.AddControllers(); + services.AddEndpointsApiExplorer(); AddSwagger(services); AddHeartTrackContextServices(services); AddModelService(services); - AddIdentityServices(services); + AddIdentityServices(services,configuration); AddApiVersioning(services); services.AddHealthChecks(); + } private void AddHeartTrackContextServices(IServiceCollection services) @@ -47,7 +51,6 @@ public class AppBootstrap(IConfiguration configuration) Console.WriteLine("========RUNNING USING THE MYSQL SERVER=============="); Console.WriteLine(connectionString); Console.WriteLine("===================================================="); - services.AddDbContext(options => options.UseInMemoryDatabase("AuthDb")); services.AddDbContext(options => options.UseMySql($"{connectionString}", new MySqlServerVersion(new Version(10, 11, 1))) , ServiceLifetime.Singleton); @@ -58,13 +61,11 @@ public class AppBootstrap(IConfiguration configuration) Console.WriteLine(connectionString); if (!string.IsNullOrWhiteSpace(connectionString)) { - services.AddDbContext(options => options.UseInMemoryDatabase("AuthDb")); services.AddDbContext(options => options.UseSqlite(connectionString), ServiceLifetime.Singleton); } else { - services.AddDbContext(options => options.UseInMemoryDatabase("AuthDb")); services.AddDbContext(); } break; @@ -94,14 +95,61 @@ public class AppBootstrap(IConfiguration configuration) } } - private void AddIdentityServices(IServiceCollection services) + private void AddIdentityServices(IServiceCollection services, IConfiguration config) { -// services.AddTransient, EmailSender>(); services.AddAuthorization(); + services.AddIdentityApiEndpoints() + .AddEntityFrameworkStores(); + + /*services.AddIdentity>(options => + { + options.PasswordHash.RequireDigit = true; + options.PasswordHash.RequireLowercase = true; + options.PasswordHash.RequireUppercase = true; + options.PasswordHash.RequireNonAlphanumeric = true; + options.PasswordHash.RequiredLength = 8; + + }) + .AddEntityFrameworkStores(); + + + services.AddAuthentication(options => + { + options.DefaultAuthenticateScheme = + options.DefaultChallengeScheme = + options.DefaultForbidScheme = + options.DefaultScheme = + options.DefaultSignInScheme = + options.DefaultSignOutScheme = JwtBearerDefaults.AuthenticationScheme; + }).AddJwtBearer(options => + { + options.TokenValidationParameters = new TokenValidationParameters + { + ValidateIssuer = true, + //ValidIssuer =config["JWT:Issuer"], + ValidateAudience = true, + //ValidAudience = config["JWT:Audience"], + ValidateIssuerSigningKey = true, + IssuerSigningKey = new SymmetricSecurityKey( + System.Text.Encoding.UTF8.GetBytes(config["JWT:SigningKey"]) + ) + }; + });*/ + + /* + app.UseCors(x => x + .AllowAnyMethod() + .AllowAnyHeader() + .AllowCredentials() + //.WithOrigins("https://localhost:44351)) + .SetIsOriginAllowed(origin => true));*/ - services.AddIdentityApiEndpoints() - .AddEntityFrameworkStores(); - // .AddEntityFrameworkStores().AddDefaultTokenProviders(); +// services.AddTransient, EmailSender>(); + // services.AddAuthorization(); + +// services.AddIdentityApiEndpoints() + // .AddEntityFrameworkStores(); + // .AddEntityFrameworkStores().AddDefaultTokenProviders(); } private void AddApiVersioning(IServiceCollection services) @@ -122,8 +170,6 @@ public class AppBootstrap(IConfiguration configuration) { services.AddSwaggerGen(options => { - options.OperationFilter(); - var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"; var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile); options.IncludeXmlComments(xmlPath); @@ -155,28 +201,28 @@ public class AppBootstrap(IConfiguration configuration) }; options.AddSecurityRequirement(scheme); - }); - services.AddTransient, SwaggerOptions>(); - services.AddSwaggerGen(options => - { options.OperationFilter(); + }); - + //services.AddTransient, SwaggerOptions>(); + /* services.AddVersionedApiExplorer(setup => { setup.GroupNameFormat = "'v'VVV"; setup.SubstituteApiVersionInUrl = true; - }); + });*/ } public void Configure(WebApplication app, IWebHostEnvironment env) { app.UseHttpsRedirection(); - app.MapIdentityApi(); + + app.UseAuthentication(); + app.UseAuthorization(); + app.MapIdentityApi(); app.MapControllers(); - app.UseAuthorization(); app.MapHealthChecks("/health"); diff --git a/src/HeartTrackAPI/appsettings.json b/src/HeartTrackAPI/appsettings.json index 5fb6b3c..1178611 100644 --- a/src/HeartTrackAPI/appsettings.json +++ b/src/HeartTrackAPI/appsettings.json @@ -8,5 +8,10 @@ "AllowedHosts": "*", "ConnectionStrings": { "HeartTrackAuthConnection": "Data Source=uca_HeartTrack.db" - } + }, + "Jwt": { + "Issuer": "HeartTrack", + "Audience": "HeartTrack", + "SigningKey": "ThisIsMySuperKey" + } } diff --git a/src/Model2Entities/UserRepository.cs b/src/Model2Entities/UserRepository.cs index 264da22..51391c3 100644 --- a/src/Model2Entities/UserRepository.cs +++ b/src/Model2Entities/UserRepository.cs @@ -57,7 +57,7 @@ public partial class DbDataManager _logger.LogInformation($"GetItemById with id {id}", id); var userEntity = await _dataManager.DbContext.AthletesSet.IncludeStandardProperties() - .SingleOrDefaultAsync(a => a.IdAthlete == id); + .SingleOrDefaultAsync(a => a.Id == id); var user = userEntity != null ? userEntity.ToModel() : null; if (user != null) _logger.LogInformation($"Retrieved user with ID {id}"); @@ -95,7 +95,7 @@ public partial class DbDataManager { _logger.LogInformation($"GetTinyItemById with id {id}", id); var userEntity = await _dataManager.DbContext.AthletesSet.IncludeStandardProperties().Include(a => a.Followers).Include(a => a.Followings) - .SingleOrDefaultAsync(a => a.IdAthlete == id); + .SingleOrDefaultAsync(a => a.Id == id); var user = userEntity != null ? userEntity.ToResponseDto() : null; if (user != null) _logger.LogInformation($"Retrieved user with ID {id}"); @@ -199,7 +199,7 @@ public partial class DbDataManager var userEntity = _dataManager.DbContext.AthletesSet .Include(a => a.Followings) - .FirstOrDefault(a => a.IdAthlete == fromUser); + .FirstOrDefault(a => a.Id == fromUser); if (userEntity == null) { @@ -232,7 +232,7 @@ public partial class DbDataManager var userEntity = _dataManager.DbContext.AthletesSet .Include(a => a.Followings) - .FirstOrDefault(a => a.IdAthlete == fromUser); + .FirstOrDefault(a => a.Id == fromUser); if (userEntity == null) { @@ -265,7 +265,7 @@ public partial class DbDataManager var athlete = await _dataManager.DbContext.AthletesSet .Include(a => a.Followers).ThenInclude(f => f.Follower) .Include(a => a.Followings).ThenInclude(f => f.Following) - .FirstOrDefaultAsync(a => a.IdAthlete == userId); // Use async version for better performance + .FirstOrDefaultAsync(a => a.Id == userId); // Use async version for better performance if (athlete == null) { @@ -299,7 +299,7 @@ public partial class DbDataManager var athlete = await _dataManager.DbContext.AthletesSet .Include(a => a.Followers).ThenInclude(f => f.Follower) .Include(a => a.Followings).ThenInclude(f => f.Following) - .FirstOrDefaultAsync(a => a.IdAthlete == userId); + .FirstOrDefaultAsync(a => a.Id == userId); if (athlete == null) { diff --git a/src/StubbedContextLib/AthleteStubbedContext.cs b/src/StubbedContextLib/AthleteStubbedContext.cs index 3b69133..b87b4b4 100644 --- a/src/StubbedContextLib/AthleteStubbedContext.cs +++ b/src/StubbedContextLib/AthleteStubbedContext.cs @@ -42,11 +42,11 @@ namespace StubbedContextLib modelBuilder.Entity().HasData(picture); modelBuilder.Entity().HasData( - 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,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 } + new AthleteEntity { Id = 1, UserName = "Doe",ProfilPicture = picture2, ImageId = Guid.Parse("{8d121cdc-6787-4738-8edd-9e026ac16b65}") ,LastName = "Doe", FirstName = "John", Email = "john.doe@example.com", PasswordHash = "password123", Sexe = "M", Length = 1.80, Weight = 75, DateOfBirth = new DateOnly(1990, 01, 01), IsCoach = true , DataSourceId = 1}, + new AthleteEntity { Id = 2, UserName = "Smith",ProfilPicture = picture2,LastName = "Smith", FirstName = "Jane", Email = "jane.smith@exemple.com", PasswordHash = "secure456", Sexe = "F", Length = 1.65, Weight = 60, DateOfBirth = new DateOnly(1995, 01, 01), IsCoach = false, DataSourceId = 1 }, + new AthleteEntity { Id = 3, UserName = "Martin",ProfilPicture = picture2,ImageId = Guid.Parse("{8d121cdc-6787-4738-8edd-9e026ac16b65}") ,LastName = "Martin", FirstName = "Paul", Email = "paul.martin@example.com", PasswordHash = "super789", Sexe = "M", Length = 1.75, Weight = 68, DateOfBirth = new DateOnly(1992, 01, 01), IsCoach = true, DataSourceId = 1}, + new AthleteEntity { Id = 4, UserName = "Brown",ProfilPicture = picture2, ImageId = Guid.Parse("{8d121cdc-6787-4738-8edd-9e026ac16b65}"),LastName = "Brown", FirstName = "Anna", Email = "anna.brown@example.com", PasswordHash = "test000", Sexe = "F", Length = 1.70, Weight = 58, DateOfBirth = new DateOnly(1993, 01, 01), IsCoach = false, DataSourceId = 2 }, + new AthleteEntity { Id = 5, UserName = "Lee", ProfilPicture = picture2, ImageId = Guid.Parse("{8d121cdc-6787-4738-8edd-9e026ac16b65}"),LastName = "Lee", FirstName = "Bruce", Email = "bruce.lee@example.com", PasswordHash = "hello321", Sexe = "M", Length = 2.0, Weight = 90, DateOfBirth = new DateOnly(1991, 01, 01), IsCoach = false, DataSourceId = 3 } ); } } diff --git a/src/Tests/ConsoleTestEntities/Program.cs b/src/Tests/ConsoleTestEntities/Program.cs index 826f345..44bc88b 100644 --- a/src/Tests/ConsoleTestEntities/Program.cs +++ b/src/Tests/ConsoleTestEntities/Program.cs @@ -56,25 +56,25 @@ class Program foreach (var athlete in db.AthletesSet) { - Console.WriteLine($"\t{athlete.IdAthlete} - {athlete.Username}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); + Console.WriteLine($"\t{athlete.Id} - {athlete.UserName}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); } Console.WriteLine("---------------------------------\n"); Console.WriteLine("Accès à l'athlete d'id '2' :"); Console.WriteLine("---------------------------------"); - foreach (var athlete in db.AthletesSet.Where(a => a.IdAthlete == 2)) + foreach (var athlete in db.AthletesSet.Where(a => a.Id == 2)) { - Console.WriteLine($"\t{athlete.IdAthlete} - {athlete.Username}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); + Console.WriteLine($"\t{athlete.Id} - {athlete.UserName}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); } Console.WriteLine("---------------------------------\n"); Console.WriteLine("Accès à l'athlete de username 'Doe' :"); Console.WriteLine("---------------------------------"); - foreach (var athlete in db.AthletesSet.Where(a => a.Username == "Doe")) + foreach (var athlete in db.AthletesSet.Where(a => a.UserName == "Doe")) { - Console.WriteLine($"\t{athlete.IdAthlete} - {athlete.Username}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); + Console.WriteLine($"\t{athlete.Id} - {athlete.UserName}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); } Console.WriteLine("---------------------------------\n"); @@ -83,7 +83,7 @@ class Program Console.WriteLine("---------------------------------"); foreach (var athlete in db.AthletesSet.Where(a => a.Sexe == "F")) { - Console.WriteLine($"\t{athlete.IdAthlete} - {athlete.Username}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); + Console.WriteLine($"\t{athlete.Id} - {athlete.UserName}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); } Console.WriteLine("---------------------------------\n"); @@ -92,7 +92,7 @@ class Program Console.WriteLine("---------------------------------"); foreach (var athlete in db.AthletesSet.Where(a => a.Email == "bruce.lee@example.com")) { - Console.WriteLine($"\t{athlete.IdAthlete} - {athlete.Username}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); + Console.WriteLine($"\t{athlete.Id} - {athlete.UserName}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); } Console.WriteLine("---------------------------------\n"); @@ -101,7 +101,7 @@ class Program Console.WriteLine("---------------------------------"); foreach (var athlete in db.AthletesSet.Where(a => a.Weight == 90)) { - Console.WriteLine($"\t{athlete.IdAthlete} - {athlete.Username}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); + Console.WriteLine($"\t{athlete.Id} - {athlete.UserName}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); } Console.WriteLine("---------------------------------\n"); @@ -110,7 +110,7 @@ class Program Console.WriteLine("---------------------------------"); foreach (var athlete in db.AthletesSet.Where(a => a.Length == 1.80)) { - Console.WriteLine($"\t{athlete.IdAthlete} - {athlete.Username}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); + Console.WriteLine($"\t{athlete.Id} - {athlete.UserName}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); } Console.WriteLine("---------------------------------\n"); @@ -119,7 +119,7 @@ class Program Console.WriteLine("---------------------------------"); foreach (var athlete in db.AthletesSet.Where(a => a.DateOfBirth == new DateOnly(1990, 01, 01))) { - Console.WriteLine($"\t{athlete.IdAthlete} - {athlete.Username}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); + Console.WriteLine($"\t{athlete.Id} - {athlete.UserName}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); } Console.WriteLine("---------------------------------\n"); @@ -128,7 +128,7 @@ class Program Console.WriteLine("---------------------------------"); foreach (var athlete in db.AthletesSet.Where(a => a.LastName == "Martin")) { - Console.WriteLine($"\t{athlete.IdAthlete} - {athlete.Username}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); + Console.WriteLine($"\t{athlete.Id} - {athlete.UserName}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); } Console.WriteLine("---------------------------------\n"); @@ -137,7 +137,7 @@ class Program Console.WriteLine("---------------------------------"); foreach (var athlete in db.AthletesSet.Where(a => a.FirstName == "Anna")) { - Console.WriteLine($"\t{athlete.IdAthlete} - {athlete.Username}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); + Console.WriteLine($"\t{athlete.Id} - {athlete.UserName}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); } Console.WriteLine("---------------------------------\n"); @@ -146,7 +146,7 @@ class Program Console.WriteLine("---------------------------------"); foreach (var athlete in db.AthletesSet.Where(a => a.LastName == "Brown" && a.FirstName == "Anna")) { - Console.WriteLine($"\t{athlete.IdAthlete} - {athlete.Username}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); + Console.WriteLine($"\t{athlete.Id} - {athlete.UserName}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); } Console.WriteLine("---------------------------------\n"); @@ -155,7 +155,7 @@ class Program Console.WriteLine("---------------------------------"); foreach (var athlete in db.AthletesSet.Where(a => a.IsCoach == true)) { - Console.WriteLine($"\t{athlete.IdAthlete} - {athlete.Username}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); + Console.WriteLine($"\t{athlete.Id} - {athlete.UserName}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); } Console.WriteLine("---------------------------------\n"); @@ -755,7 +755,7 @@ class Program Console.WriteLine("Test d'ajout, de modification et de suppression des athletes :"); 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 }; + var newAthlete = new AthleteEntity { UserName = "Doe", LastName = "Doe",ProfilPicture = picture,FirstName = "John", Email = "essaie.example.com", PasswordHash = "TheNewPassword", Sexe = "M", Length = 1.80, Weight = 90, DateOfBirth = new DateOnly(2024, 02, 22), IsCoach = false }; db.AthletesSet.Add(newAthlete); db.SaveChanges(); @@ -763,7 +763,7 @@ class Program Console.WriteLine("Athlete après ajout :"); foreach (var athlete in db.AthletesSet) { - Console.WriteLine($"\t{athlete.IdAthlete} - {athlete.Username}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); + Console.WriteLine($"\t{athlete.Id} - {athlete.UserName}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); } // Modification du titre du nouveau livre @@ -774,7 +774,7 @@ class Program Console.WriteLine("Livres après modification :"); foreach (var athlete in db.AthletesSet) { - Console.WriteLine($"\t{athlete.IdAthlete} - {athlete.Username}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); + Console.WriteLine($"\t{athlete.Id} - {athlete.UserName}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); } // Suppression du nouveau livre @@ -785,7 +785,7 @@ class Program Console.WriteLine("Livres après suppression :"); foreach (var athlete in db.AthletesSet) { - Console.WriteLine($"\t{athlete.IdAthlete} - {athlete.Username}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); + Console.WriteLine($"\t{athlete.Id} - {athlete.UserName}, {athlete.LastName}, {athlete.FirstName}, {athlete.Email}, {athlete.Sexe}, {athlete.Length}, {athlete.Weight}, {athlete.DateOfBirth}, {athlete.IsCoach}"); } } diff --git a/src/Tests/ConsoleTestRelationships/Program.cs b/src/Tests/ConsoleTestRelationships/Program.cs index fa9797e..5ff197e 100644 --- a/src/Tests/ConsoleTestRelationships/Program.cs +++ b/src/Tests/ConsoleTestRelationships/Program.cs @@ -96,7 +96,7 @@ class Program foreach (var athlete in dataSource.Athletes) { - Console.WriteLine($"\t\t\t{athlete.IdAthlete} - {athlete.FirstName}, {athlete.LastName}, {athlete.DateOfBirth}, {athlete.Sexe}, {athlete.Weight}, {athlete.IsCoach}"); + Console.WriteLine($"\t\t\t{athlete.Id} - {athlete.FirstName}, {athlete.LastName}, {athlete.DateOfBirth}, {athlete.Sexe}, {athlete.Weight}, {athlete.IsCoach}"); } } @@ -122,7 +122,7 @@ class Program foreach (var athlete in dataSource.Athletes) { - Console.WriteLine($"\t\t\t{athlete.IdAthlete} - {athlete.FirstName}, {athlete.LastName}, {athlete.DateOfBirth}, {athlete.Sexe}, {athlete.Weight}, {athlete.IsCoach}"); + Console.WriteLine($"\t\t\t{athlete.Id} - {athlete.FirstName}, {athlete.LastName}, {athlete.DateOfBirth}, {athlete.Sexe}, {athlete.Weight}, {athlete.IsCoach}"); } } @@ -138,7 +138,7 @@ class Program foreach (var athlete in db.AthletesSet.Include(a => a.Statistics).Include(a => a.Activities).Include(a => a.TrainingsAthlete).Include(a => a.NotificationsSent).Include(a => a.DataSource).Include(a => a.TrainingsCoach).Include(a => a.NotificationsReceived)) { - Console.WriteLine($"\t{athlete.IdAthlete} - {athlete.FirstName}, {athlete.LastName}, {athlete.DateOfBirth}, {athlete.Sexe}, {athlete.Weight}, {athlete.IsCoach}"); + Console.WriteLine($"\t{athlete.Id} - {athlete.FirstName}, {athlete.LastName}, {athlete.DateOfBirth}, {athlete.Sexe}, {athlete.Weight}, {athlete.IsCoach}"); Console.WriteLine("\t\tStatistiques :"); Console.WriteLine("\t\t---------------------------------"); @@ -199,9 +199,9 @@ class Program Console.WriteLine("Accès à l'athlète d'id '2' :"); Console.WriteLine("---------------------------------"); - foreach (var athlete in db.AthletesSet.Where(a => a.IdAthlete == 2).Include(a => a.Statistics).Include(a => a.Activities).Include(a => a.TrainingsAthlete).Include(a => a.NotificationsSent).Include(a => a.DataSource).Include(a => a.TrainingsCoach).Include(a => a.NotificationsReceived)) + foreach (var athlete in db.AthletesSet.Where(a => a.Id == 2).Include(a => a.Statistics).Include(a => a.Activities).Include(a => a.TrainingsAthlete).Include(a => a.NotificationsSent).Include(a => a.DataSource).Include(a => a.TrainingsCoach).Include(a => a.NotificationsReceived)) { - Console.WriteLine($"\t{athlete.IdAthlete} - {athlete.FirstName}, {athlete.LastName}, {athlete.DateOfBirth}, {athlete.Sexe}, {athlete.Weight}, {athlete.IsCoach}"); + Console.WriteLine($"\t{athlete.Id} - {athlete.FirstName}, {athlete.LastName}, {athlete.DateOfBirth}, {athlete.Sexe}, {athlete.Weight}, {athlete.IsCoach}"); Console.WriteLine("\t\tStatistiques :"); Console.WriteLine("\t\t---------------------------------"); @@ -269,7 +269,7 @@ class Program foreach (var athlete in db.AthletesSet.Include(f => f.Followers).Include(f => f.Followings)) { - Console.WriteLine($"\t{athlete.IdAthlete} - {athlete.FirstName}, {athlete.LastName}, {athlete.DateOfBirth}, {athlete.Sexe}, {athlete.Weight}, {athlete.IsCoach}"); + Console.WriteLine($"\t{athlete.Id} - {athlete.FirstName}, {athlete.LastName}, {athlete.DateOfBirth}, {athlete.Sexe}, {athlete.Weight}, {athlete.IsCoach}"); Console.WriteLine($""); Console.WriteLine($""); diff --git a/src/Tests/UnitTestsEntities/AthleteEntityTests.cs b/src/Tests/UnitTestsEntities/AthleteEntityTests.cs index 1f67d03..2bced05 100644 --- a/src/Tests/UnitTestsEntities/AthleteEntityTests.cs +++ b/src/Tests/UnitTestsEntities/AthleteEntityTests.cs @@ -12,14 +12,14 @@ public class AthleteEntityTests (DatabaseFixture fixture) : IClassFixture a.Username == "john_doe"); + var savedAthlete = context.AthletesSet.First(a => a.UserName == "john_doe"); Assert.NotNull(savedAthlete); - Assert.Equal("john_doe", savedAthlete.Username); + Assert.Equal("john_doe", savedAthlete.UserName); } } @@ -47,14 +47,14 @@ public class AthleteEntityTests (DatabaseFixture fixture) : IClassFixture a.Username == "jane_smith"); - savedAthlete.Username = "jane_doe"; + var savedAthlete = context.AthletesSet.First(a => a.UserName == "jane_smith"); + savedAthlete.UserName = "jane_doe"; context.SaveChanges(); } using (var context = new TrainingStubbedContext(fixture._options)) { - var updatedAthlete = context.AthletesSet.First(a => a.Username == "jane_doe"); + var updatedAthlete = context.AthletesSet.First(a => a.UserName == "jane_doe"); Assert.NotNull(updatedAthlete); - Assert.Equal("jane_doe", updatedAthlete.Username); + Assert.Equal("jane_doe", updatedAthlete.UserName); Assert.Equal("Smith", updatedAthlete.LastName); } } @@ -89,14 +89,14 @@ public class AthleteEntityTests (DatabaseFixture fixture) : IClassFixture a.Username == "test_user"); + var savedAthlete = context.AthletesSet.First(a => a.UserName == "test_user"); context.AthletesSet.Remove(savedAthlete); context.SaveChanges(); } using (var context = new TrainingStubbedContext(fixture._options)) { - var deletedAthlete = context.AthletesSet.FirstOrDefault(a => a.Username == "test_user"); + var deletedAthlete = context.AthletesSet.FirstOrDefault(a => a.UserName == "test_user"); Assert.Null(deletedAthlete); } } @@ -128,14 +128,14 @@ public class AthleteEntityTests (DatabaseFixture fixture) : IClassFixture a.Username == "john_doe"); + var savedAthlete = context.AthletesSet.First(a => a.UserName == "john_doe"); Assert.NotNull(savedAthlete); - Assert.Equal("john_doe", savedAthlete.Username); + Assert.Equal("john_doe", savedAthlete.UserName); Assert.Equal("Doe", savedAthlete.LastName); Assert.Equal("John", savedAthlete.FirstName); Assert.Equal("john.doe@example.com", savedAthlete.Email); Assert.Equal("M", savedAthlete.Sexe); Assert.Equal(180.0, savedAthlete.Length); Assert.Equal(75.5f, savedAthlete.Weight); - Assert.Equal("password", savedAthlete.Password); + Assert.Equal("password", savedAthlete.PasswordHash); Assert.Equal(new DateOnly(1990, 1, 1), savedAthlete.DateOfBirth); Assert.False(savedAthlete.IsCoach); } @@ -170,14 +170,14 @@ public class AthleteEntityTests (DatabaseFixture fixture) : IClassFixture a.Followers).Include(a => a.Followings).FirstOrDefault(a => a.Username == "follower_user1").Followings.FirstOrDefault(f => f.FollowerId == follower.IdAthlete && f.FollowingId == following.IdAthlete); + var savedFriendship = context.AthletesSet.Include(a => a.Followers).Include(a => a.Followings).FirstOrDefault(a => a.UserName == "follower_user1").Followings.FirstOrDefault(f => f.FollowerId == follower.Id && f.FollowingId == following.Id); Assert.NotNull(savedFriendship); if (savedFriendship != null) { - Assert.Equal(follower.IdAthlete, savedFriendship.FollowerId); - Assert.Equal(following.IdAthlete, savedFriendship.FollowingId); + Assert.Equal(follower.Id, savedFriendship.FollowerId); + Assert.Equal(following.Id, savedFriendship.FollowingId); } } } @@ -79,42 +79,42 @@ public class FriendshipEntityTests (DatabaseFixture fixture) : IClassFixture