예제 #1
0
        public Task <HttpRequestResult <Common.Models.Program> > AddProgram(Common.Models.Program program)
        {
            var fileContents = System.IO.File.ReadAllText(_fileName);
            var items        = new List <Common.Models.Program>();

            if (!String.IsNullOrEmpty(fileContents))
            {
                try
                {
                    var deserialized = JsonConvert.DeserializeObject <List <Common.Models.Program> > (fileContents);
                    items = deserialized;
                }
                catch { }
            }

            var highestIdNode = items.OrderByDescending(x => x.Id).FirstOrDefault();
            var id            = highestIdNode == null ? 1 : highestIdNode.Id + 1;

            program.Id = id;
            items.Add(program);

            try
            {
                System.IO.File.WriteAllText(_fileName, JsonConvert.SerializeObject(items));
            }
            catch (Exception ex)
            {
                return(Task.FromResult(new HttpRequestResult <Common.Models.Program>(ex.StackTrace)));
            }

            return(Task.FromResult(new HttpRequestResult <Common.Models.Program>(program)));
        }
        public async Task <ActionResult> SaveAsProgram(SaveAsProgramViewModel model)
        {
            var program     = new Common.Models.Program();
            var nodes       = new List <Node>();
            var allNodesRes = await _nodesService.GetAllNodes();

            var allNodes = allNodesRes.result;

            foreach (var id in model.NodeIds)
            {
                var node = allNodes.FirstOrDefault(x => x.Id == id);
                if (node != null)
                {
                    nodes.Add(node);
                }
            }

            program.Nodes = nodes;
            program.Name  = model.ProgramName;

            //Find all input values
            var firstNode = nodes.First();

            for (int i = 0; i < firstNode.Rule.LeftSide.Count; i++)
            {
                program.Inputs.Add(new ProgramInput()
                {
                    Letter = firstNode.Rule.LeftSide[i],
                    NodeId = firstNode.Id,
                    Type   = firstNode.InputDataType[i]
                });
            }

            var nodesWithoutFirst     = nodes.Where(x => x.Id != firstNode.Id).ToList();
            var nodesWithoutFirstCopy = new List <Node>(nodesWithoutFirst);

            foreach (var node in nodesWithoutFirst)
            {
                for (int i = 0; i < node.Rule.LeftSide.Count; i++)
                {
                    if (!nodesWithoutFirstCopy.Any(x => x.Rule.RightSide == node.Rule.LeftSide[i]) &&
                        firstNode.Rule.RightSide != node.Rule.LeftSide[i])
                    {
                        program.Inputs.Add(new ProgramInput()
                        {
                            Letter = node.Rule.LeftSide[i],
                            NodeId = node.Id,
                            Type   = node.InputDataType[i]
                        });
                    }
                }
            }

            await _programService.AddProgram(program);

            return(Json("Gamybos planas išsaugotas."));
        }