Изменил дто и добавил в контроллеры методы поиска по id зависимых сущностей

This commit is contained in:
2025-06-04 01:37:01 +04:00
parent 05b2665650
commit b70066ba5c
21 changed files with 148 additions and 52 deletions

View File

@@ -6,10 +6,9 @@ using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Direction
{
public class DirectionTableDto
public class DirectionNameDto
{
public int ID { get; set; }
public required string Name { get; set; }
public string? FacultyName { get; set; }
}
}

View File

@@ -8,12 +8,12 @@ using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Direction
{
public class DirectionDetailDto
public class DirectionReadDto
{
public int ID { get; set; }
public required string Name { get; set; }
public required string FacultyName { get; set; }
public List<DisciplineTableDto> Disciplines { get; set; } = new();
public List<GroupTableDto> Groups { get; set; } = new();
public List<DisciplineNameDto> Disciplines { get; set; } = new();
public List<GroupNameDto> Groups { get; set; } = new();
}
}

View File

@@ -6,10 +6,9 @@ using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Discipline
{
public class DisciplineTableDto
public class DisciplineNameDto
{
public int ID { get; set; }
public required string Name { get; set; }
public int Hours { get; set; }
}
}

View File

@@ -7,11 +7,11 @@ using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Discipline
{
public class DisciplineDetailDto
public class DisciplineReadDto
{
public int ID { get; set; }
public required string Name { get; set; }
public int Hours { get; set; }
public List<DirectionTableDto> Directions { get; set; } = new List<DirectionTableDto>();
public List<DirectionNameDto> Directions { get; set; } = new List<DirectionNameDto>();
}
}

View File

@@ -11,6 +11,6 @@ namespace GradeBookServer.Application.DTOs.Faculty
{
public int ID { get; set; }
public required string Name { get; set; }
public List<DirectionTableDto> Directions { get; set; } = new List<DirectionTableDto>();
public List<DirectionNameDto> Directions { get; set; } = new List<DirectionNameDto>();
}
}

View File

@@ -6,7 +6,7 @@ using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Group
{
public class GroupTableDto
public class GroupNameDto
{
public int ID { get; set; }
public required string Name { get; set; }

View File

@@ -7,7 +7,7 @@ using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Group
{
public class GroupDetailDto
public class GroupReadDto
{
public int ID { get; set; }
public required string Name { get; set; }

View File

@@ -13,5 +13,4 @@ namespace GradeBookServer.Application.DTOs.Student
public int Age { get; set; }
public string? GroupName { get; set; }
}
}

View File

@@ -14,6 +14,7 @@ namespace GradeBookServer.Application.Interfaces
{
Task<TEntity?> GetByIdAsync(int id);
Task<IEnumerable<TEntity>?> GetAllAsync(IncludeSpecification<TEntity>? includeSpec = null);
Task<IEnumerable<TEntity>> WhereAsync(Expression<Func<TEntity, bool>> predicate, IncludeSpecification<TEntity>? includeSpec = null);
Task AddAsync(TEntity entity);
Task UpdateAsync(TEntity entity);
Task DeleteAsync(int id);

View File

@@ -2,6 +2,7 @@
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;
@@ -21,7 +22,7 @@ namespace GradeBookServer.Application.Services
_baseRepository = baseRepository;
}
public async Task<DirectionDetailDto?> GetDirectionByIdAsync(int id)
public async Task<DirectionReadDto?> GetDirectionByIdAsync(int id)
{
var direction = await _baseRepository.GetByIdWithIncludeAsync(
d => d.ID == id,
@@ -34,30 +35,48 @@ namespace GradeBookServer.Application.Services
if (direction == null)
return null;
return new DirectionDetailDto
return new DirectionReadDto
{
ID = direction.ID,
Name = direction.Name,
FacultyName = direction.Faculty?.Name ?? "—",
Disciplines = direction.Disciplines.Select(x => new DisciplineTableDto { ID = x.ID, Name = x.Name, Hours = x.TotalHours}).ToList(),
Groups = direction.Groups.Select(g => new GroupTableDto { ID = g.ID, Name = g.Name}).ToList()
Disciplines = direction.Disciplines.Select(x => new DisciplineNameDto { ID = x.ID, Name = x.Name}).ToList(),
Groups = direction.Groups.Select(g => new GroupNameDto { ID = g.ID, Name = g.Name}).ToList()
};
}
public async Task<IEnumerable<DirectionTableDto>?> GetAllDirectionsAsync()
public async Task<IEnumerable<DirectionNameDto>?> GetAllDirectionsAsync()
{
var directions = await _baseRepository.GetAllAsync(
new IncludeSpecification<Direction>(d => d.Faculty!)
);
var directions = await _baseRepository.GetAllAsync();
if (directions == null)
return null;
return directions.Select(d => new DirectionTableDto
return directions.Select(d => new DirectionNameDto
{
ID = d.ID,
Name = d.Name,
FacultyName = d.Faculty?.Name ?? "—"
Name = d.Name
});
}
public async Task<IEnumerable<DirectionReadDto>> GetDirectionsByFacultyIdAsync(int facultyId)
{
var directions = await _baseRepository
.WhereAsync(
d => d.FacultyID == facultyId,
new IncludeSpecification<Direction>(
d => d.Faculty!,
d => d.Disciplines,
d => d.Groups)
);
return directions.Select(direction => new DirectionReadDto
{
ID = direction.ID,
Name = direction.Name,
FacultyName = direction.Faculty?.Name ?? "—",
Disciplines = direction.Disciplines.Select(x => new DisciplineNameDto { ID = x.ID, Name = x.Name }).ToList(),
Groups = direction.Groups.Select(g => new GroupNameDto { ID = g.ID, Name = g.Name }).ToList()
});
}

View File

@@ -1,6 +1,8 @@
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;
@@ -20,7 +22,7 @@ namespace GradeBookServer.Application.Services
_baseRepository = baseRepository;
}
public async Task<DisciplineDetailDto?> GetDisciplineByIdAsync(int id)
public async Task<DisciplineReadDto?> GetDisciplineByIdAsync(int id)
{
var discipline = await _baseRepository.GetByIdWithIncludeAsync(
d => d.ID == id,
@@ -30,32 +32,30 @@ namespace GradeBookServer.Application.Services
if (discipline == null)
return null;
return new DisciplineDetailDto
return new DisciplineReadDto
{
ID = discipline.ID,
Name = discipline.Name,
Hours = discipline.TotalHours,
Directions = discipline.Directions.Select(d => new DirectionTableDto
Directions = discipline.Directions.Select(d => new DirectionNameDto
{
ID = d.ID,
Name = d.Name,
FacultyName = d.Faculty?.Name ?? "—"
Name = d.Name
}).ToList()
};
}
public async Task<IEnumerable<DisciplineTableDto>?> GetAllDisciplinesAsync()
public async Task<IEnumerable<DisciplineNameDto>?> GetAllDisciplinesAsync()
{
var disciplines = await _baseRepository.GetAllAsync();
if (disciplines == null)
return null;
return disciplines.Select(d => new DisciplineTableDto
return disciplines.Select(d => new DisciplineNameDto
{
ID = d.ID,
Name = d.Name,
Hours = d.TotalHours
Name = d.Name
});
}

View File

@@ -36,7 +36,7 @@ namespace GradeBookServer.Application.Services
ID = faculty.ID,
Name = faculty.Name,
Directions = faculty.Directions.Select(
d => new DirectionTableDto { ID = d.ID, Name = d.Name, FacultyName = null}
d => new DirectionNameDto { ID = d.ID, Name = d.Name}
).ToList()
};
}
@@ -54,7 +54,7 @@ namespace GradeBookServer.Application.Services
{
ID = f.ID,
Name = f.Name,
Directions = f.Directions.Select(d => new DirectionTableDto { ID = d.ID, Name = d.Name, FacultyName = null }
Directions = f.Directions.Select(d => new DirectionNameDto { ID = d.ID, Name = d.Name }
).ToList()
});
}

View File

@@ -20,7 +20,7 @@ namespace GradeBookServer.Application.Services
_baseRepository = baseRepository;
}
public async Task<GroupDetailDto?> GetGroupByIdAsync(int id)
public async Task<GroupReadDto?> GetGroupByIdAsync(int id)
{
var group = await _baseRepository.GetByIdWithIncludeAsync(
g => g.ID == id,
@@ -30,7 +30,7 @@ namespace GradeBookServer.Application.Services
if (group == null)
return null;
return new GroupDetailDto
return new GroupReadDto
{
ID = group.ID,
Name = group.Name,
@@ -45,14 +45,37 @@ namespace GradeBookServer.Application.Services
};
}
public async Task<IEnumerable<GroupTableDto>?> GetAllGroupsAsync()
public async Task<IEnumerable<GroupReadDto>> GetGroupsByDirectionIdAsync(int directionId)
{
var groups = await _baseRepository
.WhereAsync(
g => g.DirectionID == directionId,
new IncludeSpecification<Group>(g => g.Students!, g => g.Direction!)
);
return groups.Select(group => new GroupReadDto
{
ID = group.ID,
Name = group.Name,
DirectionName = group.Direction?.Name ?? "—",
Students = group.Students.Select(s => new StudentReadDto
{
ID = s.ID,
FullName = s.FullName,
Age = s.Age,
GroupName = null
}).ToList()
});
}
public async Task<IEnumerable<GroupNameDto>?> GetAllGroupsAsync()
{
var groups = await _baseRepository.GetAllAsync();
if (groups == null)
return null;
return groups.Select(g => new GroupTableDto
return groups.Select(g => new GroupNameDto
{
ID = g.ID,
Name = g.Name

View File

@@ -5,6 +5,7 @@ using GradeBookServer.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
@@ -56,6 +57,23 @@ namespace GradeBookServer.Application.Services
});
}
public async Task<IEnumerable<StudentReadDto>> GetStudentsByGroupIdAsync(int groupId)
{
var students = await _baseRepository
.WhereAsync(
s => s.GroupID == groupId,
new IncludeSpecification<Student>(s => s.Group!)
);
return students.Select(s => new StudentReadDto
{
ID = s.ID,
FullName = s.FullName,
Age = s.Age,
GroupName = s.Group?.Name
});
}
public async Task AddStudentAsync(StudentCreateDto studentDto)
{
var student = new Student

View File

@@ -46,6 +46,19 @@ namespace GradeBookServer.Infrastructure.Repositories
return await query.ToListAsync();
}
public async Task<IEnumerable<TEntity>> WhereAsync(Expression<Func<TEntity, bool>> predicate, IncludeSpecification<TEntity>? includeSpec = null)
{
IQueryable<TEntity> query = _dbSet.Where(predicate);
if (includeSpec is not null)
{
foreach (var include in includeSpec.Includes)
query = query.Include(include);
}
return await query.ToListAsync();
}
public virtual async Task UpdateAsync(TEntity entity)
{
_dbSet.Update(entity);

View File

@@ -1,4 +1,5 @@
using GradeBookServer.Application.DTOs.Direction;
using GradeBookServer.Application.DTOs.Group;
using GradeBookServer.Application.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@@ -18,7 +19,7 @@ namespace GradeBookServer.WebAPI.Controllers
[HttpGet]
[Authorize(Roles = "Employee")]
public async Task<ActionResult<IEnumerable<DirectionTableDto>>> GetAll()
public async Task<ActionResult<IEnumerable<DirectionNameDto>>> GetAll()
{
var directions = await _directionService.GetAllDirectionsAsync();
return directions == null ? NotFound() : Ok(directions);
@@ -26,12 +27,20 @@ namespace GradeBookServer.WebAPI.Controllers
[HttpGet("{id}")]
[Authorize(Roles = "Employee")]
public async Task<ActionResult<DirectionDetailDto>> GetById(int id)
public async Task<ActionResult<DirectionReadDto>> GetById(int id)
{
var direction = await _directionService.GetDirectionByIdAsync(id);
return direction == null ? NotFound() : Ok(direction);
}
[HttpGet("by-faculty/{facultyId}")]
[Authorize(Roles = "Employee")]
public async Task<ActionResult<IEnumerable<DirectionReadDto>>> GetDirectionsByFacultyId(int facultyId)
{
var directions = await _directionService.GetDirectionsByFacultyIdAsync(facultyId);
return Ok(directions);
}
[HttpPost]
[Authorize(Roles = "Employee")]
public async Task<IActionResult> Create(DirectionCreateDto dto)

View File

@@ -18,7 +18,7 @@ namespace GradeBookServer.WebAPI.Controllers
[HttpGet]
[Authorize(Roles = "Employee")]
public async Task<ActionResult<IEnumerable<DisciplineTableDto>>> GetAllDisciplines()
public async Task<ActionResult<IEnumerable<DisciplineNameDto>>> GetAllDisciplines()
{
var disciplines = await _disciplineService.GetAllDisciplinesAsync();
return Ok(disciplines);
@@ -26,7 +26,7 @@ namespace GradeBookServer.WebAPI.Controllers
[HttpGet("{id}")]
[Authorize(Roles = "Employee")]
public async Task<ActionResult<DisciplineDetailDto>> GetDisciplineById(int id)
public async Task<ActionResult<DisciplineReadDto>> GetDisciplineById(int id)
{
var discipline = await _disciplineService.GetDisciplineByIdAsync(id);
if (discipline == null)

View File

@@ -1,4 +1,5 @@
using GradeBookServer.Application.DTOs.Group;
using GradeBookServer.Application.DTOs.Student;
using GradeBookServer.Application.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
@@ -18,7 +19,7 @@ namespace GradeBookServer.WebAPI.Controllers
[HttpGet]
[Authorize(Roles = "Employee")]
public async Task<ActionResult<IEnumerable<GroupTableDto>>> GetAll()
public async Task<ActionResult<IEnumerable<GroupNameDto>>> GetAll()
{
var groups = await _groupService.GetAllGroupsAsync();
return groups == null ? NotFound() : Ok(groups);
@@ -26,12 +27,20 @@ namespace GradeBookServer.WebAPI.Controllers
[HttpGet("{id}")]
[Authorize(Roles = "Employee")]
public async Task<ActionResult<GroupDetailDto>> GetById(int id)
public async Task<ActionResult<GroupReadDto>> GetById(int id)
{
var group = await _groupService.GetGroupByIdAsync(id);
return group == null ? NotFound() : Ok(group);
}
[HttpGet("by-direction/{directionId}")]
[Authorize(Roles = "Employee")]
public async Task<ActionResult<IEnumerable<GroupReadDto>>> GetGroupsByDirectionId(int directionId)
{
var groups = await _groupService.GetGroupsByDirectionIdAsync(directionId);
return Ok(groups);
}
[HttpPost]
[Authorize(Roles = "Employee")]
public async Task<IActionResult> Create(GroupCreateDto dto)

View File

@@ -28,6 +28,14 @@ namespace WebAPI.Controllers
return Ok(students);
}
[HttpGet("by-group/{groupId}")]
[Authorize(Roles = "Employee")]
public async Task<ActionResult<IEnumerable<StudentReadDto>>> GetStudentsByGroupId(int groupId)
{
var students = await _studentService.GetStudentsByGroupIdAsync(groupId);
return Ok(students);
}
[HttpGet("{id}")]
[Authorize(Roles = "Employee")]
public async Task<ActionResult<StudentReadDto>> GetStudentById(int id)
@@ -45,7 +53,7 @@ namespace WebAPI.Controllers
public async Task<ActionResult> AddStudent([FromBody] StudentCreateDto studentDto)
{
await _studentService.AddStudentAsync(studentDto);
return CreatedAtAction(nameof(GetStudentById), studentDto);
return Ok();
}
[HttpPut("{id}")]

View File

@@ -67,7 +67,6 @@ namespace GradeBookServer.WebAPI.Controllers
return Ok(token);
}
[Authorize]
[HttpPost("cancel-session")]
public IActionResult CancelSession([FromBody] string sessionId)
{
@@ -84,12 +83,12 @@ namespace GradeBookServer.WebAPI.Controllers
return user == null ? NotFound() : Ok(user);
}
[HttpGet("teachers")]
[HttpGet("professors")]
[Authorize(Roles = "Employee")]
public async Task<IActionResult> GetTeachers()
{
var teachers = await _service.GetTeachersAsync();
return Ok(teachers);
var professors = await _service.GetTeachersAsync();
return Ok(professors);
}
}
}

View File

@@ -14,7 +14,7 @@
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://192.168.1.64:5142",
"applicationUrl": "http://localhost:5142",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
@@ -24,7 +24,7 @@
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7270;http://192.168.1.64:5142",
"applicationUrl": "https://192.168.1.67:7270;http://localhost:5142",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}