108 lines
3.3 KiB
C#
108 lines
3.3 KiB
C#
using GradeBookServer.Application.Common.Specifications;
|
|
using GradeBookServer.Application.DTOs.Direction;
|
|
using GradeBookServer.Application.DTOs.Faculty;
|
|
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 FacultyService
|
|
{
|
|
private readonly IBaseRepository<Faculty> _baseRepository;
|
|
|
|
public FacultyService(IBaseRepository<Faculty> baseRepository)
|
|
{
|
|
_baseRepository = baseRepository;
|
|
}
|
|
|
|
public async Task<FacultyReadDto?> GetFacultyByIdAsync(int id)
|
|
{
|
|
var faculty = await _baseRepository.GetByIdWithIncludeAsync(
|
|
f => f.ID == id,
|
|
new IncludeSpecification<Faculty>(f => f.Directions!)
|
|
);
|
|
|
|
if (faculty == null)
|
|
return null;
|
|
|
|
return new FacultyReadDto
|
|
{
|
|
ID = faculty.ID,
|
|
Name = faculty.Name,
|
|
Directions = faculty.Directions.Select(
|
|
d => new DirectionNameDto { ID = d.ID, Name = d.Name}
|
|
).ToList()
|
|
};
|
|
}
|
|
|
|
public async Task<IEnumerable<FacultyReadDto>?> GetAllFacultiesAsync()
|
|
{
|
|
var faculties = await _baseRepository.GetAllAsync(
|
|
new IncludeSpecification<Faculty>(f => f.Directions!)
|
|
);
|
|
|
|
if (faculties == null)
|
|
return null;
|
|
|
|
return faculties.Select(f => new FacultyReadDto
|
|
{
|
|
ID = f.ID,
|
|
Name = f.Name,
|
|
Directions = f.Directions.Select(d => new DirectionNameDto { ID = d.ID, Name = d.Name }
|
|
).ToList()
|
|
});
|
|
}
|
|
|
|
public async Task AddFacultyAsync(FacultyCreateDto facultyDto)
|
|
{
|
|
var directions = await _baseRepository.GetManyByIdsAsync<Direction>(facultyDto.DirectionIds);
|
|
|
|
var faculty = new Faculty
|
|
{
|
|
Name = facultyDto.Name,
|
|
Directions = directions.ToList()
|
|
};
|
|
|
|
await _baseRepository.AddAsync(faculty);
|
|
}
|
|
|
|
public async Task<bool> UpdateFacultyAsync(int id, FacultyCreateDto facultyDto)
|
|
{
|
|
var faculty = await _baseRepository.GetByIdWithIncludeAsync(
|
|
f => f.ID == id,
|
|
new IncludeSpecification<Faculty>(f => f.Directions!)
|
|
);
|
|
|
|
if (faculty == null)
|
|
return false;
|
|
|
|
faculty.Name = facultyDto.Name;
|
|
|
|
var directions = await _baseRepository.GetManyByIdsAsync<Direction>(facultyDto.DirectionIds);
|
|
|
|
faculty.Directions.Clear();
|
|
foreach (var dir in directions)
|
|
faculty.Directions.Add(dir);
|
|
|
|
await _baseRepository.UpdateAsync(faculty);
|
|
return true;
|
|
}
|
|
|
|
public async Task<bool> DeleteFacultyAsync(int id)
|
|
{
|
|
var faculty = await _baseRepository.GetByIdAsync(id);
|
|
if (faculty == null)
|
|
return false;
|
|
|
|
await _baseRepository.DeleteAsync(id);
|
|
return true;
|
|
}
|
|
}
|
|
}
|