Diplom_O/DataBase/LearnDB.cs

182 lines
3.4 KiB
C#
Raw Normal View History

2024-09-20 08:53:52 +05:00
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Diplom_O.DataBase
{
public class Learn
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[ForeignKey("Chel")]
public int ChelId { get; set; }
public Chel Chel { get; set; }
public string NameSchool { get; set; }
public string Type { get; set; }
public string Spec { get; set; }
public string Curs { get; set; }
public string ProfPer { get; set; }
public DateTime End { get; set; }
}
public static partial class FuncDB
{
public static void LearnCheckValid(Learn obj)
{
try
{
if (obj == null) throw new Exception("Ошибка инициализации.");
if (ChelGetById(obj.ChelId) == null) throw new Exception("Человек не указан.");
if (string.IsNullOrEmpty(obj.NameSchool)) throw new Exception("Обр. учр. не указано.");
if (string.IsNullOrEmpty(obj.Type)) throw new Exception("Уровень не указан.");
if (string.IsNullOrEmpty(obj.Spec)) throw new Exception("Специальность не указана.");
}
catch { throw; }
}
public static Learn LearnGetById(int id)
{
try
{
using (var db = new MainDB())
{
var res =
from a in db.Learny
where a.Id == id
select a;
try
{
return res.Single();
}
catch
{
return null;
}
}
}
catch
{
throw;
}
}
public static void LearnAdd(Learn obj)
{
try
{
LearnCheckValid(obj);
using (var db = new MainDB())
{
db.Learny.Add(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void LearnChange(Learn obj)
{
try
{
LearnCheckValid(obj);
var localObj = LearnGetById(obj.Id);
if (localObj == null)
throw new Exception("Образование не указано.");
using (var db = new MainDB())
{
db.Learny.Update(obj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static void LearnDelete(Learn obj)
{
try
{
if (obj == null)
return;
var localObj = LearnGetById(obj.Id);
if (localObj == null)
throw new Exception("Образование не существует.");
using (var db = new MainDB())
{
db.Learny.Remove(localObj);
db.SaveChanges();
}
}
catch
{
throw;
}
}
public static Learn[] LearnList(int chelId)
{
try
{
using (var db = new MainDB())
{
var learns = (
from a in db.Learny
where a.ChelId == chelId
select a
).ToArray();
return learns;
}
}
catch
{
throw;
}
}
public static string[][] LearnListTableMain(int chelId)
{
try
{
return LearnListTableMain(LearnList(chelId));
}
catch
{
throw;
}
}
public static string[][] LearnListTableMain(Learn[] data)
{
try
{
var result = new List<string[]>();
foreach (var learn in data)
result.Add(
new string[]
{
learn.Id.ToString(),
learn.NameSchool,
learn.Type,
learn.Spec,
learn.End.ToString("yyyy.MM.dd"),
learn.Curs,
learn.ProfPer
}
);
return result.ToArray();
}
catch
{
throw;
}
}
}
}