public int CreatePersonal(QuestionCreate data, int userId) { var parentSheet = context.QuestionSheets.SingleOrDefault(x => x.Id == data.SheetId); if (parentSheet == null) { throw new ServiceException("Invalid Parent Sheet!"); } if (parentSheet.IsGlobal == true) { throw new ServiceException("Invalid Parent Sheet!"); } if (parentSheet.UserId != userId) { throw new ServiceException("Parent Sheet does not belong to you!"); } var user = context.Users.SingleOrDefault(x => x.Id == userId); if (user == null) { throw new ServiceException("Invalid User!"); } var result = new PersonalQuestionPackage { Name = data.Name, Question = data.Question, Answer = data.Answer, Comment = data.Comment, Difficulty = data.Difficulty.Value, QuestionSheetId = data.SheetId, TimesBeingAnswered = 0, YourBestAnswer = "Good try buddy!", AnswerRate = 0, }; context.PersonalQuestionPackages.Add(result); context.SaveChanges(); return(result.Id); }
public QuestionIndexWithScope CreatePersonal(QuestionCreate data, int userId) { var parentSheet = this.context.QuestionSheets .Select(qs => new { qs.Id, qs.IsGlobal, qs.UserId, orders = qs.PersonalQuestions.Select(gq => gq.Order).ToArray(), }) .SingleOrDefault(x => x.Id == data.SheetId && x.IsGlobal == false); if (parentSheet == null) { throw new ServiceException("Invalid Parent Sheet!"); } if (parentSheet.IsGlobal == true) { throw new ServiceException("Invalid Parent Sheet!"); } if (parentSheet.UserId != userId) { throw new ServiceException("Parent Sheet does not belong to you!"); } var user = context.Users.SingleOrDefault(x => x.Id == userId); if (user == null) { throw new ServiceException("Invalid User!"); } var order = 0; if (parentSheet.orders.Length > 0) { order = parentSheet.orders.Max() + 1; } var result = new PersonalQuestionPackage { Name = data.Name, Question = data.Question, Answer = data.Answer, Comment = data.Comment, Difficulty = data.Difficulty.Value, QuestionSheetId = data.SheetId, Order = order, TimesBeingAnswered = 0, YourBestAnswer = "Good try buddy!", AnswerRate = 0, }; context.PersonalQuestionPackages.Add(result); context.SaveChanges(); return(new QuestionIndexWithScope { isGlobal = false, data = Mapper.Map <QuestionGlobalIndex>(result), }); }