public override async Task <ActionResult> HandleAsync(Command request, CancellationToken cancellationToken) { var projectId = User.GetProjectId(); using var reader = new StreamReader(Request.Body); var script = await reader.ReadToEndAsync(); var mediaType = Request.GetTypedHeaders().ContentType.MediaType.Value; var generator = _generatorProvider.GetGenerator(mediaType); var errors = generator.ValidateTemplate(script); if (errors.Length > 0) { return(BadRequest(errors)); } var template = new Template(projectId, request.Name, script, mediaType); _db.Templates.Add(template); await _db.SaveChangesAsync(); return(RedirectToEndpoint(new GetById.Query() { Id = template.Id })); }
public override async Task <ActionResult> HandleAsync(Command request, CancellationToken cancellationToken) { var projectId = User.GetProjectId(); var template = await _db.Templates.SingleOrDefaultAsync(x => x.ProjectId == projectId && x.Id == request.Id); if (template is null) { return(NotFound()); } var mediaType = Request.GetTypedHeaders().ContentType.MediaType.Value; using var reader = new StreamReader(Request.Body); var script = await reader.ReadToEndAsync(); var generator = _generatorProvider.GetGenerator(mediaType); var errors = generator.ValidateTemplate(script); if (errors.Length > 0) { return(BadRequest(errors)); } template.Script = script; template.ScriptMediaType = mediaType; await _db.SaveChangesAsync(); return(Ok()); }
public override async Task <ActionResult> HandleAsync(Command request, CancellationToken cancellationToken) { var projectId = User.GetProjectId(); if (request.ZoneId == 0) { if (User.TryGetZoneId(out var zoneId)) { request.ZoneId = zoneId; } else { return(Forbid()); } } var format = await _db.Formats .Where(x => x.Zone.ProjectId == projectId && x.ZoneId == request.ZoneId) .Select(x => new { x.Alias, x.TemplateId, x.Template.Script, x.Template.ScriptMediaType }) .SingleOrDefaultAsync(x => x.Alias == request.Format); if (format is null) { return(BadRequest("Invalid format")); } var generator = _generatorProvider.GetGenerator(format.ScriptMediaType); var resources = new DbResourceManager(_db, User.GetProjectId()); var result = await generator.GenerateDocumentAsync(format.Script, request.Model, resources); var document = new Document(User.GetProjectId(), format.TemplateId, result.Content, result.ContentType); _db.Documents.Add(document); await _db.SaveChangesAsync(); return(RedirectToEndpoint(new GetById.Query() { Id = document.Id })); }