Вроде доделано Databaseimplement для Поставщика

This commit is contained in:
Danil Markov 2023-04-07 13:24:39 +04:00
parent 8423f1fdfd
commit f5c1abd6ed
25 changed files with 595 additions and 8 deletions

View File

@ -12,5 +12,6 @@ namespace UniversityContracts.BindingModels
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public DateTime Date { get; set; } = DateTime.Now;
public int UserId { get; set; }
}
}

View File

@ -11,5 +11,7 @@ namespace UniversityContracts.BindingModels
{
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public int UserId { get; set; }
}
}

View File

@ -14,6 +14,7 @@ namespace UniversityContracts.BindingModels
public string Surname { get; set; } = string.Empty;
public DateTime DateOfBirth { get; set; }
public int StudentCard { get; set; }
public int EducationStatusId { get; set; }
public int EducationStatusId { get; set; }
public int UserId { get; set; }
}
}

View File

@ -9,5 +9,6 @@ namespace UniversityContracts.SearchModels
public class DocumentSearchModel
{
public int? Id { get; set; }
public int? UserId { get; set; }
}
}

View File

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

View File

@ -9,5 +9,6 @@ namespace UniversityContracts.SearchModels
public class StudentSearchModel
{
public int? Id { get; set; }
public int? UserId { get; set; }
}
}

View File

@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.ViewModels;
namespace UniversityContracts.StoragesContracts
{

View File

@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.ViewModels;
namespace UniversityContracts.StoragesContracts
{

View File

@ -5,6 +5,7 @@ using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.ViewModels;
namespace UniversityContracts.StoragesContracts
{

View File

@ -11,6 +11,7 @@ namespace UniversityContracts.ViewModels
public class DocumentViewModel : IDocumentModel
{
public int Id { get; set; }
public int UserId { get; set; }
[DisplayName("Название документа")]
public string Name { get; set; } = string.Empty;
[DisplayName("Дата создания документа")]

View File

@ -13,5 +13,6 @@ namespace UniversityContracts.ViewModels
public int Id { get; set; }
[DisplayName("Название документа")]
public string Name { get; set; } = string.Empty;
public int UserId { get; set; }
}
}

View File

@ -11,12 +11,15 @@ namespace UniversityContracts.ViewModels
public class StudentViewModel : IStudentModel
{
public int Id { get; set; }
public int UserId { get; set; }
[DisplayName("Имя студента")]
public string Name { get; set; } = string.Empty;
[DisplayName("Фамилия студента")]
public string Surname { get; set; } = string.Empty;
[DisplayName("Дата рождения студента")]
public DateTime DateOfBirth { get; set; } = DateTime.Now;
[DisplayName("Статус обучения")]
public string EducationStatusName { get; set; } = string.Empty;
public int StudentCard { get; set; }
public int EducationStatusId { get; set; }
}

View File

@ -1,7 +0,0 @@
namespace UniversityDataBaseImplemet
{
public class Class1
{
}
}

View File

@ -0,0 +1,26 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityDataBaseImplemet.Models;
namespace UniversityDataBaseImplemet
{
public class Database : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured == false)
{
optionsBuilder.UseNpgsql("Host=localhost;Port=5432;Database=University;Username=postgres;Password=postgres");
}
base.OnConfiguring(optionsBuilder);
}
public virtual DbSet<Document> Documents { get; set; }
public virtual DbSet<EducationStatus> EducationStatuses { get; set; }
public virtual DbSet<Student> Students { get; set; }
public virtual DbSet<StudentDocument> StudentDocuments { get; set; }
}
}

View File

@ -0,0 +1,113 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
using UniversityDataBaseImplemet.Models;
namespace UniversityDataBaseImplemet.Implements
{
public class DocumentStorage : IDocumentStorage
{
public DocumentViewModel? GetElement(DocumentSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new Database();
return context.Documents
.Include(record => record.User)
.FirstOrDefault(record => record.Id == model.Id)?
.GetViewModel;
}
public List<DocumentViewModel> GetFilteredList(DocumentSearchModel model)
{
using var context = new Database();
if (model.Id.HasValue)
{
return context.Documents
.Include(x => x.User)
.Where(record => record.Id.Equals(model.Id))
.Select(record => record.GetViewModel)
.ToList();
}
else if (model.UserId.HasValue)
{
return context.Documents
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.Select(x => x.GetViewModel)
.ToList();
}
else
{
return new();
}
}
public List<DocumentViewModel> GetFullList()
{
using var context = new Database();
return context.Documents
.Include(record => record.User)
.Select(record => record.GetViewModel)
.ToList();
}
public DocumentViewModel? Insert(DocumentBindingModel model)
{
var newDocument = Document.Create(model);
if(newDocument == null)
{
return null;
}
using var context = new Database();
context.Documents.Add(newDocument);
context.SaveChanges();
return newDocument.GetViewModel;
}
public DocumentViewModel? Update(DocumentBindingModel model)
{
using var context = new Database();
using var transaction = context.Database.BeginTransaction();
try
{
var document = context.Documents
.Include(x => x.User)
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (document == null)
{
return null;
}
document.Update(model);
context.SaveChanges();
transaction.Commit();
return document.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public DocumentViewModel? Delete(DocumentBindingModel model)
{
using var context = new Database();
var document = context.Documents
.Include(x => x.User)
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (document == null)
{
return null;
}
context.Documents.Remove(document);
context.SaveChanges();
return document.GetViewModel;
}
}
}

View File

@ -0,0 +1,115 @@
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.StoragesContracts;
using UniversityContracts.ViewModels;
using UniversityDataBaseImplemet.Models;
namespace UniversityDataBaseImplemet.Implements
{
public class EducationStatusStorage : IEducationStatusStorage
{
public EducationStatusViewModel? GetElement(EducationStatusSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new Database();
return context.EducationStatuses
.Include(x => x.User)
.FirstOrDefault(record => record.Id == model.Id)?
.GetViewModel;
}
public List<EducationStatusViewModel> GetFilteredList(EducationStatusSearchModel model)
{
using var context = new Database();
if (model.Id.HasValue)
{
return context.EducationStatuses
.Include(x => x.User)
.Where(record => record.Id.Equals(model.Id))
.Select(record => record.GetViewModel)
.ToList();
}
else if (model.UserId.HasValue)
{
return context.EducationStatuses
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.Select(x => x.GetViewModel)
.ToList();
}
else
{
return new();
}
}
public List<EducationStatusViewModel> GetFullList()
{
using var context = new Database();
return context.EducationStatuses
.Include(x => x.User)
.Select(record => record.GetViewModel)
.ToList();
}
public EducationStatusViewModel? Insert(EducationStatusBindingModel model)
{
var newEducationStatus = EducationStatus.Create(model);
if (newEducationStatus == null)
{
return null;
}
using var context = new Database();
context.EducationStatuses.Add(newEducationStatus);
context.SaveChanges();
return context.EducationStatuses
.Include(x => x.User)
.FirstOrDefault(x => x.Id.Equals(newEducationStatus.Id))
?.GetViewModel;
}
public EducationStatusViewModel? Update(EducationStatusBindingModel model)
{
using var context = new Database();
using var transaction = context.Database.BeginTransaction();
try
{
var educationStatus = context.EducationStatuses
.Include(x => x.User)
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (educationStatus == null)
{
return null;
}
educationStatus.Update(model);
context.SaveChanges();
transaction.Commit();
return educationStatus.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public EducationStatusViewModel? Delete(EducationStatusBindingModel model)
{
using var context = new Database();
var educationStatus = context.EducationStatuses
.Include(x => x.User)
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (educationStatus == null)
{
return null;
}
context.EducationStatuses.Remove(educationStatus);
context.SaveChanges();
return educationStatus.GetViewModel;
}
}
}

View File

@ -0,0 +1,119 @@
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.SearchModels;
using UniversityContracts.StoragesContracts;
using UniversityContracts.ViewModels;
using UniversityDataBaseImplemet.Models;
namespace UniversityDataBaseImplemet.Implements
{
public class StudentStorage : IStudentStorage
{
public StudentViewModel? GetElement(StudentSearchModel model)
{
if (!model.Id.HasValue)
{
return null;
}
using var context = new Database();
return context.Students
.Include(record => record.User)
.Include(record => record.EducationStatus)
.FirstOrDefault(record => record.Id.Equals(model.Id))
?.GetViewModel;
}
public List<StudentViewModel> GetFilteredList(StudentSearchModel model)
{
using var context = new Database();
if (model.Id.HasValue)
{
return context.Students
.Include(record => record.EducationStatus)
.Include(x => x.User)
.Where(record => record.Id.Equals(model.Id))
.Select(record => record.GetViewModel)
.ToList();
}
else if (model.UserId.HasValue)
{
return context.Students
.Include(x => x.EducationStatus)
.Include(x => x.User)
.Where(x => x.UserId == model.UserId)
.Select(x => x.GetViewModel)
.ToList();
}
else
{
return new();
}
}
public List<StudentViewModel> GetFullList()
{
using var context = new Database();
return context.Students
.Include(x => x.User)
.Include(x => x.EducationStatus)
.Select(record => record.GetViewModel)
.ToList();
}
public StudentViewModel? Insert(StudentBindingModel model)
{
var newStudent = Student.Create(model);
if (newStudent == null)
{
return null;
}
using var context = new Database();
context.Students.Add(newStudent);
context.SaveChanges();
return newStudent.GetViewModel;
}
public StudentViewModel? Update(StudentBindingModel model)
{
using var context = new Database();
using var transaction = context.Database.BeginTransaction();
try
{
var student = context.Students
.Include(x => x.User)
.Include(x => x.EducationStatus)
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (student == null)
{
return null;
}
student.Update(model);
context.SaveChanges();
transaction.Commit();
return student.GetViewModel;
}
catch
{
transaction.Rollback();
throw;
}
}
public StudentViewModel? Delete(StudentBindingModel model)
{
using var context = new Database();
var student = context.Students
.Include(x => x.User)
.Include(x => x.EducationStatus)
.FirstOrDefault(record => record.Id.Equals(model.Id));
if (student == null)
{
return null;
}
context.Students.Remove(student);
context.SaveChanges();
return student.GetViewModel;
}
}
}

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.ViewModels;
using UniversityModels.Models;
namespace UniversityDataBaseImplemet.Models
{
public class Document : IDocumentModel
{
public int Id { get; private set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public DateTime Date { get; set; }
[Required]
public int UserId { get; set; }
[ForeignKey("DocumentId")]
public virtual List<StudentDocument> Students { get; set; } = new();
public virtual EducationStatus User { get; set; }
public static Document? Create(DocumentBindingModel model)
{
return new Document()
{
Id = model.Id,
Name = model.Name,
Date = model.Date,
UserId = model.UserId,
};
}
public void Update(DocumentBindingModel model)
{
if(model == null)
{
return;
}
Name = model.Name;
Date = model.Date;
UserId = model.UserId;
}
public DocumentViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Date = Date,
UserId = UserId,
};
}
}

View File

@ -0,0 +1,50 @@
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 UniversityModels.Models;
namespace UniversityDataBaseImplemet.Models
{
public class EducationStatus : IEducationStatusModel
{
public int Id { get; private set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public int UserId { get; set; }
[ForeignKey("EducationStatusId")]
public virtual List<Student> Students { get; set; } = new();
public virtual EducationStatus User { get; set; }
public static EducationStatus? Create(EducationStatusBindingModel model)
{
return new EducationStatus()
{
Id = model.Id,
Name = model.Name,
UserId = model.UserId,
};
}
public void Update(EducationStatusBindingModel model)
{
if (model == null)
{
return;
}
Name = model.Name;
UserId = model.UserId;
}
public EducationStatusViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
UserId = UserId,
};
}
}

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UniversityContracts.BindingModels;
using UniversityContracts.ViewModels;
using UniversityModels.Models;
namespace UniversityDataBaseImplemet.Models
{
public class Student : IStudentModel
{
public int Id { get; private set; }
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public string Surname { get; set; } = string.Empty;
[Required]
public DateTime DateOfBirth { get; set; }
[Required]
public int StudentCard { get; set; }
[Required]
public int EducationStatusId { get; set; }
[Required]
public int UserId { get; set; }
[ForeignKey("StudentId")]
public virtual List<StudentDocument> DocumentStudents { get; set; } = new();
public virtual EducationStatus EducationStatus { get; set; }
public virtual EducationStatus User { get; set; }
public static Student Create(StudentBindingModel model)
{
return new Student()
{
Id = model.Id,
Name = model.Name,
Surname = model.Surname,
DateOfBirth = model.DateOfBirth,
StudentCard = model.StudentCard,
EducationStatusId = model.EducationStatusId,
UserId = model.UserId
};
}
public void Update(StudentBindingModel model)
{
StudentCard = model.StudentCard;
EducationStatusId = model.EducationStatusId;
}
public StudentViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Surname = Surname,
DateOfBirth = DateOfBirth,
StudentCard = StudentCard,
EducationStatusId = EducationStatusId,
UserId = UserId,
EducationStatusName = EducationStatus.Name
};
}
}

View File

@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace UniversityDataBaseImplemet.Models
{
public class StudentDocument
{
public int Id { get; set; }
[Required]
public int StudentId { get; set; }
[Required]
public int DocumentId { get; set; }
public virtual Student Student { get; set; } = new();
public virtual Document Document { get; set; } = new();
}
}

View File

@ -6,4 +6,18 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\UniversityContracts\UniversityContracts.csproj" />
<ProjectReference Include="..\UniversityModels\UniversityModels.csproj" />
</ItemGroup>
</Project>

View File

@ -10,5 +10,6 @@ namespace UniversityModels.Models
{
string Name { get; }
DateTime Date { get; }
int UserId { get; }
}
}

View File

@ -9,5 +9,6 @@ namespace UniversityModels.Models
public interface IEducationStatusModel : IId
{
string Name { get; }
int UserId { get; }
}
}

View File

@ -13,5 +13,6 @@ namespace UniversityModels.Models
DateTime DateOfBirth { get; }
int StudentCard { get; }
int EducationStatusId { get; }
int UserId { get; }
}
}