示例#1
0
    public async Task <IActionResult> OnPostMarkAsActive()
    {
        TemplateVersionTbl?version = (await _templateVersionTbl.Get(x =>
                                                                    x.Id.Equals(MarkAsActive.VersionId) &&
                                                                    x.TemplateId.Equals(MarkAsActive.TemplateId) &&
                                                                    x.Template !.ProjectId.Equals(MarkAsActive.ProjectId)))
                                     .FirstOrDefault();

        if (version == null)
        {
            throw new NullReferenceException(nameof(version));
        }

        version.IsActive = true;

        _templateVersionTbl.Update(version);

        await _templateVersionTbl.UpdateFromQuery(x =>
                                                  x.IsActive &&
                                                  !x.Id.Equals(version.Id) &&
                                                  x.TemplateId.Equals(version.TemplateId),
                                                  _ =>
                                                  new TemplateVersionTbl
        {
            IsActive = false
        });

        await _templateTbl.UpdateFromQuery(x => x.Id.Equals(MarkAsActive.TemplateId), _ => new TemplateTbl
        {
            DateModified = DateTime.Now
        });

        await _projectTbl.UpdateFromQuery(x => x.Id.Equals(MarkAsActive.ProjectId), _ => new ProjectTbl
        {
            DateModified = DateTime.Now
        });

        TempData["toastStatus"]  = "success";
        TempData["toastMessage"] = "Template version marked as active";
        TempData["scrollToId"]   = $"version-{version.Id}";

        return(RedirectToPage("/Project/Details", new { id = MarkAsActive.ProjectId }));
    }
示例#2
0
    public async Task <IActionResult> OnPostUpdateTemplate()
    {
        UpdateTemplate.Html     = string.IsNullOrWhiteSpace(UpdateTemplate.Html) ? string.Empty : UpdateTemplate.Html;
        UpdateTemplate.TestData = string.IsNullOrWhiteSpace(UpdateTemplate.TestData) ? "{}" : UpdateTemplate.TestData;
        try
        {
            Handlebars.RegisterHelper("ifCond", (output, options, context, arguments) =>
            {
                if (arguments.Length != 3)
                {
                    throw new HandlebarsException("{{#StringEqualityBlockHelper}} helper must have exactly two arguments");
                }

                string v1        = arguments.At <string>(0);
                string @operator = arguments.At <string>(1);
                string v2        = arguments.At <string>(0);

                switch (@operator)
                {
                case "==":
                    if (v1 == v2)
                    {
                        options.Template(output, context);
                    }
                    else
                    {
                        options.Inverse(output, context);
                    }
                    break;

                case "!=":
                    if (v1 != v2)
                    {
                        options.Template(output, context);
                    }
                    else
                    {
                        options.Inverse(output, context);
                    }
                    break;

                case "<":
                    if (Convert.ToDouble(v1) < Convert.ToDouble(v2))
                    {
                        options.Template(output, context);
                    }
                    else
                    {
                        options.Inverse(output, context);
                    }
                    break;

                case "<=":
                    if (Convert.ToDouble(v1) <= Convert.ToDouble(v2))
                    {
                        options.Template(output, context);
                    }
                    else
                    {
                        options.Inverse(output, context);
                    }
                    break;

                case ">":
                    if (Convert.ToDouble(v1) > Convert.ToDouble(v2))
                    {
                        options.Template(output, context);
                    }
                    else
                    {
                        options.Inverse(output, context);
                    }
                    break;

                case ">=":
                    if (Convert.ToDouble(v1) >= Convert.ToDouble(v2))
                    {
                        options.Template(output, context);
                    }
                    else
                    {
                        options.Inverse(output, context);
                    }
                    break;
                }
            });
            HandlebarsTemplate <object, object> template = Handlebars.Compile(UpdateTemplate.Html);
            template(JObject.Parse(UpdateTemplate.TestData));
        }
        catch (Exception)
        {
            TempData["toastStatus"]  = "error";
            TempData["toastMessage"] = "Parsing template failed. Please check ";

            return(RedirectToPage("/Project/Template", new
            {
                projectId = UpdateTemplate.ProjectId,
                templateId = UpdateTemplate.TemplateId,
                versionId = UpdateTemplate.VersionId
            }));
        }

        // TODO: Error handling
        TemplateVersionTbl?version = (await _templateVersionTbl.Get(x =>
                                                                    x.Id.Equals(UpdateTemplate.VersionId) &&
                                                                    x.TemplateId.Equals(UpdateTemplate.TemplateId) &&
                                                                    x.Template !.ProjectId.Equals(UpdateTemplate.ProjectId)))
                                     .FirstOrDefault();

        if (version == null)
        {
            throw new NullReferenceException();
        }

        version.TestData = UpdateTemplate.TestData;
        version.Html     = UpdateTemplate.Html;

        _templateVersionTbl.Update(version);

        await _templateTbl.UpdateFromQuery(x => x.Id.Equals(UpdateTemplate.TemplateId), _ => new TemplateTbl
        {
            DateModified = DateTime.Now
        });

        await _projectTbl.UpdateFromQuery(x => x.Id.Equals(UpdateTemplate.ProjectId), _ => new ProjectTbl
        {
            DateModified = DateTime.Now
        });

        // TODO: Generate thumbnail
        _jobClient.Enqueue(() => GenerateThumbnailAndPreview(version.Id));

        TempData["toastStatus"]  = "success";
        TempData["toastMessage"] = "Template updated";

        return(RedirectToPage("/Project/Template", new
        {
            projectId = UpdateTemplate.ProjectId,
            templateId = UpdateTemplate.TemplateId,
            versionId = UpdateTemplate.VersionId
        }));
    }