public async Task <IActionResult> PutScenerio(int id, Scenerio scenerio)
        {
            if (id != scenerio.ID)
            {
                return(BadRequest());
            }

            context.Entry(scenerio).State = EntityState.Modified;
            foreach (var s in scenerio.ScenerioTasks)
            {
                s.Scenerio = scenerio;
                foreach (var a in s.Track.ActionInTracks)
                {
                    a.Track            = s.Track;
                    a.Action.State     = context.States.Find(a.Action.State.ID);
                    a.Action.NextState = context.States.Find(a.Action.NextState.ID);
                    a.Action.ChangeFields.ForEach(x =>
                    {
                        x.ChangeField = context.ChangeFields.Find(x.ChangeField.ID);
                    });
                    a.Action.ValidateActions.ForEach(x =>
                    {
                        x.ValidateField = context.ValidateField.Find(x.ValidateField.ID);
                    });
                    context.Add(a);
                    context.Add(a.Action);
                }
                context.Add(s);
                context.Add(s.Track);
            }

            try
            {
                await context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ScenerioExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
        public async Task <ActionResult <Scenerio> > PostScenerio(Scenerio scenerio)
        {
            scenerio.ScenerioTasks.ForEach(x =>
            {
                x.Track.ActionInTracks.ForEach(y =>
                {
                    y.Action.State     = context.States.Find(y.Action.State.ID);
                    y.Action.NextState = context.States.Find(y.Action.NextState.ID);
                    y.Action.ChangeFields.ForEach(z =>
                    {
                        z.ChangeField = context.ChangeFields.Find(z.ChangeField.ID);
                    });
                    y.Action.ValidateActions.ForEach(z =>
                    {
                        z.ValidateField.Operation = context.Operations.Find(z.ValidateField.Operation.ID);
                    });
                });
            });

            context.Scenerios.Add(scenerio);
            await context.SaveChangesAsync();

            return(CreatedAtAction("GetScenerio", new { id = scenerio.ID }, scenerio));
        }
示例#3
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"));
        }