update with working db api

merging_EF-API
David D'ALMEIDA 1 year ago
parent 3b1c19fb9c
commit d36bd18551

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Model\Model.csproj" />
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>
</Project>

@ -0,0 +1,53 @@
using Dto;
using Model;
using Shared;
namespace APIMappers;
public static class ActivityMapper
{
private static GenericMapper<Activity, ActivityDto> _mapper = new GenericMapper<Activity, ActivityDto>();
public static ActivityDto? ToDto(this Activity activity)
{
return activity.ToU(_mapper, activityDto => 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
});
}
public static Activity? ToModel(this ActivityDto activityDto)
{
return activityDto.ToT(_mapper, activity => new Activity
{
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
});
}
}

@ -0,0 +1,54 @@
using Dto;
using Model;
using Shared;
namespace APIMappers;
public static class UserMappeur
{
private static GenericMapper<User, UserDto> _mapper = new GenericMapper<User, UserDto>();
public static UserDto? ToDto(this User user)
{
return user.ToU(_mapper, userDto => new UserDto
{
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
});
// ), (activity, entity) => activity.Id = entity.IdActivity);
}
// ochestrateur => api gateway
// corégraphie => microservice TCP
public static User? ToModel(this UserDto userDto)
{
return userDto.ToT(_mapper, user => new User
{
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()
});
}
}

@ -55,13 +55,15 @@ namespace DbContextLib
/// <summary>
/// Initializes a new instance of the <see cref="HeartTrackContext"/> class.
/// </summary>
public HeartTrackContext() : base() { }
public HeartTrackContext() : base()
{ }
/// <summary>
/// Initializes a new instance of the <see cref="HeartTrackContext"/> class with the specified options.
/// </summary>
/// <param name="options">The options for the context.</param>
public HeartTrackContext(DbContextOptions<HeartTrackContext> options) : base(options) { }
public HeartTrackContext(DbContextOptions<HeartTrackContext> options) : base(options)
{ }
/// <summary>
/// Configures the database options if they are not already configured.

@ -3,17 +3,17 @@ namespace Dto;
public class ActivityDto
{
public int Id { get; set; }
public string Name { get; set; }
public string Type { get; set; }
public DateTime Date { get; set; }
public TimeSpan Duration { get; set; }
public float Distance { get; set; }
public float Elevation { get; set; }
public float AverageSpeed { get; set; }
public int AverageHeartRate { get; set; }
public int Calories { get; set; }
public string Description { get; set; }
public string? Gpx { get; set; }
public string? Image { get; set; }
public int AthleteId { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public int EffortFelt { get; set; }
public float Variability { get; set; }
public float Variance { get; set; }
public float StandardDeviation { get; set; }
public float Average { get; set; }
public int Maximum { get; set; }
public int Minimum { get; set; }
public float AverageTemperature { get; set; }
public bool HasAutoPause { get; set; }
}

@ -16,7 +16,7 @@ namespace Entities
/// Represents an athlete entity in the database.
/// </summary>
[Table("Athlete")]
public class AthleteEntity : IdentityUser
public class AthleteEntity
{
public AthleteEntity() : base() { }
@ -32,35 +32,35 @@ namespace Entities
/// </summary>
[MaxLength(100)]
[Required(ErrorMessage = "Athlete Username is ")]
public string Username { get; set; }
public required string Username { get; set; }
/// <summary>
/// Gets or sets the last name of the athlete.
/// </summary>
[MaxLength(100)]
[Required(ErrorMessage = "Athlete Last Name is ")]
public string LastName { get; set; }
public required string LastName { get; set; }
/// <summary>
/// Gets or sets the first name of the athlete.
/// </summary>
[MaxLength(150)]
[Required(ErrorMessage = "Athlete First Name is ")]
public string FirstName { get; set; }
public required string FirstName { get; set; }
/// <summary>
/// Gets or sets the email of the athlete.
/// </summary>
[MaxLength(100)]
[Required(ErrorMessage = "Athlete Email is ")]
public string Email { get; set; }
public required string Email { get; set; }
/// <summary>
/// Gets or sets the gender of the athlete.
/// </summary>
[MaxLength(1)]
[Required(ErrorMessage = "Athlete Sexe is ")]
public string Sexe { get; set; }
public required string Sexe { get; set; }
/// <summary>
/// Gets or sets the height of the athlete.
@ -76,7 +76,7 @@ namespace Entities
/// Gets or sets the password of the athlete.
/// </summary>
[Required(ErrorMessage = "Athlete Password is ")]
public string Password { get; set; }
public required string Password { get; set; }
/// <summary>
/// Gets or sets the date of birth of the athlete.

@ -41,6 +41,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleTestEFMapper", "Test
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EFMappers", "EFMappers\EFMappers.csproj", "{9397795D-F482-44C4-8443-A20AC26671AA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "APIMappers", "APIMappers\APIMappers.csproj", "{41D18203-1688-43BD-A3AC-FD0C2BD81909}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -134,6 +136,10 @@ Global
{9397795D-F482-44C4-8443-A20AC26671AA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9397795D-F482-44C4-8443-A20AC26671AA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9397795D-F482-44C4-8443-A20AC26671AA}.Release|Any CPU.Build.0 = Release|Any CPU
{41D18203-1688-43BD-A3AC-FD0C2BD81909}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{41D18203-1688-43BD-A3AC-FD0C2BD81909}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41D18203-1688-43BD-A3AC-FD0C2BD81909}.Release|Any CPU.ActiveCfg = Release|Any CPU
{41D18203-1688-43BD-A3AC-FD0C2BD81909}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{477D2129-A6C9-4FF8-8BE9-5E9E8E5282F8} = {2B227C67-3BEC-4A83-BDA0-F3918FBC0D18}

@ -1,15 +1,17 @@
using DbContextLib;
using DbContextLib.Identity;
using Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Versioning;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using Model.Manager;
using Model.Service;
using Model2Entities;
using StubAPI;
using Swashbuckle.AspNetCore.SwaggerGen;
@ -49,15 +51,29 @@ public class AppBootstrap(IConfiguration configuration)
else
{
services.AddDbContext<AuthDbContext>(options => options.UseInMemoryDatabase("AuthDb"));
// builder.Services.AddDbContext<HeartTrackContext>(options => options.UseSqlite(connectionString));
services.AddSingleton<IDataManager, StubData>();
//options => options.UseSqlite(connectionString)
//services.AddDbContext<HeartTrackContext>();
services.AddDbContext<HeartTrackContext>(options =>
options.UseSqlite(connectionString), ServiceLifetime.Singleton);
/*
services.AddSingleton<DbContextOptions<HeartTrackContext>>(provider =>
{
var connection = new SqliteConnection("DataSource=:memory:");
connection.Open();
var options = new DbContextOptionsBuilder<HeartTrackContext>()
.UseSqlite(connection)
.Options;
return options;
});*/
}
}
private void AddModelService(IServiceCollection services)
{
services.AddSingleton<IDataManager, StubData>();
services.AddTransient<IActivityManager, ActivityManager>();
services.AddSingleton<IDataManager>(provider => new DbDataManager(provider.GetService<HeartTrackContext>()));
//services.AddTransient<IActivityManager, ActivityManager>();
}
private void AddIdentityServices(IServiceCollection services)
@ -105,10 +121,10 @@ public class AppBootstrap(IConfiguration configuration)
public void Configure(WebApplication app, IWebHostEnvironment env)
{
app.UseHttpsRedirection();
app.MapIdentityApi<IdentityUser>();
app.MapControllers();
app.UseAuthorization();
app.MapIdentityApi<IdentityUser>();
app.MapHealthChecks("/health");

@ -1,3 +1,4 @@
using APIMappers;
using Dto;
using HeartTrackAPI.Request;
using HeartTrackAPI.Responce;
@ -16,9 +17,9 @@ public class ActivityController : Controller
private readonly ILogger<ActivityController> _logger;
private readonly IActivityManager _activityManager;
public ActivityController(IActivityRepository activityService, ILogger<ActivityController> logger)
public ActivityController(IDataManager dataManager, ILogger<ActivityController> logger)
{
_activityService = activityService;
_activityService = dataManager.ActivityRepo;
_logger = logger;
}
@ -38,8 +39,8 @@ public class ActivityController : Controller
}
_logger.LogInformation("Executing {Action} with parameters: {Parameters}", nameof(GetActivities), pageRequest);
var activities = await _activityService.GetActivities(pageRequest.Index, pageRequest.Count, ActivityOrderCriteria.None, pageRequest.Descending ?? false);
// var pageResponse = new PageResponse<ActivityDto>(pageRequest.Index, pageRequest.Count, totalCount, activities.Select(a => a.ToDto()));
// return Ok(pageResponse);
var pageResponse = new PageResponse<ActivityDto>(pageRequest.Index, pageRequest.Count, totalCount, activities.Select(a => a.ToDto()));
return Ok(pageResponse);
return Ok();
}
catch (Exception e)
@ -56,14 +57,17 @@ public class ActivityController : Controller
return BadRequest("No file was provided");
}
var activity = await _activityManager.AddActivityFromFitFile(file);
à l'intérieur du AddActivityFromFitFile ya un truc comme var result = await _activityRepo.AddActivity(activity);
if (activity == null)
{
return BadRequest("The file provided is not a valid fit file");
}
return CreatedAtAction(nameof(GetActivity), new { id = result.Id }, result.ToDto());
return CreatedAtAction(nameof(GetActivities), activity.ToDto());
}*/
/*
[HttpGet("{id}")]
public async Task<ActionResult<ActivityDto>> GetActivity(int id)
{
@ -75,18 +79,6 @@ public class ActivityController : Controller
return Ok(activity.ToDto());
}
[HttpPost]
public async Task<ActionResult<ActivityDto>> PostActivity(ActivityDto activityDto)
{
var activity = activityDto.ToModel();
var result = await _activityService.AddActivity(activity);
if (result == null)
{
return BadRequest();
}
return CreatedAtAction(nameof(GetActivity), new { id = result.Id }, result.ToDto());
}
[HttpPut("{id}")]
public async Task<IActionResult> PutActivity(int id, ActivityDto activityDto)
{
@ -112,5 +104,5 @@ public class ActivityController : Controller
return NotFound();
}
return NoContent();
}*/
}
}

@ -1,3 +1,4 @@
using APIMappers;
using Dto;
using Entities;
using HeartTrackAPI.Request;
@ -18,7 +19,6 @@ public class UsersController : Controller
{
private readonly ILogger<UsersController> _logger;
private readonly IUserRepository _userService;
// private readonly
public UsersController(ILogger<UsersController> logger, IDataManager dataManager)
{
_logger = logger;
@ -43,9 +43,8 @@ public class UsersController : Controller
_logger.LogInformation("Executing {Action} with parameters: {Parameters}", nameof(Get), null);
var athletes = await _userService.GetUsers(request.Index, request.Count, Enum.TryParse(request.OrderingPropertyName, out AthleteOrderCriteria result) ? result : AthleteOrderCriteria.None, request.Descending ?? false);
// var pageResponse = new PageResponse<UserDto>(request.Index, request.Count, totalCount, athletes.Select(a => a.ToDto()));
// return Ok(pageResponse);
return Ok();
var pageResponse = new PageResponse<UserDto>(request.Index, request.Count, totalCount, athletes.Select(a => a.ToDto()));
return Ok(pageResponse);
}
catch (Exception e)
{
@ -69,8 +68,7 @@ public class UsersController : Controller
_logger.LogError("Athlete with id {id} not found", id);
return NotFound($"Athlete with id {id} not found");
}
// return Ok(athlete.ToDto());
return Ok();
return Ok(athlete.ToDto());
}
catch (Exception e)
{
@ -113,13 +111,13 @@ public class UsersController : Controller
_logger.LogError("Athlete with id {id} not found", id);
return NotFound($"Athlete with id {id} not found");
}
// var updatedAthlete = await _userService.UpdateItem(id, user.ToModel());
// if(updatedAthlete == null)
// {
// _logger.LogError("Error while updating athlete with id {id}", id);
// return StatusCode(500);
// }
// return Ok(updatedAthlete.ToDto());
var updatedAthlete = await _userService.UpdateItem(id, user.ToModel());
if(updatedAthlete == null)
{
_logger.LogError("Error while updating athlete with id {id}", id);
return StatusCode(500);
}
return Ok(updatedAthlete.ToDto());
return Ok();
}

@ -12,14 +12,19 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning" Version="5.1.0" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.Versioning.ApiExplorer" Version="5.1.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.2" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ApiMappeur\ApiMappeur.csproj" />
<ProjectReference Include="..\APIMappers\APIMappers.csproj" />
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
<ProjectReference Include="..\Dto\Dto.csproj" />
<ProjectReference Include="..\Model2Entities\Model2Entities.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
<ProjectReference Include="..\Shared\Shared.csproj" />
<ProjectReference Include="..\StubAPI\StubAPI.csproj" />

@ -1,6 +0,0 @@
@HeartTrackAPI_HostAddress = http://localhost:5030
GET {{HeartTrackAPI_HostAddress}}/weatherforecast/
Accept: application/json
###

@ -1,13 +1,6 @@
using DbContextLib;
using DbContextLib.Identity;
using Entities;
using HeartTrackAPI;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc.Versioning;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using Model.Manager;
using StubAPI;
var builder = WebApplication.CreateBuilder(args);
builder.Logging.AddConsole();
@ -19,5 +12,7 @@ init.ConfigureServices(builder.Services);
var app = builder.Build();
init.Configure(app, app.Environment);
// app?.Services?.GetService<HeartTrackContext>()?.Database.EnsureCreated();
app.Services.GetService<HeartTrackContext>()!.Database.EnsureCreated();
app.Run();

@ -4,5 +4,8 @@
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"ConnectionStrings": {
"HeartTrackAuthConnection": "Data Source=uca.HeartTrack.db"
}
}

@ -7,18 +7,18 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Entities\Entities.csproj" />
<ProjectReference Include="..\Shared\Shared.csproj" />
<ProjectReference Include="..\Entities\Entities.csproj" />
<ProjectReference Include="..\Shared\Shared.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="8.0.2" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.AspNetCore.Identity">
<HintPath>..\..\..\..\..\.dotnet\shared\Microsoft.AspNetCore.App\8.0.1\Microsoft.AspNetCore.Identity.dll</HintPath>
</Reference>
<Reference Include="Microsoft.AspNetCore.Identity">
<HintPath>..\..\..\..\..\.dotnet\shared\Microsoft.AspNetCore.App\8.0.1\Microsoft.AspNetCore.Identity.dll</HintPath>
</Reference>
</ItemGroup>
</Project>

@ -38,7 +38,7 @@ public partial class DbDataManager : IDataManager
{
_logger.LogInformation($"GetActivityByIdAsync with id {id}", id);
// ! By None don't pass the filter
var activity = _dataManager.DbContext.ActivitiesSet.GetItemsWithFilterAndOrdering(b => b.IdActivity == id, 0, 1, ActivityOrderCriteria.ByType, false).First().ToModel();
var activity = _dataManager.DbContext.ActivitiesSet.GetItemsWithFilterAndOrdering(b => b.IdActivity == id, 0, 1, ActivityOrderCriteria.None, false).First().ToModel();
if (activity != null)
_logger.LogInformation($"Retrieved activity with ID {id}");

@ -18,6 +18,7 @@ public partial class DbDataManager: IDataManager
DbContext = dbContext;
ActivityRepo = new ActivityRepository(this);
UserRepo = new UserRepository(this);
DbContext.Database.EnsureCreated();
ActivityMapper.Reset();
// Faire pour les autres reset() des autres mappers
}

@ -7,28 +7,25 @@ namespace Model2Entities;
public static class Extensions
{
internal static Task<T?> AddItem<T>(this HeartTrackContext context, T? item) where T :class
internal static async Task<T?> AddItem<T>(this HeartTrackContext context, T? item) where T :class
{
if(item == null || context.Set<T>().Contains(item))
{
return Task.FromResult<T?>(default(T));
return await Task.FromResult<T?>(null);
}
context.Set<T>().Add(item);
context.SaveChangesAsync();
var entry = context.Set<T>().Add(item);
await context.SaveChangesAsync();
return Task.FromResult<T?>(item);
return await Task.FromResult<T?>(entry.Entity);
}
internal static Task<bool> DeleteItem<T>(this HeartTrackContext context, int? id) where T:class
internal static async Task<bool> DeleteItem<T>(this HeartTrackContext context, int? id) where T:class
{
var item = context.Set<T>().Find(id);
if(item == null)
{
return Task.FromResult(false);
}
var item = await context.Set<T>().FindAsync(id);
if(item == null) return await Task.FromResult(false);
context.Set<T>().Remove(item);
context.SaveChangesAsync();
return Task.FromResult(true);
await context.SaveChangesAsync();
return await Task.FromResult(true);
}
internal static async Task<T?> UpdateItem<T>(this IList<T> collection, T? oldItem, T? newItem) where T : class
@ -50,10 +47,11 @@ public static class Extensions
{
var filteredList = list.Where(filter);
if(orderCriterium != null)
if(orderCriterium != null && orderCriterium.ToString() != "None")
{
filteredList = filteredList.OrderByCriteria(orderCriterium, descending);
}
}
return filteredList
.Skip(index * count)
.Take(count);

@ -10,6 +10,14 @@
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
<ProjectReference Include="..\Model\Model.csproj" />
<ProjectReference Include="..\EFMappers\EFMappers.csproj" />
<ProjectReference Include="..\StubbedContextLib\StubbedContextLib.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
</Project>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

@ -1,16 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">
<ItemGroup>
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
<ProjectReference Include="..\Entities\Entities.csproj" />
<ItemGroup>
<ProjectReference Include="..\DbContextLib\DbContextLib.csproj" />
<ProjectReference Include="..\Entities\Entities.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="8.0.2" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<PropertyGroup>

Loading…
Cancel
Save