Compare commits
11 Commits
MainContra
...
Realizatio
| Author | SHA1 | Date | |
|---|---|---|---|
| 91951c9ef6 | |||
| e1f5c49c8a | |||
| 26cf1ae0c1 | |||
| b7038b508c | |||
| 9c86534470 | |||
| 655601f7c7 | |||
| 545cacbe9a | |||
| e9c6b7ff3a | |||
| 18af25be0b | |||
| adbf6eb004 | |||
| b9e8a3257f |
@@ -7,6 +7,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniversityAllExpelled_Model
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniversityAllExpelled_Tests", "UniversityAllExpelled_Tests\UniversityAllExpelled_Tests.csproj", "{E3D5BB9F-436E-44FC-AB2C-5D643628BAD2}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniversityAllExpelled_DataBase", "UniversityAllExpelled_DataBase\UniversityAllExpelled_DataBase.csproj", "{23807279-75F8-4BEC-831B-24AA27ABA57A}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UniversityAllExpelled_BusinessLogic", "UniversityAllExpelled_BusinessLogic\UniversityAllExpelled_BusinessLogic.csproj", "{6C4551E0-879E-4D28-9C2A-019929E0211A}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -21,6 +25,14 @@ Global
|
||||
{E3D5BB9F-436E-44FC-AB2C-5D643628BAD2}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{E3D5BB9F-436E-44FC-AB2C-5D643628BAD2}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{E3D5BB9F-436E-44FC-AB2C-5D643628BAD2}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{23807279-75F8-4BEC-831B-24AA27ABA57A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{23807279-75F8-4BEC-831B-24AA27ABA57A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{23807279-75F8-4BEC-831B-24AA27ABA57A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{23807279-75F8-4BEC-831B-24AA27ABA57A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{6C4551E0-879E-4D28-9C2A-019929E0211A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{6C4551E0-879E-4D28-9C2A-019929E0211A}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{6C4551E0-879E-4D28-9C2A-019929E0211A}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{6C4551E0-879E-4D28-9C2A-019929E0211A}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
||||
@@ -0,0 +1,166 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using UniversityAllExpelled_Models.BusinessLogicContracts;
|
||||
using UniversityAllExpelled_Models.DataModels.Worker;
|
||||
using UniversityAllExpelled_Models.Exceptions;
|
||||
using UniversityAllExpelled_Models.Extensions;
|
||||
using UniversityAllExpelled_Models.StorageContracts;
|
||||
using System.Data;
|
||||
using System.Text.Json;
|
||||
|
||||
namespace UniversityAllExpelled_BusinessLogic.Implementations;
|
||||
|
||||
internal class LessonBusinessLogicContract(ILessonStorageContract lessonStorageContract, ILogger logger) : ILessonBusinessLogicContract
|
||||
{
|
||||
private readonly ILessonStorageContract _lessonStorageContract = lessonStorageContract;
|
||||
private readonly ILogger _logger = logger;
|
||||
|
||||
public List<LessonDataModel> GetAllLesson()
|
||||
{
|
||||
_logger.LogInformation("GetAllLesson");
|
||||
var lessons = _lessonStorageContract.GetList();
|
||||
return lessons ?? throw new DataException("Failed to retrieve lessons list");
|
||||
}
|
||||
|
||||
public List<LessonDataModel> GetAllLessonByActive(bool isActive)
|
||||
{
|
||||
_logger.LogInformation("GetAllLessonByActive with isActive = {isActive}", isActive);
|
||||
var lessons = _lessonStorageContract.GetListByActive(isActive);
|
||||
return lessons ?? throw new DataException($"Failed to retrieve {(isActive ? "active" : "inactive")} lessons list");
|
||||
}
|
||||
|
||||
public LessonDataModel? GetLessonByName(string lessonName)
|
||||
{
|
||||
_logger.LogInformation("GetLessonByName: {lessonName}", lessonName);
|
||||
|
||||
if (lessonName.IsEmpty())
|
||||
throw new ArgumentNullException(nameof(lessonName));
|
||||
|
||||
var lessons = _lessonStorageContract.GetListByNameLesson(lessonName);
|
||||
return lessons?.FirstOrDefault();
|
||||
}
|
||||
|
||||
public LessonDataModel? GetLessonByNameByActive(string lessonName, bool isActive)
|
||||
{
|
||||
_logger.LogInformation("GetLessonByNameByActive: {lessonName} with isActive = {isActive}", lessonName, isActive);
|
||||
|
||||
if (lessonName.IsEmpty())
|
||||
throw new ArgumentNullException(nameof(lessonName));
|
||||
|
||||
var lessons = _lessonStorageContract.GetListByNameLesson(lessonName);
|
||||
return lessons?.FirstOrDefault(l => l.IsActive == isActive);
|
||||
}
|
||||
|
||||
public List<LessonDataModel> GetAllLessonByPeriod(DateTime startDate, DateTime endDate)
|
||||
{
|
||||
_logger.LogInformation("GetAllLessonByPeriod from {startDate} to {endDate}", startDate, endDate);
|
||||
|
||||
if (startDate > endDate)
|
||||
throw new ValidationException("Start date cannot be later than end date");
|
||||
|
||||
var lessons = _lessonStorageContract.GetList();
|
||||
return lessons?
|
||||
.Where(l => l.StartDate.Date >= startDate.Date && l.EndDate.Date <= endDate.Date)
|
||||
.ToList() ?? throw new DataException("Failed to retrieve lessons list");
|
||||
}
|
||||
|
||||
public List<LessonDataModel> GetAllLessonByPeriodByStudent(string studentId, DateTime startDate, DateTime endDate)
|
||||
{
|
||||
_logger.LogInformation("GetAllLessonByPeriodByStudent: {studentId} from {startDate} to {endDate}", studentId, startDate, endDate);
|
||||
|
||||
if (studentId.IsEmpty())
|
||||
throw new ArgumentNullException(nameof(studentId));
|
||||
|
||||
if (startDate > endDate)
|
||||
throw new ValidationException("Start date cannot be later than end date");
|
||||
|
||||
var lessons = _lessonStorageContract.GetListByStudentId(studentId);
|
||||
return lessons?
|
||||
.Where(l => l.StartDate.Date >= startDate.Date && l.EndDate.Date <= endDate.Date)
|
||||
.ToList() ?? throw new DataException("Failed to retrieve lessons list for the student");
|
||||
}
|
||||
|
||||
public List<LessonDataModel> GetAllLessonByPeriodByStudentByActive(string studentId, DateTime startDate, DateTime endDate, bool isActive)
|
||||
{
|
||||
_logger.LogInformation("GetAllLessonByPeriodByStudentByActive: {studentId} from {startDate} to {endDate} with isActive = {isActive}", studentId, startDate, endDate, isActive);
|
||||
|
||||
if (studentId.IsEmpty())
|
||||
throw new ArgumentNullException(nameof(studentId));
|
||||
|
||||
if (startDate > endDate)
|
||||
throw new ValidationException("Start date cannot be later than end date");
|
||||
|
||||
var lessons = _lessonStorageContract.GetListByStudentId(studentId);
|
||||
return lessons?
|
||||
.Where(l => l.StartDate.Date >= startDate.Date && l.EndDate.Date <= endDate.Date && l.IsActive == isActive)
|
||||
.ToList() ?? throw new DataException("Failed to retrieve lessons list for the student");
|
||||
}
|
||||
|
||||
public LessonDataModel? GetLessonByNameByStudentByActive(string lessonName, string studentId, bool isActive)
|
||||
{
|
||||
_logger.LogInformation("GetLessonByNameByStudentByActive: {lessonName}, {studentId} with isActive = {isActive}", lessonName, studentId, isActive);
|
||||
|
||||
if (lessonName.IsEmpty())
|
||||
throw new ArgumentNullException(nameof(lessonName));
|
||||
|
||||
if (studentId.IsEmpty())
|
||||
throw new ArgumentNullException(nameof(studentId));
|
||||
|
||||
var lessons = _lessonStorageContract.GetListByStudentId(studentId);
|
||||
return lessons?
|
||||
.FirstOrDefault(l => l.Name.Contains(lessonName) && l.IsActive == isActive);
|
||||
}
|
||||
|
||||
public void CreateLesson(LessonDataModel lessonDataModel)
|
||||
{
|
||||
_logger.LogInformation("CreateLesson: {json}", JsonSerializer.Serialize(lessonDataModel));
|
||||
ArgumentNullException.ThrowIfNull(lessonDataModel);
|
||||
lessonDataModel.Validate();
|
||||
|
||||
_lessonStorageContract.AddElement(lessonDataModel);
|
||||
}
|
||||
|
||||
public void UpdateLesson(LessonDataModel lessonDataModel)
|
||||
{
|
||||
_logger.LogInformation("UpdateLesson: {json}", JsonSerializer.Serialize(lessonDataModel));
|
||||
ArgumentNullException.ThrowIfNull(lessonDataModel);
|
||||
lessonDataModel.Validate();
|
||||
|
||||
var existingLesson = _lessonStorageContract.GetElementById(lessonDataModel.Id);
|
||||
if (existingLesson == null)
|
||||
throw new ElementNotFoundException($"Lesson with ID '{lessonDataModel.Id}' not found");
|
||||
|
||||
_lessonStorageContract.UpdElement(lessonDataModel);
|
||||
}
|
||||
|
||||
public void RestoreLesson(string id)
|
||||
{
|
||||
_logger.LogInformation("RestoreLesson with Id: {id}", id);
|
||||
|
||||
if (id.IsEmpty())
|
||||
throw new ArgumentNullException(nameof(id));
|
||||
|
||||
if (!id.IsGuid())
|
||||
throw new ValidationException("Id is not a valid GUID");
|
||||
|
||||
var lesson = _lessonStorageContract.GetElementById(id) ?? throw new ElementNotFoundException(id);
|
||||
var updatedLesson = new LessonDataModel(
|
||||
lesson.Id, lesson.Name, lesson.Price, lesson.StartDate, lesson.EndDate,
|
||||
lesson.StudentId, lesson.SalaryId, true);
|
||||
|
||||
_lessonStorageContract.UpdElement(updatedLesson);
|
||||
}
|
||||
|
||||
public void DeleteLesson(string id)
|
||||
{
|
||||
_logger.LogInformation("DeleteLesson with Id: {id}", id);
|
||||
|
||||
if (id.IsEmpty())
|
||||
throw new ArgumentNullException(nameof(id));
|
||||
|
||||
if (!id.IsGuid())
|
||||
throw new ValidationException("Id is not a valid GUID");
|
||||
|
||||
var lesson = _lessonStorageContract.GetElementById(id) ?? throw new ElementNotFoundException(id);
|
||||
_lessonStorageContract.DelElement(lesson);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,262 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using UniversityAllExpelled_Models.BusinessLogicContracts;
|
||||
using UniversityAllExpelled_Models.DataModels.Client;
|
||||
using UniversityAllExpelled_Models.Exceptions;
|
||||
using UniversityAllExpelled_Models.Extensions;
|
||||
using UniversityAllExpelled_Models.StorageContracts;
|
||||
using System.Text.Json;
|
||||
using System.Data;
|
||||
|
||||
namespace UniversityAllExpelled_BusinessLogic.Implementations;
|
||||
|
||||
internal class PaymentBusinessLogicContract : IPaymentBusinessLogicContract
|
||||
{
|
||||
private readonly IPaymentStorageContract _paymentStorage;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public PaymentBusinessLogicContract(
|
||||
IPaymentStorageContract paymentStorage,
|
||||
ILogger logger)
|
||||
{
|
||||
_paymentStorage = paymentStorage ?? throw new ArgumentNullException(nameof(paymentStorage));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
public List<PaymentDataModel> GetAllPayment()
|
||||
{
|
||||
_logger.LogInformation("Getting all payments");
|
||||
try
|
||||
{
|
||||
return _paymentStorage.GetList() ?? throw new DataException("Failed to retrieve payments list");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting all payments");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public List<PaymentDataModel> GetAllPaymentByPaidOfState(bool paidOf)
|
||||
{
|
||||
_logger.LogInformation("Getting payments by paid state: {PaidOf}", paidOf);
|
||||
try
|
||||
{
|
||||
return _paymentStorage.GetListByPaidOf(paidOf)
|
||||
?? throw new DataException($"Failed to retrieve payments with PaidOf={paidOf}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting payments by paid state");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public List<PaymentDataModel> GetAllPaymentByPeriod(DateTime startDate, DateTime endDate)
|
||||
{
|
||||
_logger.LogInformation("Getting payments by period from {StartDate} to {EndDate}", startDate, endDate);
|
||||
|
||||
if (startDate > endDate)
|
||||
throw new ValidationException("Start date cannot be greater than end date");
|
||||
|
||||
try
|
||||
{
|
||||
var allPayments = _paymentStorage.GetList()
|
||||
?? throw new DataException("Failed to retrieve payments list");
|
||||
|
||||
// Фильтрация по дате создания (если в модели нет даты, можно добавить поле CreatedDate)
|
||||
return allPayments.Where(p => p.DateOfpayment >= startDate && p.DateOfpayment <= endDate)
|
||||
.ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting payments by period");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public List<PaymentDataModel> GetAllPaymentByPeriodByPaidOfState(DateTime startDate, DateTime endDate, bool paidOf)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"Getting payments by period from {StartDate} to {EndDate} with PaidOf={PaidOf}",
|
||||
startDate, endDate, paidOf);
|
||||
|
||||
if (startDate > endDate)
|
||||
throw new ValidationException("Start date cannot be greater than end date");
|
||||
|
||||
try
|
||||
{
|
||||
var payments = _paymentStorage.GetListByPaidOf(paidOf)
|
||||
?? throw new DataException($"Failed to retrieve payments with PaidOf={paidOf}");
|
||||
|
||||
// Фильтрация по дате создания
|
||||
return payments.Where(p => p.DateOfpayment >= startDate && p.DateOfpayment <= endDate)
|
||||
.ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting payments by period and paid state");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public List<PaymentDataModel> GetAllPaymentByStudetnByPeriod(string studentId, DateTime startDate, DateTime endDate)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"Getting payments for student {StudentId} by period from {StartDate} to {EndDate}",
|
||||
studentId, startDate, endDate);
|
||||
|
||||
ValidateStudentId(studentId);
|
||||
if (startDate > endDate)
|
||||
throw new ValidationException("Start date cannot be greater than end date");
|
||||
|
||||
try
|
||||
{
|
||||
var payments = _paymentStorage.GetListByStudentId(studentId)
|
||||
?? throw new ElementNotFoundException($"Payments for student {studentId} not found");
|
||||
|
||||
// Фильтрация по дате создания
|
||||
return payments.Where(p => p.DateOfpayment >= startDate && p.DateOfpayment <= endDate)
|
||||
.ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting student payments by period");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public List<PaymentDataModel> GetAllPaymentByStudetnByPaidOfState(string studentId, bool paidOf)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"Getting payments for student {StudentId} with PaidOf={PaidOf}",
|
||||
studentId, paidOf);
|
||||
|
||||
ValidateStudentId(studentId);
|
||||
|
||||
try
|
||||
{
|
||||
var allPayments = _paymentStorage.GetListByStudentId(studentId)
|
||||
?? throw new ElementNotFoundException($"Payments for student {studentId} not found");
|
||||
|
||||
return allPayments.Where(p => p.PaidOf == paidOf).ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting student payments by paid state");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public List<PaymentDataModel> GetAllPaymentByStudetnByPeriodByPaidOfState(
|
||||
string studentId, DateTime startDate, DateTime endDate, bool paidOf)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"Getting payments for student {StudentId} by period from {StartDate} to {EndDate} with PaidOf={PaidOf}",
|
||||
studentId, startDate, endDate, paidOf);
|
||||
|
||||
ValidateStudentId(studentId);
|
||||
if (startDate > endDate)
|
||||
throw new ValidationException("Start date cannot be greater than end date");
|
||||
|
||||
try
|
||||
{
|
||||
var payments = _paymentStorage.GetListByStudentId(studentId)
|
||||
?? throw new ElementNotFoundException($"Payments for student {studentId} not found");
|
||||
|
||||
return payments
|
||||
.Where(p => p.PaidOf == paidOf)
|
||||
.Where(p => p.DateOfpayment >= startDate && p.DateOfpayment <= endDate)
|
||||
.ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting student payments by period and paid state");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public PaymentDataModel? GetPaymentByData(string data)
|
||||
{
|
||||
_logger.LogInformation("Getting payment by data: {Data}", data);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(data))
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
|
||||
try
|
||||
{
|
||||
PaymentDataModel? payment = null;
|
||||
|
||||
if (data.IsGuid())
|
||||
payment = _paymentStorage.GetElementById(data);
|
||||
|
||||
if (payment == null && data.IsGuid())
|
||||
{
|
||||
var studentPayments = _paymentStorage.GetListByStudentId(data);
|
||||
payment = studentPayments?.FirstOrDefault();
|
||||
}
|
||||
|
||||
return payment ?? throw new ElementNotFoundException($"Payment with data '{data}' not found");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting payment by data");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void InsertPayment(PaymentDataModel paymentDataModel)
|
||||
{
|
||||
_logger.LogInformation("Inserting payment: {PaymentData}",
|
||||
JsonSerializer.Serialize(paymentDataModel));
|
||||
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(paymentDataModel);
|
||||
paymentDataModel.Validate();
|
||||
|
||||
// Дополнительная проверка студента
|
||||
if (_paymentStorage.GetListByStudentId(paymentDataModel.StudentsId) == null)
|
||||
throw new ElementNotFoundException($"Student with ID {paymentDataModel.StudentsId} not found");
|
||||
|
||||
_paymentStorage.AddElement(paymentDataModel);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while inserting payment");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void CanselPayment(string id)
|
||||
{
|
||||
_logger.LogInformation("Canceling payment with ID: {Id}", id);
|
||||
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(id))
|
||||
throw new ArgumentNullException(nameof(id));
|
||||
|
||||
if (!id.IsGuid())
|
||||
throw new ValidationException("Id must be a valid GUID");
|
||||
|
||||
var payment = _paymentStorage.GetElementById(id)
|
||||
?? throw new ElementNotFoundException($"Payment with ID {id} not found");
|
||||
|
||||
_paymentStorage.DelElement(payment);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while canceling payment");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private void ValidateStudentId(string studentId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(studentId))
|
||||
throw new ArgumentNullException(nameof(studentId));
|
||||
|
||||
if (!studentId.IsGuid())
|
||||
throw new ValidationException("StudentId must be a valid GUID");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,319 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using UniversityAllExpelled_Models.BusinessLogicContracts;
|
||||
using UniversityAllExpelled_Models.DataModels;
|
||||
using UniversityAllExpelled_Models.DataModels.Client;
|
||||
using UniversityAllExpelled_Models.Enums;
|
||||
using UniversityAllExpelled_Models.Exceptions;
|
||||
using UniversityAllExpelled_Models.Extensions;
|
||||
using UniversityAllExpelled_Models.StorageContracts;
|
||||
using System.Text.Json;
|
||||
using System.Data;
|
||||
|
||||
namespace UniversityAllExpelled_BusinessLogic.Implementations;
|
||||
|
||||
internal class StudentBusinessLogicContract : IStudentBusinessLogicContract
|
||||
{
|
||||
private readonly IStudentStorageContract _studentStorage;
|
||||
private readonly IUserStorageContract _userStorage;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public StudentBusinessLogicContract(
|
||||
IStudentStorageContract studentStorage,
|
||||
IUserStorageContract userStorage,
|
||||
ILogger logger)
|
||||
{
|
||||
_studentStorage = studentStorage ?? throw new ArgumentNullException(nameof(studentStorage));
|
||||
_userStorage = userStorage ?? throw new ArgumentNullException(nameof(userStorage));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
|
||||
public List<StudentDataModel> GetAllStudents(SystemRoleType role = SystemRoleType.student, bool IsDeleted = false)
|
||||
{
|
||||
_logger.LogInformation("Getting all students with role: {Role}, IsDeleted: {IsDeleted}", role, IsDeleted);
|
||||
|
||||
try
|
||||
{
|
||||
var students = _studentStorage.GetList() ?? throw new DataException("Failed to retrieve students list");
|
||||
|
||||
var result = new List<StudentDataModel>();
|
||||
|
||||
foreach (var student in students)
|
||||
{
|
||||
var user = _userStorage.GetElementById(student.UserId);
|
||||
if (user != null && user.Role == role && user.IsDeleted == IsDeleted)
|
||||
{
|
||||
result.Add(student);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting all students");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public List<StudentDataModel> GetAllStudentsByBirthDate(DateTime startDate, DateTime endDate,
|
||||
SystemRoleType role = SystemRoleType.student, bool IsDeleted = false)
|
||||
{
|
||||
_logger.LogInformation(
|
||||
"Getting students by birth date from {StartDate} to {EndDate}, role: {Role}, IsDeleted: {IsDeleted}",
|
||||
startDate, endDate, role, IsDeleted);
|
||||
|
||||
if (startDate > endDate)
|
||||
throw new ValidationException("Start date cannot be greater than end date");
|
||||
|
||||
try
|
||||
{
|
||||
var students = _studentStorage.GetList() ?? throw new DataException("Failed to retrieve students list");
|
||||
|
||||
var result = new List<StudentDataModel>();
|
||||
|
||||
foreach (var student in students)
|
||||
{
|
||||
var user = _userStorage.GetElementById(student.UserId);
|
||||
if (user != null &&
|
||||
user.Role == role &&
|
||||
user.IsDeleted == IsDeleted &&
|
||||
user.BirthDate >= startDate &&
|
||||
user.BirthDate <= endDate)
|
||||
{
|
||||
result.Add(student);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting students by birth date");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public List<StudentDataModel> GetAllStudentsByFaculty(FacultyType faculty,
|
||||
SystemRoleType role = SystemRoleType.student, bool IsDeleted = false)
|
||||
{
|
||||
_logger.LogInformation("Getting students by faculty: {Faculty}, role: {Role}, IsDeleted: {IsDeleted}",
|
||||
faculty, role, IsDeleted);
|
||||
|
||||
try
|
||||
{
|
||||
var students = _studentStorage.GetListByFaculty(faculty.ToString())
|
||||
?? throw new DataException($"Failed to retrieve students for faculty {faculty}");
|
||||
|
||||
var result = new List<StudentDataModel>();
|
||||
|
||||
foreach (var student in students)
|
||||
{
|
||||
var user = _userStorage.GetElementById(student.UserId);
|
||||
if (user != null && user.Role == role && user.IsDeleted == IsDeleted)
|
||||
{
|
||||
result.Add(student);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting students by faculty");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public List<StudentDataModel> GetAllStudentsByGroopType(GroopType groop,
|
||||
SystemRoleType role = SystemRoleType.student, bool IsDeleted = false)
|
||||
{
|
||||
_logger.LogInformation("Getting students by group: {Groop}, role: {Role}, IsDeleted: {IsDeleted}",
|
||||
groop, role, IsDeleted);
|
||||
|
||||
try
|
||||
{
|
||||
var students = _studentStorage.GetListByGroop(groop.ToString())
|
||||
?? throw new DataException($"Failed to retrieve students for group {groop}");
|
||||
|
||||
var result = new List<StudentDataModel>();
|
||||
|
||||
foreach (var student in students)
|
||||
{
|
||||
var user = _userStorage.GetElementById(student.UserId);
|
||||
if (user != null && user.Role == role && user.IsDeleted == IsDeleted)
|
||||
{
|
||||
result.Add(student);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting students by group");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public List<StudentDataModel> GetAllStudentsByCource(int course,
|
||||
SystemRoleType role = SystemRoleType.student, bool IsDeleted = false)
|
||||
{
|
||||
_logger.LogInformation("Getting students by course: {Course}, role: {Role}, IsDeleted: {IsDeleted}",
|
||||
course, role, IsDeleted);
|
||||
|
||||
if (course < 1 || course > 6)
|
||||
throw new ValidationException("Course must be between 1 and 6");
|
||||
|
||||
try
|
||||
{
|
||||
var students = _studentStorage.GetListByCource(course)
|
||||
?? throw new DataException($"Failed to retrieve students for course {course}");
|
||||
|
||||
var result = new List<StudentDataModel>();
|
||||
|
||||
foreach (var student in students)
|
||||
{
|
||||
var user = _userStorage.GetElementById(student.UserId);
|
||||
if (user != null && user.Role == role && user.IsDeleted == IsDeleted)
|
||||
{
|
||||
result.Add(student);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting students by course");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public StudentDataModel? GetStudentByData(string data,
|
||||
SystemRoleType role = SystemRoleType.student, bool IsDeleted = false)
|
||||
{
|
||||
_logger.LogInformation("Getting student by data: {Data}, role: {Role}, IsDeleted: {IsDeleted}",
|
||||
data, role, IsDeleted);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(data))
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
|
||||
try
|
||||
{
|
||||
StudentDataModel? student = null;
|
||||
|
||||
// Try by student ID
|
||||
if (data.IsGuid())
|
||||
student = _studentStorage.GetElementById(data);
|
||||
|
||||
// Try by user ID
|
||||
if (student == null && data.IsGuid())
|
||||
student = _studentStorage.GetElementByUserId(data);
|
||||
|
||||
// Try by user data
|
||||
if (student == null)
|
||||
{
|
||||
var user = _userStorage.GetElementById(data) ??
|
||||
_userStorage.GetElementByPhoneNomber(data) ??
|
||||
_userStorage.GetListByLogin(data);
|
||||
if (user != null)
|
||||
student = _studentStorage.GetElementByUserId(user.Id);
|
||||
}
|
||||
|
||||
if (student != null)
|
||||
{
|
||||
var user = _userStorage.GetElementById(student.UserId);
|
||||
if (user != null && user.Role == role && user.IsDeleted == IsDeleted)
|
||||
return student;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting student by data");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void InsertStudent(StudentDataModel studentDataModel)
|
||||
{
|
||||
_logger.LogInformation("Inserting new student: {StudentData}",
|
||||
JsonSerializer.Serialize(studentDataModel));
|
||||
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(studentDataModel);
|
||||
studentDataModel.Validate();
|
||||
|
||||
var user = _userStorage.GetElementById(studentDataModel.UserId)
|
||||
?? throw new ElementNotFoundException($"User with ID {studentDataModel.UserId} not found");
|
||||
|
||||
if (user.Role != SystemRoleType.student)
|
||||
throw new ValidationException("User must have student role");
|
||||
|
||||
if (_studentStorage.GetElementByUserId(studentDataModel.UserId) != null)
|
||||
throw new DuplicateException($"Student for user {studentDataModel.UserId} already exists");
|
||||
|
||||
_studentStorage.AddElement(studentDataModel);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while inserting student");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateStudent(StudentDataModel studentDataModel)
|
||||
{
|
||||
_logger.LogInformation("Updating student: {StudentData}",
|
||||
JsonSerializer.Serialize(studentDataModel));
|
||||
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(studentDataModel);
|
||||
studentDataModel.Validate();
|
||||
|
||||
var existingStudent = _studentStorage.GetElementById(studentDataModel.Id)
|
||||
?? throw new ElementNotFoundException($"Student with ID {studentDataModel.Id} not found");
|
||||
|
||||
if (existingStudent.UserId != studentDataModel.UserId)
|
||||
throw new ValidationException("Cannot change UserId for existing student");
|
||||
|
||||
_studentStorage.UpdElement(studentDataModel);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while updating student");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void RestoreStudent(string id)
|
||||
{
|
||||
_logger.LogInformation("Restoring student with ID: {Id}", id);
|
||||
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(id))
|
||||
throw new ArgumentNullException(nameof(id));
|
||||
|
||||
if (!id.IsGuid())
|
||||
throw new ValidationException("Id must be a valid GUID");
|
||||
|
||||
var student = _studentStorage.GetElementById(id)
|
||||
?? throw new ElementNotFoundException($"Student with ID {id} not found");
|
||||
|
||||
var user = _userStorage.GetElementById(student.UserId)
|
||||
?? throw new ElementNotFoundException($"User with ID {student.UserId} not found");
|
||||
|
||||
user.IsDeleted = false;
|
||||
_userStorage.UpdElement(user);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while restoring student");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using UniversityAllExpelled_Models.BusinessLogicContracts;
|
||||
using UniversityAllExpelled_Models.DataModels;
|
||||
using UniversityAllExpelled_Models.Extensions;
|
||||
using UniversityAllExpelled_Models.Exceptions;
|
||||
using UniversityAllExpelled_Models.StorageContracts;
|
||||
using System.Text.Json;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Data;
|
||||
|
||||
namespace UniversityAllExpelled_BusinessLogic.Implementations;
|
||||
|
||||
internal class UserBusinessLogicContract(IUserStorageContract userStorageContract, ILogger logger) : IUserBusinessLogicContract
|
||||
{
|
||||
private readonly IUserStorageContract _userStorageContract = userStorageContract;
|
||||
private readonly ILogger _logger = logger;
|
||||
|
||||
public List<UserDataModel> GetAllUsers()
|
||||
{
|
||||
_logger.LogInformation("GetAllUsers");
|
||||
var users = _userStorageContract.GetList();
|
||||
return users ?? throw new DataException("Failed to retrieve users list");
|
||||
}
|
||||
|
||||
public List<UserDataModel> GetAllActiveUsers(bool IsDeleted = false)
|
||||
{
|
||||
_logger.LogInformation("GetAllActiveUsers with IsDeleted = {IsDeleted}", IsDeleted);
|
||||
var users = _userStorageContract.GetListByIsDeleted(IsDeleted);
|
||||
return users ?? throw new DataException($"Failed to retrieve {(IsDeleted ? "deleted" : "active")} users list");
|
||||
}
|
||||
|
||||
public List<UserDataModel> GetAllUserByBirthDate(DateTime fromDate, DateTime toDate, bool IsDeleted = false)
|
||||
{
|
||||
_logger.LogInformation("GetAllUserByBirthDate from {fromDate} to {toDate} with IsDeleted = {IsDeleted}",
|
||||
fromDate, toDate, IsDeleted);
|
||||
|
||||
if (fromDate > toDate)
|
||||
{
|
||||
throw new ValidationException("FromDate cannot be greater than ToDate");
|
||||
}
|
||||
|
||||
var allUsers = _userStorageContract.GetList() ?? throw new DataException("Failed to retrieve users list");
|
||||
|
||||
return allUsers
|
||||
.Where(u => u.BirthDate >= fromDate &&
|
||||
u.BirthDate <= toDate &&
|
||||
u.IsDeleted == IsDeleted)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public UserDataModel GetUserByData(string data)
|
||||
{
|
||||
_logger.LogInformation("GetUserByData: {data}", data);
|
||||
|
||||
if (data.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
}
|
||||
|
||||
// Try by ID
|
||||
if (data.IsGuid())
|
||||
{
|
||||
var userById = _userStorageContract.GetElementById(data);
|
||||
if (userById != null) return userById;
|
||||
}
|
||||
|
||||
// Try by phone number
|
||||
if (Regex.IsMatch(data, @"^(\+7|8)(-?\d{3}-?\d{3}-?\d{2}-?\d{2}|-?\d{10})$"))
|
||||
{
|
||||
var userByPhone = _userStorageContract.GetElementByPhoneNomber(data);
|
||||
if (userByPhone != null) return userByPhone;
|
||||
}
|
||||
|
||||
// Try by login
|
||||
var userByLogin = _userStorageContract.GetListByLogin(data);
|
||||
if (userByLogin != null) return userByLogin;
|
||||
|
||||
var usersByFio = _userStorageContract.GetListByFIO(data);
|
||||
if (usersByFio != null && usersByFio.Count > 0) return usersByFio[0];
|
||||
|
||||
throw new ElementNotFoundException($"User with data '{data}' not found");
|
||||
}
|
||||
|
||||
public void InsertUser(UserDataModel userDataModel)
|
||||
{
|
||||
_logger.LogInformation("InsertUser: {json}", JsonSerializer.Serialize(userDataModel));
|
||||
ArgumentNullException.ThrowIfNull(userDataModel);
|
||||
userDataModel.Validate();
|
||||
|
||||
// Check if login already exists
|
||||
var existingUser = _userStorageContract.GetListByLogin(userDataModel.Login);
|
||||
if (existingUser != null)
|
||||
{
|
||||
throw new DuplicateException($"User with login '{userDataModel.Login}' already exists");
|
||||
}
|
||||
|
||||
_userStorageContract.AddElement(userDataModel);
|
||||
}
|
||||
|
||||
public void UpdateUser(UserDataModel userDataModel)
|
||||
{
|
||||
_logger.LogInformation("UpdateUser: {json}", JsonSerializer.Serialize(userDataModel));
|
||||
ArgumentNullException.ThrowIfNull(userDataModel);
|
||||
userDataModel.Validate();
|
||||
|
||||
// Verify user exists
|
||||
var existingUser = _userStorageContract.GetElementById(userDataModel.Id);
|
||||
if (existingUser == null)
|
||||
{
|
||||
throw new ElementNotFoundException($"User with ID '{userDataModel.Id}' not found");
|
||||
}
|
||||
|
||||
_userStorageContract.UpdElement(userDataModel);
|
||||
}
|
||||
|
||||
public void RestoreUser(string id)
|
||||
{
|
||||
_logger.LogInformation("RestoreUser with Id: {id}", id);
|
||||
|
||||
if (id.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(id));
|
||||
}
|
||||
|
||||
if (!id.IsGuid())
|
||||
{
|
||||
throw new ValidationException("Id is not a valid GUID");
|
||||
}
|
||||
|
||||
var user = _userStorageContract.GetElementById(id) ?? throw new ElementNotFoundException(id);
|
||||
user.IsDeleted = false;
|
||||
_userStorageContract.UpdElement(user);
|
||||
}
|
||||
|
||||
public void DeleteUser(string id)
|
||||
{
|
||||
_logger.LogInformation("DeleteUser with Id: {id}", id);
|
||||
|
||||
if (id.IsEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(nameof(id));
|
||||
}
|
||||
|
||||
if (!id.IsGuid())
|
||||
{
|
||||
throw new ValidationException("Id is not a valid GUID");
|
||||
}
|
||||
|
||||
var user = _userStorageContract.GetElementById(id) ?? throw new ElementNotFoundException(id);
|
||||
user.IsDeleted = true;
|
||||
_userStorageContract.UpdElement(user);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,164 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UniversityAllExpelled_DataBase.Models;
|
||||
using UniversityAllExpelled_Models.DataModels.Worker;
|
||||
using UniversityAllExpelled_Models.StorageContracts;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Implementations;
|
||||
|
||||
internal class EducationLessonsStorageContract : IEducationLessonsStorageContract
|
||||
{
|
||||
private readonly UniversityAllExpelledDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public EducationLessonsStorageContract(UniversityAllExpelledDbContext dbContext, Mapper mapper)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public void AddElement(EducationLessonsDataModel educationLessonsDataModel)
|
||||
{
|
||||
if (educationLessonsDataModel == null)
|
||||
throw new ArgumentNullException(nameof(educationLessonsDataModel));
|
||||
|
||||
educationLessonsDataModel.Validate();
|
||||
|
||||
var educationLessonsEntity = _mapper.Map<EducationLessons>(educationLessonsDataModel);
|
||||
_dbContext.EducationLessons.Add(educationLessonsEntity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public EducationLessonsDataModel? GetElement(string educationId, string lessonsId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(educationId) || string.IsNullOrWhiteSpace(lessonsId))
|
||||
throw new ArgumentException("Education ID and Lessons ID cannot be null or empty");
|
||||
|
||||
var entity = _dbContext.EducationLessons
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault(el => el.EducationId == educationId && el.LessonsId == lessonsId);
|
||||
|
||||
return entity != null ? _mapper.Map<EducationLessonsDataModel>(entity) : null;
|
||||
}
|
||||
|
||||
public List<EducationLessonsDataModel> GetList()
|
||||
{
|
||||
return _dbContext.EducationLessons
|
||||
.AsNoTracking()
|
||||
.Select(el => _mapper.Map<EducationLessonsDataModel>(el))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<EducationLessonsDataModel> GetListByEducationId(string educationId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(educationId))
|
||||
return new List<EducationLessonsDataModel>();
|
||||
|
||||
return _dbContext.EducationLessons
|
||||
.AsNoTracking()
|
||||
.Where(el => el.EducationId == educationId)
|
||||
.Select(el => _mapper.Map<EducationLessonsDataModel>(el))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<EducationLessonsDataModel> GetListByLessonsId(string lessonsId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(lessonsId))
|
||||
return new List<EducationLessonsDataModel>();
|
||||
|
||||
return _dbContext.EducationLessons
|
||||
.AsNoTracking()
|
||||
.Where(el => el.LessonsId == lessonsId)
|
||||
.Select(el => _mapper.Map<EducationLessonsDataModel>(el))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public void UpdElement(EducationLessonsDataModel educationLessonDataModel)
|
||||
{
|
||||
if (educationLessonDataModel == null)
|
||||
throw new ArgumentNullException(nameof(educationLessonDataModel));
|
||||
|
||||
educationLessonDataModel.Validate();
|
||||
|
||||
var existingEntity = _dbContext.EducationLessons
|
||||
.FirstOrDefault(el => el.EducationId == educationLessonDataModel.EducationId &&
|
||||
el.LessonsId == educationLessonDataModel.LessonsId);
|
||||
|
||||
if (existingEntity == null)
|
||||
throw new KeyNotFoundException("Education-Lesson connection not found");
|
||||
|
||||
_mapper.Map(educationLessonDataModel, existingEntity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public void DelElement(EducationLessonsDataModel educationLessonDataModel)
|
||||
{
|
||||
if (educationLessonDataModel == null)
|
||||
throw new ArgumentNullException(nameof(educationLessonDataModel));
|
||||
|
||||
var entity = _dbContext.EducationLessons
|
||||
.FirstOrDefault(el => el.EducationId == educationLessonDataModel.EducationId &&
|
||||
el.LessonsId == educationLessonDataModel.LessonsId);
|
||||
|
||||
if (entity == null)
|
||||
throw new KeyNotFoundException("Education-Lesson connection not found");
|
||||
|
||||
_dbContext.EducationLessons.Remove(entity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public void DelElementsByEducationId(string educationId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(educationId))
|
||||
throw new ArgumentException("Education ID cannot be null or empty", nameof(educationId));
|
||||
|
||||
var entities = _dbContext.EducationLessons
|
||||
.Where(el => el.EducationId == educationId)
|
||||
.ToList();
|
||||
|
||||
_dbContext.EducationLessons.RemoveRange(entities);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public void DelElementsByLessonId(string lessonId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(lessonId))
|
||||
throw new ArgumentException("Lesson ID cannot be null or empty", nameof(lessonId));
|
||||
|
||||
var entities = _dbContext.EducationLessons
|
||||
.Where(el => el.LessonsId == lessonId)
|
||||
.ToList();
|
||||
|
||||
_dbContext.EducationLessons.RemoveRange(entities);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public EducationLessonsDataModel? GetElementByIds(string educationId, string lessonId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(educationId) || string.IsNullOrWhiteSpace(lessonId))
|
||||
return null;
|
||||
|
||||
var entity = _dbContext.EducationLessons
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault(el => el.EducationId == educationId && el.LessonsId == lessonId);
|
||||
|
||||
return entity != null ? _mapper.Map<EducationLessonsDataModel>(entity) : null;
|
||||
}
|
||||
|
||||
public List<EducationLessonsDataModel> GetListByLessonId(string lessonId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(lessonId))
|
||||
return new List<EducationLessonsDataModel>();
|
||||
|
||||
return _dbContext.EducationLessons
|
||||
.AsNoTracking()
|
||||
.Where(el => el.LessonsId == lessonId)
|
||||
.Select(el => _mapper.Map<EducationLessonsDataModel>(el))
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UniversityAllExpelled_DataBase.Models;
|
||||
using UniversityAllExpelled_Models.DataModels.Worker;
|
||||
using UniversityAllExpelled_Models.StorageContracts;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Implementations;
|
||||
|
||||
internal class EducationStorageContract : IEducationStorageContract
|
||||
{
|
||||
private readonly UniversityAllExpelledDbContext _dbContext;
|
||||
private readonly IMapper _mapper;
|
||||
|
||||
public EducationStorageContract(UniversityAllExpelledDbContext dbContext, IMapper mapper)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public void AddElement(EducationDataModel educationDataModel)
|
||||
{
|
||||
if (educationDataModel == null)
|
||||
throw new ArgumentNullException(nameof(educationDataModel));
|
||||
|
||||
educationDataModel.Validate();
|
||||
|
||||
var educationEntity = _mapper.Map<Education>(educationDataModel);
|
||||
_dbContext.Educations.Add(educationEntity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public EducationDataModel? GetElementById(string id)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(id) || !Guid.TryParse(id, out _))
|
||||
return null;
|
||||
|
||||
var educationEntity = _dbContext.Educations
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault(e => e.Id == id);
|
||||
|
||||
return educationEntity != null ? _mapper.Map<EducationDataModel>(educationEntity) : null;
|
||||
}
|
||||
|
||||
public List<EducationDataModel> GetList()
|
||||
{
|
||||
return _dbContext.Educations
|
||||
.AsNoTracking()
|
||||
.Select(e => new EducationDataModel(
|
||||
e.Id,
|
||||
e.TeacherId,
|
||||
e.Amount,
|
||||
e.StartDate,
|
||||
e.EndDate,
|
||||
e.Active))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<EducationDataModel> GetListByTeacherId(string teacherId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(teacherId) || !Guid.TryParse(teacherId, out _))
|
||||
return new List<EducationDataModel>();
|
||||
|
||||
return _dbContext.Educations
|
||||
.AsNoTracking()
|
||||
.Where(e => e.TeacherId == teacherId)
|
||||
.Select(e => new EducationDataModel(
|
||||
e.Id,
|
||||
e.TeacherId,
|
||||
e.Amount,
|
||||
e.StartDate,
|
||||
e.EndDate,
|
||||
e.Active))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<EducationDataModel> GetListByActive(bool active)
|
||||
{
|
||||
return _dbContext.Educations
|
||||
.AsNoTracking()
|
||||
.Where(e => e.Active == active)
|
||||
.Select(e => new EducationDataModel(
|
||||
e.Id,
|
||||
e.TeacherId,
|
||||
e.Amount,
|
||||
e.StartDate,
|
||||
e.EndDate,
|
||||
e.Active))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<EducationDataModel> GetListByDateRange(DateTime startDate, DateTime endDate)
|
||||
{
|
||||
if (startDate > endDate)
|
||||
return new List<EducationDataModel>();
|
||||
|
||||
return _dbContext.Educations
|
||||
.AsNoTracking()
|
||||
.Where(e => e.StartDate >= startDate && e.EndDate <= endDate)
|
||||
.Select(e => new EducationDataModel(
|
||||
e.Id,
|
||||
e.TeacherId,
|
||||
e.Amount,
|
||||
e.StartDate,
|
||||
e.EndDate,
|
||||
e.Active))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public void UpdElement(EducationDataModel educationDataModel)
|
||||
{
|
||||
if (educationDataModel == null)
|
||||
throw new ArgumentNullException(nameof(educationDataModel));
|
||||
|
||||
educationDataModel.Validate();
|
||||
|
||||
var existingEducation = _dbContext.Educations.Find(educationDataModel.Id);
|
||||
if (existingEducation == null)
|
||||
throw new KeyNotFoundException($"Education record with id {educationDataModel.Id} not found");
|
||||
|
||||
existingEducation.TeacherId = educationDataModel.TeacherId;
|
||||
existingEducation.Amount = educationDataModel.Amount;
|
||||
existingEducation.StartDate = educationDataModel.StartDate;
|
||||
existingEducation.EndDate = educationDataModel.EndDate;
|
||||
existingEducation.Active = educationDataModel.Active;
|
||||
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public void DelElement(EducationDataModel educationDataModel)
|
||||
{
|
||||
if (educationDataModel == null)
|
||||
throw new ArgumentNullException(nameof(educationDataModel));
|
||||
|
||||
var educationEntity = _dbContext.Educations.Find(educationDataModel.Id);
|
||||
if (educationEntity == null)
|
||||
throw new KeyNotFoundException($"Education record with id {educationDataModel.Id} not found");
|
||||
|
||||
_dbContext.Educations.Remove(educationEntity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using UniversityAllExpelled_DataBase.Models;
|
||||
using UniversityAllExpelled_Models.DataModels.Worker;
|
||||
using UniversityAllExpelled_Models.StorageContracts;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Implementations
|
||||
{
|
||||
internal class LessonStorageContract : ILessonStorageContract
|
||||
{
|
||||
private readonly UniversityAllExpelledDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public LessonStorageContract(UniversityAllExpelledDbContext dbContext, Mapper mapper)
|
||||
{
|
||||
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
|
||||
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
|
||||
}
|
||||
|
||||
public List<LessonDataModel> GetList()
|
||||
{
|
||||
return _dbContext.Lessons
|
||||
.AsNoTracking()
|
||||
.Include(l => l.Student)
|
||||
.Include(l => l.Salary)
|
||||
.Select(l => _mapper.Map<LessonDataModel>(l))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public LessonDataModel? GetElementById(string id)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(id))
|
||||
throw new ArgumentException("Id cannot be null or empty", nameof(id));
|
||||
|
||||
var lessonEntity = _dbContext.Lessons
|
||||
.AsNoTracking()
|
||||
.Include(l => l.Student)
|
||||
.Include(l => l.Salary)
|
||||
.FirstOrDefault(l => l.Id == id);
|
||||
|
||||
return lessonEntity != null ? _mapper.Map<LessonDataModel>(lessonEntity) : null;
|
||||
}
|
||||
|
||||
public List<LessonDataModel>? GetListByStudentId(string studentId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(studentId))
|
||||
return null;
|
||||
|
||||
return _dbContext.Lessons
|
||||
.AsNoTracking()
|
||||
.Include(l => l.Student)
|
||||
.Include(l => l.Salary)
|
||||
.Where(l => l.StudentId == studentId)
|
||||
.Select(l => _mapper.Map<LessonDataModel>(l))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<LessonDataModel>? GetListByNameLesson(string nameLesson)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(nameLesson))
|
||||
return null;
|
||||
|
||||
return _dbContext.Lessons
|
||||
.AsNoTracking()
|
||||
.Include(l => l.Student)
|
||||
.Include(l => l.Salary)
|
||||
.Where(l => l.Name.Contains(nameLesson))
|
||||
.Select(l => _mapper.Map<LessonDataModel>(l))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<LessonDataModel>? GetListByActive(bool active)
|
||||
{
|
||||
return _dbContext.Lessons
|
||||
.AsNoTracking()
|
||||
.Include(l => l.Student)
|
||||
.Include(l => l.Salary)
|
||||
.Where(l => l.IsActive == active)
|
||||
.Select(l => _mapper.Map<LessonDataModel>(l))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public void AddElement(LessonDataModel lessonDataModel)
|
||||
{
|
||||
if (lessonDataModel == null)
|
||||
throw new ArgumentNullException(nameof(lessonDataModel));
|
||||
|
||||
lessonDataModel.Validate();
|
||||
|
||||
var lessonEntity = _mapper.Map<Lesson>(lessonDataModel);
|
||||
_dbContext.Lessons.Add(lessonEntity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public void UpdElement(LessonDataModel lessonDataModel)
|
||||
{
|
||||
if (lessonDataModel == null)
|
||||
throw new ArgumentNullException(nameof(lessonDataModel));
|
||||
|
||||
lessonDataModel.Validate();
|
||||
|
||||
var existingLesson = _dbContext.Lessons
|
||||
.Include(l => l.Student)
|
||||
.Include(l => l.Salary)
|
||||
.FirstOrDefault(l => l.Id == lessonDataModel.Id);
|
||||
|
||||
if (existingLesson == null)
|
||||
throw new KeyNotFoundException($"Lesson with id {lessonDataModel.Id} not found");
|
||||
|
||||
_mapper.Map(lessonDataModel, existingLesson);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public void DelElement(LessonDataModel lessonDataModel)
|
||||
{
|
||||
if (lessonDataModel == null)
|
||||
throw new ArgumentNullException(nameof(lessonDataModel));
|
||||
|
||||
var lessonEntity = _dbContext.Lessons.Find(lessonDataModel.Id);
|
||||
if (lessonEntity == null)
|
||||
throw new KeyNotFoundException($"Lesson with id {lessonDataModel.Id} not found");
|
||||
|
||||
_dbContext.Lessons.Remove(lessonEntity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using UniversityAllExpelled_DataBase.Models;
|
||||
using UniversityAllExpelled_Models.DataModels.Client;
|
||||
using UniversityAllExpelled_Models.StorageContracts;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Implementations
|
||||
{
|
||||
internal class PaymentStorageContract : IPaymentStorageContract
|
||||
{
|
||||
private readonly UniversityAllExpelledDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public PaymentStorageContract(UniversityAllExpelledDbContext dbContext, Mapper mapper)
|
||||
{
|
||||
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
|
||||
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
|
||||
}
|
||||
|
||||
public List<PaymentDataModel> GetList()
|
||||
{
|
||||
return _dbContext.Payments
|
||||
.AsNoTracking()
|
||||
.Include(p => p.Student)
|
||||
.Select(p => _mapper.Map<PaymentDataModel>(p))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public PaymentDataModel? GetElementById(string id)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(id))
|
||||
throw new ArgumentException("Id cannot be null or empty", nameof(id));
|
||||
|
||||
var paymentEntity = _dbContext.Payments
|
||||
.AsNoTracking()
|
||||
.Include(p => p.Student)
|
||||
.FirstOrDefault(p => p.Id == id);
|
||||
|
||||
return paymentEntity != null ? _mapper.Map<PaymentDataModel>(paymentEntity) : null;
|
||||
}
|
||||
|
||||
public List<PaymentDataModel>? GetListByStudentId(string studentId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(studentId))
|
||||
return null;
|
||||
|
||||
return _dbContext.Payments
|
||||
.AsNoTracking()
|
||||
.Include(p => p.Student)
|
||||
.Where(p => p.StudentsId == studentId)
|
||||
.Select(p => _mapper.Map<PaymentDataModel>(p))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<PaymentDataModel>? GetListByPaidOf(bool paidOf)
|
||||
{
|
||||
return _dbContext.Payments
|
||||
.AsNoTracking()
|
||||
.Include(p => p.Student)
|
||||
.Where(p => p.PaidOf == paidOf)
|
||||
.Select(p => _mapper.Map<PaymentDataModel>(p))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public void AddElement(PaymentDataModel paymentDataModel)
|
||||
{
|
||||
if (paymentDataModel == null)
|
||||
throw new ArgumentNullException(nameof(paymentDataModel));
|
||||
|
||||
paymentDataModel.Validate();
|
||||
|
||||
var paymentEntity = _mapper.Map<Payment>(paymentDataModel);
|
||||
_dbContext.Payments.Add(paymentEntity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public void UpdElement(PaymentDataModel paymentDataModel)
|
||||
{
|
||||
if (paymentDataModel == null)
|
||||
throw new ArgumentNullException(nameof(paymentDataModel));
|
||||
|
||||
paymentDataModel.Validate();
|
||||
|
||||
var existingPayment = _dbContext.Payments
|
||||
.Include(p => p.Student)
|
||||
.FirstOrDefault(p => p.Id == paymentDataModel.Id);
|
||||
|
||||
if (existingPayment == null)
|
||||
throw new KeyNotFoundException($"Payment with id {paymentDataModel.Id} not found");
|
||||
|
||||
_mapper.Map(paymentDataModel, existingPayment);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public void DelElement(PaymentDataModel paymentDataModel)
|
||||
{
|
||||
if (paymentDataModel == null)
|
||||
throw new ArgumentNullException(nameof(paymentDataModel));
|
||||
|
||||
var paymentEntity = _dbContext.Payments.Find(paymentDataModel.Id);
|
||||
if (paymentEntity == null)
|
||||
throw new KeyNotFoundException($"Payment with id {paymentDataModel.Id} not found");
|
||||
|
||||
_dbContext.Payments.Remove(paymentEntity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UniversityAllExpelled_DataBase.Models;
|
||||
using UniversityAllExpelled_Models.DataModels.Worker;
|
||||
using UniversityAllExpelled_Models.StorageContracts;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Implementations;
|
||||
|
||||
internal class SalaryStorageContract : ISalaryStorageContract
|
||||
{
|
||||
private readonly UniversityAllExpelledDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public SalaryStorageContract(UniversityAllExpelledDbContext dbContext, Mapper mapper)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public void AddElement(SalaryDataModel salaryDataModel)
|
||||
{
|
||||
if (salaryDataModel == null)
|
||||
throw new ArgumentNullException(nameof(salaryDataModel));
|
||||
|
||||
salaryDataModel.Validate();
|
||||
|
||||
var salaryEntity = _mapper.Map<Salary>(salaryDataModel);
|
||||
_dbContext.Salaries.Add(salaryEntity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public SalaryDataModel? GetElementById(string id)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(id))
|
||||
throw new ArgumentException("Id cannot be null or empty", nameof(id));
|
||||
|
||||
var salaryEntity = _dbContext.Salaries
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault(s => s.Id == id);
|
||||
|
||||
return salaryEntity != null ? _mapper.Map<SalaryDataModel>(salaryEntity) : null;
|
||||
}
|
||||
|
||||
public List<SalaryDataModel> GetList()
|
||||
{
|
||||
return _dbContext.Salaries
|
||||
.AsNoTracking()
|
||||
.Select(s => _mapper.Map<SalaryDataModel>(s))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<SalaryDataModel> GetListByTeacherId(string teacherId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(teacherId))
|
||||
return new List<SalaryDataModel>();
|
||||
|
||||
return _dbContext.Salaries
|
||||
.AsNoTracking()
|
||||
.Where(s => s.TeacherId == teacherId)
|
||||
.Select(s => _mapper.Map<SalaryDataModel>(s))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<SalaryDataModel> GetListByDate(DateTime date)
|
||||
{
|
||||
return _dbContext.Salaries
|
||||
.AsNoTracking()
|
||||
.Where(s => s.Date.Date == date.Date)
|
||||
.Select(s => _mapper.Map<SalaryDataModel>(s))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<SalaryDataModel> GetListByDateAndTeacherId(DateTime date, string teacherId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(teacherId))
|
||||
return new List<SalaryDataModel>();
|
||||
|
||||
return _dbContext.Salaries
|
||||
.AsNoTracking()
|
||||
.Where(s => s.Date.Date == date.Date && s.TeacherId == teacherId)
|
||||
.Select(s => _mapper.Map<SalaryDataModel>(s))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<SalaryDataModel> GetListByDateRange(DateTime startDate, DateTime endDate)
|
||||
{
|
||||
return _dbContext.Salaries
|
||||
.AsNoTracking()
|
||||
.Where(s => s.Date.Date >= startDate.Date && s.Date.Date <= endDate.Date)
|
||||
.Select(s => _mapper.Map<SalaryDataModel>(s))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<SalaryDataModel> GetListByDateRangeAndTeacherId(DateTime startDate, DateTime endDate, string teacherId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(teacherId))
|
||||
return new List<SalaryDataModel>();
|
||||
|
||||
return _dbContext.Salaries
|
||||
.AsNoTracking()
|
||||
.Where(s => s.Date.Date >= startDate.Date &&
|
||||
s.Date.Date <= endDate.Date &&
|
||||
s.TeacherId == teacherId)
|
||||
.Select(s => _mapper.Map<SalaryDataModel>(s))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<SalaryDataModel> GetListByAmountRange(double minAmount, double maxAmount)
|
||||
{
|
||||
return _dbContext.Salaries
|
||||
.AsNoTracking()
|
||||
.Where(s => s.Count >= minAmount && s.Count <= maxAmount)
|
||||
.Select(s => _mapper.Map<SalaryDataModel>(s))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<SalaryDataModel> GetListByAmountRangeAndTeacherId(double minAmount, double maxAmount, string teacherId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(teacherId))
|
||||
return new List<SalaryDataModel>();
|
||||
|
||||
return _dbContext.Salaries
|
||||
.AsNoTracking()
|
||||
.Where(s => s.Count >= minAmount &&
|
||||
s.Count <= maxAmount &&
|
||||
s.TeacherId == teacherId)
|
||||
.Select(s => _mapper.Map<SalaryDataModel>(s))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public void UpdElement(SalaryDataModel salaryDataModel)
|
||||
{
|
||||
if (salaryDataModel == null)
|
||||
throw new ArgumentNullException(nameof(salaryDataModel));
|
||||
|
||||
salaryDataModel.Validate();
|
||||
|
||||
var existingSalary = _dbContext.Salaries.Find(salaryDataModel.Id);
|
||||
if (existingSalary == null)
|
||||
throw new KeyNotFoundException($"Salary record with id {salaryDataModel.Id} not found");
|
||||
|
||||
_mapper.Map(salaryDataModel, existingSalary);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public void DelElement(SalaryDataModel salaryDataModel)
|
||||
{
|
||||
if (salaryDataModel == null)
|
||||
throw new ArgumentNullException(nameof(salaryDataModel));
|
||||
|
||||
var salaryEntity = _dbContext.Salaries.Find(salaryDataModel.Id);
|
||||
if (salaryEntity == null)
|
||||
throw new KeyNotFoundException($"Salary record with id {salaryDataModel.Id} not found");
|
||||
|
||||
_dbContext.Salaries.Remove(salaryEntity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using UniversityAllExpelled_DataBase.Models;
|
||||
using UniversityAllExpelled_Models.DataModels.Client;
|
||||
using UniversityAllExpelled_Models.Enums;
|
||||
using UniversityAllExpelled_Models.StorageContracts;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Implementations
|
||||
{
|
||||
internal class StudentStorageContract : IStudentStorageContract
|
||||
{
|
||||
private readonly UniversityAllExpelledDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public StudentStorageContract(UniversityAllExpelledDbContext dbContext, Mapper mapper)
|
||||
{
|
||||
_dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
|
||||
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
|
||||
}
|
||||
|
||||
public List<StudentDataModel> GetList()
|
||||
{
|
||||
return _dbContext.Students
|
||||
.AsNoTracking()
|
||||
.Include(s => s.User)
|
||||
.Select(s => _mapper.Map<StudentDataModel>(s))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public StudentDataModel? GetElementById(string id)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(id))
|
||||
throw new ArgumentException("Id cannot be null or empty", nameof(id));
|
||||
|
||||
var studentEntity = _dbContext.Students
|
||||
.AsNoTracking()
|
||||
.Include(s => s.User)
|
||||
.FirstOrDefault(s => s.Id == id);
|
||||
|
||||
return studentEntity != null ? _mapper.Map<StudentDataModel>(studentEntity) : null;
|
||||
}
|
||||
|
||||
public StudentDataModel? GetElementByUserId(string userId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(userId))
|
||||
throw new ArgumentException("UserId cannot be null or empty", nameof(userId));
|
||||
|
||||
var studentEntity = _dbContext.Students
|
||||
.AsNoTracking()
|
||||
.Include(s => s.User)
|
||||
.FirstOrDefault(s => s.UserId == userId);
|
||||
|
||||
return studentEntity != null ? _mapper.Map<StudentDataModel>(studentEntity) : null;
|
||||
}
|
||||
|
||||
public List<StudentDataModel>? GetListByFaculty(string nameFaculty)
|
||||
{
|
||||
if (!Enum.TryParse(nameFaculty, out FacultyType facultyType))
|
||||
return null;
|
||||
|
||||
return _dbContext.Students
|
||||
.AsNoTracking()
|
||||
.Include(s => s.User)
|
||||
.Where(s => s.Faculty == facultyType)
|
||||
.Select(s => _mapper.Map<StudentDataModel>(s))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<StudentDataModel>? GetListByGroop(string groop)
|
||||
{
|
||||
if (!Enum.TryParse(groop, out GroopType groopType))
|
||||
return null;
|
||||
|
||||
return _dbContext.Students
|
||||
.AsNoTracking()
|
||||
.Include(s => s.User)
|
||||
.Where(s => s.Groop == groopType)
|
||||
.Select(s => _mapper.Map<StudentDataModel>(s))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<StudentDataModel>? GetListByCource(int cource)
|
||||
{
|
||||
if (cource < 1 || cource > 6)
|
||||
return null;
|
||||
|
||||
return _dbContext.Students
|
||||
.AsNoTracking()
|
||||
.Include(s => s.User)
|
||||
.Where(s => s.Course == cource)
|
||||
.Select(s => _mapper.Map<StudentDataModel>(s))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public void AddElement(StudentDataModel studentDataModel)
|
||||
{
|
||||
if (studentDataModel == null)
|
||||
throw new ArgumentNullException(nameof(studentDataModel));
|
||||
|
||||
studentDataModel.Validate();
|
||||
|
||||
var studentEntity = _mapper.Map<Student>(studentDataModel);
|
||||
_dbContext.Students.Add(studentEntity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public void UpdElement(StudentDataModel studentDataModel)
|
||||
{
|
||||
if (studentDataModel == null)
|
||||
throw new ArgumentNullException(nameof(studentDataModel));
|
||||
|
||||
studentDataModel.Validate();
|
||||
|
||||
var existingStudent = _dbContext.Students
|
||||
.Include(s => s.User)
|
||||
.FirstOrDefault(s => s.Id == studentDataModel.Id);
|
||||
|
||||
if (existingStudent == null)
|
||||
throw new KeyNotFoundException($"Student with id {studentDataModel.Id} not found");
|
||||
|
||||
_mapper.Map(studentDataModel, existingStudent);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using UniversityAllExpelled_DataBase.Models;
|
||||
using UniversityAllExpelled_Models.DataModels.Worker;
|
||||
using UniversityAllExpelled_Models.Enums;
|
||||
using UniversityAllExpelled_Models.StorageContracts;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Implementations;
|
||||
|
||||
internal class TeacherStorageContract : ITeacherStorageContract
|
||||
{
|
||||
private readonly UniversityAllExpelledDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
public TeacherStorageContract(UniversityAllExpelledDbContext dbContext, Mapper mapper)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public void AddElement(TeacherDataModel teacherDataModel)
|
||||
{
|
||||
if (teacherDataModel == null)
|
||||
throw new ArgumentNullException(nameof(teacherDataModel));
|
||||
|
||||
teacherDataModel.Validate();
|
||||
|
||||
var teacherEntity = _mapper.Map<Teacher>(teacherDataModel);
|
||||
_dbContext.Teachers.Add(teacherEntity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public TeacherDataModel? GetElementById(string id)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(id))
|
||||
throw new ArgumentException("Id cannot be null or empty", nameof(id));
|
||||
|
||||
var teacherEntity = _dbContext.Teachers
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault(t => t.Id == id);
|
||||
|
||||
return teacherEntity != null ? _mapper.Map<TeacherDataModel>(teacherEntity) : null;
|
||||
}
|
||||
|
||||
public List<TeacherDataModel> GetList()
|
||||
{
|
||||
return _dbContext.Teachers
|
||||
.AsNoTracking()
|
||||
.Select(t => _mapper.Map<TeacherDataModel>(t))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<TeacherDataModel> GetListByPosition(TeacherPositionType position)
|
||||
{
|
||||
if (position == TeacherPositionType.None)
|
||||
return new List<TeacherDataModel>();
|
||||
|
||||
return _dbContext.Teachers
|
||||
.AsNoTracking()
|
||||
.Where(t => t.Position == position)
|
||||
.Select(t => _mapper.Map<TeacherDataModel>(t))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<TeacherDataModel> GetListByDateHiring(DateTime dateHiring)
|
||||
{
|
||||
return _dbContext.Teachers
|
||||
.AsNoTracking()
|
||||
.Where(t => t.DateHiring.Date == dateHiring.Date)
|
||||
.Select(t => _mapper.Map<TeacherDataModel>(t))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public void UpdElement(TeacherDataModel teacherDataModel)
|
||||
{
|
||||
if (teacherDataModel == null)
|
||||
throw new ArgumentNullException(nameof(teacherDataModel));
|
||||
|
||||
teacherDataModel.Validate();
|
||||
|
||||
var existingTeacher = _dbContext.Teachers.Find(teacherDataModel.Id);
|
||||
if (existingTeacher == null)
|
||||
throw new KeyNotFoundException($"Teacher with id {teacherDataModel.Id} not found");
|
||||
|
||||
_mapper.Map(teacherDataModel, existingTeacher);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public TeacherDataModel? GetElementByUserId(string userId)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(userId))
|
||||
return null;
|
||||
|
||||
var teacherEntity = _dbContext.Teachers
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault(t => t.Userid == userId);
|
||||
|
||||
return teacherEntity != null ? _mapper.Map<TeacherDataModel>(teacherEntity) : null;
|
||||
}
|
||||
|
||||
public List<TeacherDataModel> GetListByHireDateRange(DateTime startDate, DateTime endDate)
|
||||
{
|
||||
if (startDate > endDate)
|
||||
return new List<TeacherDataModel>();
|
||||
|
||||
return _dbContext.Teachers
|
||||
.AsNoTracking()
|
||||
.Where(t => t.DateHiring >= startDate && t.DateHiring <= endDate)
|
||||
.Select(t => _mapper.Map<TeacherDataModel>(t))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public void DelElement(TeacherDataModel teacherDataModel)
|
||||
{
|
||||
if (teacherDataModel == null)
|
||||
throw new ArgumentNullException(nameof(teacherDataModel));
|
||||
|
||||
var teacherEntity = _dbContext.Teachers.Find(teacherDataModel.Id);
|
||||
if (teacherEntity == null)
|
||||
throw new KeyNotFoundException($"Teacher with id {teacherDataModel.Id} not found");
|
||||
|
||||
_dbContext.Teachers.Remove(teacherEntity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,134 @@
|
||||
using AutoMapper;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using UniversityAllExpelled_DataBase.Models;
|
||||
using UniversityAllExpelled_Models.DataModels;
|
||||
using UniversityAllExpelled_Models.Enums;
|
||||
using UniversityAllExpelled_Models.StorageContracts;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Implementations;
|
||||
|
||||
internal class UserStorageContract : IUserStorageContract
|
||||
{
|
||||
private readonly UniversityAllExpelledDbContext _dbContext;
|
||||
private readonly Mapper _mapper;
|
||||
|
||||
|
||||
public UserStorageContract(UniversityAllExpelledDbContext dbContext, Mapper mapper)
|
||||
{
|
||||
_dbContext = dbContext;
|
||||
_mapper = mapper;
|
||||
}
|
||||
|
||||
public void AddElement(UserDataModel userDataModel)
|
||||
{
|
||||
if (userDataModel == null)
|
||||
throw new ArgumentNullException(nameof(userDataModel));
|
||||
|
||||
userDataModel.Validate();
|
||||
|
||||
var userEntity = _mapper.Map<User>(userDataModel);
|
||||
_dbContext.Users.Add(userEntity);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
|
||||
public UserDataModel? GetElementById(string id)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(id))
|
||||
throw new ArgumentException("Id cannot be null or empty", nameof(id));
|
||||
|
||||
var userEntity = _dbContext.Users
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault(u => u.Id == id);
|
||||
|
||||
return userEntity != null ? _mapper.Map<UserDataModel>(userEntity) : null;
|
||||
}
|
||||
|
||||
public UserDataModel? GetElementByPhoneNomber(string phoneNomber)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(phoneNomber))
|
||||
throw new ArgumentException("Phone number cannot be null or empty", nameof(phoneNomber));
|
||||
|
||||
var userEntity = _dbContext.Users
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault(u => u.PhoneNomber == phoneNomber);
|
||||
|
||||
return userEntity != null ? _mapper.Map<UserDataModel>(userEntity) : null;
|
||||
}
|
||||
|
||||
public List<UserDataModel> GetList()
|
||||
{
|
||||
return _dbContext.Users
|
||||
.AsNoTracking()
|
||||
.Select(u => _mapper.Map<UserDataModel>(u))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<UserDataModel>? GetListByBirthDate(DateTime date)
|
||||
{
|
||||
return _dbContext.Users
|
||||
.AsNoTracking()
|
||||
.Where(u => u.BirthDate.Date == date.Date)
|
||||
.Select(u => _mapper.Map<UserDataModel>(u))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<UserDataModel>? GetListByFIO(string fio)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(fio))
|
||||
return null;
|
||||
|
||||
return _dbContext.Users
|
||||
.AsNoTracking()
|
||||
.Where(u => u.FIO.Contains(fio))
|
||||
.Select(u => _mapper.Map<UserDataModel>(u))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public List<UserDataModel>? GetListByIsDeleted(bool isDeleted)
|
||||
{
|
||||
return _dbContext.Users
|
||||
.AsNoTracking()
|
||||
.Where(u => u.IsDeleted == isDeleted)
|
||||
.Select(u => _mapper.Map<UserDataModel>(u))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public UserDataModel? GetListByLogin(string login)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(login))
|
||||
throw new ArgumentException("Login cannot be null or empty", nameof(login));
|
||||
|
||||
var userEntity = _dbContext.Users
|
||||
.AsNoTracking()
|
||||
.FirstOrDefault(u => u.Login == login);
|
||||
|
||||
return userEntity != null ? _mapper.Map<UserDataModel>(userEntity) : null;
|
||||
}
|
||||
|
||||
public List<UserDataModel>? GetListByRole(string role)
|
||||
{
|
||||
if (!Enum.TryParse(role, out SystemRoleType roleType))
|
||||
return null;
|
||||
|
||||
return _dbContext.Users
|
||||
.AsNoTracking()
|
||||
.Where(u => u.Role == roleType)
|
||||
.Select(u => _mapper.Map<UserDataModel>(u))
|
||||
.ToList();
|
||||
}
|
||||
|
||||
public void UpdElement(UserDataModel userDataModel)
|
||||
{
|
||||
if (userDataModel == null)
|
||||
throw new ArgumentNullException(nameof(userDataModel));
|
||||
|
||||
userDataModel.Validate();
|
||||
|
||||
var existingUser = _dbContext.Users.Find(userDataModel.Id);
|
||||
if (existingUser == null)
|
||||
throw new KeyNotFoundException($"User with id {userDataModel.Id} not found");
|
||||
|
||||
_mapper.Map(userDataModel, existingUser);
|
||||
_dbContext.SaveChanges();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UniversityAllExpelled_Models.DataModels.Worker;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Models;
|
||||
internal class Education : EducationDataModel
|
||||
{
|
||||
public Education(string id, string teacherId, float amount, DateTime startDate, DateTime endDate, bool active)
|
||||
: base(id, teacherId, amount, startDate, endDate, active) { }
|
||||
|
||||
protected Education()
|
||||
: base(string.Empty, string.Empty, 0f, DateTime.MinValue, DateTime.MinValue, false) { }
|
||||
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public new string Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public new string TeacherId { get; set; }
|
||||
|
||||
[Required]
|
||||
[Range(0.01, float.MaxValue)]
|
||||
public new float Amount { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "date")]
|
||||
public new DateTime StartDate { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "date")]
|
||||
public new DateTime EndDate { get; set; }
|
||||
|
||||
[Required]
|
||||
public new bool Active { get; set; }
|
||||
|
||||
|
||||
public Teacher? Teacher { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UniversityAllExpelled_Models.DataModels.Worker;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Models;
|
||||
|
||||
internal class EducationLessons : EducationLessonsDataModel
|
||||
{
|
||||
public EducationLessons(string educationId, string lessonsId)
|
||||
: base(educationId, lessonsId) { }
|
||||
|
||||
protected EducationLessons()
|
||||
: base(string.Empty, string.Empty) { }
|
||||
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public new string EducationId { get; set; }
|
||||
|
||||
[Required]
|
||||
public new string LessonsId { get; set; }
|
||||
|
||||
public Education? Education { get; set; }
|
||||
public Lesson? Lesson { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UniversityAllExpelled_Models.DataModels.Worker;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Models;
|
||||
|
||||
internal class Salary : SalaryDataModel
|
||||
{
|
||||
public Salary(string id, string teacherId, double count, DateTime date)
|
||||
: base(id, teacherId, count, date) { }
|
||||
|
||||
protected Salary()
|
||||
: base(string.Empty, string.Empty, 0, DateTime.MinValue) { }
|
||||
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public new string Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public new string TeacherId { get; set; }
|
||||
|
||||
[Required]
|
||||
[Range(0.01, double.MaxValue)]
|
||||
public new double Count { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "date")]
|
||||
public new DateTime Date { get; set; }
|
||||
|
||||
[ForeignKey("SalaryId")]
|
||||
public Lesson? Lesson { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using UniversityAllExpelled_Models.Enums;
|
||||
using UniversityAllExpelled_Models.DataModels.Worker;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Models;
|
||||
|
||||
internal class Teacher : TeacherDataModel
|
||||
{
|
||||
public Teacher(string id, TeacherPositionType position, string userid, DateTime dateHiring)
|
||||
: base(id, position, userid, dateHiring) { }
|
||||
|
||||
protected Teacher()
|
||||
: base(string.Empty, TeacherPositionType.None, string.Empty, DateTime.MinValue) { }
|
||||
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public new string Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public new TeacherPositionType Position { get; set; }
|
||||
|
||||
[Required]
|
||||
public new string Userid { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "date")]
|
||||
public new DateTime DateHiring { get; set; }
|
||||
|
||||
[ForeignKey("TeacherId")]
|
||||
public List<Education> Educations { get; set; }
|
||||
public User? User { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using UniversityAllExpelled_Models.DataModels;
|
||||
using UniversityAllExpelled_Models.Enums;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Models;
|
||||
|
||||
internal class User : UserDataModel
|
||||
{
|
||||
public User(string id, string login, string password, SystemRoleType role, string fio, DateTime birthdate,
|
||||
string phoneNomber, string email, bool isDeleted) : base(id, login, password, role, fio, birthdate,
|
||||
phoneNomber, email, isDeleted) {}
|
||||
protected User()
|
||||
: base(string.Empty, string.Empty, string.Empty, SystemRoleType.None,
|
||||
string.Empty, DateTime.MinValue, string.Empty, string.Empty, false){}
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public new string Id { get; set; }
|
||||
|
||||
[Required]
|
||||
[StringLength(50)]
|
||||
public new string Login { get; set; }
|
||||
|
||||
[Required]
|
||||
[StringLength(100)]
|
||||
public new string Password { get; set; }
|
||||
|
||||
[Required]
|
||||
public new SystemRoleType Role { get; set; }
|
||||
|
||||
[Required]
|
||||
[StringLength(150)]
|
||||
public new string FIO { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "date")]
|
||||
public new DateTime BirthDate { get; set; }
|
||||
|
||||
[Required]
|
||||
[StringLength(20)]
|
||||
public new string PhoneNomber { get; set; }
|
||||
|
||||
[Required]
|
||||
[StringLength(100)]
|
||||
public new string Email { get; set; }
|
||||
|
||||
[Required]
|
||||
public new bool IsDeleted { get; set; }
|
||||
|
||||
[ForeignKey("UserId")]
|
||||
public List<Student> Students { get; set; }
|
||||
|
||||
[ForeignKey("UserId")]
|
||||
public List<Teacher> Teachers { get; set; }
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,105 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using UniversityAllExpelled_DataBase.Models;
|
||||
using UniversityAllExpelled_Models.Infrostructure;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase;
|
||||
|
||||
internal class UniversityAllExpelledDbContext : DbContext
|
||||
{
|
||||
private readonly IConfigurationDatabase _configurationDatabase;
|
||||
|
||||
public UniversityAllExpelledDbContext(IConfigurationDatabase configurationDatabase)
|
||||
{
|
||||
_configurationDatabase = configurationDatabase ?? throw new ArgumentNullException(nameof(configurationDatabase));
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
optionsBuilder.UseNpgsql(_configurationDatabase.ConnectionString, o =>
|
||||
{
|
||||
o.SetPostgresVersion(12, 2);
|
||||
o.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
|
||||
});
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
// Настройка User
|
||||
modelBuilder.Entity<User>()
|
||||
.HasIndex(u => u.Login)
|
||||
.IsUnique();
|
||||
|
||||
modelBuilder.Entity<User>()
|
||||
.HasIndex(u => u.Email)
|
||||
.IsUnique();
|
||||
|
||||
modelBuilder.Entity<User>()
|
||||
.HasIndex(u => u.PhoneNomber)
|
||||
.IsUnique();
|
||||
|
||||
// Настройка Student
|
||||
modelBuilder.Entity<Student>()
|
||||
.HasIndex(s => s.UserId)
|
||||
.IsUnique();
|
||||
|
||||
// Настройка Teacher
|
||||
modelBuilder.Entity<Teacher>()
|
||||
.HasIndex(t => t.Userid)
|
||||
.IsUnique();
|
||||
|
||||
// Настройка Lesson
|
||||
modelBuilder.Entity<Lesson>()
|
||||
.HasIndex(l => new { l.StudentId, l.StartDate, l.EndDate });
|
||||
|
||||
// Настройка Payment
|
||||
modelBuilder.Entity<Payment>()
|
||||
.HasIndex(p => new { p.StudentsId, p.PaidOf })
|
||||
.HasFilter($"\"{nameof(Payment.PaidOf)}\" = FALSE");
|
||||
|
||||
// Настройка Education
|
||||
modelBuilder.Entity<Education>()
|
||||
.HasIndex(e => new { e.TeacherId, e.Active })
|
||||
.HasFilter($"\"{nameof(Education.Active)}\" = TRUE");
|
||||
|
||||
// Настройка связей
|
||||
modelBuilder.Entity<Student>()
|
||||
.HasOne(s => s.User)
|
||||
.WithMany()
|
||||
.HasForeignKey(s => s.UserId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
modelBuilder.Entity<Teacher>()
|
||||
.HasOne(t => t.User)
|
||||
.WithMany()
|
||||
.HasForeignKey(t => t.Userid)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
modelBuilder.Entity<Payment>()
|
||||
.HasOne(p => p.Student)
|
||||
.WithMany()
|
||||
.HasForeignKey(p => p.StudentsId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
modelBuilder.Entity<Lesson>()
|
||||
.HasOne(l => l.Student)
|
||||
.WithMany()
|
||||
.HasForeignKey(l => l.StudentId)
|
||||
.OnDelete(DeleteBehavior.Restrict);
|
||||
|
||||
// Настройка EducationLessons (если используется many-to-many)
|
||||
modelBuilder.Entity<EducationLessons>()
|
||||
.HasKey(el => new { el.EducationId, el.LessonsId });
|
||||
}
|
||||
|
||||
public DbSet<User> Users { get; set; }
|
||||
public DbSet<Student> Students { get; set; }
|
||||
public DbSet<Teacher> Teachers { get; set; }
|
||||
public DbSet<Payment> Payments { get; set; }
|
||||
public DbSet<Lesson> Lessons { get; set; }
|
||||
public DbSet<Salary> Salaries { get; set; }
|
||||
public DbSet<Education> Educations { get; set; }
|
||||
public DbSet<EducationLessons> EducationLessons { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="14.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="9.0.4" />
|
||||
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="9.0.4" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\UniversityAllExpelled_Models\UniversityAllExpelled_Models.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UniversityAllExpelled_Models.DataModels.Worker;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Models;
|
||||
internal class Education : EducationDataModel
|
||||
{
|
||||
public Education(string id, string teacherId, float amount, DateTime startDate, DateTime endDate, bool active)
|
||||
: base(id, teacherId, amount, startDate, endDate, active) { }
|
||||
|
||||
protected Education()
|
||||
: base(string.Empty, string.Empty, 0f, DateTime.MinValue, DateTime.MinValue, false) { }
|
||||
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public new string Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public new string TeacherId { get; set; }
|
||||
|
||||
[Required]
|
||||
[Range(0.01, float.MaxValue)]
|
||||
public new float Amount { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "date")]
|
||||
public new DateTime StartDate { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "date")]
|
||||
public new DateTime EndDate { get; set; }
|
||||
|
||||
[Required]
|
||||
public new bool Active { get; set; }
|
||||
|
||||
|
||||
public Teacher? Teacher { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UniversityAllExpelled_Models.DataModels.Worker;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Models;
|
||||
|
||||
internal class EducationLessons : EducationLessonsDataModel
|
||||
{
|
||||
public EducationLessons(string educationId, string lessonsId)
|
||||
: base(educationId, lessonsId) { }
|
||||
|
||||
protected EducationLessons()
|
||||
: base(string.Empty, string.Empty) { }
|
||||
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public int Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public new string EducationId { get; set; }
|
||||
|
||||
[Required]
|
||||
public new string LessonsId { get; set; }
|
||||
|
||||
public Education? Education { get; set; }
|
||||
public Lesson? Lesson { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using UniversityAllExpelled_Models.DataModels.Worker;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Models;
|
||||
|
||||
[Table("Lessons")]
|
||||
internal class Lesson : LessonDataModel
|
||||
{
|
||||
// Конструктор для EF Core
|
||||
protected Lesson()
|
||||
: base(string.Empty, string.Empty, 0, DateTime.MinValue, DateTime.MinValue,
|
||||
string.Empty, string.Empty, false)
|
||||
{
|
||||
}
|
||||
|
||||
public Lesson(string id, string name, double price, DateTime startDate,
|
||||
DateTime endDate, string studentId, string salaryId, bool isActive)
|
||||
: base(id, name, price, startDate, endDate, studentId, salaryId, isActive)
|
||||
{
|
||||
}
|
||||
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public new string Id { get; set; }
|
||||
|
||||
[Required]
|
||||
[StringLength(100)]
|
||||
public new string Name { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "decimal(18,2)")]
|
||||
[Range(0.01, double.MaxValue)]
|
||||
public new double Price { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "datetime2")]
|
||||
public new DateTime StartDate { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "datetime2")]
|
||||
public new DateTime EndDate { get; set; }
|
||||
|
||||
[Required]
|
||||
[ForeignKey(nameof(Student))]
|
||||
public new string StudentId { get; set; }
|
||||
|
||||
[ForeignKey(nameof(Salary))]
|
||||
public new string SalaryId { get; set; }
|
||||
|
||||
[Required]
|
||||
public new bool IsActive { get; set; }
|
||||
|
||||
// Навигационные свойства
|
||||
public Student? Student { get; set; }
|
||||
public Salary? Salary { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using UniversityAllExpelled_Models.DataModels.Client;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Models
|
||||
{
|
||||
[Table("Payments")]
|
||||
internal class Payment : PaymentDataModel
|
||||
{
|
||||
protected Payment()
|
||||
: base(string.Empty, string.Empty, 0, 0, DateTime.Now, false)
|
||||
{
|
||||
}
|
||||
|
||||
public Payment(string id, string studentId, double paidAmount, double arrears,DateTime dateOfpayment, bool paidOf)
|
||||
: base(id, studentId, paidAmount, arrears, dateOfpayment, paidOf)
|
||||
{
|
||||
}
|
||||
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public new string Id { get; set; }
|
||||
|
||||
[Required]
|
||||
[ForeignKey(nameof(Student))]
|
||||
public new string StudentsId { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "decimal(18,2)")]
|
||||
[Range(0, double.MaxValue)]
|
||||
public new double PaidAmount { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "decimal(18,2)")]
|
||||
[Range(0, double.MaxValue)]
|
||||
public new double Arrears { get; set; }
|
||||
|
||||
[Required]
|
||||
public new DateTime DateOfpayment { get; set; }
|
||||
|
||||
[Required]
|
||||
public new bool PaidOf { get; set; }
|
||||
|
||||
public virtual Student? Student { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UniversityAllExpelled_Models.DataModels.Worker;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Models;
|
||||
|
||||
internal class Salary : SalaryDataModel
|
||||
{
|
||||
public Salary(string id, string teacherId, double count, DateTime date)
|
||||
: base(id, teacherId, count, date) { }
|
||||
|
||||
protected Salary()
|
||||
: base(string.Empty, string.Empty, 0, DateTime.MinValue) { }
|
||||
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public new string Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public new string TeacherId { get; set; }
|
||||
|
||||
[Required]
|
||||
[Range(0.01, double.MaxValue)]
|
||||
public new double Count { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "date")]
|
||||
public new DateTime Date { get; set; }
|
||||
|
||||
[ForeignKey("SalaryId")]
|
||||
public Lesson? Lesson { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using UniversityAllExpelled_Models.DataModels.Client;
|
||||
using UniversityAllExpelled_Models.Enums;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Models;
|
||||
|
||||
[Table("Students")]
|
||||
internal class Student : StudentDataModel
|
||||
{
|
||||
// Конструктор для Entity Framework
|
||||
protected Student()
|
||||
: base(string.Empty, string.Empty, FacultyType.None, GroopType.None, 0)
|
||||
{
|
||||
}
|
||||
|
||||
public Student(string id, string userId, FacultyType faculty, GroopType groop, int course)
|
||||
: base(id, userId, faculty, groop, course)
|
||||
{
|
||||
}
|
||||
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public new string Id { get; set; }
|
||||
|
||||
[Required]
|
||||
[ForeignKey(nameof(User))]
|
||||
public new string UserId { get; set; }
|
||||
|
||||
[Required]
|
||||
public new FacultyType Faculty { get; set; }
|
||||
|
||||
[Required]
|
||||
public new GroopType Groop { get; set; }
|
||||
|
||||
[Required]
|
||||
[Range(1, 6)]
|
||||
public new int Course { get; set; }
|
||||
|
||||
|
||||
[ForeignKey("StudentId")]
|
||||
public List<Payment> Payment { get; set; }
|
||||
public User? User { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using UniversityAllExpelled_Models.Enums;
|
||||
using UniversityAllExpelled_Models.DataModels.Worker;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Models;
|
||||
|
||||
internal class Teacher : TeacherDataModel
|
||||
{
|
||||
public Teacher(string id, TeacherPositionType position, string userid, DateTime dateHiring)
|
||||
: base(id, position, userid, dateHiring) { }
|
||||
|
||||
protected Teacher()
|
||||
: base(string.Empty, TeacherPositionType.None, string.Empty, DateTime.MinValue) { }
|
||||
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public new string Id { get; set; }
|
||||
|
||||
[Required]
|
||||
public new TeacherPositionType Position { get; set; }
|
||||
|
||||
[Required]
|
||||
public new string Userid { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "date")]
|
||||
public new DateTime DateHiring { get; set; }
|
||||
|
||||
[ForeignKey("TeacherId")]
|
||||
public List<Education> Educations { get; set; }
|
||||
public User? User { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using UniversityAllExpelled_Models.DataModels;
|
||||
using UniversityAllExpelled_Models.Enums;
|
||||
|
||||
namespace UniversityAllExpelled_DataBase.Models;
|
||||
|
||||
internal class User : UserDataModel
|
||||
{
|
||||
public User(string id, string login, string password, SystemRoleType role, string fio, DateTime birthdate,
|
||||
string phoneNomber, string email, bool isDeleted) : base(id, login, password, role, fio, birthdate,
|
||||
phoneNomber, email, isDeleted) {}
|
||||
protected User()
|
||||
: base(string.Empty, string.Empty, string.Empty, SystemRoleType.None,
|
||||
string.Empty, DateTime.MinValue, string.Empty, string.Empty, false){}
|
||||
[Key]
|
||||
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
|
||||
public new string Id { get; set; }
|
||||
|
||||
[Required]
|
||||
[StringLength(50)]
|
||||
public new string Login { get; set; }
|
||||
|
||||
[Required]
|
||||
[StringLength(100)]
|
||||
public new string Password { get; set; }
|
||||
|
||||
[Required]
|
||||
public new SystemRoleType Role { get; set; }
|
||||
|
||||
[Required]
|
||||
[StringLength(150)]
|
||||
public new string FIO { get; set; }
|
||||
|
||||
[Required]
|
||||
[Column(TypeName = "date")]
|
||||
public new DateTime BirthDate { get; set; }
|
||||
|
||||
[Required]
|
||||
[StringLength(20)]
|
||||
public new string PhoneNomber { get; set; }
|
||||
|
||||
[Required]
|
||||
[StringLength(100)]
|
||||
public new string Email { get; set; }
|
||||
|
||||
[Required]
|
||||
public new bool IsDeleted { get; set; }
|
||||
|
||||
[ForeignKey("UserId")]
|
||||
public List<Student> Students { get; set; }
|
||||
|
||||
[ForeignKey("UserId")]
|
||||
public List<Teacher> Teachers { get; set; }
|
||||
|
||||
}
|
||||
|
||||
@@ -7,17 +7,30 @@ interface ISalaryBusinessLogicContract
|
||||
List<SalaryDataModel> GetAllSalaries();
|
||||
SalaryDataModel? GetSalaryById(string salaryId);
|
||||
|
||||
List<SalaryDataModel> GetSalariesByTeacher(string teacherId);
|
||||
|
||||
List<SalaryDataModel> GetSalariesByDate(DateTime date);
|
||||
List<SalaryDataModel> GetSalariesByDateAndTeacher(DateTime date, string teacherId);
|
||||
|
||||
List<SalaryDataModel> GetSalariesByDateRange(DateTime startDate, DateTime endDate);
|
||||
List<SalaryDataModel> GetSalariesByDateRangeAndTeacher(DateTime startDate, DateTime endDate, string teacherId);
|
||||
|
||||
List<SalaryDataModel> GetSalariesAboveAmount(double amount);
|
||||
List<SalaryDataModel> GetSalariesAboveAmountForTeacher(double amount, string teacherId);
|
||||
|
||||
List<SalaryDataModel> GetSalariesBelowAmount(double amount);
|
||||
List<SalaryDataModel> GetSalariesBelowAmountForTeacher(double amount, string teacherId);
|
||||
|
||||
List<SalaryDataModel> GetSalariesInAmountRange(double minAmount, double maxAmount);
|
||||
List<SalaryDataModel> GetSalariesInAmountRangeForTeacher(double minAmount, double maxAmount, string teacherId);
|
||||
|
||||
void CreateSalary(SalaryDataModel salaryDataModel);
|
||||
void UpdateSalary(SalaryDataModel salaryDataModel);
|
||||
void DeleteSalary(string id);
|
||||
|
||||
double CalculateTotalSalariesForPeriod(DateTime startDate, DateTime endDate);
|
||||
double CalculateTotalSalariesForTeacherAndPeriod(string teacherId, DateTime startDate, DateTime endDate);
|
||||
|
||||
double CalculateAverageSalaryForPeriod(DateTime startDate, DateTime endDate);
|
||||
double CalculateAverageSalaryForTeacherAndPeriod(string teacherId, DateTime startDate, DateTime endDate);
|
||||
}
|
||||
@@ -15,7 +15,7 @@ interface IStudentBusinessLogicContract
|
||||
List<StudentDataModel> GetAllStudentsByCource(int Cource, SystemRoleType role = SystemRoleType.student, bool IsDeleted = false);
|
||||
StudentDataModel? GetStudentByData(string data,SystemRoleType role = SystemRoleType.student, bool IsDeleted = false);
|
||||
|
||||
void InsertStudent(StudentDataModel studentDataModel);
|
||||
void InsertStudent(StudentDataModel studentDataModel);
|
||||
void UpdateStudent(StudentDataModel studentDataModel);
|
||||
void RestoreStudent(string Id);
|
||||
}
|
||||
|
||||
@@ -1,17 +1,17 @@
|
||||
|
||||
using UniversityAllExpelled_Models.Exceptions;
|
||||
using UniversityAllExpelled_Models.Exceptions;
|
||||
using UniversityAllExpelled_Models.Extensions;
|
||||
using UniversityAllExpelled_Models.Infrostructure;
|
||||
|
||||
namespace UniversityAllExpelled_Models.DataModels.Client;
|
||||
|
||||
|
||||
public class PaymentDataModel(string id, string studentId, double paidAmount, double arrears, bool paidOf) : IValidation
|
||||
public class PaymentDataModel(string id, string studentId, double paidAmount, double arrears,DateTime dateOfpayment, bool paidOf) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
public string StudentsId { get; private set; } = studentId;
|
||||
public double PaidAmount { get; private set; } = paidAmount;
|
||||
public double Arrears { get; private set; } = arrears;
|
||||
public DateTime DateOfpayment { get; private set; } = DateTime.Now;
|
||||
public bool PaidOf { get; private set; } = paidOf;
|
||||
|
||||
public void Validate()
|
||||
|
||||
@@ -1,30 +1,319 @@
|
||||
using UniversityAllExpelled_Models.Exceptions;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using UniversityAllExpelled_Models.BusinessLogicContracts;
|
||||
using UniversityAllExpelled_Models.DataModels.Client;
|
||||
using UniversityAllExpelled_Models.Enums;
|
||||
using UniversityAllExpelled_Models.Exceptions;
|
||||
using UniversityAllExpelled_Models.Extensions;
|
||||
using UniversityAllExpelled_Models.Infrostructure;
|
||||
using UniversityAllExpelled_Models.StorageContracts;
|
||||
using System.Text.Json;
|
||||
using System.Data;
|
||||
|
||||
namespace UniversityAllExpelled_Models.DataModels.Client;
|
||||
|
||||
public class PaymentLessonDataModel(string paymentId, string lessonId) : IValidation
|
||||
namespace UniversityAllExpelled_BusinessLogic.Implementations
|
||||
{
|
||||
public string PaymentId { get; private set; }
|
||||
public string LessonId { get; private set; }
|
||||
public void Validate()
|
||||
internal class StudentBusinessLogicContract : IStudentBusinessLogicContract
|
||||
{
|
||||
if (PaymentId.IsEmpty())
|
||||
private readonly IStudentStorageContract _studentStorage;
|
||||
private readonly IUserStorageContract _userStorage;
|
||||
private readonly ILogger<StudentBusinessLogicContract> _logger;
|
||||
|
||||
public StudentBusinessLogicContract(
|
||||
IStudentStorageContract studentStorage,
|
||||
IUserStorageContract userStorage,
|
||||
ILogger<StudentBusinessLogicContract> logger)
|
||||
{
|
||||
throw new ValidationException("Field PaymentId is empty.");
|
||||
_studentStorage = studentStorage ?? throw new ArgumentNullException(nameof(studentStorage));
|
||||
_userStorage = userStorage ?? throw new ArgumentNullException(nameof(userStorage));
|
||||
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||
}
|
||||
if (!PaymentId.IsGuid())
|
||||
|
||||
public List<StudentDataModel> GetAllStudents(SystemRoleType role = SystemRoleType.student, bool IsDeleted = false)
|
||||
{
|
||||
throw new ValidationException("The value in the field PaymentId is not a valid GUID.");
|
||||
_logger.LogInformation("Getting all students with role: {Role}, IsDeleted: {IsDeleted}", role, IsDeleted);
|
||||
|
||||
try
|
||||
{
|
||||
var students = _studentStorage.GetList() ?? throw new DataException("Failed to retrieve students list");
|
||||
|
||||
var result = new List<StudentDataModel>();
|
||||
|
||||
foreach (var student in students)
|
||||
{
|
||||
var user = _userStorage.GetElementById(student.UserId);
|
||||
if (user != null && user.Role == role && user.IsDeleted == IsDeleted)
|
||||
{
|
||||
result.Add(student);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting all students");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
if (LessonId.IsEmpty())
|
||||
|
||||
public List<StudentDataModel> GetAllStudentsByBirthDate(DateTime startDate, DateTime endDate,
|
||||
SystemRoleType role = SystemRoleType.student, bool IsDeleted = false)
|
||||
{
|
||||
throw new ValidationException("Field LessonId is empty.");
|
||||
_logger.LogInformation(
|
||||
"Getting students by birth date from {StartDate} to {EndDate}, role: {Role}, IsDeleted: {IsDeleted}",
|
||||
startDate, endDate, role, IsDeleted);
|
||||
|
||||
if (startDate > endDate)
|
||||
throw new ValidationException("Start date cannot be greater than end date");
|
||||
|
||||
try
|
||||
{
|
||||
var students = _studentStorage.GetList() ?? throw new DataException("Failed to retrieve students list");
|
||||
|
||||
var result = new List<StudentDataModel>();
|
||||
|
||||
foreach (var student in students)
|
||||
{
|
||||
var user = _userStorage.GetElementById(student.UserId);
|
||||
if (user != null && user.Role == role && user.IsDeleted == IsDeleted
|
||||
&& user.BirthDate >= startDate && user.BirthDate <= endDate)
|
||||
{
|
||||
result.Add(student);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting students by birth date");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
if (!LessonId.IsGuid())
|
||||
|
||||
public List<StudentDataModel> GetAllStudentsByFaculty(FacultyType faculty,
|
||||
SystemRoleType role = SystemRoleType.student, bool IsDeleted = false)
|
||||
{
|
||||
throw new ValidationException("The value in the field LessonId is not a valid GUID.");
|
||||
_logger.LogInformation("Getting students by faculty: {Faculty}, role: {Role}, IsDeleted: {IsDeleted}",
|
||||
faculty, role, IsDeleted);
|
||||
|
||||
try
|
||||
{
|
||||
var students = _studentStorage.GetListByFaculty(faculty.ToString())
|
||||
?? throw new DataException($"Failed to retrieve students for faculty {faculty}");
|
||||
|
||||
var result = new List<StudentDataModel>();
|
||||
|
||||
foreach (var student in students)
|
||||
{
|
||||
var user = _userStorage.GetElementById(student.UserId);
|
||||
if (user != null && user.Role == role && user.IsDeleted == IsDeleted)
|
||||
{
|
||||
result.Add(student);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting students by faculty");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public List<StudentDataModel> GetAllStudentsByGroopType(GroopType groop,
|
||||
SystemRoleType role = SystemRoleType.student, bool IsDeleted = false)
|
||||
{
|
||||
_logger.LogInformation("Getting students by group: {Groop}, role: {Role}, IsDeleted: {IsDeleted}",
|
||||
groop, role, IsDeleted);
|
||||
|
||||
try
|
||||
{
|
||||
var students = _studentStorage.GetListByGroop(groop.ToString())
|
||||
?? throw new DataException($"Failed to retrieve students for group {groop}");
|
||||
|
||||
var result = new List<StudentDataModel>();
|
||||
|
||||
foreach (var student in students)
|
||||
{
|
||||
var user = _userStorage.GetElementById(student.UserId);
|
||||
if (user != null && user.Role == role && user.IsDeleted == IsDeleted)
|
||||
{
|
||||
result.Add(student);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting students by group");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public List<StudentDataModel> GetAllStudentsByCource(int course,
|
||||
SystemRoleType role = SystemRoleType.student, bool IsDeleted = false)
|
||||
{
|
||||
_logger.LogInformation("Getting students by course: {Course}, role: {Role}, IsDeleted: {IsDeleted}",
|
||||
course, role, IsDeleted);
|
||||
|
||||
if (course < 1 || course > 6)
|
||||
throw new ValidationException("Course must be between 1 and 6");
|
||||
|
||||
try
|
||||
{
|
||||
var students = _studentStorage.GetListByCource(course)
|
||||
?? throw new DataException($"Failed to retrieve students for course {course}");
|
||||
|
||||
var result = new List<StudentDataModel>();
|
||||
|
||||
foreach (var student in students)
|
||||
{
|
||||
var user = _userStorage.GetElementById(student.UserId);
|
||||
if (user != null && user.Role == role && user.IsDeleted == IsDeleted)
|
||||
{
|
||||
result.Add(student);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting students by course");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public StudentDataModel? GetStudentByData(string data,
|
||||
SystemRoleType role = SystemRoleType.student, bool IsDeleted = false)
|
||||
{
|
||||
_logger.LogInformation("Getting student by data: {Data}, role: {Role}, IsDeleted: {IsDeleted}",
|
||||
data, role, IsDeleted);
|
||||
|
||||
if (string.IsNullOrWhiteSpace(data))
|
||||
throw new ArgumentNullException(nameof(data));
|
||||
|
||||
try
|
||||
{
|
||||
StudentDataModel? student = null;
|
||||
|
||||
// Try by student ID
|
||||
if (data.IsGuid())
|
||||
student = _studentStorage.GetElementById(data);
|
||||
|
||||
// Try by user ID
|
||||
if (student == null && data.IsGuid())
|
||||
student = _studentStorage.GetElementByUserId(data);
|
||||
|
||||
if (student == null)
|
||||
{
|
||||
var user = _userStorage.GetElementById(data)
|
||||
?? _userStorage.GetElementByPhoneNomber(data)
|
||||
?? _userStorage.GetListByLogin(data);
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
student = _studentStorage.GetElementByUserId(user.Id);
|
||||
}
|
||||
}
|
||||
|
||||
if (student != null)
|
||||
{
|
||||
var user = _userStorage.GetElementById(student.UserId);
|
||||
|
||||
if (user != null && user.Role == role && user.IsDeleted == IsDeleted)
|
||||
return student;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while getting student by data");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void InsertStudent(StudentDataModel studentDataModel)
|
||||
{
|
||||
_logger.LogInformation("Inserting new student: {StudentData}",
|
||||
JsonSerializer.Serialize(studentDataModel));
|
||||
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(studentDataModel);
|
||||
studentDataModel.Validate();
|
||||
|
||||
var user = _userStorage.GetElementById(studentDataModel.UserId)
|
||||
?? throw new ElementNotFoundException($"User with ID {studentDataModel.UserId} not found");
|
||||
|
||||
if (user.Role != SystemRoleType.student)
|
||||
throw new ValidationException("User must have student role");
|
||||
|
||||
if (_studentStorage.GetElementByUserId(studentDataModel.UserId) != null)
|
||||
throw new DuplicateException($"Student for user {studentDataModel.UserId} already exists");
|
||||
|
||||
_studentStorage.AddElement(studentDataModel);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while inserting student");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void UpdateStudent(StudentDataModel studentDataModel)
|
||||
{
|
||||
_logger.LogInformation("Updating student: {StudentData}",
|
||||
JsonSerializer.Serialize(studentDataModel));
|
||||
|
||||
try
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(studentDataModel);
|
||||
studentDataModel.Validate();
|
||||
|
||||
var existingStudent = _studentStorage.GetElementById(studentDataModel.Id)
|
||||
?? throw new ElementNotFoundException($"Student with ID {studentDataModel.Id} not found");
|
||||
|
||||
if (existingStudent.UserId != studentDataModel.UserId)
|
||||
throw new ValidationException("Cannot change UserId for existing student");
|
||||
|
||||
_studentStorage.UpdElement(studentDataModel);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while updating student");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public void RestoreStudent(string id)
|
||||
{
|
||||
_logger.LogInformation("Restoring student with ID: {Id}", id);
|
||||
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(id))
|
||||
throw new ArgumentNullException(nameof(id));
|
||||
|
||||
if (!id.IsGuid())
|
||||
throw new ValidationException("Id must be a valid GUID");
|
||||
|
||||
var student = _studentStorage.GetElementById(id)
|
||||
?? throw new ElementNotFoundException($"Student with ID {id} not found");
|
||||
|
||||
var user = _userStorage.GetElementById(student.UserId)
|
||||
?? throw new DataException($"User data not found for student {id}");
|
||||
|
||||
user.IsDeleted = false;
|
||||
_userStorage.UpdElement(user);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error while restoring student");
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using UniversityAllExpelled_Models.Enums;
|
||||
using UniversityAllExpelled_Models.Enums;
|
||||
using UniversityAllExpelled_Models.Exceptions;
|
||||
using UniversityAllExpelled_Models.Extensions;
|
||||
using UniversityAllExpelled_Models.Infrostructure;
|
||||
@@ -12,11 +7,11 @@ namespace UniversityAllExpelled_Models.DataModels.Client;
|
||||
|
||||
public class StudentDataModel(string id, string userId, FacultyType faculty, GroopType groop, int course) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
public string UserId { get; private set; } = userId;
|
||||
public FacultyType Faculty { get; private set; } = faculty;
|
||||
public GroopType Groop { get; private set; } = groop;
|
||||
public int Course { get; private set; } = course;
|
||||
public string Id { get; set; } = id;
|
||||
public string UserId { get; set; } = userId;
|
||||
public FacultyType Faculty { get; set; } = faculty;
|
||||
public GroopType Groop { get; set; } = groop;
|
||||
public int Course { get; set; } = course;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
|
||||
@@ -10,23 +10,23 @@ namespace UniversityAllExpelled_Models.DataModels;
|
||||
public class UserDataModel(string id, string login, string password, SystemRoleType role,
|
||||
string fio, DateTime birthdate,string phoneNomber, string email, bool isDeleted) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
public string Id { get; set; } = id;
|
||||
|
||||
public string Login { get; private set; } = login;
|
||||
public string Login { get; set; } = login;
|
||||
|
||||
public string Password { get; private set; } = password;
|
||||
public string Password { get; set; } = password;
|
||||
|
||||
public SystemRoleType Role { get; private set; } = role;
|
||||
public SystemRoleType Role { get; set; } = role;
|
||||
|
||||
public string FIO { get; private set; } = fio;
|
||||
public string FIO { get; set; } = fio;
|
||||
|
||||
public DateTime BirthDate { get; private set; } = birthdate;
|
||||
public DateTime BirthDate { get; set; } = birthdate;
|
||||
|
||||
public string PhoneNomber { get; private set; } = phoneNomber;
|
||||
public string PhoneNomber { get; set; } = phoneNomber;
|
||||
|
||||
public string Email { get; private set; } = email;
|
||||
public string Email { get; set; } = email;
|
||||
|
||||
public bool IsDeleted { get; private set; } = isDeleted;
|
||||
public bool IsDeleted { get; set; } = isDeleted;
|
||||
|
||||
public void Validate()
|
||||
{
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using UniversityAllExpelled_Models.Extensions;
|
||||
using UniversityAllExpelled_Models.Infrostructure;
|
||||
|
||||
namespace UniversityAllExpelled_Models.DataModels.Worker;
|
||||
|
||||
public class SalaryDataModel(string id, double count, DateTime date) : IValidation
|
||||
public class SalaryDataModel(string id,string teacherId, double count, DateTime date) : IValidation
|
||||
{
|
||||
public string Id { get; private set; } = id;
|
||||
public string TeacherId { get; private set; } = teacherId;
|
||||
public double Count { get; private set; } = count;
|
||||
public DateTime Date { get; private set; } = date;
|
||||
|
||||
@@ -19,6 +19,12 @@ public class SalaryDataModel(string id, double count, DateTime date) : IValidati
|
||||
if (!Id.IsGuid())
|
||||
throw new ValidationException("Salary record ID must be in valid GUID format");
|
||||
|
||||
if (TeacherId.IsEmpty())
|
||||
throw new ValidationException("Salary record TeacherId cannot be empty");
|
||||
|
||||
if (!TeacherId.IsGuid())
|
||||
throw new ValidationException("Salary record TeacherId must be in valid GUID format");
|
||||
|
||||
if (Count <= 0)
|
||||
throw new ValidationException("Salary amount must be greater than zero");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Runtime.Serialization;
|
||||
|
||||
namespace UniversityAllExpelled_Models.Exceptions
|
||||
{
|
||||
|
||||
[Serializable]
|
||||
public class DuplicateException : Exception
|
||||
{
|
||||
public DuplicateException()
|
||||
{
|
||||
}
|
||||
|
||||
public DuplicateException(string message) : base(message)
|
||||
{
|
||||
}
|
||||
|
||||
public DuplicateException(string message, Exception innerException) : base(message, innerException)
|
||||
{
|
||||
}
|
||||
|
||||
protected DuplicateException(SerializationInfo info, StreamingContext context) : base(info, context)
|
||||
{
|
||||
}
|
||||
/// <param name="entityName">Название сущности</param>
|
||||
/// <param name="fieldName">Название поля</param>
|
||||
/// <param name="fieldValue">Значение поля</param>
|
||||
|
||||
public static DuplicateException ForField(string entityName, string fieldName, string fieldValue)
|
||||
{
|
||||
return new DuplicateException(
|
||||
$"{entityName} with {fieldName} '{fieldValue}' already exists. {fieldName} must be unique.");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UniversityAllExpelled_Models.Exceptions;
|
||||
|
||||
public class ElementDeletedException : Exception
|
||||
{
|
||||
public ElementDeletedException(string id) : base($"Cannot modify a deleted item (id: {id})") { }
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UniversityAllExpelled_Models.Exceptions;
|
||||
|
||||
public class ElementExistsException : Exception
|
||||
{
|
||||
public string ParamName { get; private set; }
|
||||
public string ParamValue { get; private set; }
|
||||
|
||||
public ElementExistsException(string paramName, string paramValue) : base($"There is already an element with value{paramValue} of parameter {paramName}")
|
||||
{
|
||||
ParamName = paramName;
|
||||
ParamValue = paramValue;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UniversityAllExpelled_Models.Exceptions;
|
||||
|
||||
public class ElementNotFoundException : Exception
|
||||
{
|
||||
public string Value { get; private set; }
|
||||
public ElementNotFoundException(string value) : base ($"Element not found at value = {value}")
|
||||
{
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace UniversityAllExpelled_Models.Exceptions;
|
||||
|
||||
public class IncorrectDatesException : Exception
|
||||
{
|
||||
public IncorrectDatesException(DateTime start, DateTime end) : base($"The end date must be later than the start date.. StartDate: {start:dd.MM.YYYY}. EndDate: {end:dd.MM.YYYY}") { }
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UniversityAllExpelled_Models.Exceptions;
|
||||
|
||||
public class NullListException : Exception
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace UniversityAllExpelled_Models.Exceptions;
|
||||
|
||||
public class StorageException : Exception
|
||||
{
|
||||
public StorageException(Exception ex) : base($"Error while working in storage: {ex.Message}", ex){}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace UniversityAllExpelled_Models.Infrostructure;
|
||||
|
||||
public interface IConfigurationDatabase
|
||||
{
|
||||
string ConnectionString { get; }
|
||||
}
|
||||
@@ -1,16 +1,19 @@
|
||||
using UniversityAllExpelled_Models.DataModels.Worker;
|
||||
|
||||
namespace UniversityAllExpelled_Models.StorageContracts
|
||||
namespace UniversityAllExpelled_Models.StorageContracts;
|
||||
|
||||
public interface ISalaryStorageContract
|
||||
{
|
||||
public interface ISalaryStorageContract
|
||||
{
|
||||
List<SalaryDataModel> GetList();
|
||||
SalaryDataModel? GetElementById(string id);
|
||||
List<SalaryDataModel>? GetListByDate(DateTime date);
|
||||
List<SalaryDataModel>? GetListByDateRange(DateTime startDate, DateTime endDate);
|
||||
List<SalaryDataModel>? GetListByAmountRange(double minAmount, double maxAmount);
|
||||
void AddElement(SalaryDataModel salaryDataModel);
|
||||
void UpdElement(SalaryDataModel salaryDataModel);
|
||||
void DelElement(SalaryDataModel salaryDataModel);
|
||||
}
|
||||
List<SalaryDataModel> GetList();
|
||||
SalaryDataModel? GetElementById(string id);
|
||||
List<SalaryDataModel>? GetListByTeacherId(string teacherId);
|
||||
List<SalaryDataModel>? GetListByDate(DateTime date);
|
||||
List<SalaryDataModel>? GetListByDateAndTeacherId(DateTime date, string teacherId);
|
||||
List<SalaryDataModel>? GetListByDateRange(DateTime startDate, DateTime endDate);
|
||||
List<SalaryDataModel>? GetListByDateRangeAndTeacherId(DateTime startDate, DateTime endDate, string teacherId);
|
||||
List<SalaryDataModel>? GetListByAmountRange(double minAmount, double maxAmount);
|
||||
List<SalaryDataModel>? GetListByAmountRangeAndTeacherId(double minAmount, double maxAmount, string teacherId);
|
||||
void AddElement(SalaryDataModel salaryDataModel);
|
||||
void UpdElement(SalaryDataModel salaryDataModel);
|
||||
void DelElement(SalaryDataModel salaryDataModel);
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
|
||||
|
||||
using UniversityAllExpelled_Models.DataModels.Client;
|
||||
using UniversityAllExpelled_Models.DataModels.Worker;
|
||||
|
||||
namespace UniversityAllExpelled_Models.StorageContracts;
|
||||
|
||||
|
||||
@@ -5,5 +5,12 @@
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="UniversityAllExpelled_DataBase" />
|
||||
<InternalsVisibleTo Include="UniversityAllExpelled_BusinessLogic" />
|
||||
<InternalsVisibleTo Include="DynamicProxyGenAssembly2" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="9.0.4" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user