You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OptifitWebService/Server/Services/TrainingProgramService.cs

67 lines
2.6 KiB

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;
}
}
}