new update
This commit is contained in:
108
ProtRPP/SchoolAgainStudyDataBaseImplements/Models/Diy.cs
Normal file
108
ProtRPP/SchoolAgainStudyDataBaseImplements/Models/Diy.cs
Normal file
@@ -0,0 +1,108 @@
|
||||
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; }
|
||||
[Required]
|
||||
public int TaskId { get; set; }
|
||||
public string TaskName { get; set; } = string.Empty;
|
||||
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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user