Esempio n. 1
0
        public async Task <IActionResult> Update(string id, [FromForm] AutomationViewModel request)
        {
            Guid entityId           = new Guid(id);
            var  existingAutomation = repository.GetOne(entityId);

            if (existingAutomation == null)
            {
                return(NotFound());
            }

            if (request.File == null)
            {
                ModelState.AddModelError("Save", "No data passed");
                return(BadRequest(ModelState));
            }

            long size = request.File.Length;

            if (size <= 0)
            {
                ModelState.AddModelError("Automation Upload", $"File size of automation {request.File.FileName} cannot be 0");
                return(BadRequest(ModelState));
            }

            string binaryObjectId = existingAutomation.BinaryObjectId.ToString();
            var    binaryObject   = binaryObjectRepo.GetOne(Guid.Parse(binaryObjectId));
            string organizationId = binaryObject.OrganizationId.ToString();

            if (!string.IsNullOrEmpty(organizationId))
            {
                organizationId = manager.GetOrganizationId().ToString();
            }

            try
            {
                BinaryObject newBinaryObject = new BinaryObject();
                if (existingAutomation.BinaryObjectId != Guid.Empty && size > 0)
                {
                    string apiComponent = "AutomationAPI";
                    //update file in OpenBots.Server.Web using relative directory
                    newBinaryObject.Id                  = Guid.NewGuid();
                    newBinaryObject.Name                = request.File.FileName;
                    newBinaryObject.Folder              = apiComponent;
                    newBinaryObject.StoragePath         = Path.Combine("BinaryObjects", organizationId, apiComponent, newBinaryObject.Id.ToString());
                    newBinaryObject.CreatedBy           = applicationUser?.UserName;
                    newBinaryObject.CreatedOn           = DateTime.UtcNow;
                    newBinaryObject.CorrelationEntityId = request.Id;
                    binaryObjectRepo.Add(newBinaryObject);
                    binaryObjectManager.Upload(request.File, organizationId, apiComponent, newBinaryObject.Id.ToString());
                    binaryObjectManager.SaveEntity(request.File, newBinaryObject.StoragePath, newBinaryObject, apiComponent, organizationId);
                    binaryObjectRepo.Update(binaryObject);
                }

                //update automation (create new automation and automation version entities)
                Automation        response          = existingAutomation;
                AutomationVersion automationVersion = automationVersionRepo.Find(null, q => q.AutomationId == response.Id).Items?.FirstOrDefault();
                if (existingAutomation.Name.Trim().ToLower() != request.Name.Trim().ToLower() || automationVersion.Status.Trim().ToLower() != request.Status?.Trim().ToLower())
                {
                    existingAutomation.OriginalPackageName = request.File.FileName;
                    existingAutomation.AutomationEngine    = request.AutomationEngine;
                    automationVersion.Status = request.Status;
                }
                else
                {
                    request.BinaryObjectId = newBinaryObject.Id;
                    response = manager.UpdateAutomation(existingAutomation, request);
                }

                await webhookPublisher.PublishAsync("Files.NewFileCreated", newBinaryObject.Id.ToString(), newBinaryObject.Name).ConfigureAwait(false);

                await webhookPublisher.PublishAsync("Automations.AutomationUpdated", existingAutomation.Id.ToString(), existingAutomation.Name).ConfigureAwait(false);

                return(Ok(response));
            }
            catch (Exception ex)
            {
                return(ex.GetActionResult());
            }
        }
        public async Task <IActionResult> Put(string id, [FromForm] UpdateAssetViewModel request)
        {
            try
            {
                Guid entityId      = new Guid(id);
                var  existingAsset = repository.GetOne(Guid.Parse(id));
                if (existingAsset == null)
                {
                    ModelState.AddModelError("Asset", "Asset cannot be found or does not exist.");
                    return(NotFound(ModelState));
                }

                string binaryObjectId = existingAsset.BinaryObjectID.ToString();
                var    binaryObject   = binaryObjectRepo.GetOne(Guid.Parse(binaryObjectId));

                string organizationId = binaryObject.OrganizationId.ToString();
                if (!string.IsNullOrEmpty(organizationId))
                {
                    organizationId = automationManager.GetOrganizationId().ToString();
                }

                if (request.Name != null)
                {
                    var asset = repository.Find(null, d => d.Name.ToLower(null) == request.Name.ToLower(null))?.Items?.FirstOrDefault();
                    if (asset != null && asset.Id != entityId)
                    {
                        ModelState.AddModelError("Asset", "Asset name already exists");
                        return(BadRequest(ModelState));
                    }
                }
                if (request.File == null)
                {
                    ModelState.AddModelError("Save", "No asset uploaded");
                    return(BadRequest(ModelState));
                }

                long size = request.File == null ? 0 : request.File.Length;
                if (size <= 0)
                {
                    ModelState.AddModelError("Asset Upload", $"File size of asset {request.File.FileName} cannot be 0");
                    return(BadRequest(ModelState));
                }

                try
                {
                    if (!string.IsNullOrEmpty(request.Name))
                    {
                        existingAsset.Name = request.Name;
                    }
                    else
                    {
                        existingAsset.Name = existingAsset.Name;
                    }

                    if (!string.IsNullOrEmpty(request.Type))
                    {
                        existingAsset.Type = request.Type;
                    }
                    else
                    {
                        existingAsset.Type = existingAsset.Type;
                    }

                    existingAsset.TextValue   = request.TextValue;
                    existingAsset.NumberValue = request.NumberValue;
                    existingAsset.JsonValue   = request.JsonValue;
                    existingAsset.SizeInBytes = request.File.Length;

                    if (existingAsset.BinaryObjectID != Guid.Empty && size > 0)
                    {
                        //update asset file in OpenBots.Server.Web using relative directory
                        string apiComponent = "AssetAPI";
                        await automationManager.Update(existingAsset.BinaryObjectID.Value, request.File, organizationId, apiComponent, request.File.FileName);
                    }

                    //update asset entity
                    await webhookPublisher.PublishAsync("Assets.AssetUpdated", existingAsset.Id.ToString(), existingAsset.Name).ConfigureAwait(false);

                    await base.PutEntity(id, existingAsset);

                    return(Ok(existingAsset));
                }
                catch (Exception ex)
                {
                    return(ex.GetActionResult());
                }
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("Asset", ex.Message);
                return(BadRequest(ModelState));
            }
        }