コード例 #1
0
        public async Task <IActionResult> PostCreate()
        {
            var scenario = new Scenerio
            {
                Json           = Request.Query["json"],
                CategoryID     = int.Parse(Request.Query["category"]),
                TypeID         = int.Parse(Request.Query["categorytype"]),
                Name           = Request.Query["name"],
                ScenerioTasks  = new List <ScenerioTask>(),
                ViewCategoryID = int.Parse(Request.Query["categoryView"])
            };

            var track = new Track {
            };

            scenario.ScenerioTasks.Add(new ScenerioTask {
                Scenerio = scenario, Track = track
            });

            var flows = Request.Query.Where(x => x.Key.Contains("usertype_")).Select(x => x.Key.Replace("usertype_", string.Empty));

            foreach (var flow in flows)
            {
                var action = new CommonLibrary.DatabaseModels.Action
                {
                    IsAuto          = Request.Query.ContainsKey($"auto_{flow}"),
                    ChangeFields    = new List <ChangeFieldInAction>(),
                    ValidateActions = new List <ValidateAction>(),
                    UserType        = (UserType)int.Parse(Request.Query[$"usertype_{flow}"]),
                    DaysCount       = int.Parse(Request.Query[$"day_{flow}"]),
                    State           = await NetworkMethodModel.Get <State>(_consulWrapper["track-api"]["States"], int.Parse(Request.Query[$"from_{flow}"])),
                    NextState       = await NetworkMethodModel.Get <State>(_consulWrapper["track-api"]["States"], int.Parse(Request.Query[$"to_{flow}"])),
                };
                track.ActionInTracks.Add(new ActionInTrack {
                    Action = action, Track = track
                });

                var groupFields = Request.Query.Where(x => x.Key.Contains($"select_{flow}")).ToList();
                foreach (var field in groupFields)
                {
                    action.ChangeFields.Add(new ChangeFieldInAction
                    {
                        Action      = action,
                        ChangeField = (await NetworkMethodModel.GetByName <ChangeField>(_consulWrapper["track-api"]["ChangeFields"], field.Value))
                    });
                }

                var groupValid = Request.Query.Where(x => x.Key.StartsWith(flow)).GroupBy(x => x.Key.Substring(x.Key.IndexOf("comp")));
                foreach (var validGroup in groupValid)
                {
                    action.ValidateActions.Add(new ValidateAction
                    {
                        Action        = action,
                        ValidateField = new ValidateField
                        {
                            Field     = validGroup.First().Value,
                            Operation = (await NetworkMethodModel.GetByName <Operation>(_consulWrapper["track-api"]["Operations"], validGroup.ElementAt(1).Value)),
                            With      = validGroup.Last().Value
                        }
                    });
                }
            }

            await NetworkMethodModel.Post(_consulWrapper["track-api"]["Scenerios"], scenario);

            return(Redirect("Index"));
        }