96 lines
2.9 KiB
C#
96 lines
2.9 KiB
C#
using GradeBookServer.Application.Common.Specifications;
|
|
using GradeBookServer.Application.DTOs.Direction;
|
|
using GradeBookServer.Application.DTOs.Discipline;
|
|
using GradeBookServer.Application.DTOs.Group;
|
|
using GradeBookServer.Application.DTOs.Student;
|
|
using GradeBookServer.Application.Interfaces;
|
|
using GradeBookServer.Domain.Entities;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GradeBookServer.Application.Services
|
|
{
|
|
public class DisciplineService
|
|
{
|
|
private readonly IBaseRepository<Discipline> _baseRepository;
|
|
|
|
public DisciplineService(IBaseRepository<Discipline> baseRepository)
|
|
{
|
|
_baseRepository = baseRepository;
|
|
}
|
|
|
|
public async Task<DisciplineReadDto?> GetDisciplineByIdAsync(int id)
|
|
{
|
|
var discipline = await _baseRepository.GetByIdWithIncludeAsync(
|
|
d => d.ID == id,
|
|
new IncludeSpecification<Discipline>(d => d.Directions!)
|
|
);
|
|
|
|
if (discipline == null)
|
|
return null;
|
|
|
|
return new DisciplineReadDto
|
|
{
|
|
ID = discipline.ID,
|
|
Name = discipline.Name,
|
|
Hours = discipline.TotalHours,
|
|
Directions = discipline.Directions.Select(d => new DirectionNameDto
|
|
{
|
|
ID = d.ID,
|
|
Name = d.Name
|
|
}).ToList()
|
|
};
|
|
}
|
|
|
|
public async Task<IEnumerable<DisciplineNameDto>?> GetAllDisciplinesAsync()
|
|
{
|
|
var disciplines = await _baseRepository.GetAllAsync();
|
|
|
|
if (disciplines == null)
|
|
return null;
|
|
|
|
return disciplines.Select(d => new DisciplineNameDto
|
|
{
|
|
ID = d.ID,
|
|
Name = d.Name
|
|
});
|
|
}
|
|
|
|
public async Task AddDisciplineAsync(DisciplineCreateDto dto)
|
|
{
|
|
var directions = await _baseRepository.GetManyByIdsAsync<Direction>(dto.DirectionIds);
|
|
|
|
var discipline = new Discipline
|
|
{
|
|
Name = dto.Name,
|
|
TotalHours = dto.Hours,
|
|
Directions = directions.ToList()
|
|
};
|
|
|
|
await _baseRepository.AddAsync(discipline);
|
|
}
|
|
|
|
public async Task UpdateDisciplineAsync(int id, DisciplineCreateDto dto)
|
|
{
|
|
var discipline = await _baseRepository.GetByIdAsync(id);
|
|
if (discipline == null) return;
|
|
|
|
var directions = await _baseRepository.GetManyByIdsAsync<Direction>(dto.DirectionIds);
|
|
|
|
discipline.Name = dto.Name;
|
|
discipline.TotalHours = dto.Hours;
|
|
discipline.Directions = directions.ToList();
|
|
|
|
await _baseRepository.UpdateAsync(discipline);
|
|
}
|
|
|
|
public async Task DeleteDisciplineAsync(int id)
|
|
{
|
|
await _baseRepository.DeleteAsync(id);
|
|
}
|
|
}
|
|
}
|