Ejemplo n.º 1
0
            public async Task <ScriptCreated> Handle(CreateScriptCommand request, CancellationToken cancellationToken)
            {
                var script = new Script
                {
                    Id              = 0,
                    Name            = request.Name,
                    Description     = request.Description,
                    GroupName       = request.GroupName,
                    Author          = request.Author,
                    AccordingTo     = request.AccordingTo,
                    Notes           = request.Notes,
                    DefaultLanguage = request.DefaultLanguage,
                    Added           = _dateTime.Now,
                    Modified        = _dateTime.Now,
                    Version         = "1"
                };

                RemoveNotAddedTags(request, script);
                AddNewTags(request, script);

                await _scriptRepository.AddAsync(script);

                await _unitOfWork.CompleteAsync(cancellationToken);

                await _mediator.Publish(new ScriptCreated { Id = script.Id }, cancellationToken);

                // return Unit.Value; HACK: Needs to be this
                return(new ScriptCreated {
                    Id = script.Id
                });
            }
Ejemplo n.º 2
0
        public async Task <Response <BaseDto> > AddAsync(ScriptBindingModel scriptBindingModel, string userName)
        {
            var result   = new Response <BaseDto>();
            var lines    = scriptBindingModel.Script.Split('\n').Length;
            var robot    = _robotRepository.Get(r => r.Id == scriptBindingModel.RobotId);
            var user     = _userRepository.Get(u => u.Username == userName);
            var filePath = $@"../Scripts/{userName}/{scriptBindingModel.Name}.rl";

            if (user == null)
            {
                result.Errors.Add("Something went wrong, please log out and log in to proceed");
                return(result);
            }

            var player = _playerRepository.Get(p => p.UserId == user.Id);

            if (robot == null)
            {
                result.Errors.Add("The robot you have selected does not exist");
                return(result);
            }
            if (File.Exists(filePath))
            {
                result.Errors.Add($"A script named {scriptBindingModel.Name} already exists");
                return(result);
            }
            //try
            //{
            //    File.CreateText(filePath);
            //}
            //catch (Exception e)
            //{
            //    result.Errors.Add(e.Message);
            //    result.Errors.Add(e.InnerException.Message);
            //    return result;
            //}
            using (var writer = File.CreateText(filePath))
            {
                try
                {
                    writer.Write(scriptBindingModel.Script); //or .Write(), if you wish
                }
                catch (Exception e)
                {
                    result.Errors.Add(e.Message);
                    result.Errors.Add(e.InnerException.Message);
                    return(result);
                }
            }


            //here goes script validation

            var script = new Script
            {
                CreatedAt  = DateTime.Now,
                ForRobot   = robot,
                Lines      = lines,
                Name       = scriptBindingModel.Name,
                ServerPath = filePath,
                Owner      = player,
                UpdatedAt  = DateTime.Now,
            };

            var insertResult = await _scriptRepository.AddAsync(script);

            if (!insertResult)
            {
                result.Errors.Add("Something went wrong, please contact DevBots support");
                return(result);
            }

            return(result);
        }