using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Diplom_O.DataBase { public class RabotnikTypeStaj { public int Id { get; set; } [ForeignKey("Rabotnik")] public int RabitnikId { get; set; } [ForeignKey("TypeStaj")] public int TypeStajId { get; set; } } public static partial class FuncDB { public static void RabotnikTypeStajCheckValid(RabotnikTypeStaj obj) { try { if (obj == null) throw new Exception("Ошибка инициализации стажа сотрудника."); } catch { throw; } } public static RabotnikTypeStaj RabotnikTypeStajGetById(int id) { try { using (var db = new MainDB()) { var res = from a in db.RabotnikTypeStajy where a.Id == id select a; try { return res.Single(); } catch { return null; } } } catch { throw; } } public static RabotnikTypeStaj RabotnikTypeStajGetByTypeStajIdRabotnikId(int typeStajId, int rabotnikId) { try { using (var db = new MainDB()) { var res = from a in db.RabotnikTypeStajy where a.RabitnikId == rabotnikId && a.TypeStajId == typeStajId select a; try { return res.Single(); } catch { return null; } } } catch { throw; } } public static void RabotnikTypeStajAdd(RabotnikTypeStaj obj) { try { RabotnikTypeStajCheckValid(obj); if (RabotnikTypeStajGetByTypeStajIdRabotnikId(obj.TypeStajId, obj.RabitnikId) != null ) throw new Exception("Тип стажа сотрудника существует."); using (var db = new MainDB()) { db.RabotnikTypeStajy.Add(obj); db.SaveChanges(); } } catch { throw; } } public static void RabotnikTypeStajDelete(RabotnikTypeStaj obj) { try { if (obj == null) return; var localObj = RabotnikTypeStajGetById(obj.Id); if (localObj == null) throw new Exception("Тип стажа сотрудника не существует."); using (var db = new MainDB()) { db.RabotnikTypeStajy.Remove(localObj); db.SaveChanges(); } } catch { throw; } } public static RabotnikTypeStaj[] RabotnikTypeStajList(int rabId) { try { using (var db = new MainDB()) { var res = ( from a in db.RabotnikTypeStajy where a.RabitnikId == rabId select a ).ToList(); return res.ToArray(); } } catch { throw; } } public static string[][] RabotnikTypeStajListString(int rabId) { try { var rts = RabotnikTypeStajList(rabId); var res = new List(); foreach (var r in rts) { res.Add( new string[] { r.Id.ToString(), TypeStajGetById(r.TypeStajId).Name } ); } return res.ToArray(); } catch { throw; } } public static RabotnikTypeStaj[] RabotnikTypeStajListByTypeStaj(int typeStaj) { try { using (var db = new MainDB()) { var res = ( from a in db.RabotnikTypeStajy where a.TypeStajId == typeStaj select a ).ToList(); return res.ToArray(); } } catch { throw; } } } }