Полу сделанная бизнес-логика + начало работы с базой данных

This commit is contained in:
DyCTaTOR 2024-04-21 19:12:38 +04:00
parent 6dfec78206
commit 3f9d9ce648
12 changed files with 389 additions and 8 deletions

View File

@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.4.0" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,136 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicsContracts;
using UniversityContracts.SearchModels;
using UniversityContracts.StorageContracts;
using UniversityContracts.ViewModels;
using UniversityDataModels.Enums;
namespace UniversityBusinessLogic.BusinessLogics
{/*
public class AttestationLogic : IAttestationLogic
{
private readonly ILogger _logger;
private readonly IAttestationStorage _attestationStorage;
public AttestationLogic(ILogger<AttestationLogic> logger, IAttestationStorage
attestationStorage)
{
_logger = logger;
_attestationStorage = attestationStorage;
}
public List<AttestationViewModel>? ReadList(AttestationSearchModel? model)
{
_logger.LogInformation("ReadList. FormOfEvaluation: {FormOfEvaluation}.Id:{Id} ",
model?.FormOfEvaluation, model?.Id);
var list = model == null ? _attestationStorage.GetFullList() :
_attestationStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public AttestationViewModel? ReadElement(AttestationSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. FormOfEvaluation:{FormOfEvaluation}.Id:{Id}",
model.FormOfEvaluation, model.Id);
var element = _attestationStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(AttestationBindingModel model)
{
CheckModel(model);
model.Score = AttestationScore.Неявка;
if (_attestationStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool StatusUpdate(AttestationBindingModel model, AttestationScore newScore)
{
CheckModel(model);
if (model.Score + 1 != newScore)
{
_logger.LogWarning("Status update to " + newScore.ToString() + " operation failed. Order status incorrect.");
return false;
}
model.Score = newScore;
if (model.Score == AttestationScore.Выдан)
model.DateImplement = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
if (_attestationStorage.Update(model) == null)
{
model.Score--;
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool TakeOrderInWork(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.Выполняется);
}
public bool DeliveryOrder(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.Готов);
}
public bool FinishOrder(OrderBindingModel model)
{
return StatusUpdate(model, OrderStatus.Выдан);
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.WorkId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор изделия", nameof(model.WorkId));
}
if (model.Count <= 0)
{
throw new ArgumentNullException("Количество изделий в заказе должно быть больше 0", nameof(model.Count));
}
if (model.Sum <= 0)
{
throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum));
}
_logger.LogInformation("Order. OrderId:{Id}.Sum:{ Sum}. WorkId: { WorkId}", model.Id, model.Sum, model.WorkId);
}
}*/
}

View File

@ -0,0 +1,118 @@
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.BusinessLogicsContracts;
using UniversityContracts.SearchModels;
using UniversityContracts.StorageContracts;
using UniversityContracts.ViewModels;
namespace UniversityBusinessLogic.BusinessLogics
{
public class PlanOfStudyLogic : IPlanOfStudyLogic
{
private readonly ILogger _logger;
private readonly IPlanOfStudyStorage _planOfStudyStorage;
public PlanOfStudyLogic(ILogger<StudentLogic> logger, IPlanOfStudyStorage
planOfStudyStorage)
{
_logger = logger;
_planOfStudyStorage = planOfStudyStorage;
}
public List<PlanOfStudyViewModel>? ReadList(PlanOfStudySearchModel? model)
{
_logger.LogInformation("ReadList. Profile: {Profile}.Id:{Id} ",
model?.Profile, model?.Id);
var list = model == null ? _planOfStudyStorage.GetFullList() :
_planOfStudyStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public PlanOfStudyViewModel? ReadElement(PlanOfStudySearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Profile:{Profile}.Id:{Id}",
model.Profile, model.Id);
var element = _planOfStudyStorage.GetElement(model);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(PlanOfStudyBindingModel model)
{
CheckModel(model);
if (_planOfStudyStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(PlanOfStudyBindingModel model)
{
CheckModel(model);
if (_planOfStudyStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(PlanOfStudyBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_planOfStudyStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(PlanOfStudyBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Profile))
{
throw new ArgumentNullException("Нет названия профиля",
nameof(model.Profile));
}
if (string.IsNullOrEmpty(model.FormOfStudy))
{
throw new ArgumentNullException("Не указана форма обучения", nameof(model.FormOfStudy));
}
_logger.LogInformation("Student. Profile:{Profile}.FormOfStudy:{FormOfStudy}. Id: {Id}",
model.Profile, model.FormOfStudy, model.Id);
var element = _planOfStudyStorage.GetElement(new PlanOfStudySearchModel
{
Profile = model.Profile
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Данный план обучения уже существует");
}
}
}
}

View File

@ -107,7 +107,7 @@ namespace UniversityBusinessLogic.BusinessLogics
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Student с таким названием уже есть");
throw new InvalidOperationException("Данный студент уже существует");
}
}
}

View File

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityDataModels.Enums;
using UniversityDataModels.Models;
namespace UniversityContracts.BindingModels
@ -11,7 +12,7 @@ namespace UniversityContracts.BindingModels
{
public int Id { get; set; }
public string FormOfEvaluation { get; set; } = string.Empty;
public string Score { get; set; } = string.Empty;
public AttestationScore Score { get; set; } = AttestationScore.Неявка;
public int StudentId { get; set; }
}
}

View File

@ -9,7 +9,7 @@ using UniversityContracts.ViewModels;
namespace UniversityContracts.BusinessLogicsContracts
{
internal interface IAttestationLogic
public interface IAttestationLogic
{
List<AttestationViewModel>? ReadList(AttestationSearchModel? model);
AttestationViewModel? ReadElement(AttestationSearchModel model);

View File

@ -9,7 +9,7 @@ using UniversityContracts.ViewModels;
namespace UniversityContracts.BusinessLogicsContracts
{
internal interface IPlanOfStudyLogic
public interface IPlanOfStudyLogic
{
List<PlanOfStudyViewModel>? ReadList(PlanOfStudySearchModel? model);
PlanOfStudyViewModel? ReadElement(PlanOfStudySearchModel model);

View File

@ -10,6 +10,5 @@ namespace UniversityContracts.SearchModels
{
public int? Id { get; set; }
public string? FormOfEvaluation { get; set; }
public string? Score { get; set; }
}
}

View File

@ -4,6 +4,7 @@ using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityDataModels.Enums;
using UniversityDataModels.Models;
namespace UniversityContracts.ViewModels
@ -15,6 +16,6 @@ namespace UniversityContracts.ViewModels
[DisplayName("Форма оценивания")]
public string FormOfEvaluation { get; set; } = string.Empty;
[DisplayName("Оценка")]
public string Score { get; set; } = string.Empty;
public AttestationScore Score { get; set; } = AttestationScore.Неявка;
}
}

View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UniversityDataModels.Enums
{
public enum AttestationScore
{
Неявка = -1,
Незачёт = 0,
Зачёт = 1,
Неудовлетворительно = 2,
Удовлетворительно = 3,
Хорошо = 4,
Отлично = 5
}
}

View File

@ -1,9 +1,11 @@
namespace UniversityDataModels.Models
using UniversityDataModels.Enums;
namespace UniversityDataModels.Models
{
public interface IAttestationModel : IId
{
string FormOfEvaluation { get; }
string Score { get; }
AttestationScore Score { get; }
int StudentId { get; }
}
}

View File

@ -1,13 +1,99 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.StorageContracts;
using UniversityContracts.ViewModels;
using UniversityDatabaseImplement.Models;
namespace UniversityDatabaseImplement.Implements
{
public class StudentStorage : IStudentStorage
{
public List<StudentViewModel> GetFullList()
{
using var context = new UniversityDatabase();
return context.Students.Select(x => x.GetViewModel).ToList();
}
public List<StudentViewModel> GetFilteredList(StudentSearchModel model)
{
if (string.IsNullOrEmpty(model.Name))
{
return new();
}
using var context = new UniversityDatabase();
return context.Students.Where(x => x.Name.Contains(model.Name)).Select(x => x.GetViewModel).ToList();
}
public StudentViewModel? GetElement(StudentSearchModel model)
{
if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue)
{
return null;
}
using var context = new UniversityDatabase();
return context.Students.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name)
&& x.Name == model.Name) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public StudentViewModel? Insert(StudentBindingModel model)
{
var newStudent = Student.Create(model);
if (newStudent == null)
{
return null;
}
using var context = new UniversityDatabase();
context.Students.Add(newStudent);
context.SaveChanges();
return newStudent.GetViewModel;
}
public StudentViewModel? Update(StudentBindingModel model)
{
using var context = new UniversityDatabase();
var student = context.Students.FirstOrDefault(x => x.Id == model.Id);
if (student == null)
{
return null;
}
student.Update(model);
context.SaveChanges();
return student.GetViewModel;
}
public StudentViewModel? Delete(StudentBindingModel model)
{
using var context = new UniversityDatabase();
var element = context.Students.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Students.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
}
}