public ActionResult CreateManyToMany(ManyToManyRelationshipModel manyToMany) { if (!Services.Authorizer.Authorize(Permissions.PublishContent, T("Not allowed to edit a content."))) { return(new HttpUnauthorizedResult()); } if (manyToMany.IsCreate) { var checkNameMessage = _relationshipService.CheckRelationName(manyToMany.Name); if (!string.IsNullOrWhiteSpace(checkNameMessage)) { ModelState.AddModelError("ManyToManyRelation", T(checkNameMessage).ToString()); return(ResponseError("")); } var backMessage = _relationshipService.CreateRelationship(manyToMany); int relationId; if (int.TryParse(backMessage.ToString(), out relationId)) { return(Json(new { relationId = relationId })); } if (!string.IsNullOrWhiteSpace(backMessage)) { ModelState.AddModelError("ManyToManyRelation", T(backMessage).ToString()); return(ResponseError("")); } } return(new HttpStatusCodeResult(HttpStatusCode.OK)); }
/// <summary> /// Column alter method is not the best. /// </summary> /// <param name="relationshipId"></param> /// <param name="manyToMany"></param> /// <returns>Error message string or null for correctly edited</returns> public string EditRelationship(int relationshipId, ManyToManyRelationshipModel manyToMany) { var manyToManyRecord = _manyToManyRepository.Get(record => record.Relationship.Id == relationshipId); if (manyToManyRecord == null) { return("Invalid relashionship ID."); } var relationshipRecord = manyToManyRecord.Relationship; manyToManyRecord.ShowPrimaryList = manyToMany.ShowPrimaryList; manyToManyRecord.PrimaryListLabel = manyToMany.PrimaryListLabel; manyToManyRecord.ShowRelatedList = manyToMany.ShowRelatedList; manyToManyRecord.RelatedListLabel = manyToMany.RelatedListLabel; var primaryPart = _contentDefinitionManager.GetPartDefinition(relationshipRecord.Name + relationshipRecord.PrimaryEntity.Name + "Part"); primaryPart.Settings["DisplayName"] = manyToMany.RelatedListLabel; _contentDefinitionManager.StorePartDefinition(primaryPart); var relatedPart = _contentDefinitionManager.GetPartDefinition(relationshipRecord.Name + relationshipRecord.RelatedEntity.Name + "Part"); relatedPart.Settings["DisplayName"] = manyToMany.PrimaryListLabel; _contentDefinitionManager.StorePartDefinition(relatedPart); _manyToManyRepository.Update(manyToManyRecord); UpdateLayoutProperties(relationshipRecord.PrimaryEntity.Name, manyToManyRecord.PrimaryListProjection.LayoutRecord, manyToMany.PrimaryColumnList); UpdateLayoutProperties(relationshipRecord.RelatedEntity.Name, manyToManyRecord.RelatedListProjection.LayoutRecord, manyToMany.RelatedColumnList); return(null); }
public ActionResult EditManyToMany(int relationId, ManyToManyRelationshipModel manyToMany) { if (!Services.Authorizer.Authorize(Permissions.EditContent, T("Not allowed to edit a content."))) { return(new HttpUnauthorizedResult()); } var errorMessage = _relationshipService.EditRelationship(relationId, manyToMany); if (!string.IsNullOrWhiteSpace(errorMessage)) { ModelState.AddModelError("ManyToManyRelation", T(errorMessage).ToString()); return(ResponseError("")); } return(new HttpStatusCodeResult(HttpStatusCode.OK)); }
public string CreateRelationship(ManyToManyRelationshipModel manyToMany) { if (manyToMany == null) { return("Invalid model."); } var primaryEntity = _contentMetadataService.GetEntity(manyToMany.PrimaryEntity); var relatedEntity = _contentMetadataService.GetEntity(manyToMany.RelatedEntity); if (primaryEntity == null || relatedEntity == null || !primaryEntity.HasPublished() || !relatedEntity.HasPublished()) { return("Invalid entity"); } if (RelationshipExists(manyToMany.Name)) { return("Name already exist."); } var relationship = CreateRelation(new RelationshipRecord { Name = manyToMany.Name, PrimaryEntity = primaryEntity.Record, RelatedEntity = relatedEntity.Record, Type = (byte)RelationshipType.ManyToMany }); var primaryProjectionPart = CreateProjection(manyToMany.PrimaryEntity, manyToMany.PrimaryColumnList); var relatedProjectionPart = CreateProjection(manyToMany.RelatedEntity, manyToMany.RelatedColumnList); _manyToManyRepository.Create(new ManyToManyRelationshipRecord { PrimaryListProjection = primaryProjectionPart.Record, PrimaryListLabel = manyToMany.PrimaryListLabel, RelatedListProjection = relatedProjectionPart.Record, RelatedListLabel = manyToMany.RelatedListLabel, Relationship = relationship, ShowPrimaryList = manyToMany.ShowPrimaryList, ShowRelatedList = manyToMany.ShowRelatedList }); GenerateManyToManyParts(manyToMany); return(relationship.Id.ToString()); }
private void GenerateManyToManyParts(ManyToManyRelationshipModel manyToMany) { var primaryName = manyToMany.Name + manyToMany.PrimaryEntity; var relatedName = manyToMany.Name + manyToMany.RelatedEntity; _schemaUpdateService.CreateCustomTable( "Coevery_DynamicTypes_" + primaryName + "PartRecord", table => table.ContentPartRecord() ); _schemaUpdateService.CreateCustomTable( "Coevery_DynamicTypes_" + relatedName + "PartRecord", table => table.ContentPartRecord() ); _schemaUpdateService.CreateCustomTable( "Coevery_DynamicTypes_" + manyToMany.Name + "ContentLinkRecord", table => table.Column <int>("Id", column => column.PrimaryKey().Identity()) .Column <int>("PrimaryPartRecord_Id") .Column <int>("RelatedPartRecord_Id") ); _contentDefinitionManager.AlterPartDefinition( primaryName + "Part", builder => builder .Attachable() .WithSetting("DisplayName", manyToMany.RelatedListLabel)); _contentDefinitionManager.AlterPartDefinition( relatedName + "Part", builder => builder .Attachable() .WithSetting("DisplayName", manyToMany.PrimaryListLabel)); _contentDefinitionManager.AlterTypeDefinition(manyToMany.PrimaryEntity, typeBuilder => typeBuilder.WithPart(primaryName + "Part")); _contentDefinitionManager.AlterTypeDefinition(manyToMany.RelatedEntity, typeBuilder => typeBuilder.WithPart(relatedName + "Part")); _dynamicAssemblyBuilder.Build(); }