public QuestionIndexWithScope CreateGlobal(QuestionCreate data) { var parentSheet = this.context.QuestionSheets .Select(qs => new { qs.Id, qs.IsGlobal, orders = qs.GlobalQuestions.Select(gq => gq.Order).ToArray(), }) .SingleOrDefault(x => x.Id == data.SheetId && x.IsGlobal == true); if (parentSheet == null) { throw new ServiceException("Invalid Parent Sheet!"); } var order = 0; if (parentSheet.orders.Length > 0) { order = parentSheet.orders.Max() + 1; } var result = new GlobalQuestionPackage { Name = data.Name, Question = data.Question, Answer = data.Answer, Comment = data.Comment, Difficulty = data.Difficulty.Value, QuestionSheetId = data.SheetId, Order = order, }; context.GlobalQuestionPackages.Add(result); context.SaveChanges(); return(new QuestionIndexWithScope { isGlobal = true, data = Mapper.Map <QuestionGlobalIndex>(result), }); }
public int CreateGlobal(QuestionCreate data) { var parentSheet = this.context.QuestionSheets .SingleOrDefault(x => x.Id == data.SheetId && x.IsGlobal == true); if (parentSheet == null) { throw new ServiceException("Invalid Parent Sheet!"); } var result = new GlobalQuestionPackage { Name = data.Name, Question = data.Question, Answer = data.Answer, Comment = data.Comment, Difficulty = data.Difficulty.Value, QuestionSheetId = data.SheetId, }; context.GlobalQuestionPackages.Add(result); context.SaveChanges(); return(result.Id); }