Esempio n. 1
0
        public async Task <IActionResult> Execute(JObject jObj, string subject, string commonId, HttpRequest request)
        {
            if (jObj == null)
            {
                throw new ArgumentNullException(nameof(jObj));
            }

            if (string.IsNullOrWhiteSpace(subject))
            {
                throw new ArgumentNullException(nameof(subject));
            }

            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            // 1. Check the request
            AddProductCommand command = null;

            try
            {
                command = _requestBuilder.GetAddProduct(jObj);
            }
            catch (ArgumentException ex)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, ex.Message);
                return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error));
            }

            var validationResult = await _validator.Validate(command, subject);

            if (!validationResult.IsValid)
            {
                var error = _responseBuilder.GetError(ErrorCodes.Request, validationResult.Message);
                return(_controllerHelper.BuildResponse(HttpStatusCode.BadRequest, error));
            }

            // 2. Add images
            var images = new List <string>();

            if (command.PartialImagesUrl != null)
            {
                foreach (var image in command.PartialImagesUrl)
                {
                    string path;
                    if (!AddImage(image, request, out path))
                    {
                        continue;
                    }

                    images.Add(path);
                }
            }

            command.Id               = Guid.NewGuid().ToString();
            command.CreateDateTime   = DateTime.UtcNow;
            command.UpdateDateTime   = DateTime.UtcNow;
            command.CommonId         = commonId;
            command.PartialImagesUrl = images;
            var res = new { id = command.Id };

            _commandSender.Send(command);
            return(new OkObjectResult(res));
        }