Исполнитель : слой хранения данных (модели)

This commit is contained in:
Володя 2023-04-03 20:27:31 +03:00
parent 0e0a605915
commit d81a2ea768
8 changed files with 475 additions and 0 deletions

View File

@ -0,0 +1,110 @@
using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.ViewModel;
using SchoolAgainStudyDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace SchoolAgainStudyDataBaseImplements.Models
{
public class Diy : IDiy
{
[Required]
public string Title { get; set; } = string.Empty;
[Required]
public string Description { get; set; } = string.Empty;
[Required]
public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc);
[Required]
public int TaskId { get; set; }
public string TaskName { get; set; } = string.Empty;
public virtual Task task { get; set; }
public int StudentId { get; set; }
private Dictionary<int, IInterest>? _DiyInterests = null;
[NotMapped]
public Dictionary<int, IInterest> DiyInterests {
get
{
if (_DiyInterests == null)
{
_DiyInterests = Interests
.ToDictionary(recPC => recPC.InterestId, recPC => (recPC.Interest as IInterest));
}
return _DiyInterests;
}
}
public int Id { get; set; }
[ForeignKey("DiyId")]
public virtual List<DiyInterest> Interests { get; set; } = new();
public static Diy Create(SchoolDataBase context, DiyBindingModel model)
{
return new Diy()
{
Id = model.Id,
Title = model.Title,
Description = model.Description,
DateCreate = model.DateCreate,
TaskId = model.TaskId,
TaskName = model.TaskName,
StudentId = model.StudentId,
Interests = model.DiyInterests.Select(x => new DiyInterest
{
Interest = context.Interests.First(y => y.Id == x.Key),
}).ToList()
};
}
public void Update(DiyBindingModel model)
{
Title = model.Title;
Description = model.Description;
DateCreate = model.DateCreate;
}
public DiyViewModel GetViewModel => new()
{
Id = Id,
Title=Title,
Description=Description,
DateCreate = DateCreate,
TaskId = TaskId,
TaskName = TaskName,
StudentId = StudentId,
DiyInterests=DiyInterests
};
public void UpdateInterests(SchoolDataBase context, DiyBindingModel model)
{
var diyInterests = context.DiyInterests.Where(rec => rec.DiyId == model.Id).ToList();
if (diyInterests != null && diyInterests.Count > 0)
{
context.DiyInterests.RemoveRange(diyInterests.Where(rec => !model.DiyInterests.ContainsKey(rec.InterestId)));
context.SaveChanges();
foreach (var updateInterest in diyInterests)
{
model.DiyInterests.Remove(updateInterest.InterestId);
}
context.SaveChanges();
}
var diy = context.Diys.First(x => x.Id == Id);
foreach (var pc in model.DiyInterests)
{
context.DiyInterests.Add(new DiyInterest
{
Diy = diy,
Interest = context.Interests.First(x => x.Id == pc.Key),
});
context.SaveChanges();
}
_DiyInterests = null;
}
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolAgainStudyDataBaseImplements.Models
{
public class DiyInterest
{
public int Id { get; set; }
[Required]
public int DiyId { get; set; }
[Required]
public int InterestId { get; set; }
public virtual Diy Diy { get; set; } = new();
public virtual Interest Interest { get; set; } = new();
}
}

View File

@ -0,0 +1,71 @@
using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.ViewModel;
using SchoolAgainStudyDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolAgainStudyDataBaseImplements.Models
{
public class Interest : IInterest
{
[Required]
public string Title { get; set; } = string.Empty;
[Required]
public string Description { get; set; } = string.Empty;
public int Id { get; set; }
[ForeignKey("InterestId")]
public virtual List<StudentInterest> StudentInterests { get; set; } = new();
[ForeignKey("InterestId")]
public virtual List<DiyInterest> DiyInterests { get; set; } = new();
[ForeignKey("InterestId")]
public virtual List<ProductInterest> ProductInterests { get; set; } = new();
public static Interest? Create(InterestBindingModel model)
{
if (model == null)
{
return null;
}
return new Interest()
{
Id = model.Id,
Title = model.Title,
Description = model.Description
};
}
public static Interest Create(InterestViewModel model)
{
return new Interest
{
Id = model.Id,
Title = model.Title,
Description = model.Description
};
}
public void Update(InterestBindingModel model)
{
if (model == null)
{
return;
}
Title = model.Title;
Description = model.Description;
}
public InterestViewModel GetViewModel => new()
{
Id = Id,
Title = Title,
Description = Description
};
}
}

View File

@ -0,0 +1,103 @@
using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.ViewModel;
using SchoolAgainStudyDataModels.Models;
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;
namespace SchoolAgainStudyDataBaseImplements.Models
{
public class Product : IProduct
{
[Required]
public string Title { get; set; } = string.Empty;
[Required]
public string Description { get; set; } = string.Empty;
[Required]
public DateTime DateCreate { get; set; }
public int StudentId { get; set; }
private Dictionary<int, IInterest>? _ProductInterests = null;
[NotMapped]
public Dictionary<int, IInterest> ProductInterests
{
get
{
if (_ProductInterests == null)
{
_ProductInterests = Interests
.ToDictionary(recPC => recPC.InterestId, recPC => (recPC.Interest as IInterest));
}
return _ProductInterests;
}
}
public int Id { get; set; }
[ForeignKey("ProductId")]
public virtual List<ProductInterest> Interests { get; set; } = new();
[ForeignKey("ProductId")]
public virtual List<Lesson> Lessons { get; set; } = new();
public static Product Create(SchoolDataBase context, ProductBindingModel model)
{
return new Product()
{
Id = model.Id,
Title = model.Title,
Description = model.Description,
DateCreate = model.DateCreate,
StudentId = model.StudentId,
Interests = model.ProductInterests.Select(x => new ProductInterest
{
Interest = context.Interests.First(y => y.Id == x.Key),
}).ToList()
};
}
public void Update(ProductBindingModel model)
{
Title = model.Title;
Description = model.Description;
DateCreate = model.DateCreate;
}
public ProductViewModel GetViewModel => new()
{
Id = Id,
Title = Title,
Description = Description,
DateCreate = DateCreate,
StudentId = StudentId,
ProductInterests = ProductInterests
};
public void UpdateInterests(SchoolDataBase context, ProductBindingModel model)
{
var productInterests = context.ProductInterests.Where(rec => rec.ProductId == model.Id).ToList();
if (productInterests != null && productInterests.Count > 0)
{
context.ProductInterests.RemoveRange(productInterests.Where(rec => !model.ProductInterests.ContainsKey(rec.InterestId)));
context.SaveChanges();
foreach (var updateInterest in productInterests)
{
model.ProductInterests.Remove(updateInterest.InterestId);
}
context.SaveChanges();
}
var product = context.Products.First(x => x.Id == Id);
foreach (var pc in model.ProductInterests)
{
context.ProductInterests.Add(new ProductInterest
{
Product = product,
Interest = context.Interests.First(x => x.Id == pc.Key),
});
context.SaveChanges();
}
_ProductInterests = null;
}
}
}

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 SchoolAgainStudyDataBaseImplements.Models
{
public class ProductInterest
{
public int Id { get; set; }
[Required]
public int ProductId { get; set; }
[Required]
public int InterestId { get; set; }
public virtual Product Product { get; set; } = new();
public virtual Interest Interest { get; set; } = new();
}
}

View File

@ -0,0 +1,116 @@
using SchoolAgainStudyContracts.BindingModel;
using SchoolAgainStudyContracts.ViewModel;
using SchoolAgainStudyDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
namespace SchoolAgainStudyDataBaseImplements.Models
{
public class Student : IStudent
{
[Required]
public string Name { get; set; } = string.Empty;
[Required]
public int Class { get; set; }
[Required]
public string Email { get; set; } = string.Empty;
[Required]
public string Login { get; set; } = string.Empty;
[Required]
public string Password { get; set; } = string.Empty;
private Dictionary<int, IInterest>? _StudentInterests = null;
[NotMapped]
public Dictionary<int, IInterest> StudentInterests
{
get
{
if (_StudentInterests == null)
{
_StudentInterests = Interests
.ToDictionary(recPC => recPC.InterestId, recPC => (recPC.Interest as IInterest));
}
return _StudentInterests;
}
}
public int Id { get; set; }
[ForeignKey("StudentId")]
public virtual List<StudentInterest> Interests { get; set; } = new();
[ForeignKey("StudentId")]
public virtual List<Diy> Diys { get; set; } = new();
[ForeignKey("StudentId")]
public virtual List<Product> Products { get; set; } = new();
public static Student Create(SchoolDataBase context, StudentBindingModel model)
{
return new Student()
{
Id = model.Id,
Name = model.Name,
Class = model.Class,
Email = model.Email,
Login = model.Login,
Password = model.Password,
Interests = model.StudentInterests.Select(x => new StudentInterest
{
Interest = context.Interests.First(y => y.Id == x.Key),
}).ToList()
};
}
public void Update(StudentBindingModel model)
{
Name = model.Name;
Class = model.Class;
Email = model.Email;
Login = model.Login;
Password = model.Password;
}
public StudentViewModel GetViewModel => new()
{
Id = Id,
Name = Name,
Class = Class,
Email = Email,
Login = Login,
Password = Password,
StudentInterests = StudentInterests
};
public void UpdateInterests(SchoolDataBase context, StudentBindingModel model)
{
var studentInterests = context.StudentInterests.Where(rec => rec.StudentId == model.Id).ToList();
if (studentInterests != null && studentInterests.Count > 0)
{
context.StudentInterests.RemoveRange(studentInterests.Where(rec => !model.StudentInterests.ContainsKey(rec.InterestId)));
context.SaveChanges();
foreach (var updateInterest in studentInterests)
{
model.StudentInterests.Remove(updateInterest.InterestId);
}
context.SaveChanges();
}
var student = context.Students.First(x => x.Id == Id);
foreach (var pc in model.StudentInterests)
{
context.StudentInterests.Add(new StudentInterest
{
Student = student,
Interest = context.Interests.First(x => x.Id == pc.Key),
});
context.SaveChanges();
}
_StudentInterests = null;
}
}
}

View File

@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SchoolAgainStudyDataBaseImplements.Models
{
public class StudentInterest
{
public int Id { get; set; }
[Required]
public int StudentId { get; set; }
[Required]
public int InterestId { get; set; }
public virtual Student Student { get; set; } = new();
public virtual Interest Interest { get; set; } = new();
}
}

View File

@ -6,4 +6,17 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.4" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.4">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\SchoolAgainStudyContracts\SchoolAgainStudyContracts.csproj" />
</ItemGroup>
</Project>