CRUD операции для факультета, направления, дисциплины, студента, группы

This commit is contained in:
2025-05-27 20:56:58 +04:00
parent 94905f8984
commit 4320469e88
37 changed files with 882 additions and 82 deletions

View File

@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Direction
{
public class DirectionCreateDto
{
public required string Name { get; set; }
public int FacultyID { get; set; }
public List<int> DisciplinIds { get; set; } = new List<int>();
public List<int> GroupIds { get; set; } = new List<int>();
}
}

View File

@@ -0,0 +1,19 @@
using GradeBookServer.Application.DTOs.Discipline;
using GradeBookServer.Application.DTOs.Group;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Direction
{
public class DirectionDetailDto
{
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();
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Direction
{
public class DirectionTableDto
{
public int ID { get; set; }
public required string Name { get; set; }
public string? FacultyName { get; set; }
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Discipline
{
public class DisciplineCreateDto
{
public required string Name { get; set; }
public int Hours { get; set; }
public List<int> DirectionIds { get; set; } = new List<int>();
}
}

View File

@@ -0,0 +1,17 @@
using GradeBookServer.Application.DTOs.Direction;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Discipline
{
public class DisciplineDetailDto
{
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>();
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Discipline
{
public class DisciplineTableDto
{
public int ID { get; set; }
public required string Name { get; set; }
public int Hours { get; set; }
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Faculty
{
public class FacultyCreateDto
{
public required string Name { get; set; }
public List<int> DirectionIds { get; set; } = new List<int>();
}
}

View File

@@ -0,0 +1,16 @@
using GradeBookServer.Application.DTOs.Direction;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Faculty
{
public class FacultyReadDto
{
public int ID { get; set; }
public required string Name { get; set; }
public List<DirectionTableDto> Directions { get; set; } = new List<DirectionTableDto>();
}
}

View File

@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Group
{
public class GroupCreateDto
{
public required string Name { get; set; }
public int DirectionID { get; set; }
public List<int> StudentIds { get; set; } = new();
}
}

View File

@@ -0,0 +1,17 @@
using GradeBookServer.Application.DTOs.Student;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Group
{
public class GroupDetailDto
{
public int ID { get; set; }
public required string Name { get; set; }
public required string DirectionName { get; set; }
public List<StudentReadDto> Students { get; set; } = new();
}
}

View File

@@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Group
{
public class GroupTableDto
{
public int ID { get; set; }
public required string Name { get; set; }
}
}

View File

@@ -1,21 +0,0 @@
using GradeBookServer.Domain.Enums;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Student
{
public class GradeInStudentDto
{
public int ID { get; set; }
public GradeValue GradeValue { get; set; }
public int VedomostID { get; set; }
public string DisciplineName { get; set; } = string.Empty;
public int Semester { get; set; }
public DateOnly AcademicYear { get; set; }
}
}

View File

@@ -12,5 +12,4 @@ namespace GradeBookServer.Application.DTOs.Student
public int Age { get; set; }
public int GroupID { get; set; }
}
}

View File

@@ -11,11 +11,7 @@ namespace GradeBookServer.Application.DTOs.Student
public int ID { get; set; }
public required string FullName { get; set; }
public int Age { get; set; }
public int GroupID { get; set; }
public string? GroupName { get; set; }
public List<GradeInStudentDto> Grades { get; set; } = new();
}
}

View File

@@ -1,15 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GradeBookServer.Application.DTOs.Student
{
public class StudentTableDto
{
public int ID { get; set; }
public required string FullName { get; set; }
public int Age { get; set; }
}
}

View File

@@ -1,5 +1,6 @@
using GradeBookServer.Application.Common.Specifications;
using GradeBookServer.Domain.Entities;
using GradeBookServer.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -9,13 +10,14 @@ using System.Threading.Tasks;
namespace GradeBookServer.Application.Interfaces
{
public interface IBaseRepository<TEntity> where TEntity : class
public interface IBaseRepository<TEntity> where TEntity : class, IHasId
{
Task<TEntity?> GetByIdAsync(int id);
Task<IEnumerable<TEntity>?> GetAllAsync();
Task<IEnumerable<TEntity>?> GetAllAsync(IncludeSpecification<TEntity>? includeSpec = null);
Task AddAsync(TEntity entity);
Task UpdateAsync(TEntity entity);
Task DeleteAsync(int id);
Task<IEnumerable<T>> GetManyByIdsAsync<T>(IEnumerable<int> ids) where T : class, IHasId;
Task<TEntity?> GetByIdWithIncludeAsync
(Expression<Func<TEntity, bool>> idSelector,
IncludeSpecification<TEntity>? includeSpec = null);

View File

@@ -0,0 +1,114 @@
using GradeBookServer.Application.Common.Specifications;
using GradeBookServer.Application.DTOs.Direction;
using GradeBookServer.Application.DTOs.Discipline;
using GradeBookServer.Application.DTOs.Group;
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 DirectionService
{
private readonly IBaseRepository<Direction> _baseRepository;
public DirectionService(IBaseRepository<Direction> baseRepository)
{
_baseRepository = baseRepository;
}
public async Task<DirectionDetailDto?> GetDirectionByIdAsync(int id)
{
var direction = await _baseRepository.GetByIdWithIncludeAsync(
d => d.ID == id,
new IncludeSpecification<Direction>(
d => d.Faculty!,
d => d.Disciplines,
d => d.Groups
));
if (direction == null)
return null;
return new DirectionDetailDto
{
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()
};
}
public async Task<IEnumerable<DirectionTableDto>?> GetAllDirectionsAsync()
{
var directions = await _baseRepository.GetAllAsync(
new IncludeSpecification<Direction>(d => d.Faculty!)
);
if (directions == null)
return null;
return directions.Select(d => new DirectionTableDto
{
ID = d.ID,
Name = d.Name,
FacultyName = d.Faculty?.Name ?? "—"
});
}
public async Task AddDirectionAsync(DirectionCreateDto dto)
{
var disciplins = await _baseRepository.GetManyByIdsAsync<Discipline>(dto.DisciplinIds);
var groups = await _baseRepository.GetManyByIdsAsync<Group>(dto.GroupIds);
var direction = new Direction
{
Name = dto.Name,
FacultyID = dto.FacultyID,
Disciplines = disciplins.ToList(),
Groups = groups.ToList()
};
await _baseRepository.AddAsync(direction);
}
public async Task UpdateDirectionAsync(int id, DirectionCreateDto dto)
{
var direction = await _baseRepository.GetByIdWithIncludeAsync(
d => d.ID == id,
new IncludeSpecification<Direction>(
d => d.Disciplines!,
d => d.Groups!
)
);
if (direction == null) return;
var disciplines = await _baseRepository.GetManyByIdsAsync<Discipline>(dto.DisciplinIds);
var groups = await _baseRepository.GetManyByIdsAsync<Group>(dto.GroupIds);
direction.Name = dto.Name;
direction.FacultyID = dto.FacultyID;
direction.Disciplines.Clear();
foreach (var d in disciplines)
direction.Disciplines.Add(d);
direction.Groups.Clear();
foreach (var g in groups)
direction.Groups.Add(g);
await _baseRepository.UpdateAsync(direction);
}
public async Task DeleteDirectionAsync(int id)
{
await _baseRepository.DeleteAsync(id);
}
}
}

View File

@@ -0,0 +1,95 @@
using GradeBookServer.Application.Common.Specifications;
using GradeBookServer.Application.DTOs.Direction;
using GradeBookServer.Application.DTOs.Discipline;
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<DisciplineDetailDto?> 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 DisciplineDetailDto
{
ID = discipline.ID,
Name = discipline.Name,
Hours = discipline.TotalHours,
Directions = discipline.Directions.Select(d => new DirectionTableDto
{
ID = d.ID,
Name = d.Name,
FacultyName = d.Faculty?.Name ?? "—"
}).ToList()
};
}
public async Task<IEnumerable<DisciplineTableDto>?> GetAllDisciplinesAsync()
{
var disciplines = await _baseRepository.GetAllAsync();
if (disciplines == null)
return null;
return disciplines.Select(d => new DisciplineTableDto
{
ID = d.ID,
Name = d.Name,
Hours = d.TotalHours
});
}
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);
}
}
}

View File

@@ -0,0 +1,107 @@
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 DirectionTableDto { ID = d.ID, Name = d.Name, FacultyName = null}
).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 DirectionTableDto { ID = d.ID, Name = d.Name, FacultyName = null }
).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;
}
}
}

View File

@@ -0,0 +1,98 @@
using GradeBookServer.Application.Common.Specifications;
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 GroupService
{
private readonly IBaseRepository<Group> _baseRepository;
public GroupService(IBaseRepository<Group> baseRepository)
{
_baseRepository = baseRepository;
}
public async Task<GroupDetailDto?> GetGroupByIdAsync(int id)
{
var group = await _baseRepository.GetByIdWithIncludeAsync(
g => g.ID == id,
new IncludeSpecification<Group>(g => g.Students!, g => g.Direction!)
);
if (group == null)
return null;
return new GroupDetailDto
{
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<GroupTableDto>?> GetAllGroupsAsync()
{
var groups = await _baseRepository.GetAllAsync();
if (groups == null)
return null;
return groups.Select(g => new GroupTableDto
{
ID = g.ID,
Name = g.Name
});
}
public async Task AddGroupAsync(GroupCreateDto dto)
{
var students = await _baseRepository.GetManyByIdsAsync<Student>(dto.StudentIds);
var group = new Group
{
Name = dto.Name,
DirectionID = dto.DirectionID,
Students = students.ToList()
};
await _baseRepository.AddAsync(group);
}
public async Task UpdateGroupAsync(int id, GroupCreateDto dto)
{
var group = await _baseRepository.GetByIdAsync(id);
if (group == null) return;
group.Name = dto.Name;
group.DirectionID = dto.DirectionID;
var students = await _baseRepository.GetManyByIdsAsync<Student>(dto.StudentIds);
group.Students.Clear();
foreach (var s in students)
group.Students.Add(s);
await _baseRepository.UpdateAsync(group);
}
public async Task DeleteGroupAsync(int id)
{
await _baseRepository.DeleteAsync(id);
}
}
}

View File

@@ -23,12 +23,8 @@ namespace GradeBookServer.Application.Services
{
var student = await _baseRepository.GetByIdWithIncludeAsync(
s => s.ID == id,
new IncludeSpecification<Student>(
s => s.Group!,
s => s.Grades,
s => s.Grades.Select(g => g.Vedomost),
s => s.Grades.Select(g => g.Vedomost!.Discipline)
));
new IncludeSpecification<Student>(s => s.Group!)
);
if (student == null)
return null;
@@ -39,29 +35,24 @@ namespace GradeBookServer.Application.Services
FullName = student.FullName,
Age = student.Age,
GroupName = student.Group?.Name ?? "—",
Grades = student.Grades.Select(g => new GradeInStudentDto
{
ID = g.ID,
GradeValue = g.GradeValue,
DisciplineName = g.Vedomost?.Discipline?.Name ?? "Не указано",
Semester = g.Vedomost?.Semester ?? 0,
AcademicYear = g.Vedomost?.AcademicYear ?? default
}).ToList()
};
}
public async Task<IEnumerable<StudentTableDto>?> GetAllStudentsAsync()
public async Task<IEnumerable<StudentReadDto>?> GetAllStudentsAsync()
{
var students = await _baseRepository.GetAllAsync();
var students = await _baseRepository.GetAllAsync(
new IncludeSpecification<Student>(s => s.Group!)
);
if (students == null)
return null;
return students.Select(s => new StudentTableDto
return students.Select(s => new StudentReadDto
{
ID = s.ID,
FullName = s.FullName,
Age = s.Age
Age = s.Age,
GroupName = s.Group?.Name ?? "—",
});
}

View File

@@ -1,4 +1,5 @@
using System;
using GradeBookServer.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -7,7 +8,7 @@ using System.Threading.Tasks;
namespace GradeBookServer.Domain.Entities
{
public class Direction
public class Direction : IHasId
{
public int ID { get; set; }
public required string Name { get; set; }

View File

@@ -1,4 +1,5 @@
using System;
using GradeBookServer.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -6,7 +7,7 @@ using System.Threading.Tasks;
namespace GradeBookServer.Domain.Entities
{
public class Discipline
public class Discipline : IHasId
{
public int ID { get; set; }
public required string Name { get; set; }
@@ -15,5 +16,4 @@ namespace GradeBookServer.Domain.Entities
public ICollection<Direction> Directions { get; set; } = new List<Direction>();
public ICollection<Vedomost> Vedomosti { get; set; } = new List<Vedomost>();
}
}

View File

@@ -1,4 +1,5 @@
using System;
using GradeBookServer.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -6,7 +7,7 @@ using System.Threading.Tasks;
namespace GradeBookServer.Domain.Entities
{
public class Faculty
public class Faculty : IHasId
{
public int ID { get; set; }
public required string Name { get; set; }

View File

@@ -1,4 +1,5 @@
using GradeBookServer.Domain.Enums;
using GradeBookServer.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -7,7 +8,7 @@ using System.Threading.Tasks;
namespace GradeBookServer.Domain.Entities
{
public class Grade
public class Grade : IHasId
{
public int ID { get; set; }

View File

@@ -1,4 +1,5 @@
using System;
using GradeBookServer.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -6,7 +7,7 @@ using System.Threading.Tasks;
namespace GradeBookServer.Domain.Entities
{
public class Group
public class Group : IHasId
{
public int ID { get; set; }
public required string Name { get; set; }

View File

@@ -1,4 +1,5 @@
using System;
using GradeBookServer.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
@@ -6,7 +7,7 @@ using System.Threading.Tasks;
namespace GradeBookServer.Domain.Entities
{
public class Student
public class Student : IHasId
{
public int ID { get; set; }
public required string FullName { get; set; }
@@ -17,5 +18,4 @@ namespace GradeBookServer.Domain.Entities
public ICollection<Grade> Grades { get; set; } = new List<Grade>();
}
}

View File

@@ -1,4 +1,5 @@
using GradeBookServer.Domain.Enums;
using GradeBookServer.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -7,9 +8,9 @@ using System.Threading.Tasks;
namespace GradeBookServer.Domain.Entities
{
public class User
public class User : IHasId
{
public int Id { get; set; }
public int ID { get; set; }
public required string Name { get; set; }
public required string Email { get; set; }
public string? PhoneNumber { get; set; }
@@ -17,6 +18,5 @@ namespace GradeBookServer.Domain.Entities
public UserRole Role { get; set; }
public ICollection<Vedomost> Vedomosti { get; set; } = new List<Vedomost>();
}
}

View File

@@ -1,4 +1,5 @@
using GradeBookServer.Domain.Enums;
using GradeBookServer.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -7,7 +8,7 @@ using System.Threading.Tasks;
namespace GradeBookServer.Domain.Entities
{
public class Vedomost
public class Vedomost : IHasId
{
public int ID { get; set; }
public ExamType ExamType { get; set; }

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GradeBookServer.Domain.Interface
{
public interface IHasId
{
int ID { get; }
}
}

View File

@@ -1,6 +1,7 @@
using GradeBookServer.Application.Common.Specifications;
using GradeBookServer.Application.Interfaces;
using GradeBookServer.Domain.Entities;
using GradeBookServer.Domain.Interface;
using GradeBookServer.Infrastructure.Specifications;
using Microsoft.EntityFrameworkCore;
using System;
@@ -12,7 +13,7 @@ using System.Threading.Tasks;
namespace GradeBookServer.Infrastructure.Repositories
{
public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class
public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class, IHasId
{
protected readonly DbContext _context;
protected readonly DbSet<TEntity> _dbSet;
@@ -32,8 +33,18 @@ namespace GradeBookServer.Infrastructure.Repositories
await _context.SaveChangesAsync();
}
public virtual async Task<IEnumerable<TEntity>?> GetAllAsync()
=> await _dbSet.ToListAsync();
public async Task<IEnumerable<TEntity>?> GetAllAsync(IncludeSpecification<TEntity>? includeSpec = null)
{
IQueryable<TEntity> query = _context.Set<TEntity>();
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)
{
@@ -52,6 +63,14 @@ namespace GradeBookServer.Infrastructure.Repositories
}
}
public async Task<IEnumerable<T>> GetManyByIdsAsync<T>(IEnumerable<int> ids) where T : class, IHasId
{
var dbSet = _context.Set<T>();
return await dbSet.Where(e => ids.Contains(e.ID)).ToListAsync();
}
public async Task<TEntity?> GetByIdWithIncludeAsync
(Expression<Func<TEntity, bool>> idSelector,
IncludeSpecification<TEntity>? includeSpec = null)

View File

@@ -0,0 +1,53 @@
using GradeBookServer.Application.DTOs.Direction;
using GradeBookServer.Application.Services;
using Microsoft.AspNetCore.Mvc;
namespace GradeBookServer.WebAPI.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class DirectionsController : ControllerBase
{
private readonly DirectionService _directionService;
public DirectionsController(DirectionService directionService)
{
_directionService = directionService;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<DirectionTableDto>>> GetAll()
{
var directions = await _directionService.GetAllDirectionsAsync();
return directions == null ? NotFound() : Ok(directions);
}
[HttpGet("{id}")]
public async Task<ActionResult<DirectionDetailDto>> GetById(int id)
{
var direction = await _directionService.GetDirectionByIdAsync(id);
return direction == null ? NotFound() : Ok(direction);
}
[HttpPost]
public async Task<IActionResult> Create(DirectionCreateDto dto)
{
await _directionService.AddDirectionAsync(dto);
return Ok();
}
[HttpPut("{id}")]
public async Task<IActionResult> Update(int id, DirectionCreateDto dto)
{
await _directionService.UpdateDirectionAsync(id, dto);
return Ok();
}
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
await _directionService.DeleteDirectionAsync(id);
return Ok();
}
}
}

View File

@@ -0,0 +1,56 @@
using GradeBookServer.Application.DTOs.Discipline;
using GradeBookServer.Application.Services;
using Microsoft.AspNetCore.Mvc;
namespace GradeBookServer.WebAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class DisciplineController : ControllerBase
{
private readonly DisciplineService _disciplineService;
public DisciplineController(DisciplineService disciplineService)
{
_disciplineService = disciplineService;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<DisciplineTableDto>>> GetAllDisciplines()
{
var disciplines = await _disciplineService.GetAllDisciplinesAsync();
return Ok(disciplines);
}
[HttpGet("{id}")]
public async Task<ActionResult<DisciplineDetailDto>> GetDisciplineById(int id)
{
var discipline = await _disciplineService.GetDisciplineByIdAsync(id);
if (discipline == null)
return NotFound();
return Ok(discipline);
}
[HttpPost]
public async Task<ActionResult> AddDiscipline([FromBody] DisciplineCreateDto disciplineDto)
{
await _disciplineService.AddDisciplineAsync(disciplineDto);
return Ok();
}
[HttpPut("{id}")]
public async Task<ActionResult> UpdateDiscipline(int id, [FromBody] DisciplineCreateDto disciplineDto)
{
await _disciplineService.UpdateDisciplineAsync(id, disciplineDto);
return NoContent();
}
[HttpDelete("{id}")]
public async Task<ActionResult> DeleteDiscipline(int id)
{
await _disciplineService.DeleteDisciplineAsync(id);
return NoContent();
}
}
}

View File

@@ -0,0 +1,57 @@
using GradeBookServer.Application.DTOs.Faculty;
using GradeBookServer.Application.Services;
using Microsoft.AspNetCore.Mvc;
namespace GradeBookServer.WebAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class FacultyController : ControllerBase
{
private readonly FacultyService _facultyService;
public FacultyController(FacultyService facultyService)
{
_facultyService = facultyService;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<FacultyReadDto>>> GetAllFaculties()
{
var faculties = await _facultyService.GetAllFacultiesAsync();
return Ok(faculties);
}
[HttpGet("{id}")]
public async Task<ActionResult<FacultyReadDto>> GetFacultyById(int id)
{
var faculty = await _facultyService.GetFacultyByIdAsync(id);
if (faculty == null)
{
return NotFound();
}
return Ok(faculty);
}
[HttpPost]
public async Task<ActionResult> AddFaculty([FromBody] FacultyCreateDto facultyDto)
{
await _facultyService.AddFacultyAsync(facultyDto);
return CreatedAtAction(nameof(GetFacultyById), new { id = facultyDto.Name }, facultyDto);
}
[HttpPut("{id}")]
public async Task<ActionResult> UpdateFaculty(int id, [FromBody] FacultyCreateDto facultyDto)
{
await _facultyService.UpdateFacultyAsync(id, facultyDto);
return NoContent();
}
[HttpDelete("{id}")]
public async Task<ActionResult> DeleteFaculty(int id)
{
await _facultyService.DeleteFacultyAsync(id);
return NoContent();
}
}
}

View File

@@ -0,0 +1,53 @@
using GradeBookServer.Application.DTOs.Group;
using GradeBookServer.Application.Services;
using Microsoft.AspNetCore.Mvc;
namespace GradeBookServer.WebAPI.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class GroupsController : ControllerBase
{
private readonly GroupService _groupService;
public GroupsController(GroupService groupService)
{
_groupService = groupService;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<GroupTableDto>>> GetAll()
{
var groups = await _groupService.GetAllGroupsAsync();
return groups == null ? NotFound() : Ok(groups);
}
[HttpGet("{id}")]
public async Task<ActionResult<GroupDetailDto>> GetById(int id)
{
var group = await _groupService.GetGroupByIdAsync(id);
return group == null ? NotFound() : Ok(group);
}
[HttpPost]
public async Task<IActionResult> Create(GroupCreateDto dto)
{
await _groupService.AddGroupAsync(dto);
return Ok();
}
[HttpPut("{id}")]
public async Task<IActionResult> Update(int id, GroupCreateDto dto)
{
await _groupService.UpdateGroupAsync(id, dto);
return Ok();
}
[HttpDelete("{id}")]
public async Task<IActionResult> Delete(int id)
{
await _groupService.DeleteGroupAsync(id);
return Ok();
}
}
}

View File

@@ -20,7 +20,7 @@ namespace WebAPI.Controllers
}
[HttpGet]
public async Task<ActionResult<IEnumerable<StudentTableDto>>> GetAllStudents()
public async Task<ActionResult<IEnumerable<StudentReadDto>>> GetAllStudents()
{
var students = await _studentService.GetAllStudentsAsync();
return Ok(students);

View File

@@ -30,6 +30,11 @@ builder.Services.AddSwaggerGen(options =>
builder.Services.AddScoped<DbContext, ApplicationDbContext>();
builder.Services.AddScoped(typeof(IBaseRepository<>), typeof(BaseRepository<>));
builder.Services.AddScoped<DirectionService>();
builder.Services.AddScoped<DisciplineService>();
builder.Services.AddScoped<FacultyService>();
builder.Services.AddScoped<GroupService>();
builder.Services.AddScoped<StudentService>();
builder.Services.AddControllers();