public void CreateRoot(int userId)
        {
            var user = context.Users
                       .Include(x => x.QuestionSheets)
                       .SingleOrDefault(x => x.Id == userId);

            if (user == null)
            {
                throw new ServiceException("User Not Found!");
            }

            if (user.QuestionSheets.Any())
            {
                throw new ServiceException("User Already Has Sheets!");
            }

            var sheet = new QuestionSheet
            {
                Description     = "Personal Root Sheet",
                Difficulty      = 1,
                Importance      = 10,
                Name            = $"{user.Username}{Constants.PersonalRootSheetNameSuffix}",
                Order           = 1,
                QuestionSheetId = null,
                IsGlobal        = false,
                UserId          = user.Id,
            };

            context.QuestionSheets.Add(sheet);
            context.SaveChanges();
        }
 public bool DeleteQuestionInSheet(QuestionSheet objQuestionSheet)
 {
     try
     {
         db.QuestionSheet.Remove(objQuestionSheet);
         db.SaveChanges();
         return(true);
     }
     catch
     {
         return(false);
     }
 }
        public SheetIndexWithScope CreatePersonal(QuestionSheetCreate data, int userId)
        {
            var parentSheet = this.context.QuestionSheets
                              .SingleOrDefault(x => x.Id == data.ParentSheetId);

            if (parentSheet == null)
            {
                throw new ServiceException("Parent Sheet Could Not Be Found!");
            }

            var user = this.context.Users.SingleOrDefault(x => x.Id == userId);

            if (parentSheet.UserId != user.Id)
            {
                throw new ServiceException("Parent Sheet Does Not Belong To You!");
            }

            var order  = 0;
            var orders = context.QuestionSheets
                         .Where(x => x.QuestionSheetId == data.ParentSheetId)
                         .Select(x => x.Order)
                         .ToArray();

            if (orders.Length > 0)
            {
                order = orders.Max() + 1;
            }

            var sheet = new QuestionSheet
            {
                Description     = data.Description,
                Difficulty      = data.Difficulty.Value,
                Importance      = data.Importance.Value,
                Name            = data.Name,
                Order           = order,
                QuestionSheetId = data.ParentSheetId,
                IsGlobal        = false,
                UserId          = user.Id,
            };

            context.QuestionSheets.Add(sheet);
            context.SaveChanges();

            return(new SheetIndexWithScope
            {
                isGlobal = false,
                data = Mapper.Map <QuestionSheetChildIndex>(sheet),
            });
        }
        private void CascadeDeletePersonal(QuestionSheet sheet)
        {
            var children = context.QuestionSheets
                           .Include(x => x.GlobalQuestions)
                           .Include(x => x.PersonalQuestions)
                           .Where(x => x.QuestionSheetId == sheet.Id && x.IsGlobal == false);

            foreach (var child in children)
            {
                this.CascadeDeletePersonal(child);
            }

            context.PersonalQuestionPackages.RemoveRange(sheet.PersonalQuestions);
            context.GlobalQuestionPackages.RemoveRange(sheet.GlobalQuestions);
            context.QuestionSheets.Remove(sheet);
        }
        public SheetIndexWithScope CreateGlobal(QuestionSheetCreate data)
        {
            var parentSheet = this.context.QuestionSheets
                              .SingleOrDefault(x => x.Id == data.ParentSheetId);

            if (parentSheet.IsGlobal == false)
            {
                throw new ServiceException("Bad Request!");
            }

            var order  = 0;
            var orders = context.QuestionSheets
                         .Where(x => x.QuestionSheetId == data.ParentSheetId)
                         .Select(x => x.Order)
                         .ToArray();

            if (orders.Length > 0)
            {
                order = orders.Max() + 1;
            }

            var sheet = new QuestionSheet
            {
                Description     = data.Description,
                Difficulty      = data.Difficulty,
                Importance      = data.Importance.Value,
                Name            = data.Name,
                Order           = order,
                QuestionSheetId = data.ParentSheetId,
                IsGlobal        = true,
                UserId          = null,
            };

            context.QuestionSheets.Add(sheet);
            context.SaveChanges();

            return(new SheetIndexWithScope
            {
                isGlobal = true,
                data = Mapper.Map <QuestionSheetChildIndex>(sheet),
            });
        }
Beispiel #6
0
        public void SeedGlobalRoot()
        {
            if (context.QuestionSheets.Any(x => x.Name == Constants.GlobalRootQestionSheetName))
            {
                return;
            }

            var sheet = new QuestionSheet {
                Description     = "Global Root Question Sheet",
                Difficulty      = 1,
                Importance      = 10,
                Name            = Constants.GlobalRootQestionSheetName,
                Order           = 1,
                QuestionSheetId = null,
                IsGlobal        = true,
                UserId          = null,
            };

            context.QuestionSheets.Add(sheet);
            context.SaveChanges();
        }