コード例 #1
0
        public async Task <IActionResult> PostGenerate([FromBody] RoboFanGenerate generate, CancellationToken ct = default)
        {
            try
            {
                // ensure input values are valid
                _log.Information("PostGenerate: [{0}]", generate.Num);
                if ((generate.Num <= 0) || (generate.Num > 10))
                {
                    return(BadRequest());
                }

                // determine the robot image path and generate the requested number of fans
                var rootpath = _hostingEnvironment.WebRootPath;
                var robopath = System.IO.Path.Combine(rootpath, "images/robots");
                var listfans = await RoboFanGenerator.GenerateAsync(generate.Num, robopath, true);

                var status = await _roboFanRepository.AddManyAsync(listfans, ct);

                return(StatusCode(201, status));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex));
            }
        }
コード例 #2
0
        public async Task <IActionResult> PostCreate([FromBody] RoboFanCreate create, CancellationToken ct = default)
        {
            try
            {
                // determine the robot image path
                _log.Information("PostCreate: [{0} {1}]", create.FirstName, create.LastName);
                var rootpath = _hostingEnvironment.WebRootPath;
                var robopath = System.IO.Path.Combine(rootpath, "images/robots");

                // generate a random fan and overide values received from this post request
                // todo: look into setting up automapper?
                var listfans = await RoboFanGenerator.GenerateAsync(1, robopath, true);

                if (!string.IsNullOrEmpty(create.FirstName))
                {
                    listfans[0].FirstName = create.FirstName;
                }
                if (!string.IsNullOrEmpty(create.LastName))
                {
                    listfans[0].LastName = create.LastName;
                }
                if (!string.IsNullOrEmpty(create.Address))
                {
                    listfans[0].Address = create.Address;
                }
                if (!string.IsNullOrEmpty(create.City))
                {
                    listfans[0].City = create.City;
                }
                if (!string.IsNullOrEmpty(create.State))
                {
                    listfans[0].State = create.State;
                }

                // add the new fan to the databasse
                var status = await _roboFanRepository.AddManyAsync(listfans, ct);

                return(StatusCode(201, status));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex));
            }
        }