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

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 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.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 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.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 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

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

View File

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

View File

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

View File

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