From 1e46482e0421ced79512f6f130d34af2ac22e0d4 Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 18 Jan 2017 23:43:47 +0100 Subject: [PATCH] Refactorings --- .../Contents/MongoContentRepository.cs | 20 ++ .../Contents/ContentCommandHandler.cs | 10 + .../Contents/ContentDomainObject.cs | 38 ++++ .../Schemas/SchemaDomainObject.cs | 4 + .../ContentApi/ContentsController.cs | 22 +++ .../Generator/SchemasSwaggerGenerator.cs | 174 ++++++++++-------- .../app/features/content/declarations.ts | 1 + src/Squidex/app/features/content/module.ts | 2 + .../content/content-field.component.html | 4 +- .../pages/content/content-field.component.ts | 14 +- .../pages/content/content-page.component.html | 1 + .../pages/content/content-page.component.ts | 92 +++++---- .../contents/contents-page.component.html | 34 ++-- .../contents/contents-page.component.scss | 14 +- .../pages/contents/contents-page.component.ts | 117 +++++++++--- .../app/features/content/pages/messages.ts | 23 ++- .../pages/schemas/schemas-page.component.html | 3 +- .../app/framework/utils/immutable-array.ts | 7 + .../shared/services/contents.service.spec.ts | 32 ++++ .../app/shared/services/contents.service.ts | 18 +- src/Squidex/app/theme/_bootstrap.scss | 20 +- .../Contents/ContentCommandHandlerTests.cs | 28 +++ .../Contents/ContentDomainObjectTests.cs | 72 ++++++++ 23 files changed, 563 insertions(+), 187 deletions(-) diff --git a/src/Squidex.Store.MongoDb/Contents/MongoContentRepository.cs b/src/Squidex.Store.MongoDb/Contents/MongoContentRepository.cs index 9afee4029..2b1e7b9c5 100644 --- a/src/Squidex.Store.MongoDb/Contents/MongoContentRepository.cs +++ b/src/Squidex.Store.MongoDb/Contents/MongoContentRepository.cs @@ -182,6 +182,26 @@ namespace Squidex.Store.MongoDb.Contents }); } + protected Task On(ContentPublished @event, EnvelopeHeaders headers) + { + var collection = GetCollection(headers.SchemaId()); + + return collection.UpdateAsync(headers, x => + { + x.IsPublished = true; + }); + } + + protected Task On(ContentUnpublished @event, EnvelopeHeaders headers) + { + var collection = GetCollection(headers.SchemaId()); + + return collection.UpdateAsync(headers, x => + { + x.IsPublished = false; + }); + } + protected Task On(ContentDeleted @event, EnvelopeHeaders headers) { var collection = GetCollection(headers.SchemaId()); diff --git a/src/Squidex.Write/Contents/ContentCommandHandler.cs b/src/Squidex.Write/Contents/ContentCommandHandler.cs index 622e307e0..b261c80ac 100644 --- a/src/Squidex.Write/Contents/ContentCommandHandler.cs +++ b/src/Squidex.Write/Contents/ContentCommandHandler.cs @@ -57,6 +57,16 @@ namespace Squidex.Write.Contents await handler.UpdateAsync(command, s => s.Update(command)); } + protected Task On(PublishContent command, CommandContext context) + { + return handler.UpdateAsync(command, s => s.Publish(command)); + } + + protected Task On(UnpublishContent command, CommandContext context) + { + return handler.UpdateAsync(command, s => s.Unpublish(command)); + } + protected Task On(DeleteContent command, CommandContext context) { return handler.UpdateAsync(command, s => s.Delete(command)); diff --git a/src/Squidex.Write/Contents/ContentDomainObject.cs b/src/Squidex.Write/Contents/ContentDomainObject.cs index 8489ccb2e..6728c2218 100644 --- a/src/Squidex.Write/Contents/ContentDomainObject.cs +++ b/src/Squidex.Write/Contents/ContentDomainObject.cs @@ -21,12 +21,18 @@ namespace Squidex.Write.Contents { private bool isDeleted; private bool isCreated; + private bool isPublished; public bool IsDeleted { get { return isDeleted; } } + public bool IsPublished + { + get { return isPublished; } + } + public ContentDomainObject(Guid id, int version) : base(id, version) { @@ -37,6 +43,16 @@ namespace Squidex.Write.Contents isCreated = true; } + protected void On(ContentPublished @event) + { + isPublished = true; + } + + protected void On(ContentUnpublished @event) + { + isPublished = false; + } + protected void On(ContentDeleted @event) { isDeleted = true; @@ -75,6 +91,28 @@ namespace Squidex.Write.Contents return this; } + public ContentDomainObject Publish(PublishContent command) + { + Guard.NotNull(command, nameof(command)); + + VerifyCreatedAndNotDeleted(); + + RaiseEvent(SimpleMapper.Map(command, new ContentPublished())); + + return this; + } + + public ContentDomainObject Unpublish(UnpublishContent command) + { + Guard.NotNull(command, nameof(command)); + + VerifyCreatedAndNotDeleted(); + + RaiseEvent(SimpleMapper.Map(command, new ContentUnpublished())); + + return this; + } + private void VerifyNotCreated() { if (isCreated) diff --git a/src/Squidex.Write/Schemas/SchemaDomainObject.cs b/src/Squidex.Write/Schemas/SchemaDomainObject.cs index 9974d6c45..8f365785b 100644 --- a/src/Squidex.Write/Schemas/SchemaDomainObject.cs +++ b/src/Squidex.Write/Schemas/SchemaDomainObject.cs @@ -207,6 +207,8 @@ namespace Squidex.Write.Schemas public SchemaDomainObject Publish(PublishSchema command) { + Guard.NotNull(command, nameof(command)); + VerifyCreatedAndNotDeleted(); RaiseEvent(new SchemaPublished()); @@ -216,6 +218,8 @@ namespace Squidex.Write.Schemas public SchemaDomainObject Unpublish(UnpublishSchema command) { + Guard.NotNull(command, nameof(command)); + VerifyCreatedAndNotDeleted(); RaiseEvent(new SchemaUnpublished()); diff --git a/src/Squidex/Controllers/ContentApi/ContentsController.cs b/src/Squidex/Controllers/ContentApi/ContentsController.cs index 47f0c35ce..8589e2c5a 100644 --- a/src/Squidex/Controllers/ContentApi/ContentsController.cs +++ b/src/Squidex/Controllers/ContentApi/ContentsController.cs @@ -126,6 +126,28 @@ namespace Squidex.Controllers.ContentApi return NoContent(); } + [HttpPut] + [Route("content/{app}/{name}/{id}/publish")] + public async Task PublishContent(Guid id) + { + var command = new PublishContent { AggregateId = id }; + + await CommandBus.PublishAsync(command); + + return NoContent(); + } + + [HttpPut] + [Route("content/{app}/{name}/{id}/unpublish")] + public async Task UnpublishContent(Guid id) + { + var command = new UnpublishContent { AggregateId = id }; + + await CommandBus.PublishAsync(command); + + return NoContent(); + } + [HttpDelete] [Route("content/{app}/{name}/{id}")] public async Task PutContent(Guid id) diff --git a/src/Squidex/Controllers/ContentApi/Generator/SchemasSwaggerGenerator.cs b/src/Squidex/Controllers/ContentApi/Generator/SchemasSwaggerGenerator.cs index 2dcf89f3b..97fb75770 100644 --- a/src/Squidex/Controllers/ContentApi/Generator/SchemasSwaggerGenerator.cs +++ b/src/Squidex/Controllers/ContentApi/Generator/SchemasSwaggerGenerator.cs @@ -24,6 +24,7 @@ using Squidex.Infrastructure; using Squidex.Pipeline.Swagger; using Squidex.Read.Apps; using Squidex.Read.Schemas; +// ReSharper disable InvertIf // ReSharper disable SuggestBaseTypeForParameter // ReSharper disable PrivateFieldCanBeConvertedToLocalVariable @@ -81,6 +82,7 @@ When you change the field to be localizable the value will become the value for GenerateSchemasOperations(schemas); GenerateSecurityDefinitions(); GenerateSecurityRequirements(); + GenerateDefaultErrors(); return document; } @@ -152,6 +154,14 @@ When you change the field to be localizable the value will become the value for } } + private void GenerateDefaultErrors() + { + foreach (var operation in document.Paths.Values.SelectMany(x => x.Values)) + { + operation.Responses.Add("500", new SwaggerResponse { Description = "Operations failed with internal server error.", Schema = errorDtoSchema }); + } + } + private void GenerateSchemasOperations(IEnumerable schemas) { foreach (var schema in schemas.Select(x => x.Schema)) @@ -170,123 +180,135 @@ When you change the field to be localizable the value will become the value for Name = schemaName, Description = $"API to managed {schemaName} content elements." }); - var noIdItemOperations = - document.Paths.GetOrAdd($"{appBasePath}/{schema.Name}/", k => new SwaggerOperations()); - - var idItemOperations = - document.Paths.GetOrAdd($"{appBasePath}/{schema.Name}/{{id}}/", k => new SwaggerOperations()); - - GenerateSchemaQueryOperation(noIdItemOperations, schema, schemaName); - GenerateSchemaCreateOperation(noIdItemOperations, schema, schemaName); - - GenerateSchemaGetOperation(idItemOperations, schema, schemaName); - GenerateSchemaUpdateOperation(idItemOperations, schema, schemaName); - GenerateSchemaDeleteOperation(idItemOperations, schemaName); + var schemaOperations = new List + { + GenerateSchemaQueryOperation(schema, schemaName), + GenerateSchemaCreateOperation(schema, schemaName), + GenerateSchemaGetOperation(schema, schemaName), + GenerateSchemaUpdateOperation(schema, schemaName), + GenerateSchemaPublishOperation(schema, schemaName), + GenerateSchemaUnpublishOperation(schema, schemaName), + GenerateSchemaDeleteOperation(schema, schemaName) + }; - foreach (var operation in idItemOperations.Values.Union(noIdItemOperations.Values)) + foreach (var operation in schemaOperations.SelectMany(x => x.Values).Distinct()) { operation.Tags = new List { schemaName }; } + } - foreach (var operation in idItemOperations.Values) + private SwaggerOperations GenerateSchemaQueryOperation(Schema schema, string schemaName) + { + return AddOperation(SwaggerOperationMethod.Get, null, $"{appBasePath}/{schema.Name}", operation => { - operation.Responses.Add("404", - new SwaggerResponse { Description = $"App, schema or {schemaName} not found." }); + operation.Summary = $"Queries {schemaName} content elements."; - operation.Parameters.AddPathParameter("id", JsonObjectType.String, $"The id of the {schemaName} (GUID)."); - } + operation.Parameters.AddQueryParameter("take", JsonObjectType.Number, "The number of elements to take."); + operation.Parameters.AddQueryParameter("skip", JsonObjectType.Number, "The number of elements to skip."); + + operation.Parameters.AddQueryParameter("query", JsonObjectType.String, "Optional full text query skip."); + + var responseSchema = CreateContentsSchema(schema.BuildSchema(languages, AppendSchema), schemaName, schema.Name); + + operation.Responses.Add("200", + new SwaggerResponse { Description = $"{schemaName} content elements retrieved.", Schema = responseSchema }); + }); } - private void GenerateSchemaQueryOperation(SwaggerOperations operations, Schema schema, string schemaName) + private SwaggerOperations GenerateSchemaGetOperation(Schema schema, string schemaName) { - var operation = new SwaggerOperation + return AddOperation(SwaggerOperationMethod.Get, schemaName, $"{appBasePath}/{schema.Name}/{{id}}", operation => { - Summary = $"Queries {schemaName} content elements." - }; + operation.Summary = $"Get a {schemaName} content element."; - operation.Parameters.AddQueryParameter("take", JsonObjectType.Number, "The number of elements to take."); - operation.Parameters.AddQueryParameter("skip", JsonObjectType.Number, "The number of elements to skip."); + var responseSchema = CreateContentSchema(schema.BuildSchema(languages, AppendSchema), schemaName, schema.Name); - operation.Parameters.AddQueryParameter("query", JsonObjectType.String, "Optional full text query skip."); + operation.Responses.Add("200", + new SwaggerResponse { Description = $"{schemaName} element found.", Schema = responseSchema }); + }); + } - var responseSchema = CreateContentsSchema(schema.BuildSchema(languages, AppendSchema), schemaName, schema.Name); + private SwaggerOperations GenerateSchemaCreateOperation(Schema schema, string schemaName) + { + return AddOperation(SwaggerOperationMethod.Post, null, $"{appBasePath}/{schema.Name}", operation => + { + operation.Summary = $"Create a {schemaName} content element."; + + var bodySchema = AppendSchema($"{schema.Name}Dto", schema.BuildSchema(languages, AppendSchema)); - operation.Responses.Add("200", - new SwaggerResponse { Description = $"{schemaName} content elements retrieved.", Schema = responseSchema }); - operation.Responses.Add("500", - new SwaggerResponse { Description = $"Querying {schemaName} element failed with internal server error.", Schema = errorDtoSchema }); + operation.Parameters.AddBodyParameter(bodySchema, "data", string.Format(BodyDescription, schemaName)); - operations[SwaggerOperationMethod.Get] = operation; + operation.Responses.Add("201", + new SwaggerResponse { Description = $"{schemaName} created.", Schema = entityCreatedDtoSchema }); + }); } - private void GenerateSchemaCreateOperation(SwaggerOperations operations, Schema schema, string schemaName) + private SwaggerOperations GenerateSchemaUpdateOperation(Schema schema, string schemaName) { - var operation = new SwaggerOperation + return AddOperation(SwaggerOperationMethod.Put, schemaName, $"{appBasePath}/{schema.Name}/{{id}}", operation => { - Summary = $"Create a {schemaName} content element." - }; - - var bodySchema = AppendSchema($"{schema.Name}Dto", schema.BuildSchema(languages, AppendSchema)); + operation.Summary = $"Update a {schemaName} content element."; - operation.Parameters.AddBodyParameter(bodySchema, "data", string.Format(BodyDescription, schemaName)); + var bodySchema = AppendSchema($"{schema.Name}Dto", schema.BuildSchema(languages, AppendSchema)); - operation.Responses.Add("201", - new SwaggerResponse { Description = $"{schemaName} created.", Schema = entityCreatedDtoSchema }); - operation.Responses.Add("500", - new SwaggerResponse { Description = $"Creating {schemaName} element failed with internal server error.", Schema = errorDtoSchema }); + operation.Parameters.AddBodyParameter(bodySchema, "data", string.Format(BodyDescription, schemaName)); - operations[SwaggerOperationMethod.Post] = operation; + operation.Responses.Add("204", + new SwaggerResponse { Description = $"{schemaName} element updated." }); + }); } - private void GenerateSchemaGetOperation(SwaggerOperations operations, Schema schema, string schemaName) + private SwaggerOperations GenerateSchemaPublishOperation(Schema schema, string schemaName) { - var operation = new SwaggerOperation + return AddOperation(SwaggerOperationMethod.Put, schemaName, $"{appBasePath}/{schema.Name}/{{id}}/publish", operation => { - Summary = $"Gets a {schemaName} content element" - }; + operation.Summary = $"Publish a {schemaName} content element."; - var responseSchema = CreateContentSchema(schema.BuildSchema(languages, AppendSchema), schemaName, schema.Name); + operation.Responses.Add("204", + new SwaggerResponse { Description = $"{schemaName} element published." }); + }); + } - operation.Responses.Add("209", - new SwaggerResponse { Description = $"{schemaName} element found.", Schema = responseSchema }); - operation.Responses.Add("500", - new SwaggerResponse { Description = $"Retrieving {schemaName} element failed with internal server error.", Schema = errorDtoSchema }); + private SwaggerOperations GenerateSchemaUnpublishOperation(Schema schema, string schemaName) + { + return AddOperation(SwaggerOperationMethod.Put, schemaName, $"{appBasePath}/{schema.Name}/{{id}}/unpublish", operation => + { + operation.Summary = $"Unpublish a {schemaName} content element."; - operations[SwaggerOperationMethod.Get] = operation; + operation.Responses.Add("204", + new SwaggerResponse { Description = $"{schemaName} element unpublished." }); + }); } - private void GenerateSchemaUpdateOperation(SwaggerOperations operations, Schema schema, string schemaName) + private SwaggerOperations GenerateSchemaDeleteOperation(Schema schema, string schemaName) { - var operation = new SwaggerOperation + return AddOperation(SwaggerOperationMethod.Delete, schemaName, $"{appBasePath}/{schema.Name}/{{id}}/", operation => { - Summary = $"Update {schemaName} content element." - }; + operation.Summary = $"Delete a {schemaName} content element."; - var bodySchema = AppendSchema($"{schema.Name}Dto", schema.BuildSchema(languages, AppendSchema)); + operation.Responses.Add("204", + new SwaggerResponse { Description = $"{schemaName} element deleted." }); + }); + } - operation.Parameters.AddBodyParameter(bodySchema, "data", string.Format(BodyDescription, schemaName)); + private SwaggerOperations AddOperation(SwaggerOperationMethod method, string entityName, string path, Action updater) + { + var operations = document.Paths.GetOrAdd(path, k => new SwaggerOperations()); + var operation = new SwaggerOperation(); - operation.Responses.Add("204", - new SwaggerResponse { Description = $"{schemaName} element updated." }); - operation.Responses.Add("500", - new SwaggerResponse { Description = $"Updating {schemaName} element failed with internal server error.", Schema = errorDtoSchema }); + updater(operation); - operations[SwaggerOperationMethod.Put] = operation; - } + operations[method] = operation; - private void GenerateSchemaDeleteOperation(SwaggerOperations operations, string schemaName) - { - var operation = new SwaggerOperation + if (entityName != null) { - Summary = $"Delete a {schemaName} content element." - }; - - operation.Responses.Add("204", - new SwaggerResponse { Description = $"{schemaName} element deleted." }); - operation.Responses.Add("500", - new SwaggerResponse { Description = $"Deleting {schemaName} element failed with internal server error.", Schema = errorDtoSchema }); + operation.Parameters.AddPathParameter("id", JsonObjectType.String, $"The id of the {entityName} (GUID)."); + + operation.Responses.Add("404", + new SwaggerResponse { Description = $"App, schema or {entityName} not found." }); + } - operations[SwaggerOperationMethod.Delete] = operation; + return operations; } private JsonSchema4 CreateContentsSchema(JsonSchema4 dataSchema, string schemaName, string id) diff --git a/src/Squidex/app/features/content/declarations.ts b/src/Squidex/app/features/content/declarations.ts index db3f86fba..9b7e90ee5 100644 --- a/src/Squidex/app/features/content/declarations.ts +++ b/src/Squidex/app/features/content/declarations.ts @@ -7,5 +7,6 @@ export * from './pages/content/content-field.component'; export * from './pages/content/content-page.component'; +export * from './pages/contents/content-item.component'; export * from './pages/contents/contents-page.component'; export * from './pages/schemas/schemas-page.component'; \ No newline at end of file diff --git a/src/Squidex/app/features/content/module.ts b/src/Squidex/app/features/content/module.ts index 06700cc4e..d8a80f7c1 100644 --- a/src/Squidex/app/features/content/module.ts +++ b/src/Squidex/app/features/content/module.ts @@ -20,6 +20,7 @@ import { import { ContentFieldComponent, ContentPageComponent, + ContentItemComponent, ContentsPageComponent, SchemasPageComponent } from './declarations'; @@ -77,6 +78,7 @@ const routes: Routes = [ ], declarations: [ ContentFieldComponent, + ContentItemComponent, ContentPageComponent, ContentsPageComponent, SchemasPageComponent diff --git a/src/Squidex/app/features/content/pages/content/content-field.component.html b/src/Squidex/app/features/content/pages/content/content-field.component.html index 296b488d0..fb26e2f32 100644 --- a/src/Squidex/app/features/content/pages/content/content-field.component.html +++ b/src/Squidex/app/features/content/pages/content/content-field.component.html @@ -10,14 +10,14 @@
-
+
diff --git a/src/Squidex/app/features/content/pages/content/content-field.component.ts b/src/Squidex/app/features/content/pages/content/content-field.component.ts index f862f8a81..c0ff4b9ef 100644 --- a/src/Squidex/app/features/content/pages/content/content-field.component.ts +++ b/src/Squidex/app/features/content/pages/content/content-field.component.ts @@ -8,10 +8,7 @@ import { Component, Input, OnInit } from '@angular/core'; import { FormGroup } from '@angular/forms'; -import { - AppLanguageDto, - FieldDto -} from 'shared'; +import { AppLanguageDto, FieldDto } from 'shared'; @Component({ selector: 'sqx-content-field', @@ -32,11 +29,10 @@ export class ContentFieldComponent implements OnInit { public contentFormSubmitted: boolean; public fieldLanguages: string[]; - - public selectedLanguage: string; + public fieldLanguage: string; public selectLanguage(language: AppLanguageDto) { - this.selectedLanguage = language.iso2Code; + this.fieldLanguage = language.iso2Code; } public ngOnInit() { @@ -46,10 +42,10 @@ export class ContentFieldComponent implements OnInit { if (this.field.properties.isLocalizable) { this.fieldLanguages = this.languages.map(t => t.iso2Code); - this.selectedLanguage = this.fieldLanguages[0]; + this.fieldLanguage = this.fieldLanguages[0]; } else { this.fieldLanguages = ['iv']; - this.selectedLanguage = 'iv'; + this.fieldLanguage = 'iv'; } } } diff --git a/src/Squidex/app/features/content/pages/content/content-page.component.html b/src/Squidex/app/features/content/pages/content/content-page.component.html index 3d4263893..3e84f868a 100644 --- a/src/Squidex/app/features/content/pages/content/content-page.component.html +++ b/src/Squidex/app/features/content/pages/content/content-page.component.html @@ -11,6 +11,7 @@

New {{schema|displayName}}

+

Edit {{schema|displayName}}

diff --git a/src/Squidex/app/features/content/pages/content/content-page.component.ts b/src/Squidex/app/features/content/pages/content/content-page.component.ts index ba2d5704d..366529a50 100644 --- a/src/Squidex/app/features/content/pages/content/content-page.component.ts +++ b/src/Squidex/app/features/content/pages/content/content-page.component.ts @@ -5,11 +5,16 @@ * Copyright (c) Sebastian Stehle. All rights reserved */ -import { Component } from '@angular/core'; +import { Component, OnDestroy, OnInit } from '@angular/core'; import { AbstractControl, FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms'; import { ActivatedRoute, Router } from '@angular/router'; +import { Subscription } from 'rxjs'; -import { ContentChanged } from './../messages'; +import { + ContentCreated, + ContentDeleted, + ContentUpdated +} from './../messages'; import { AppComponentBase, @@ -31,17 +36,20 @@ import { styleUrls: ['./content-page.component.scss'], templateUrl: './content-page.component.html' }) -export class ContentPageComponent extends AppComponentBase { +export class ContentPageComponent extends AppComponentBase implements OnDestroy, OnInit { + private messageSubscription: Subscription; + public schema: SchemaDetailsDto; public contentFormSubmitted = false; public contentForm: FormGroup; - public content: ContentDto = null; + public contentData: any = null; + public contentId: string; public languages: AppLanguageDto[] = []; public get isNewMode() { - return this.content !== null; + return !this.contentData; } constructor(apps: AppsStoreService, notifications: NotificationService, users: UsersProviderService, @@ -53,13 +61,24 @@ export class ContentPageComponent extends AppComponentBase { super(apps, notifications, users); } + public ngOnDestroy() { + this.messageSubscription.unsubscribe(); + } + public ngOnInit() { + this.messageSubscription = + this.messageBus.of(ContentDeleted).subscribe(message => { + if (message.id === this.contentId) { + this.router.navigate(['../'], { relativeTo: this.route }); + } + }); + this.route.parent.data.map(p => p['appLanguages']).subscribe((languages: AppLanguageDto[]) => { this.languages = languages; }); this.route.parent.data.map(p => p['schema']).subscribe((schema: SchemaDetailsDto) => { - this.setupForm(schema, this.route.snapshot.data['content']); + this.setupForm(schema); }); this.route.data.map(p => p['content']).subscribe((content: ContentDto) => { @@ -75,33 +94,33 @@ export class ContentPageComponent extends AppComponentBase { const data = this.contentForm.value; - this.appName() - .switchMap(app => { - if (this.isNewMode) { - return this.contentsService.postContent(app, this.schema.name, data); - } else { - return this.contentsService.putContent(app, this.schema.name, data, this.content.id); - } - }) - .subscribe(() => { - this.router.navigate(['../'], { relativeTo: this.route }); - - this.messageBus.publish(new ContentChanged()); - }, error => { - this.notifyError(error); - this.enable(); - }); + if (this.isNewMode) { + this.appName() + .switchMap(app => this.contentsService.postContent(app, this.schema.name, data)) + .subscribe(created => { + this.messageBus.publish(new ContentCreated(created.id, data)); + + this.router.navigate(['../'], { relativeTo: this.route }); + }, error => { + this.notifyError(error); + this.enable(); + }); + } else { + this.appName() + .switchMap(app => this.contentsService.putContent(app, this.schema.name, this.contentId, data)) + .subscribe(() => { + this.messageBus.publish(new ContentUpdated(this.contentId, data)); + + this.router.navigate(['../'], { relativeTo: this.route }); + }, error => { + this.notifyError(error); + this.enable(); + }); + } } } - public reset() { - this.enable(); - - this.contentForm.reset(); - this.contentFormSubmitted = false; - } - - public enable() { + private enable() { for (const field of this.schema.fields.filter(f => !f.isDisabled)) { const fieldForm = this.contentForm.controls[field.name]; @@ -109,7 +128,7 @@ export class ContentPageComponent extends AppComponentBase { } } - public disable() { + private disable() { for (const field of this.schema.fields.filter(f => !f.isDisabled)) { const fieldForm = this.contentForm.controls[field.name]; @@ -117,7 +136,7 @@ export class ContentPageComponent extends AppComponentBase { } } - private setupForm(schema: SchemaDetailsDto, content?: ContentDto) { + private setupForm(schema: SchemaDetailsDto) { this.schema = schema; const controls: { [key: string]: AbstractControl } = {}; @@ -160,7 +179,14 @@ export class ContentPageComponent extends AppComponentBase { } private populateForm(content: ContentDto) { - this.content = content; + if (!content) { + this.contentData = undefined; + this.contentId = undefined; + return; + } else { + this.contentData = content.data; + this.contentId = content.id; + } for (const field of this.schema.fields) { const fieldValue = content.data[field.name] || {}; diff --git a/src/Squidex/app/features/content/pages/contents/contents-page.component.html b/src/Squidex/app/features/content/pages/contents/contents-page.component.html index 6785d8f0f..7aaa5b686 100644 --- a/src/Squidex/app/features/content/pages/contents/contents-page.component.html +++ b/src/Squidex/app/features/content/pages/contents/contents-page.component.html @@ -5,7 +5,7 @@
-
@@ -25,10 +25,10 @@
- +
- + @@ -51,25 +51,15 @@ -