2023-04-03 21:27:31 +04:00
|
|
|
|
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]
|
2023-04-05 21:11:51 +04:00
|
|
|
|
public DateTime DateCreate { get; set; }
|
2023-04-03 21:27:31 +04:00
|
|
|
|
[Required]
|
|
|
|
|
public int TaskId { get; set; }
|
|
|
|
|
public string TaskName { get; set; } = string.Empty;
|
2023-04-05 21:11:51 +04:00
|
|
|
|
public virtual Task Task { get; set; }
|
|
|
|
|
[Required]
|
2023-04-03 21:27:31 +04:00
|
|
|
|
public int StudentId { get; set; }
|
2023-04-05 21:11:51 +04:00
|
|
|
|
public string StudentName { get; set; } = string.Empty;
|
|
|
|
|
public virtual Student Student { get; set; }
|
2023-04-03 21:27:31 +04:00
|
|
|
|
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,
|
2023-04-05 21:11:51 +04:00
|
|
|
|
StudentName = model.StudentName,
|
2023-04-03 21:27:31 +04:00
|
|
|
|
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,
|
2023-04-05 21:11:51 +04:00
|
|
|
|
StudentName = StudentName,
|
2023-04-03 21:27:31 +04:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|