示例#1
0
        public async Task <IHttpActionResult> SaveByLocal([FromBody] ImagenLocalFormModel formModel)
        {
            GenericResult itemResult = null;

            try
            {
                if (await _authorizationService.AuthorizeAsync(User))
                {
                    var imagenLocal = _localService.GetImage(formModel.Id) ?? new ImagenLocal();

                    if (imagenLocal.Id == Guid.Empty)
                    {
                        imagenLocal.CreadoEn  = DateTime.Now;
                        imagenLocal.CreadoPor = User.Identity.Name;
                    }

                    imagenLocal.ActualizadoEn  = DateTime.Now;
                    imagenLocal.ActualizadoPor = User.Identity.Name;
                    imagenLocal.Descripcion    = !string.IsNullOrEmpty(formModel.Descripcion) ? formModel.Descripcion : "";
                    imagenLocal.Tipo           = formModel.Tipo;
                    imagenLocal.Orden          = formModel.Orden;

                    var local = _localService.Get(formModel.LocalId);
                    imagenLocal.Local = local;

                    if (!string.IsNullOrEmpty(formModel.ImagenData))
                    {
                        var imageBase64 = formModel.ImagenData.Replace("data:image/jpeg;base64,", "");
                        var imageBytes  = Convert.FromBase64String(imageBase64);
                        var imageUrl    = _blobImageService.UploadImage(imageBytes, BlobContainers.Locales());
                        imagenLocal.Imagen = imageUrl;
                    }

                    ActionConfirmation confirmation = _localService.SaveOrUpdateImagen(imagenLocal);
                    if (confirmation.WasSuccessful)
                    {
                        itemResult = GenericResult.Ok(confirmation.Message);
                        var item = confirmation.Value as ImagenLocal;
                        itemResult.ReturnValue = new { Id = item.Id };
                    }
                    else
                    {
                        itemResult = GenericResult.Failure(confirmation.Message);
                    }
                }
            }
            catch (Exception exception)
            {
                itemResult = GenericResult.Failure(exception.Message);
            }

            return(Ok(itemResult));
        }