Sorry Leo 💄 💄

main
Louis LABORIE 5 months ago
commit 8029b78885

@ -4,7 +4,7 @@ using Infrastructure.Base;
namespace Infrastructure.Entities;
public class Program : EntityBase
public class TrainingProgram : EntityBase
{
[Required]
public string Name { get; set; }

@ -0,0 +1,191 @@
// <auto-generated />
using Infrastructure;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace Infrastructure.Migrations
{
[DbContext(typeof(OptifitDbContext))]
[Migration("20250109094119_TrainingProgram")]
partial class TrainingProgram
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.1");
modelBuilder.Entity("Infrastructure.Entities.Exercice", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("TEXT");
b.Property<float>("Duration")
.HasColumnType("REAL");
b.Property<string>("Image")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int>("NbRepetitions")
.HasColumnType("INTEGER");
b.Property<int>("NbSeries")
.HasColumnType("INTEGER");
b.Property<string>("SessionId")
.HasColumnType("TEXT");
b.Property<string>("Video")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("SessionId");
b.ToTable("Exercices");
});
modelBuilder.Entity("Infrastructure.Entities.Program", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Difficulty")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int>("WeekDuration")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.ToTable("Programs");
});
modelBuilder.Entity("Infrastructure.Entities.Session", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("TEXT");
b.Property<float>("Duration")
.HasColumnType("REAL");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("ProgramId")
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("ProgramId");
b.ToTable("Sessions");
});
modelBuilder.Entity("Infrastructure.Entities.User", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.Property<int>("Age")
.HasColumnType("INTEGER");
b.Property<string>("EGoal")
.HasColumnType("TEXT");
b.Property<string>("ESleepLevel")
.HasColumnType("TEXT");
b.Property<string>("ESportLevel")
.HasColumnType("TEXT");
b.Property<string>("HashPassword")
.IsRequired()
.HasColumnType("TEXT");
b.Property<float>("Height")
.HasColumnType("REAL");
b.Property<string>("Logo")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int>("NbSessionPerWeek")
.HasColumnType("INTEGER");
b.Property<string>("OAuthId")
.HasColumnType("TEXT");
b.Property<string>("OAuthProvider")
.HasColumnType("TEXT");
b.Property<bool>("Sexe")
.HasColumnType("INTEGER");
b.Property<float>("Weight")
.HasColumnType("REAL");
b.HasKey("Id");
b.ToTable("Users");
});
modelBuilder.Entity("Infrastructure.Entities.Exercice", b =>
{
b.HasOne("Infrastructure.Entities.Session", null)
.WithMany("Exercices")
.HasForeignKey("SessionId");
});
modelBuilder.Entity("Infrastructure.Entities.Session", b =>
{
b.HasOne("Infrastructure.Entities.Program", null)
.WithMany("Sessions")
.HasForeignKey("ProgramId");
});
modelBuilder.Entity("Infrastructure.Entities.Program", b =>
{
b.Navigation("Sessions");
});
modelBuilder.Entity("Infrastructure.Entities.Session", b =>
{
b.Navigation("Exercices");
});
#pragma warning restore 612, 618
}
}
}

@ -0,0 +1,94 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Infrastructure.Migrations
{
/// <inheritdoc />
public partial class TrainingProgram : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "SessionId",
table: "Exercices",
type: "TEXT",
nullable: true);
migrationBuilder.CreateTable(
name: "Programs",
columns: table => new
{
Id = table.Column<string>(type: "TEXT", nullable: false),
Name = table.Column<string>(type: "TEXT", nullable: false),
WeekDuration = table.Column<int>(type: "INTEGER", nullable: false),
Description = table.Column<string>(type: "TEXT", nullable: false),
Difficulty = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Programs", x => x.Id);
});
migrationBuilder.CreateTable(
name: "Sessions",
columns: table => new
{
Id = table.Column<string>(type: "TEXT", nullable: false),
Name = table.Column<string>(type: "TEXT", nullable: false),
Description = table.Column<string>(type: "TEXT", nullable: false),
Duration = table.Column<float>(type: "REAL", nullable: false),
ProgramId = table.Column<string>(type: "TEXT", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Sessions", x => x.Id);
table.ForeignKey(
name: "FK_Sessions_Programs_ProgramId",
column: x => x.ProgramId,
principalTable: "Programs",
principalColumn: "Id");
});
migrationBuilder.CreateIndex(
name: "IX_Exercices_SessionId",
table: "Exercices",
column: "SessionId");
migrationBuilder.CreateIndex(
name: "IX_Sessions_ProgramId",
table: "Sessions",
column: "ProgramId");
migrationBuilder.AddForeignKey(
name: "FK_Exercices_Sessions_SessionId",
table: "Exercices",
column: "SessionId",
principalTable: "Sessions",
principalColumn: "Id");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_Exercices_Sessions_SessionId",
table: "Exercices");
migrationBuilder.DropTable(
name: "Sessions");
migrationBuilder.DropTable(
name: "Programs");
migrationBuilder.DropIndex(
name: "IX_Exercices_SessionId",
table: "Exercices");
migrationBuilder.DropColumn(
name: "SessionId",
table: "Exercices");
}
}
}

@ -42,15 +42,71 @@ namespace Infrastructure.Migrations
b.Property<int>("NbSeries")
.HasColumnType("INTEGER");
b.Property<string>("SessionId")
.HasColumnType("TEXT");
b.Property<string>("Video")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("SessionId");
b.ToTable("Exercices");
});
modelBuilder.Entity("Infrastructure.Entities.Program", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Difficulty")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<int>("WeekDuration")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.ToTable("Programs");
});
modelBuilder.Entity("Infrastructure.Entities.Session", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.Property<string>("Description")
.IsRequired()
.HasColumnType("TEXT");
b.Property<float>("Duration")
.HasColumnType("REAL");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("ProgramId")
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("ProgramId");
b.ToTable("Sessions");
});
modelBuilder.Entity("Infrastructure.Entities.User", b =>
{
b.Property<string>("Id")
@ -108,6 +164,30 @@ namespace Infrastructure.Migrations
b.ToTable("Users");
});
modelBuilder.Entity("Infrastructure.Entities.Exercice", b =>
{
b.HasOne("Infrastructure.Entities.Session", null)
.WithMany("Exercices")
.HasForeignKey("SessionId");
});
modelBuilder.Entity("Infrastructure.Entities.Session", b =>
{
b.HasOne("Infrastructure.Entities.Program", null)
.WithMany("Sessions")
.HasForeignKey("ProgramId");
});
modelBuilder.Entity("Infrastructure.Entities.Program", b =>
{
b.Navigation("Sessions");
});
modelBuilder.Entity("Infrastructure.Entities.Session", b =>
{
b.Navigation("Exercices");
});
#pragma warning restore 612, 618
}
}

@ -7,6 +7,8 @@ namespace Infrastructure
{
public virtual DbSet<User> Users { get; set; }
public virtual DbSet<Exercice> Exercices { get; set; }
public virtual DbSet<Session> Sessions { get; set; }
public virtual DbSet<TrainingProgram> Programs { get; set; }
public OptifitDbContext()
{

@ -0,0 +1,7 @@
using Infrastructure.Entities;
namespace Infrastructure.Repositories;
public interface ITrainingProgramRepository : IRepository<TrainingProgram>
{
}

@ -0,0 +1,10 @@
using Infrastructure.Entities;
namespace Infrastructure.Repositories;
public class TrainingProgramRepository : GenericRepository<TrainingProgram>, ITrainingProgramRepository
{
public TrainingProgramRepository(OptifitDbContext context) : base(context)
{
}
}

@ -24,7 +24,6 @@ public class ExercicesController : ControllerBase
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<ResponseExerciceDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[RequireHttps]
[AllowAnonymous]
public async Task<IActionResult> GetExercices([FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 5, [FromQuery] bool ascending = true)
{
@ -35,7 +34,6 @@ public class ExercicesController : ControllerBase
[HttpGet("{id}")]
[ProducesResponseType(typeof(ResponseExerciceDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[RequireHttps]
[AllowAnonymous]
public async Task<IActionResult> GetExerciceById(string id)
{
@ -59,7 +57,6 @@ public class ExercicesController : ControllerBase
[ProducesResponseType(typeof(ResponseExerciceDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[RequireHttps]
[AllowAnonymous]
public async Task<IActionResult> UpdateExercice(string id, [FromBody] RequestExerciceDto request)
{

@ -1,6 +1,79 @@
namespace Server.Controller.v1;
using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Server.Dto.Request;
using Server.Dto.Response;
using Server.IServices;
public class SessionsController
namespace Server.Controller.v1
{
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
public class SessionsController : ControllerBase
{
private readonly ILogger<SessionsController> _logger;
private readonly ISessionService _dataServices;
public SessionsController(ILogger<SessionsController> logger, ISessionService dataServices)
{
_logger = logger;
_dataServices = dataServices;
}
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<ResponseSessionDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[AllowAnonymous]
public async Task<IActionResult> GetSessions([FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 5, [FromQuery] bool ascending = true)
{
var sessions = await _dataServices.GetSessions(pageIndex, pageSize, ascending);
return sessions.TotalCount == 0 ? NoContent() : Ok(sessions);
}
[HttpGet("{id}")]
[ProducesResponseType(typeof(ResponseSessionDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[AllowAnonymous]
public async Task<IActionResult> GetSessionById(string id)
{
var session = await _dataServices.GetSessionById(id);
return session == null ? NotFound() : Ok(session);
}
[HttpPost]
[ProducesResponseType(typeof(ResponseSessionDto), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[AllowAnonymous]
public async Task<IActionResult> CreateSession([FromBody] RequestSessionDto request)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var createdSession = await _dataServices.CreateSession(request);
return CreatedAtAction(nameof(GetSessionById), new { id = createdSession.Id }, createdSession);
}
[HttpPut("{id}")]
[ProducesResponseType(typeof(ResponseSessionDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[AllowAnonymous]
public async Task<IActionResult> UpdateSession(string id, [FromBody] RequestSessionDto request)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var updatedSession = await _dataServices.UpdateSession(id, request);
return updatedSession == null ? NotFound() : Ok(updatedSession);
}
[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[AllowAnonymous]
public async Task<IActionResult> DeleteSession(string id)
{
var deleted = await _dataServices.DeleteSession(id);
return deleted ? NoContent() : NotFound();
}
}
}

@ -0,0 +1,79 @@
using Asp.Versioning;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Server.Dto.Request;
using Server.Dto.Response;
using Server.IServices;
namespace Server.Controller.v1
{
[ApiController]
[ApiVersion("1.0")]
[Route("api/v{version:apiVersion}/[controller]")]
public class TrainingProgramsController : ControllerBase
{
private readonly ILogger<TrainingProgramsController> _logger;
private readonly ITrainingProgramService _dataServices;
public TrainingProgramsController(ILogger<TrainingProgramsController> logger, ITrainingProgramService dataServices)
{
_logger = logger;
_dataServices = dataServices;
}
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<ResponseTrainingProgramDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[AllowAnonymous]
public async Task<IActionResult> GetTrainingPrograms([FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 5, [FromQuery] bool ascending = true)
{
var programs = await _dataServices.GetTrainingPrograms(pageIndex, pageSize, ascending);
return programs.TotalCount == 0 ? NoContent() : Ok(programs);
}
[HttpGet("{id}")]
[ProducesResponseType(typeof(ResponseTrainingProgramDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[AllowAnonymous]
public async Task<IActionResult> GetTrainingProgramById(string id)
{
var program = await _dataServices.GetTrainingProgramById(id);
return program == null ? NotFound() : Ok(program);
}
[HttpPost]
[ProducesResponseType(typeof(ResponseTrainingProgramDto), StatusCodes.Status201Created)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[AllowAnonymous]
public async Task<IActionResult> CreateTrainingProgram([FromBody] RequestTrainingProgramDto request)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var createdProgram = await _dataServices.CreateTrainingProgram(request);
return CreatedAtAction(nameof(GetTrainingProgramById), new { id = createdProgram.Id }, createdProgram);
}
[HttpPut("{id}")]
[ProducesResponseType(typeof(ResponseTrainingProgramDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[AllowAnonymous]
public async Task<IActionResult> UpdateTrainingProgram(string id, [FromBody] RequestTrainingProgramDto request)
{
if (!ModelState.IsValid) return BadRequest(ModelState);
var updatedProgram = await _dataServices.UpdateTrainingProgram(id, request);
return updatedProgram == null ? NotFound() : Ok(updatedProgram);
}
[HttpDelete("{id}")]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[AllowAnonymous]
public async Task<IActionResult> DeleteTrainingProgram(string id)
{
var deleted = await _dataServices.DeleteTrainingProgram(id);
return deleted ? NoContent() : NotFound();
}
}
}

@ -23,7 +23,6 @@ public class UsersController : ControllerBase
[HttpGet]
[ProducesResponseType(typeof(IEnumerable<ResponseUserDto>), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status204NoContent)]
[RequireHttps]
public IActionResult GetUsers([FromQuery] int pageIndex = 1, [FromQuery] int pageSize = 5, [FromQuery] bool ascending = true)
{
var users = _dataServices.GetUsers(pageIndex, pageSize, ascending);
@ -35,8 +34,6 @@ public class UsersController : ControllerBase
[HttpGet("{id}")]
[ProducesResponseType(typeof(ResponseUserDto), StatusCodes.Status200OK)]
[ProducesResponseType(StatusCodes.Status404NotFound)]
[RequireHttps]
public IActionResult GetAlumniById(string id)
{
var alumni = _dataServices.GetUserById(id);

@ -1,4 +1,3 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Server.Dto.Request
@ -12,6 +11,6 @@ namespace Server.Dto.Request
public float Duration { get; set; }
public List<RequestExerciceDto> Exercices { get; set; } = new List<RequestExerciceDto>();
public List<string> ExerciceIds { get; set; } = new List<string>();
}
}

@ -3,7 +3,7 @@ using System.ComponentModel.DataAnnotations;
namespace Server.Dto.Request
{
public class RequestProgramDto
public class RequestTrainingProgramDto
{
[Required]
public string Name { get; set; }

@ -1,5 +1,3 @@
using System.Collections.Generic;
namespace Server.Dto.Response
{
public class ResponseSessionDto

@ -2,7 +2,7 @@ using System.Collections.Generic;
namespace Server.Dto.Response
{
public class ResponseProgramDto
public class ResponseTrainingProgramDto
{
public string Id { get; set; }

Binary file not shown.

@ -1,6 +1,15 @@
namespace Server.IServices;
using Server.Dto.Request;
using Server.Dto.Response;
using Shared;
public class ISessionService
namespace Server.IServices
{
public interface ISessionService
{
Task<PaginatedResult<ResponseSessionDto>> GetSessions(int page, int size, bool ascending = true);
Task<ResponseSessionDto?> GetSessionById(string id);
Task<ResponseSessionDto> CreateSession(RequestSessionDto request);
Task<ResponseSessionDto?> UpdateSession(string id, RequestSessionDto request);
Task<bool> DeleteSession(string id);
}
}

@ -0,0 +1,15 @@
using Server.Dto.Request;
using Server.Dto.Response;
using Shared;
namespace Server.IServices
{
public interface ITrainingProgramService
{
Task<PaginatedResult<ResponseTrainingProgramDto>> GetTrainingPrograms(int page, int size, bool ascending = true);
Task<ResponseTrainingProgramDto?> GetTrainingProgramById(string id);
Task<ResponseTrainingProgramDto> CreateTrainingProgram(RequestTrainingProgramDto request);
Task<ResponseTrainingProgramDto?> UpdateTrainingProgram(string id, RequestTrainingProgramDto request);
Task<bool> DeleteTrainingProgram(string id);
}
}

@ -1,6 +1,16 @@
namespace Server.Mappers;
using AutoMapper;
using Shared;
using Infrastructure.Entities;
using Server.Dto.Response;
public class PaginatedResultProfile
namespace Server.Mappers
{
public class PaginatedResultProfile : Profile
{
public PaginatedResultProfile()
{
CreateMap<PaginatedResult<Exercice>, PaginatedResult<ResponseExerciceDto>>()
.ForMember(dest => dest.Data, opt => opt.MapFrom(src => src.Data));
}
}
}

@ -1,6 +1,19 @@
namespace Server.Mappers;
using AutoMapper;
using Infrastructure.Entities;
using Server.Dto.Request;
using Server.Dto.Response;
public class SessionProfile
namespace Server.Mappers
{
public class SessionProfile : Profile
{
public SessionProfile()
{
CreateMap<Session, ResponseSessionDto>()
.ForMember(dest => dest.Exercices, opt => opt.MapFrom(src => src.Exercices));
CreateMap<RequestSessionDto, Session>()
.ForMember(dest => dest.Exercices, opt => opt.Ignore());
}
}
}

@ -0,0 +1,19 @@
using AutoMapper;
using Infrastructure.Entities;
using Server.Dto.Request;
using Server.Dto.Response;
namespace Server.Mappers
{
public class TrainingProgramProfile : Profile
{
public TrainingProgramProfile()
{
CreateMap<TrainingProgram, ResponseTrainingProgramDto>()
.ForMember(dest => dest.Sessions, opt => opt.MapFrom(src => src.Sessions));
CreateMap<RequestTrainingProgramDto, TrainingProgram>()
.ForMember(dest => dest, opt => opt.Ignore());
}
}
}

@ -13,18 +13,26 @@ var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddScoped<IUsersService, UsersService>();
builder.Services.AddScoped<IExerciceService, ExerciceService>();
builder.Services.AddScoped<ISessionService, SessionService>();
builder.Services.AddScoped<ITrainingProgramService, TrainingProgramService>();
builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IExerciceRepository, ExerciceRepository>();
builder.Services.AddScoped<ISessionRepository, SessionRepository>();
builder.Services.AddScoped<ITrainingProgramRepository, TrainingProgramRepository>();
builder.Services.AddControllers();
builder.Services.AddDbContext<OptifitDbContext>(options =>
options.UseSqlite("Data Source=FirstTest.db"));
options.UseSqlite("Data Source=../Server/FirstTest.db", b => b.MigrationsAssembly("Infrastructure")));
// Register AutoMapper
builder.Services.AddAutoMapper(typeof(UserProfile));
builder.Services.AddAutoMapper(typeof(ExerciceProfile));
builder.Services.AddAutoMapper(typeof(SessionProfile));
builder.Services.AddAutoMapper(typeof(PaginatedResultProfile));
builder.Services.AddAutoMapper(typeof(TrainingProgramProfile));
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();

@ -1,6 +1,71 @@
namespace Server.Services;
using AutoMapper;
using Infrastructure;
using Infrastructure.Entities;
using Infrastructure.Repositories;
using Server.Dto.Request;
using Server.Dto.Response;
using Server.IServices;
using Shared;
public class SessionService
namespace Server.Services
{
public class SessionService : ISessionService
{
private readonly OptifitDbContext _context;
private readonly ISessionRepository _sessionRepository;
private readonly IExerciceRepository _exerciceRepository;
private readonly IMapper _mapper;
public SessionService(OptifitDbContext context, ISessionRepository sessionRepository, IExerciceRepository exerciceRepository, IMapper mapper)
{
_context = context;
_sessionRepository = sessionRepository;
_exerciceRepository = exerciceRepository;
_mapper = mapper;
}
public async Task<PaginatedResult<ResponseSessionDto>> GetSessions(int page, int size, bool ascending = true)
{
var sessions = await _sessionRepository.GetPaginatedListAsync(page - 1, size, null, null);
var result = _mapper.Map<PaginatedResult<ResponseSessionDto>>(sessions);
return result;
}
public async Task<ResponseSessionDto?> GetSessionById(string id)
{
var session = await _sessionRepository.GetByIdAsync(id, s => s.Exercices);
return session == null ? null : _mapper.Map<ResponseSessionDto>(session);
}
public async Task<ResponseSessionDto> CreateSession(RequestSessionDto request)
{
var session = _mapper.Map<Session>(request);
session.Exercices = (ICollection<Exercice>)await _exerciceRepository.GetAllAsync(e => request.ExerciceIds.Contains(e.Id));
await _sessionRepository.InsertAsync(session);
await _context.SaveChangesAsync();
return _mapper.Map<ResponseSessionDto>(session);
}
public async Task<ResponseSessionDto?> UpdateSession(string id, RequestSessionDto request)
{
var session = await _sessionRepository.GetByIdAsync(id, s => s.Exercices);
if (session == null) return null;
_mapper.Map(request, session);
session.Exercices = (ICollection<Exercice>)await _exerciceRepository.GetAllAsync(e => request.ExerciceIds.Contains(e.Id));
_sessionRepository.Update(session);
await _context.SaveChangesAsync();
return _mapper.Map<ResponseSessionDto>(session);
}
public async Task<bool> DeleteSession(string id)
{
var session = await _sessionRepository.GetByIdAsync(id);
if (session == null) return false;
_sessionRepository.Delete(id);
await _context.SaveChangesAsync();
return true;
}
}
}

@ -0,0 +1,67 @@
using AutoMapper;
using Infrastructure;
using Infrastructure.Entities;
using Infrastructure.Repositories;
using Server.Dto.Request;
using Server.Dto.Response;
using Server.IServices;
using Shared;
namespace Server.Services
{
public class TrainingProgramService : ITrainingProgramService
{
private readonly OptifitDbContext _context;
private readonly ITrainingProgramRepository _trainingProgramRepository;
private readonly IMapper _mapper;
public TrainingProgramService(OptifitDbContext context, ITrainingProgramRepository trainingProgramRepository, IMapper mapper)
{
_context = context;
_trainingProgramRepository = trainingProgramRepository;
_mapper = mapper;
}
public async Task<PaginatedResult<ResponseTrainingProgramDto>> GetTrainingPrograms(int page, int size, bool ascending = true)
{
var programs = await _trainingProgramRepository.GetPaginatedListAsync(page - 1, size, null, null);
var result = _mapper.Map<PaginatedResult<ResponseTrainingProgramDto>>(programs);
return result;
}
public async Task<ResponseTrainingProgramDto?> GetTrainingProgramById(string id)
{
var program = await _trainingProgramRepository.GetByIdAsync(id, p => p.Sessions);
return program == null ? null : _mapper.Map<ResponseTrainingProgramDto>(program);
}
public async Task<ResponseTrainingProgramDto> CreateTrainingProgram(RequestTrainingProgramDto request)
{
var program = _mapper.Map<TrainingProgram>(request);
await _trainingProgramRepository.InsertAsync(program);
await _context.SaveChangesAsync();
return _mapper.Map<ResponseTrainingProgramDto>(program);
}
public async Task<ResponseTrainingProgramDto?> UpdateTrainingProgram(string id, RequestTrainingProgramDto request)
{
var program = await _trainingProgramRepository.GetByIdAsync(id, p => p.Sessions);
if (program == null) return null;
_mapper.Map(request, program);
_trainingProgramRepository.Update(program);
await _context.SaveChangesAsync();
return _mapper.Map<ResponseTrainingProgramDto>(program);
}
public async Task<bool> DeleteTrainingProgram(string id)
{
var program = await _trainingProgramRepository.GetByIdAsync(id);
if (program == null) return false;
_trainingProgramRepository.Delete(id);
await _context.SaveChangesAsync();
return true;
}
}
}
Loading…
Cancel
Save