ASP.NET API #1
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
.idea/
|
||||
.vs/
|
||||
.vscode/
|
||||
[Bb]in/
|
||||
[Oo]bj/
|
@ -1,4 +1,6 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using domain.UseCase;
|
||||
using domain.Request;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace Presence.API.Controllers
|
||||
{
|
||||
@ -6,5 +8,31 @@ namespace Presence.API.Controllers
|
||||
[Route("api/[controller]")]
|
||||
public class AdminController : ControllerBase
|
||||
{
|
||||
private readonly IAdminUseCase _adminUseCase;
|
||||
|
||||
public AdminController(IAdminUseCase adminUseCase)
|
||||
{
|
||||
_adminUseCase = adminUseCase;
|
||||
}
|
||||
|
||||
[HttpPost("/group/{groupId}/students")]
|
||||
public IActionResult AddStudentsToGroup(int groupId, [FromBody] AddStudentsToGroupRequest request)
|
||||
{
|
||||
if (request?.Students == null)
|
||||
return BadRequest();
|
||||
|
||||
_adminUseCase.AddStudentsToGroup(groupId, request);
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
[HttpPost("/group/{groupId}/subjects")]
|
||||
public IActionResult AddSubjectsToGroup(int groupId, [FromBody] AddSubjectsToGroupRequest request)
|
||||
{
|
||||
if (request?.Subjects == null)
|
||||
return BadRequest();
|
||||
|
||||
_adminUseCase.AddSubjectsToGroup(groupId, request);
|
||||
return NoContent();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
using domain.Request;
|
||||
using domain.Request;
|
||||
using domain.UseCase;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Presence.API.Response;
|
||||
@ -52,24 +52,28 @@ namespace Presence.API.Controllers
|
||||
if (addGroupRequest is null)
|
||||
return BadRequest(new ArgumentNullException());
|
||||
|
||||
bool isCreated = _groupService.AddGroup(addGroupRequest);
|
||||
if (isCreated) return CreatedAtAction(nameof(GetGroupByName), new { name = addGroupRequest.Name }, addGroupRequest );
|
||||
else return BadRequest();
|
||||
var groupWithStudentsRequest = new AddGroupWithStudentRequest
|
||||
{
|
||||
AddGroupRequest = new AddGroupRequest { Name = addGroupRequest.Name },
|
||||
AddStudentRequests = addGroupRequest.Students
|
||||
};
|
||||
|
||||
_groupService.AddGroupWithStudents(groupWithStudentsRequest);
|
||||
|
||||
var createdGroup = _groupService.GetGroupsWithStudents()
|
||||
.FirstOrDefault(g => g.Name == addGroupRequest.Name);
|
||||
|
||||
return CreatedAtAction(nameof(GetGroupById), new { id = createdGroup.Id }, createdGroup);
|
||||
}
|
||||
|
||||
[HttpGet("/group/{id}/subjects")]
|
||||
public ActionResult<GroupSubjectResponse> GetGroupSubject(int id)
|
||||
[HttpGet("/group/{id:int}")]
|
||||
public ActionResult<GroupResponse> GetGroupById(int id)
|
||||
{
|
||||
return Ok(_groupService.GetGroupSubject(id));
|
||||
}
|
||||
var group = _groupService.GetGroupsWithStudents().FirstOrDefault(g => g.Id == id);
|
||||
if (group == null)
|
||||
return NotFound();
|
||||
|
||||
[HttpGet("/group/{name}")]
|
||||
public ActionResult<GroupResponse> GetGroupByName(string name)
|
||||
{
|
||||
var result = _groupService
|
||||
.GetGroupsWithStudents()
|
||||
.Where(g => g.Name == name)
|
||||
.Select(group => new GroupResponse
|
||||
var response = new GroupResponse
|
||||
{
|
||||
Id = group.Id,
|
||||
Name = group.Name,
|
||||
@ -81,8 +85,14 @@ namespace Presence.API.Controllers
|
||||
FirstName = user.FirstName,
|
||||
Patronymic = user.Patronymic
|
||||
}).ToList()
|
||||
}).ToList();
|
||||
return result.Count > 0 ? Ok(result) : NotFound();
|
||||
};
|
||||
return Ok(response);
|
||||
}
|
||||
|
||||
[HttpGet("/group/{id}/subjects")]
|
||||
public ActionResult<GroupSubjectResponse> GetGroupSubject(int id)
|
||||
{
|
||||
return Ok(_groupService.GetGroupSubject(id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,8 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using domain.UseCase;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Presence.API.Response;
|
||||
using System;
|
||||
using System.Linq;
|
||||
|
||||
namespace Presence.API.Controllers
|
||||
{
|
||||
@ -6,5 +10,33 @@ namespace Presence.API.Controllers
|
||||
[Route("api/[controller]")]
|
||||
public class PresenceController : ControllerBase
|
||||
{
|
||||
private readonly IPresenceUseCase _presenceUseCase;
|
||||
|
||||
public PresenceController(IPresenceUseCase presenceUseCase)
|
||||
{
|
||||
_presenceUseCase = presenceUseCase;
|
||||
}
|
||||
|
||||
[HttpGet("/presence/{groupId}")]
|
||||
public ActionResult<PresenceResponse> GetPresence(
|
||||
int groupId,
|
||||
[FromQuery] int? subject,
|
||||
[FromQuery] DateTime? date,
|
||||
[FromQuery] int? student)
|
||||
{
|
||||
var presences = _presenceUseCase.GetPresence(groupId, subject, date, student)
|
||||
.Select(p => new PresenceResponse
|
||||
{
|
||||
Id = p.Id,
|
||||
StudentId = p.StudentId,
|
||||
StudentName = $"{p.Student.LastName} {p.Student.FirstName} {p.Student.Patronymic}",
|
||||
SubjectId = p.SubjectId,
|
||||
SubjectName = p.Subject.Name,
|
||||
Date = p.Date
|
||||
})
|
||||
.ToList();
|
||||
|
||||
return Ok(presences);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,9 +14,19 @@ namespace Presence.API.Extensions
|
||||
.AddDbContext<DatabaseContext>()
|
||||
.AddScoped<IGroupRepository, SQLGroupRepository>()
|
||||
.AddScoped<IGroupUseCase, GroupService>()
|
||||
.AddScoped<GroupController>()
|
||||
.AddScoped<PresenceController>()
|
||||
.AddScoped<GroupController>();
|
||||
|
||||
services
|
||||
.AddDbContext<DatabaseContext>()
|
||||
.AddScoped<IAdminRepository, SQLAdminRepository>()
|
||||
.AddScoped<IAdminUseCase, AdminService>()
|
||||
.AddScoped<AdminController>();
|
||||
|
||||
services
|
||||
.AddDbContext<DatabaseContext>()
|
||||
.AddScoped<IPresenceRepository, SQLPresenceRepository>()
|
||||
.AddScoped<IPresenceUseCase, PresenceService>()
|
||||
.AddScoped<PresenceController>();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +0,0 @@
|
||||
namespace Presence.API.Request
|
||||
{
|
||||
public class AddGroupRequest
|
||||
{
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
14
Presence.API/Response/PresenceResponse.cs
Normal file
14
Presence.API/Response/PresenceResponse.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace Presence.API.Response
|
||||
{
|
||||
public class PresenceResponse
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int StudentId { get; set; }
|
||||
public string StudentName { get; set; }
|
||||
public int SubjectId { get; set; }
|
||||
public string SubjectName { get; set; }
|
||||
public DateOnly Date { get; set; }
|
||||
}
|
||||
}
|
10
data/Repository/IAdminRepository.cs
Normal file
10
data/Repository/IAdminRepository.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace data.Repository
|
||||
{
|
||||
public interface IAdminRepository
|
||||
{
|
||||
void AddStudentsToGroup(int groupId, IEnumerable<int> studentIds);
|
||||
void AddSubjectsToGroup(int groupId, IEnumerable<int> subjectIds);
|
||||
}
|
||||
}
|
15
data/Repository/IPresenceRepository.cs
Normal file
15
data/Repository/IPresenceRepository.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using data.DAO;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace data.Repository
|
||||
{
|
||||
public interface IPresenceRepository
|
||||
{
|
||||
IEnumerable<Diary> GetPresence(
|
||||
int groupId,
|
||||
int? subjectId = null,
|
||||
DateTime? date = null,
|
||||
int? studentId = null);
|
||||
}
|
||||
}
|
58
data/Repository/SQLAdminRepository.cs
Normal file
58
data/Repository/SQLAdminRepository.cs
Normal file
@ -0,0 +1,58 @@
|
||||
using data.Repository;
|
||||
using data.DAO;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace data.Repository
|
||||
{
|
||||
public class SQLAdminRepository : IAdminRepository
|
||||
{
|
||||
private readonly DatabaseContext _context;
|
||||
|
||||
public SQLAdminRepository(DatabaseContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public void AddStudentsToGroup(int groupId, IEnumerable<int> studentIds)
|
||||
{
|
||||
var group = _context.Groups.Include(g => g.Students).FirstOrDefault(g => g.Id == groupId);
|
||||
if (group == null) return;
|
||||
|
||||
var studentsToAdd = _context.Students
|
||||
.Where(u => studentIds.Contains(u.Id))
|
||||
.ToList();
|
||||
|
||||
foreach (var student in studentsToAdd)
|
||||
{
|
||||
if (!group.Students.Any(u => u.Id == student.Id))
|
||||
{
|
||||
group.Students.Add(student);
|
||||
}
|
||||
}
|
||||
|
||||
_context.SaveChanges();
|
||||
}
|
||||
|
||||
public void AddSubjectsToGroup(int groupId, IEnumerable<int> subjectIds)
|
||||
{
|
||||
var group = _context.Groups.Include(g => g.StudentGroupsSubjects).ThenInclude(s => s.Subject).FirstOrDefault(g => g.Id == groupId);
|
||||
if (group == null) return;
|
||||
|
||||
var subjectsToAdd = _context.Subjects
|
||||
.Where(s => subjectIds.Contains(s.Id))
|
||||
.ToList();
|
||||
|
||||
foreach (var subject in subjectsToAdd)
|
||||
{
|
||||
if (!group.StudentGroupsSubjects.Any(s => s.Subject.Id == subject.Id))
|
||||
{
|
||||
group.StudentGroupsSubjects.Add(new StudentGroupSubject { Subject = subject });
|
||||
}
|
||||
}
|
||||
|
||||
_context.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
42
data/Repository/SQLPresenceRepository.cs
Normal file
42
data/Repository/SQLPresenceRepository.cs
Normal file
@ -0,0 +1,42 @@
|
||||
using data.DAO;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
namespace data.Repository
|
||||
{
|
||||
public class SQLPresenceRepository : IPresenceRepository
|
||||
{
|
||||
private readonly DatabaseContext _context;
|
||||
|
||||
public SQLPresenceRepository(DatabaseContext context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
|
||||
public IEnumerable<Diary> GetPresence(
|
||||
int groupId,
|
||||
int? subjectId = null,
|
||||
DateTime? date = null,
|
||||
int? studentId = null)
|
||||
{
|
||||
var query = _context.Diaries
|
||||
.Include(p => p.Student)
|
||||
.Include(p => p.StudentGroupSubject)
|
||||
.ThenInclude(p => p.Subject)
|
||||
.Where(p => p.Student.GroupId == groupId);
|
||||
|
||||
if (subjectId.HasValue)
|
||||
query = query.Where(p => p.StudentGroupSubject.Subject.Id == subjectId.Value);
|
||||
|
||||
if (date.HasValue)
|
||||
query = query.Where(p => p.Date == DateOnly.FromDateTime(date.Value));
|
||||
|
||||
if (studentId.HasValue)
|
||||
query = query.Where(p => p.Student.Id == studentId.Value);
|
||||
|
||||
return query.ToList();
|
||||
}
|
||||
}
|
||||
}
|
14
domain/Entity/PresenceEntity.cs
Normal file
14
domain/Entity/PresenceEntity.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using System;
|
||||
|
||||
namespace domain.Entity
|
||||
{
|
||||
public class PresenceEntity
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public int StudentId { get; set; }
|
||||
public UserEntity Student { get; set; }
|
||||
public int SubjectId { get; set; }
|
||||
public SubjectEntity Subject { get; set; }
|
||||
public DateOnly Date { get; set; }
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@ -9,5 +9,6 @@ namespace domain.Request
|
||||
public class AddGroupRequest
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public List<AddStudentRequest> Students { get; set; } = new List<AddStudentRequest>();
|
||||
}
|
||||
}
|
||||
|
9
domain/Request/AddStudentsToGroupRequest.cs
Normal file
9
domain/Request/AddStudentsToGroupRequest.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace domain.Request
|
||||
{
|
||||
public class AddStudentsToGroupRequest
|
||||
{
|
||||
public List<int> Students { get; set; } = new List<int>();
|
||||
}
|
||||
}
|
9
domain/Request/AddSubjectsToGroupRequest.cs
Normal file
9
domain/Request/AddSubjectsToGroupRequest.cs
Normal file
@ -0,0 +1,9 @@
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace domain.Request
|
||||
{
|
||||
public class AddSubjectsToGroupRequest
|
||||
{
|
||||
public List<int> Subjects { get; set; } = new List<int>();
|
||||
}
|
||||
}
|
26
domain/Service/AdminService.cs
Normal file
26
domain/Service/AdminService.cs
Normal file
@ -0,0 +1,26 @@
|
||||
using data.Repository;
|
||||
using domain.Request;
|
||||
using domain.UseCase;
|
||||
|
||||
namespace domain.Service
|
||||
{
|
||||
public class AdminService : IAdminUseCase
|
||||
{
|
||||
private readonly IAdminRepository _adminRepository;
|
||||
|
||||
public AdminService(IAdminRepository adminRepository)
|
||||
{
|
||||
_adminRepository = adminRepository;
|
||||
}
|
||||
|
||||
public void AddStudentsToGroup(int groupId, AddStudentsToGroupRequest request)
|
||||
{
|
||||
_adminRepository.AddStudentsToGroup(groupId, request.Students);
|
||||
}
|
||||
|
||||
public void AddSubjectsToGroup(int groupId, AddSubjectsToGroupRequest request)
|
||||
{
|
||||
_adminRepository.AddSubjectsToGroup(groupId, request.Subjects);
|
||||
}
|
||||
}
|
||||
}
|
46
domain/Service/PresenceService.cs
Normal file
46
domain/Service/PresenceService.cs
Normal file
@ -0,0 +1,46 @@
|
||||
using data.Repository;
|
||||
using domain.Entity;
|
||||
using domain.UseCase;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace domain.Service
|
||||
{
|
||||
public class PresenceService : IPresenceUseCase
|
||||
{
|
||||
private readonly IPresenceRepository _presenceRepository;
|
||||
|
||||
public PresenceService(IPresenceRepository presenceRepository)
|
||||
{
|
||||
_presenceRepository = presenceRepository;
|
||||
}
|
||||
|
||||
public IEnumerable<PresenceEntity> GetPresence(
|
||||
int groupId,
|
||||
int? subjectId = null,
|
||||
DateTime? date = null,
|
||||
int? studentId = null)
|
||||
{
|
||||
return _presenceRepository.GetPresence(groupId, subjectId, date, studentId).Select(p => new PresenceEntity
|
||||
{
|
||||
Id = p.Id,
|
||||
StudentId = p.Student.Id,
|
||||
Student = new UserEntity
|
||||
{
|
||||
Id = p.Student.Id,
|
||||
FirstName = p.Student.FirstName,
|
||||
LastName = p.Student.LastName,
|
||||
Patronymic = p.Student.Patronymic,
|
||||
Group = new GroupEntity()
|
||||
},
|
||||
SubjectId = p.StudentGroupSubject.Subject.Id,
|
||||
Subject = new SubjectEntity
|
||||
{
|
||||
Id = p.StudentGroupSubject.Subject.Id,
|
||||
Name = p.StudentGroupSubject.Subject.Name
|
||||
},
|
||||
Date = p.Date
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
10
domain/UseCase/IAdminUseCase.cs
Normal file
10
domain/UseCase/IAdminUseCase.cs
Normal file
@ -0,0 +1,10 @@
|
||||
using domain.Request;
|
||||
|
||||
namespace domain.UseCase
|
||||
{
|
||||
public interface IAdminUseCase
|
||||
{
|
||||
void AddStudentsToGroup(int groupId, AddStudentsToGroupRequest request);
|
||||
void AddSubjectsToGroup(int groupId, AddSubjectsToGroupRequest request);
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
using domain.Entity;
|
||||
using domain.Entity;
|
||||
using domain.Request;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
15
domain/UseCase/IPresenceUseCase.cs
Normal file
15
domain/UseCase/IPresenceUseCase.cs
Normal file
@ -0,0 +1,15 @@
|
||||
using domain.Entity;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace domain.UseCase
|
||||
{
|
||||
public interface IPresenceUseCase
|
||||
{
|
||||
IEnumerable<PresenceEntity> GetPresence(
|
||||
int groupId,
|
||||
int? subjectId = null,
|
||||
DateTime? date = null,
|
||||
int? studentId = null);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user