Ejemplo n.º 1
0
        public virtual ActionResult CreateNew(HiveId?id)
        {
            if (id.IsNullValueOrEmpty())
            {
                return(HttpNotFound());
            }

            //store the parent document type to the view
            var model = new CreateContentModel {
                ParentId = id.Value
            };

            return(CreateNewView(model));
        }
Ejemplo n.º 2
0
        public async Task <Guid> CreateAsync(CreateContentModel model)
        {
            if (model == null)
            {
                throw new ArgumentNullException(nameof(model));
            }

            var content = new Content
            {
                Id              = Guid.NewGuid(),
                CreatedAt       = DateTime.UtcNow,
                Title           = model.Title,
                Status          = ContentStatus.Active,
                Type            = model.Type,
                BrandId         = model.BrandId,
                SocialMediaType = model.SocialMediaType,
                Comment         = model.Comment,
                ReleaseDateUtc  = model.ReleaseDate.Date,
                EndDateUtc      = model.EndDate?.Date,
                PersonId        = model.PersonId
            };

            if (!model.IsFree)
            {
                var bill = new Bill
                {
                    Id          = Guid.NewGuid(),
                    CreatedAt   = DateTime.UtcNow,
                    Comment     = $"Created automatically for content with id {content.Id}",
                    Value       = model.Bill.Value,
                    ContentId   = content.Id,
                    Contact     = model.Bill.Contact,
                    ContactName = model.Bill.ContactName,
                    ContactType = model.Bill.ContactType,
                    Status      = model.Bill.Value == 0 ? BillStatus.Paid : BillStatus.Active,
                    Type        = BillType.Content,
                    BrandId     = model.BrandId,
                    ClientId    = model.Bill.ClientId
                };

                content.Bill   = bill;
                content.BillId = bill.Id;
            }

            var result = await unitOfWork.Content.InsertAsync(content);

            return(result.Id);
        }
        public void ContentEditorControllerTests_Create_New_Wizard_Step_Bound_And_Validated()
        {
            //Arrange

            var selectedDocTypeId = new HiveId("content", "", new HiveIdValue(Guid.NewGuid()));
            var createModel       = new CreateContentModel {
                Name = "test", SelectedDocumentTypeId = selectedDocTypeId
            };

            // Get the parent content schema
            using (var writer = RebelApplicationContext.Hive.OpenWriter <IContentStore>())
            {
                var contentSchemaRoot = writer.Repositories.Schemas.Get <EntitySchema>(FixedHiveIds.ContentRootSchema);
                //create doc type in persistence layer
                var schema = HiveModelCreationHelper.CreateEntitySchema("test", "Test", new AttributeDefinition[] { });
                schema.Id = selectedDocTypeId;
                schema.RelationProxies.EnlistParent(contentSchemaRoot, FixedRelationTypes.DefaultRelationType);
                writer.Repositories.Schemas.AddOrUpdate(schema);
                writer.Complete();
            }

            var controller = new ContentEditorController(GetBackOfficeRequestContext());

            controller.InjectDependencies(new Dictionary <string, string>(), new Dictionary <string, string>
            {
                { "Name", "test" },
                { "SelectedDocumentTypeId", selectedDocTypeId.ToString() }
            }, GetBackOfficeRequestContext());

            //Act

            var result = (ViewResult)controller.CreateNewForm(createModel);
            var model  = (CreateContentModel)result.Model;

            //Assert

            Assert.IsTrue(controller.ModelState.IsValidField("Name"),
                          string.Join("; ", controller.ModelState["Name"].Errors.Select(x => x.ErrorMessage)));
            Assert.IsTrue(controller.ModelState.IsValidField("SelectedDocumentTypeId"),
                          string.Join("; ", controller.ModelState["SelectedDocumentTypeId"].Errors.Select(x => x.ErrorMessage)));

            Assert.AreEqual("test", model.Name);
            Assert.AreEqual((Guid)selectedDocTypeId.Value, (Guid)model.SelectedDocumentTypeId.Value);
        }
Ejemplo n.º 4
0
        public virtual ActionResult CreateNewForm(CreateContentModel createModel)
        {
            Mandate.ParameterNotNull(createModel, "createModel");
            Mandate.That <NullReferenceException>(!createModel.ParentId.IsNullValueOrEmpty());
            Mandate.That <NullReferenceException>(!createModel.SelectedDocumentTypeId.IsNullValueOrEmpty());

            //validate the model
            TryUpdateModel(createModel);
            //get the create new result view which will validate that the selected doc type id is in fact allowed
            var result = CreateNewView(createModel);

            //if at this point the model state is invalid, return the result which is the CreateNew view
            if (!ModelState.IsValid)
            {
                return(result);
            }

            using (var uow = Hive.Create <IContentStore>())
            {
                var schema = uow.Repositories.Schemas.Get <EntitySchema>(createModel.SelectedDocumentTypeId);
                if (schema == null)
                {
                    throw new ArgumentException(string.Format("No schema found for id: {0} on action Create", createModel.SelectedDocumentTypeId));
                }

                //create the empty content item
                var contentViewModel = CreateNewContentEntity(schema, createModel.Name, createModel.ParentId);
                //map the Ids correctly to the model so it binds
                ReconstructModelPropertyIds(contentViewModel);

                return(ProcessCreate(contentViewModel, true));
            }

            //everything is valid, now we need to render out the editor for this document type without any data
            //return RedirectToAction("Create", new
            //{
            //    docTypeId = createModel.SelectedDocumentTypeId,
            //    name = createModel.Name,
            //    parentId = createModel.ParentId
            //});
        }
        public void ContentEditorControllerTests_Create_New_Wizard_Step_Bound_And_Invalidated()
        {
            //Arrange

            var selectedDocTypeId = Guid.NewGuid();
            var createModel       = new CreateContentModel {
                Name = "", SelectedDocumentTypeId = new HiveId(selectedDocTypeId)
            };

            var controller = new ContentEditorController(GetBackOfficeRequestContext());

            controller.InjectDependencies(new Dictionary <string, string>(), new Dictionary <string, string>
            {
                { "Name", "" },
                { "SelectedDocumentTypeId", selectedDocTypeId.ToString("N") }
            }, GetBackOfficeRequestContext());

            //Act
            var result = (ViewResult)controller.CreateNewForm(createModel);

            //Assert
            Assert.IsFalse(controller.ModelState.IsValidField("Name"));
            Assert.IsFalse(controller.ModelState.IsValidField("SelectedDocumentTypeId"));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Returns the ActionResult for the CreateNew wizard view
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        protected virtual ActionResult CreateNewView(CreateContentModel model)
        {
            Mandate.ParameterNotNull(model, "model");

            //lookup the doc type for the node id, find out which doc type children are allowed

            using (var uow = Hive.Create <IContentStore>())
            {
                var allSchemaTypeIds = uow.Repositories.Schemas.GetDescendentRelations(RootSchemaNodeId, FixedRelationTypes.DefaultRelationType)
                                       .DistinctBy(x => x.DestinationId)
                                       .Select(x => x.DestinationId).ToArray();

                var schemas = uow.Repositories.Schemas.Get <EntitySchema>(true, allSchemaTypeIds);

                //the filtered doc types to choose from based on the parent node (default is all of them)
                var filteredSchemas = schemas;

                //get the parent content if it's not the root
                if (model.ParentId != VirtualRootNodeId)
                {
                    //ensure the parent exists!
                    var parentEntity = uow.Repositories.Get <TypedEntity>(model.ParentId);
                    if (parentEntity == null)
                    {
                        throw new ArgumentException(string.Format("No content found for id: {0} on action CreateNew", model.ParentId));
                    }

                    //ensure the doc type exists!
                    //TODO: We reload the EntitySchema here so it has the right providerid, but as soon as TypedEntity.EntitySchema.Id gets mapped properly
                    //when loading TypedEntity we won't have to
                    var parentSc = uow.Repositories.Schemas.Get <EntitySchema>(parentEntity.EntitySchema.Id);
                    if (parentSc == null)
                    {
                        throw new ArgumentException(string.Format("No doc type found for id: {0} on action CreateNew",
                                                                  parentEntity.EntitySchema.Id));
                    }
                    var parentDocType = BackOfficeRequestContext.Application.FrameworkContext.TypeMappers.Map <EntitySchema, DocumentTypeEditorModel>(parentSc);

                    //filter the doc types to the allowed ones
                    filteredSchemas = schemas
                                      .Where(x => parentDocType.AllowedChildIds.Contains(x.Id, new HiveIdComparer(true)))
                                      .ToArray();
                }

                //validate the the selected doc type in the model is in fact one of the child doc types
                if (!model.SelectedDocumentTypeId.IsNullValueOrEmpty())
                {
                    if (!filteredSchemas.Select(x => x.Id)
                        .Contains(model.SelectedDocumentTypeId, new HiveIdComparer(true)))
                    {
                        ModelState.AddModelError("SelectedDocumentTypeId", "The selected document type id specified was not found in the allowed document types collection for the current node");
                    }
                }

                EnsureCreateWizardViewBagData(filteredSchemas);

                if (!filteredSchemas.Any())
                {
                    model.NoticeBoard.Add(new NotificationMessage("Content.NoChildTypesAllowed.Message".Localize(this), NotificationType.Warning));
                }
            }

            return(View("CreateNew", model));
        }