public static long CreateUpdateLessonTypes(LessonTypesDTO lessonTypes) { try { using (var ctx = new DAL.tutorDBEntities()) { var dbLessonType = ctx.LessonTypes.FirstOrDefault(x => x.id == lessonTypes.id) ?? ctx.LessonTypes.Add(new DAL.LessonTypes()); if (ctx.LessonTypes.Any(x => x.name == lessonTypes.name && x.id != dbLessonType.id)) { throw new Exception($"Вариант проведения занятий: {lessonTypes.name} уже существует"); } dbLessonType.name = lessonTypes.name; ctx.SaveChanges(); return(dbLessonType.id); } } catch (Exception ex) { throw; //return -1; } }
public static LessonTypesDTO GetLessonType(long?id = null, string name = null) { if (!id.HasValue && string.IsNullOrEmpty(name)) { return(null); } try { using (var ctx = new DAL.tutorDBEntities()) { var dbLessonType = ctx.LessonTypes.FirstOrDefault(x => x.id == id || x.name == name); if (dbLessonType != null) { var lessonType = new LessonTypesDTO { id = dbLessonType.id, name = dbLessonType.name }; return(lessonType); } throw new Exception($"Вариант проведения занятий: {name} не найден"); } } catch (Exception ex) { return(null); } }