public ExercisesDTO ToDTO(ExercisesEntity entity) { ExercisesDTO dto = new ExercisesDTO(); dto.CreateDateTime = entity.CreateDateTime; dto.Id = entity.Id; dto.OptionA = entity.OptionA; dto.OptionB = entity.OptionB; dto.OptionC = entity.OptionC; dto.OptionD = entity.OptionD; dto.RightKeyId = entity.RightKeyId; dto.RightKeyName = entity.RightKey.Name; dto.TestPaperId = entity.TestPaperId; dto.TestPaperTitle = entity.TestPaper.TestTitle; dto.Title = entity.Title; dto.Tip = entity.Tip; return(dto); }
public long AddNew(string title, long testPaperId, string optionA, string optionB, string optionC, string optionD, long rightKeyId) { using (MyDbContext dbc = new MyDbContext()) { CommonService <ExercisesEntity> cs = new CommonService <ExercisesEntity>(dbc); if (cs.GetAll().Any(e => e.Title == title)) { return(-1); } ExercisesEntity exercises = new ExercisesEntity(); exercises.Title = title; exercises.TestPaperId = testPaperId; exercises.OptionA = optionA; exercises.OptionB = optionB; exercises.OptionC = optionC; exercises.OptionD = optionD; exercises.RightKeyId = rightKeyId; exercises.Point = 0; switch (rightKeyId) { case 1: exercises.Tip = title + optionA; break; case 2: exercises.Tip = title + optionB; break; case 3: exercises.Tip = title + optionC; break; case 4: exercises.Tip = title + optionD; break; } dbc.Exercises.Add(exercises); dbc.SaveChanges(); string sql = "update T_TestPapers set ExercisesCount=c.b from T_TestPapers, (select t.Id, COUNT(e.TestPaperId) as b from T_TestPapers t inner join (select TestPaperId from T_Exercises where IsDeleted=0) as e on t.Id=e.TestPaperId group by t.id) as c where T_TestPapers.Id=c.Id"; dbc.Database.ExecuteSqlCommand(sql); return(exercises.Id); } }