From c2353bd94ab57ba9db47aa134b2b60f485dfdba2 Mon Sep 17 00:00:00 2001 From: Zagrebin Date: Wed, 4 Dec 2024 15:21:15 +0300 Subject: [PATCH] Group subjects --- Presence.API/Controllers/AdminController.cs | 10 +++++++ Presence.API/Controllers/GroupController.cs | 26 ++++--------------- .../Controllers/PresenceController.cs | 10 +++++++ .../Extensions/ServiceCollectionExtension.cs | 4 ++- Presence.API/Response/GroupSubjectResponse.cs | 9 +++++++ Presence.API/Response/SubjectResponse.cs | 8 ++++++ data/DAO/Group.cs | 1 + data/DAO/Student.cs | 2 ++ data/DAO/StudentGroupSubject.cs | 2 ++ data/DAO/Subject.cs | 2 ++ data/DAO/Traffic.cs | 2 ++ data/Repository/SQLGroupRepository.cs | 6 ++++- domain/Entity/GroupSubjectEntity.cs | 15 +++++++++++ domain/Entity/SubjectEntity.cs | 14 ++++++++++ domain/Service/GroupService.cs | 16 ++++++++++++ domain/UseCase/IGroupUseCase.cs | 2 ++ 16 files changed, 106 insertions(+), 23 deletions(-) create mode 100644 Presence.API/Controllers/AdminController.cs create mode 100644 Presence.API/Controllers/PresenceController.cs create mode 100644 Presence.API/Response/GroupSubjectResponse.cs create mode 100644 Presence.API/Response/SubjectResponse.cs create mode 100644 domain/Entity/GroupSubjectEntity.cs create mode 100644 domain/Entity/SubjectEntity.cs diff --git a/Presence.API/Controllers/AdminController.cs b/Presence.API/Controllers/AdminController.cs new file mode 100644 index 0000000..8505728 --- /dev/null +++ b/Presence.API/Controllers/AdminController.cs @@ -0,0 +1,10 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Presence.API.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class AdminController : ControllerBase + { + } +} diff --git a/Presence.API/Controllers/GroupController.cs b/Presence.API/Controllers/GroupController.cs index 7426d52..2dd382d 100644 --- a/Presence.API/Controllers/GroupController.cs +++ b/Presence.API/Controllers/GroupController.cs @@ -56,29 +56,13 @@ namespace Presence.API.Controllers if (isCreated) return CreatedAtAction(nameof(GetGroupByName), new { name = addGroupRequest.Name }, addGroupRequest ); else return BadRequest(); } -/* - [HttpGet("/group/{id}")] - public ActionResult GetGroupById(int id) + + [HttpGet("/group/{id}/subjects")] + public ActionResult GetGroupSubject(int id) { - var result = _groupService - .GetGroupsWithStudents() - .Where(g => g.Id == id) - .Select(group => new GroupResponse - { - Id = group.Id, - Name = group.Name, - Users = group.Users?.Select( - user => new UserResponse - { - Id = user.Id, - LastName = user.LastName, - FirstName = user.FirstName, - Patronymic = user.Patronymic - }).ToList() - }).ToList(); - return result.Count > 0 ? Ok(result) : NotFound(); + return Ok(_groupService.GetGroupSubject(id)); } -*/ + [HttpGet("/group/{name}")] public ActionResult GetGroupByName(string name) { diff --git a/Presence.API/Controllers/PresenceController.cs b/Presence.API/Controllers/PresenceController.cs new file mode 100644 index 0000000..5dcd62a --- /dev/null +++ b/Presence.API/Controllers/PresenceController.cs @@ -0,0 +1,10 @@ +using Microsoft.AspNetCore.Mvc; + +namespace Presence.API.Controllers +{ + [ApiController] + [Route("api/[controller]")] + public class PresenceController : ControllerBase + { + } +} diff --git a/Presence.API/Extensions/ServiceCollectionExtension.cs b/Presence.API/Extensions/ServiceCollectionExtension.cs index 890ad9f..0198cd3 100644 --- a/Presence.API/Extensions/ServiceCollectionExtension.cs +++ b/Presence.API/Extensions/ServiceCollectionExtension.cs @@ -14,7 +14,9 @@ namespace Presence.API.Extensions .AddDbContext() .AddScoped() .AddScoped() - .AddScoped(); + .AddScoped() + .AddScoped() + .AddScoped(); } } } diff --git a/Presence.API/Response/GroupSubjectResponse.cs b/Presence.API/Response/GroupSubjectResponse.cs new file mode 100644 index 0000000..1ce580a --- /dev/null +++ b/Presence.API/Response/GroupSubjectResponse.cs @@ -0,0 +1,9 @@ +namespace Presence.API.Response +{ + public class GroupSubjectResponse + { + public string Name { get; set; } + + public IEnumerable Subjects { get; set; } + } +} diff --git a/Presence.API/Response/SubjectResponse.cs b/Presence.API/Response/SubjectResponse.cs new file mode 100644 index 0000000..bdd85fd --- /dev/null +++ b/Presence.API/Response/SubjectResponse.cs @@ -0,0 +1,8 @@ +namespace Presence.API.Response +{ + public class SubjectResponse + { + public int Id { get; set; } + public string Name { get; set; } + } +} diff --git a/data/DAO/Group.cs b/data/DAO/Group.cs index e9fcb81..c8b6ad1 100644 --- a/data/DAO/Group.cs +++ b/data/DAO/Group.cs @@ -15,5 +15,6 @@ namespace data.DAO public string Name { get; set; } public virtual ICollection Students { get; set; } = new List(); + public virtual ICollection StudentGroupsSubjects { get; set; } = new List(); } } diff --git a/data/DAO/Student.cs b/data/DAO/Student.cs index 3150df5..8251d10 100644 --- a/data/DAO/Student.cs +++ b/data/DAO/Student.cs @@ -20,6 +20,8 @@ namespace data.DAO public int GroupId { get; set; } + public virtual ICollection Diaries { get; set; } = new List(); + public virtual Group Group { get; set; } } } diff --git a/data/DAO/StudentGroupSubject.cs b/data/DAO/StudentGroupSubject.cs index 4908547..6d46bbe 100644 --- a/data/DAO/StudentGroupSubject.cs +++ b/data/DAO/StudentGroupSubject.cs @@ -15,5 +15,7 @@ namespace data.DAO public int SemesterStart { get; set; } public int SemesterEnd { get; set; } public virtual Subject Subject { get; set; } + + public virtual ICollection Diaries { get; set; } = new List(); } } diff --git a/data/DAO/Subject.cs b/data/DAO/Subject.cs index 2c5e547..cacba6f 100644 --- a/data/DAO/Subject.cs +++ b/data/DAO/Subject.cs @@ -13,5 +13,7 @@ namespace data.DAO [Key] public int Id { get; set; } public string Name { get; set; } + + public virtual ICollection StudentGroupsSubjects { get; set; } = new List(); } } diff --git a/data/DAO/Traffic.cs b/data/DAO/Traffic.cs index 4e37168..c365b85 100644 --- a/data/DAO/Traffic.cs +++ b/data/DAO/Traffic.cs @@ -12,5 +12,7 @@ namespace data.DAO [Key] public int Id { get; set; } public string Name { get; set; } + + public virtual ICollection Diaries { get; set; } = new List(); } } diff --git a/data/Repository/SQLGroupRepository.cs b/data/Repository/SQLGroupRepository.cs index 49fbc94..edfb99e 100644 --- a/data/Repository/SQLGroupRepository.cs +++ b/data/Repository/SQLGroupRepository.cs @@ -55,7 +55,11 @@ namespace data.Repository } public IEnumerable GetAllGroups() - => _dbContext.Groups.Include(g => g.Students).ToList(); + => _dbContext.Groups + .Include(g => g.Students) + .Include(g => g.StudentGroupsSubjects) + .ThenInclude(sgs => sgs.Subject) + .ToList(); public bool UpdateGroup(int id, string name) { diff --git a/domain/Entity/GroupSubjectEntity.cs b/domain/Entity/GroupSubjectEntity.cs new file mode 100644 index 0000000..8713a5c --- /dev/null +++ b/domain/Entity/GroupSubjectEntity.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace domain.Entity +{ + public class GroupSubjectEntity + { + public int Id { get; set; } + public string Name { get; set; } + public IEnumerable Subjects { get; set; } + } +} diff --git a/domain/Entity/SubjectEntity.cs b/domain/Entity/SubjectEntity.cs new file mode 100644 index 0000000..170f179 --- /dev/null +++ b/domain/Entity/SubjectEntity.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace domain.Entity +{ + public class SubjectEntity + { + public int Id { get; set; } + public string Name { get; set; } + } +} diff --git a/domain/Service/GroupService.cs b/domain/Service/GroupService.cs index fa3a308..98e85b2 100644 --- a/domain/Service/GroupService.cs +++ b/domain/Service/GroupService.cs @@ -62,5 +62,21 @@ namespace domain.Service public bool RemoveGroup(RemoveGroupRequest removeGroupRequest) => _groupRepository.DeleteGroup(removeGroupRequest.GroupId); + + public GroupSubjectEntity GetGroupSubject(int id) + { + return _groupRepository.GetAllGroups().Select( + group => new GroupSubjectEntity + { + Id = group.Id, + Name = group.Name, + Subjects = group.StudentGroupsSubjects.Select( + subject => new SubjectEntity + { + Id = subject.Subject.Id, + Name = subject.Subject.Name, + }) + }).Single(g => g.Id == id); + } } } diff --git a/domain/UseCase/IGroupUseCase.cs b/domain/UseCase/IGroupUseCase.cs index 7589a56..9d87f78 100644 --- a/domain/UseCase/IGroupUseCase.cs +++ b/domain/UseCase/IGroupUseCase.cs @@ -19,5 +19,7 @@ namespace domain.UseCase public void EditGroup(EditGroupRequest editGroupRequest); public IEnumerable GetGroupsWithStudents(); + + public GroupSubjectEntity GetGroupSubject(int id); } }