コード例 #1
0
        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),
            });
        }