Готовые модели бд + продолжение бизнес логики

This commit is contained in:
DyCTaTOR 2024-04-25 17:00:45 +04:00
parent e1482243ed
commit 93f012e03b
13 changed files with 278 additions and 13 deletions

View File

@ -12,7 +12,7 @@ using UniversityContracts.ViewModels;
using UniversityDataModels.Enums;
namespace UniversityBusinessLogic.BusinessLogics
{/*
{
public class AttestationLogic : IAttestationLogic
{
private readonly ILogger _logger;
@ -25,8 +25,8 @@ namespace UniversityBusinessLogic.BusinessLogics
}
public List<AttestationViewModel>? ReadList(AttestationSearchModel? model)
{
_logger.LogInformation("ReadList. FormOfEvaluation: {FormOfEvaluation}.Id:{Id} ",
model?.FormOfEvaluation, model?.Id);
_logger.LogInformation("ReadList.AttestationId:{Id} ",
model?.Id);
var list = model == null ? _attestationStorage.GetFullList() :
_attestationStorage.GetFilteredList(model);
if (list == null)
@ -43,8 +43,8 @@ namespace UniversityBusinessLogic.BusinessLogics
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. FormOfEvaluation:{FormOfEvaluation}.Id:{Id}",
model.FormOfEvaluation, model.Id);
_logger.LogInformation("ReadElement.Id:{Id}",
model.Id);
var element = _attestationStorage.GetElement(model);
if (element == null)
{
@ -54,7 +54,7 @@ namespace UniversityBusinessLogic.BusinessLogics
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(AttestationBindingModel model)
public bool CreateAttestation(AttestationBindingModel model)
{
CheckModel(model);
@ -68,7 +68,7 @@ namespace UniversityBusinessLogic.BusinessLogics
return true;
}
public bool StatusUpdate(AttestationBindingModel model, AttestationScore newScore)
public bool ScoreUpdate(AttestationBindingModel model, AttestationScore newScore)
{
CheckModel(model);
if (model.Score + 1 != newScore)
@ -132,5 +132,5 @@ namespace UniversityBusinessLogic.BusinessLogics
}
_logger.LogInformation("Order. OrderId:{Id}.Sum:{ Sum}. WorkId: { WorkId}", model.Id, model.Sum, model.WorkId);
}
}*/
}
}

View File

@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityDataModels.Models;
namespace UniversityContracts.BindingModels
{
public class WorkerBindingModel : IWorkerModel
{
public int Id { get; set; }
public string FirstName { get; set; } = string.Empty;
public string LastName { get; set; } = string.Empty;
public string MiddleName { get; set; } = string.Empty;
public string PhoneNumber { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
}
}

View File

@ -12,9 +12,13 @@ namespace UniversityContracts.BusinessLogicsContracts
public interface IAttestationLogic
{
List<AttestationViewModel>? ReadList(AttestationSearchModel? model);
AttestationViewModel? ReadElement(AttestationSearchModel model);
bool Create(AttestationBindingModel model);
bool Update(AttestationBindingModel model);
bool Delete(AttestationBindingModel model);
bool CreateAttestation(AttestationBindingModel model);
bool SetPass(AttestationBindingModel model);
bool SetNotPass(AttestationBindingModel model);
bool Set(AttestationBindingModel model);
bool Set(AttestationBindingModel model);
bool Set(AttestationBindingModel model);
bool SetNotCredit(AttestationBindingModel model);
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.ViewModels;
namespace UniversityContracts.BusinessLogicsContracts
{
public interface IWorkerLogic
{
List<WorkerViewModel>? ReadList(WorkerSearchModel? model);
WorkerViewModel? ReadElement(WorkerSearchModel model);
bool Create(WorkerBindingModel model);
bool Update(WorkerBindingModel model);
bool Delete(WorkerBindingModel model);
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UniversityContracts.SearchModels
{
public class WorkerSearchModel
{
public int? Id { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? MiddleName { get; set; }
public string? PhoneNumber { get; set; }
public string? Email { get; set; }
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.ViewModels;
namespace UniversityContracts.StorageContracts
{
public interface IWorkerStorage
{
List<WorkerViewModel> GetFullList();
List<WorkerViewModel> GetFilteredList(WorkerSearchModel model);
WorkerViewModel? GetElement(WorkerSearchModel model);
WorkerViewModel? Insert(WorkerBindingModel model);
WorkerViewModel? Update(WorkerBindingModel model);
WorkerViewModel? Delete(WorkerBindingModel model);
}
}

View File

@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityDataModels.Models;
namespace UniversityContracts.ViewModels
{
public class WorkerViewModel : IWorkerModel
{
public int Id { get; set; }
[DisplayName("Имя")]
public string FirstName { get; set; } = string.Empty;
[DisplayName("Фамилия")]
public string LastName { get; set; } = string.Empty;
[DisplayName("Отчество")]
public string MiddleName { get; set; } = string.Empty;
[DisplayName("Номер телефона")]
public string PhoneNumber { get; set; } = string.Empty;
[DisplayName("Почта")]
public string Email { get; set; } = string.Empty;
}
}

View File

@ -0,0 +1,86 @@
using System;
using System.Collections.Generic;
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 WorkerStorage : IWorkerStorage
{
public WorkerViewModel? GetElement(WorkerSearchModel model)
{
if (string.IsNullOrEmpty(model.Email) && !model.Id.HasValue)
{
return null;
}
using var context = new UniversityDatabase();
return context.Workers.FirstOrDefault(x =>
(!string.IsNullOrEmpty(model.Email) && x.Email == model.Email)
|| (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public List<WorkerViewModel> GetFilteredList(WorkerSearchModel model)
{
if (string.IsNullOrEmpty(model.Email))
{
return new();
}
using var context = new UniversityDatabase();
return context.Workers
.Where(x => x.Email.Contains(model.Email))
.Select(x => x.GetViewModel)
.ToList();
}
public List<WorkerViewModel> GetFullList()
{
using var context = new UniversityDatabase();
return context.Workers.Select(x => x.GetViewModel).ToList();
}
public WorkerViewModel? Insert(WorkerBindingModel model)
{
var newWorker = Worker.Create(model);
if (newWorker == null)
{
return null;
}
using var context = new UniversityDatabase();
context.Workers.Add(newWorker);
context.SaveChanges();
return newWorker.GetViewModel;
}
public WorkerViewModel? Update(WorkerBindingModel model)
{
using var context = new UniversityDatabase();
var client = context.Workers.FirstOrDefault(x => x.Id == model.Id);
if (client == null)
{
return null;
}
client.Update(model);
context.SaveChanges();
return client.GetViewModel;
}
public WorkerViewModel? Delete(WorkerBindingModel model)
{
using var context = new UniversityDatabase();
var client = context.Workers.FirstOrDefault(x => x.Id == model.Id);
if (client == null)
{
return null;
}
context.Workers.Remove(client);
context.SaveChanges();
return client.GetViewModel;
}
}
}

View File

@ -35,6 +35,8 @@ namespace UniversityDatabaseImplement.Models
}
[ForeignKey("PlanOfStudyId")]
public virtual List<PlanOfStudyTeacher> Teachers { get; set; } = new();
[ForeignKey("PlanOfStudyId")]
public virtual List<Student> Students { get; set; } = new();
public virtual Worker Worker { get; set; } = new();
public static PlanOfStudy Create(UniversityDatabase context, PlanOfStudyBindingModel model)
{

View File

@ -22,6 +22,8 @@ namespace UniversityDatabaseImplement.Models
public string PhoneNumber { get; private set; } = string.Empty;
[ForeignKey("StudentId")]
public virtual List<StudentDiscipline> StudentDiscipline { get; set; } = new();
[ForeignKey("StudentId")]
public virtual List<Attestation> Attestations { get; set; } = new();
public virtual PlanOfStudy PlanOfStudy { get; set; } = new();
public static Student? Create(StudentBindingModel model)
{

View File

@ -28,6 +28,7 @@ namespace UniversityDatabaseImplement.Models
public virtual List<Statement> Statements { get; set; } = new();
[ForeignKey("TeacherId")]
public virtual List<Discipline> Disciplines { get; set; } = new();
[ForeignKey("TeacherId")]
public virtual List<PlanOfStudyTeacher> PlanOfStudyTeachers { get; set; } = new();
public static Teacher? Create(TeacherBindingModel model)
{

View File

@ -1,14 +1,81 @@
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 UniversityContracts.BindingModels;
using UniversityContracts.ViewModels;
using UniversityDataModels.Models;
namespace UniversityDatabaseImplement.Models
{
public class Worker : IWorkerModel
{
public int Id { get; private set; }
[Required]
public string FirstName { get; private set; } = string.Empty;
[Required]
public string LastName { get; private set; } = string.Empty;
[Required]
public string MiddleName { get; private set; } = string.Empty;
[Required]
public string PhoneNumber { get; private set; } = string.Empty;
[Required]
public string Email { get; private set; } = string.Empty;
[ForeignKey("WorkerId")]
public virtual List<PlanOfStudy> PlanOfStudys { get; set; } = new();
public static Worker? Create(WorkerBindingModel model)
{
if (model == null)
{
return null;
}
return new Worker()
{
Id = model.Id,
FirstName = model.FirstName,
LastName = model.LastName,
MiddleName = model.MiddleName,
PhoneNumber = model.PhoneNumber,
Email = model.Email,
};
}
public static Worker Create(WorkerViewModel model)
{
return new Worker
{
Id = model.Id,
FirstName = model.FirstName,
LastName = model.LastName,
MiddleName = model.MiddleName,
PhoneNumber = model.PhoneNumber,
Email = model.Email,
};
}
public void Update(WorkerBindingModel model)
{
if (model == null)
{
return;
}
Id = model.Id;
FirstName = model.FirstName;
LastName = model.LastName;
MiddleName = model.MiddleName;
PhoneNumber = model.PhoneNumber;
Email = model.Email;
}
public WorkerViewModel GetViewModel => new()
{
Id = Id,
FirstName = FirstName,
LastName = LastName,
MiddleName = MiddleName,
PhoneNumber = PhoneNumber,
Email = Email,
};
}
}

View File

@ -19,7 +19,7 @@ namespace UniversityDatabaseImplement
public virtual DbSet<Student> Students { set; get; }
public virtual DbSet<PlanOfStudy> PlanOfStudys { set; get; }
public virtual DbSet<Attestation> Attestations { set; get; }
// public virtual DbSet<Worker> Workers { set; get; }
public virtual DbSet<Worker> Workers { set; get; }
public virtual DbSet<Storekeeper> Storekeepers { set; get; }
public virtual DbSet<Teacher> Teachers { set; get; }
public virtual DbSet<Discipline> Disciplines { set; get; }