CourseWork_SchoolStudyAgain/SchoolAgainStudy/SchoolAgainStudyDataBaseImplements/Models/Diy.cs

118 lines
4.0 KiB
C#
Raw Normal View History

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 virtual Task Task { get; set; }
[Required]
public int StudentId { get; set; }
public string StudentName { get; set; } = string.Empty;
public virtual Student Student { 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,
StudentName = model.StudentName,
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;
2023-05-19 00:05:58 +04:00
TaskId = model.TaskId;
TaskName = model.TaskName;
Description = model.Description;
DateCreate = model.DateCreate;
}
public DiyViewModel GetViewModel => new()
{
Id = Id,
Title=Title,
Description=Description,
DateCreate = DateCreate,
TaskId = TaskId,
TaskName = TaskName,
StudentId = StudentId,
StudentName = StudentName,
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;
}
}
}