Начата работа над базой данных

This commit is contained in:
DyCTaTOR 2024-04-24 14:57:31 +04:00
parent f93bc290e3
commit e1482243ed
8 changed files with 255 additions and 64 deletions

View File

@ -9,6 +9,6 @@ namespace UniversityContracts.SearchModels
public class AttestationSearchModel public class AttestationSearchModel
{ {
public int? Id { get; set; } public int? Id { get; set; }
public string? FormOfEvaluation { get; set; } public int? StudentId { get; set; }
} }
} }

View File

@ -1,13 +1,99 @@
using System; using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.StorageContracts; using UniversityContracts.StorageContracts;
using UniversityContracts.ViewModels;
using UniversityDatabaseImplement.Models;
namespace UniversityDatabaseImplement.Implements namespace UniversityDatabaseImplement.Implements
{ {
public class AttestationStorage : IAttestationStorage public class AttestationStorage : IAttestationStorage
{ {
public List<AttestationViewModel> GetFullList()
{
using var context = new UniversityDatabase();
return context.Attestations.Select(x => x.GetViewModel).ToList();
}
public List<AttestationViewModel> GetFilteredList(AttestationSearchModel model)
{
using var context = new UniversityDatabase();
if (model.Id.HasValue)
{
return context.Attestations
.Include(x => x.Student)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.StudentId.HasValue)
{
return context.Attestations
.Include(x => x.Student)
.Where(x => x.StudentId == model.StudentId)
.Select(x => x.GetViewModel)
.ToList();
}
return new();
}
public AttestationViewModel? GetElement(AttestationSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new UniversityDatabase();
return context.Attestations.Include(x => x.Student).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public AttestationViewModel? Insert(AttestationBindingModel model)
{
var newAttestation = Attestation.Create(model);
if (newAttestation == null)
{
return null;
}
using var context = new UniversityDatabase();
context.Attestations.Add(newAttestation);
context.SaveChanges();
return newAttestation.GetViewModel;
}
public AttestationViewModel? Update(AttestationBindingModel model)
{
using var context = new UniversityDatabase();
var order = context.Attestations.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return context.Attestations.Include(x => x.Student).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public AttestationViewModel? Delete(AttestationBindingModel model)
{
using var context = new UniversityDatabase();
var element = context.Attestations.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Attestations.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
} }
} }

View File

@ -1,13 +1,99 @@
using System; using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.StorageContracts; using UniversityContracts.StorageContracts;
using UniversityContracts.ViewModels;
using UniversityDatabaseImplement.Models;
namespace UniversityDatabaseImplement.Implements namespace UniversityDatabaseImplement.Implements
{ {
public class PlanOfStudyStorage : IPlanOfStudyStorage public class PlanOfStudyStorage : IPlanOfStudyStorage
{ {
public List<PlanOfStudyViewModel> GetFullList()
{
using var context = new UniversityDatabase();
return context.PlanOfStudys.Select(x => x.GetViewModel).ToList();
}
public List<PlanOfStudyViewModel> GetFilteredList(PlanOfStudySearchModel model)
{
using var context = new UniversityDatabase();
if (model.Id.HasValue)
{
return context.PlanOfStudys
.Include(x => x.Student)
.Where(x => x.Id == model.Id)
.Select(x => x.GetViewModel)
.ToList();
}
else if (model.StudentId.HasValue)
{
return context.Attestations
.Include(x => x.Student)
.Where(x => x.StudentId == model.StudentId)
.Select(x => x.GetViewModel)
.ToList();
}
return new();
}
public PlanOfStudyViewModel? GetElement(PlanOfStudySearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new UniversityDatabase();
return context.Attestations.Include(x => x.Student).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel;
}
public PlanOfStudyViewModel? Insert(PlanOfStudyBindingModel model)
{
var newAttestation = Attestation.Create(model);
if (newAttestation == null)
{
return null;
}
using var context = new UniversityDatabase();
context.Attestations.Add(newAttestation);
context.SaveChanges();
return newAttestation.GetViewModel;
}
public PlanOfStudyViewModel? Update(PlanOfStudyBindingModel model)
{
using var context = new UniversityDatabase();
var order = context.Attestations.FirstOrDefault(x => x.Id == model.Id);
if (order == null)
{
return null;
}
order.Update(model);
context.SaveChanges();
return context.Attestations.Include(x => x.Student).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel;
}
public PlanOfStudyViewModel? Delete(PlanOfStudyBindingModel model)
{
using var context = new UniversityDatabase();
var element = context.Attestations.FirstOrDefault(rec => rec.Id == model.Id);
if (element != null)
{
context.Attestations.Remove(element);
context.SaveChanges();
return element.GetViewModel;
}
return null;
}
} }
} }

View File

@ -6,58 +6,61 @@ using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using UniversityContracts.BindingModels; using UniversityContracts.BindingModels;
using UniversityContracts.ViewModels; using UniversityContracts.ViewModels;
using UniversityDataModels.Enums;
using UniversityDataModels.Models; using UniversityDataModels.Models;
using static System.Formats.Asn1.AsnWriter;
namespace UniversityDatabaseImplement.Models namespace UniversityDatabaseImplement.Models
{ {
public class Attestation : IAttestationModel public class Attestation : IAttestationModel
{ {
public int Id { get; set; } public int Id { get; private set; }
public int StudentId { get; set; } public int StudentId { get; private set; }
[Required] [Required]
public string FormOfEvaluation { get; private set; } = string.Empty; public string FormOfEvaluation { get; private set; } = string.Empty;
[Required] [Required]
public string Score { get; private set; } = string.Empty; public AttestationScore Score { get; private set; } = AttestationScore.Неявка;
} public virtual Student Student { get; set; } = new();
public static Student? Create(StudentBindingModel model) public static Attestation? Create(AttestationBindingModel model)
{ {
if (model == null) if (model == null)
{ {
return null; return null;
} }
return new Student() return new Attestation()
{ {
Id = model.Id, Id = model.Id,
PlanOfStudyId = model.PlanOfStudyId, StudentId = model.StudentId,
Name = model.Name, FormOfEvaluation = model.FormOfEvaluation,
PhoneNumber = model.PhoneNumber Score = model.Score
}; };
} }
public static Student Create(StudentViewModel model) public static Attestation Create(AttestationViewModel model)
{ {
return new Student return new Attestation
{ {
Id = model.Id, Id = model.Id,
PlanOfStudyId = model.PlanOfStudyId, StudentId = model.StudentId,
Name = model.Name, FormOfEvaluation = model.FormOfEvaluation,
PhoneNumber = model.PhoneNumber Score = model.Score
}; };
} }
public void Update(StudentBindingModel model) public void Update(AttestationBindingModel model)
{ {
if (model == null) if (model == null)
{ {
return; return;
} }
PhoneNumber = model.PhoneNumber; StudentId = model.StudentId;
Name = model.Name; FormOfEvaluation = model.FormOfEvaluation;
PlanOfStudyId = model.PlanOfStudyId; Score = model.Score;
} }
public StudentViewModel GetViewModel => new() public AttestationViewModel GetViewModel => new()
{ {
Id = Id, Id = Id,
PlanOfStudyId = PlanOfStudyId, StudentId = StudentId,
Name = Name, FormOfEvaluation = FormOfEvaluation,
PhoneNumber = PhoneNumber Score = Score
}; };
} }
}

View File

@ -13,8 +13,8 @@ namespace UniversityDatabaseImplement.Models
{ {
public class PlanOfStudy : IPlanOfStudyModel public class PlanOfStudy : IPlanOfStudyModel
{ {
public int Id { get; set; } public int Id { get; private set; }
public int WorkerId { get; set; } public int WorkerId { get; private set; }
[Required] [Required]
public string Profile { get; private set; } = string.Empty; public string Profile { get; private set; } = string.Empty;
[Required] [Required]
@ -35,6 +35,7 @@ namespace UniversityDatabaseImplement.Models
} }
[ForeignKey("PlanOfStudyId")] [ForeignKey("PlanOfStudyId")]
public virtual List<PlanOfStudyTeacher> Teachers { get; set; } = new(); public virtual List<PlanOfStudyTeacher> Teachers { get; set; } = new();
public virtual Worker Worker { get; set; } = new();
public static PlanOfStudy Create(UniversityDatabase context, PlanOfStudyBindingModel model) public static PlanOfStudy Create(UniversityDatabase context, PlanOfStudyBindingModel model)
{ {
return new PlanOfStudy() return new PlanOfStudy()

View File

@ -14,7 +14,7 @@ namespace UniversityDatabaseImplement.Models
public int PlanOfStudyId { get; set; } public int PlanOfStudyId { get; set; }
[Required] [Required]
public int TeacherId { get; set; } public int TeacherId { get; set; }
public virtual PlanOfStudy PlanOfStudy { get; set; } public virtual PlanOfStudy PlanOfStudy { get; set; } = new();
public virtual Teacher Teacher { get; set; } public virtual Teacher Teacher { get; set; } = new();
} }
} }

View File

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

View File

@ -0,0 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityDataModels.Models;
namespace UniversityDatabaseImplement.Models
{
public class Worker : IWorkerModel
{
}
}