public ActionResult <MacroDisplay> Save(MacroDisplay macroDisplay)
    {
        if (macroDisplay == null)
        {
            return(ValidationProblem("No macro data found in request"));
        }

        if (macroDisplay.Name == null || macroDisplay.Name.Length > 255)
        {
            return(ValidationProblem("Name cannnot be more than 255 characters in length."));
        }

        IMacro?macro = macroDisplay.Id is null
            ? null
            : _macroService.GetById(int.Parse(macroDisplay.Id.ToString() !, CultureInfo.InvariantCulture));

        if (macro == null)
        {
            return(ValidationProblem($"Macro with id {macroDisplay.Id} does not exist"));
        }

        if (macroDisplay.Alias != macro.Alias)
        {
            IMacro?macroByAlias = _macroService.GetByAlias(macroDisplay.Alias);

            if (macroByAlias != null)
            {
                return(ValidationProblem("Macro with this alias already exists"));
            }
        }

        macro.Alias         = macroDisplay.Alias;
        macro.Name          = macroDisplay.Name;
        macro.CacheByMember = macroDisplay.CacheByUser;
        macro.CacheByPage   = macroDisplay.CacheByPage;
        macro.CacheDuration = macroDisplay.CachePeriod;
        macro.DontRender    = !macroDisplay.RenderInEditor;
        macro.UseInEditor   = macroDisplay.UseInEditor;
        macro.MacroSource   = macroDisplay.View;
        macro.Properties.ReplaceAll(
            macroDisplay.Parameters.Select((x, i) => new MacroProperty(x.Key, x.Label, i, x.Editor)));

        try
        {
            _macroService.Save(macro, _backofficeSecurityAccessor.BackOfficeSecurity?.CurrentUser?.Id ?? -1);

            macroDisplay.Notifications.Clear();

            macroDisplay.Notifications.Add(new BackOfficeNotification("Success", "Macro saved", NotificationStyle.Success));

            return(macroDisplay);
        }
        catch (Exception exception)
        {
            const string errorMessage = "Error creating macro";
            _logger.LogError(exception, errorMessage);
            return(ValidationProblem(errorMessage));
        }
    }
        public HttpResponseMessage Save(MacroDisplay macroDisplay)
        {
            if (macroDisplay == null)
            {
                return(this.ReturnErrorResponse($"No macro data found in request"));
            }

            if (macroDisplay.Name == null || macroDisplay.Name.Length > 255)
            {
                return(this.ReturnErrorResponse("Name cannnot be more than 255 characters in length."));
            }

            var macro = _macroService.GetById(int.Parse(macroDisplay.Id.ToString()));

            if (macro == null)
            {
                return(this.ReturnErrorResponse($"Macro with id {macroDisplay.Id} does not exist"));
            }

            if (macroDisplay.Alias != macro.Alias)
            {
                var macroByAlias = _macroService.GetByAlias(macroDisplay.Alias);

                if (macroByAlias != null)
                {
                    return(this.ReturnErrorResponse("Macro with this alias already exists"));
                }
            }

            macro.Alias         = macroDisplay.Alias;
            macro.Name          = macroDisplay.Name;
            macro.CacheByMember = macroDisplay.CacheByUser;
            macro.CacheByPage   = macroDisplay.CacheByPage;
            macro.CacheDuration = macroDisplay.CachePeriod;
            macro.DontRender    = !macroDisplay.RenderInEditor;
            macro.UseInEditor   = macroDisplay.UseInEditor;
            macro.MacroSource   = macroDisplay.View;
            macro.MacroType     = MacroTypes.PartialView;
            macro.Properties.ReplaceAll(macroDisplay.Parameters.Select((x, i) => new MacroProperty(x.Key, x.Label, i, x.Editor)));

            try
            {
                _macroService.Save(macro, this.Security.CurrentUser.Id);

                macroDisplay.Notifications.Clear();

                macroDisplay.Notifications.Add(new Models.ContentEditing.Notification("Success", "Macro saved", NotificationStyle.Success));

                return(this.Request.CreateResponse(HttpStatusCode.OK, macroDisplay));
            }
            catch (Exception exception)
            {
                return(this.ReturnErrorResponse("Error creating macro", true, exception));
            }
        }
 private void Map(IMacro source, MacroDisplay target, MapperContext context)
 {
     target.Alias          = source.Alias;
     target.Icon           = Constants.Icons.Macro;
     target.Id             = source.Id;
     target.Key            = source.Key;
     target.Name           = source.Name;
     target.ParentId       = -1;
     target.Path           = "-1," + source.Id;
     target.Udi            = Udi.Create(Constants.UdiEntityType.Macro, source.Key);
     target.CacheByPage    = source.CacheByPage;
     target.CacheByUser    = source.CacheByMember;
     target.CachePeriod    = source.CacheDuration;
     target.UseInEditor    = source.UseInEditor;
     target.RenderInEditor = !source.DontRender;
     target.View           = source.MacroSource;
 }
Exemple #4
0
        public HttpResponseMessage GetById(int id)
        {
            var macro = _macroService.GetById(id);

            if (macro == null)
            {
                return(this.ReturnErrorResponse($"Macro with id {id} does not exist"));
            }

            var macroDisplay = new MacroDisplay
            {
                Alias          = macro.Alias,
                Id             = macro.Id,
                Key            = macro.Key,
                Name           = macro.Name,
                CacheByPage    = macro.CacheByPage,
                CacheByUser    = macro.CacheByMember,
                CachePeriod    = macro.CacheDuration,
                View           = macro.MacroSource,
                RenderInEditor = !macro.DontRender,
                UseInEditor    = macro.UseInEditor,
                Path           = $"-1,{macro.Id}"
            };

            var parameters = new List <MacroParameterDisplay>();

            foreach (var param in macro.Properties.Values.OrderBy(x => x.SortOrder))
            {
                parameters.Add(new MacroParameterDisplay
                {
                    Editor = param.EditorAlias,
                    Key    = param.Alias,
                    Label  = param.Name,
                    Id     = param.Id
                });
            }

            macroDisplay.Parameters = parameters;

            return(this.Request.CreateResponse(HttpStatusCode.OK, macroDisplay));
        }