Ejemplo n.º 1
0
        public async Task <DeviceModelScriptApiModel> PostAsync(IFormFile file)
        {
            if (file == null)
            {
                this.log.Warn("No data provided");
                throw new BadRequestException("No data provided.");
            }

            if (file.ContentType != TEXT_JAVASCRIPT)
            {
                this.log.Warn("Wrong content type provided", () => new { file.ContentType });
                throw new BadRequestException("Wrong content type provided.");
            }

            var deviceModelScript = new DeviceModelScript();

            try
            {
                var content = this.javascriptInterpreter.Validate(file.OpenReadStream());
                deviceModelScript.Content = content;
                deviceModelScript.Name    = file.FileName;
            }
            catch (Exception e)
            {
                throw new BadRequestException(e.Message);
            }

            return(DeviceModelScriptApiModel.FromServiceModel(await this.simulationScriptService.InsertAsync(deviceModelScript)));
        }
Ejemplo n.º 2
0
        public async Task <DeviceModelScriptApiModel> PutAsync(
            IFormFile file,
            string eTag,
            string id)
        {
            if (file == null)
            {
                this.log.Warn("No data provided");
                throw new BadRequestException("No data provided.");
            }

            if (string.IsNullOrEmpty(id))
            {
                this.log.Warn("No id provided");
                throw new BadRequestException("No id provided.");
            }

            if (string.IsNullOrEmpty(eTag))
            {
                this.log.Warn("No ETag provided");
                throw new BadRequestException("No ETag provided.");
            }

            var simulationScript = new DataFile
            {
                ETag = eTag,
                Id   = id
            };

            try
            {
                var reader = new StreamReader(file.OpenReadStream());
                simulationScript.Content = reader.ReadToEnd();
                simulationScript.Name    = file.FileName;
                simulationScript.Type    = ScriptInterpreter.JAVASCRIPT_SCRIPT;
            }
            catch (Exception e)
            {
                throw new BadRequestException(e.Message);
            }

            return(DeviceModelScriptApiModel.FromServiceModel(await this.simulationScriptService.UpsertAsync(simulationScript)));
        }
Ejemplo n.º 3
0
 public async Task <DeviceModelScriptApiModel> GetAsync(string id)
 {
     return(DeviceModelScriptApiModel.FromServiceModel(await this.simulationScriptService.GetAsync(id)));
 }