Skip to content

Commit

Permalink
Improve trash handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Jump committed Sep 24, 2019
1 parent c9e3fba commit a6028aa
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 35 deletions.
33 changes: 22 additions & 11 deletions uSync8.ContentEdition/Serializers/ContentSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,9 @@ protected override SyncAttempt<IContent> DeserializeCore(XElement node)

var item = FindOrCreate(node);

if (item.Trashed)
{
// TODO: Where has changed trashed state gone?
}

DeserializeBase(item, node);
DeserializeTemplate(item, node);

// contentService.Save(item);

return SyncAttempt<IContent>.Succeed(
item.Name,
item,
Expand Down Expand Up @@ -166,10 +159,11 @@ public override SyncAttempt<IContent> DeserializeSecondPass(IContent item, XElem

// sort order
var sortOrder = node.Element("Info").Element("SortOrder").ValueOrDefault(-1);
if (sortOrder != -1)
{
item.SortOrder = sortOrder;
}
HandleSortOrder(item, sortOrder);


var trashed = node.Element("Info").Element("Trashed").ValueOrDefault(false);
HandleTrashedState(item, trashed);

// published status
// this does the last save and publish
Expand All @@ -184,6 +178,21 @@ public override SyncAttempt<IContent> DeserializeSecondPass(IContent item, XElem
// second pass, is when we do the publish and stuff.
}

protected override void HandleTrashedState(IContent item, bool trashed)
{
if (!trashed && item.Trashed)
{
// if the item is trashed, then the change of it's parent
// should restore it (as long as we do a move!)
contentService.Move(item, item.ParentId);
}
else if (trashed && !item.Trashed)
{
// move to the recycle bin
contentService.MoveToRecycleBin(item);
}
}

protected virtual Attempt<string> DoSaveOrPublish(IContent item, XElement node)
{
var info = node.Element("Info");
Expand Down Expand Up @@ -227,6 +236,8 @@ protected virtual Attempt<string> DoSaveOrPublish(IContent item, XElement node)

}



#endregion

protected override IContent CreateItem(string alias, ITreeEntity parent, string itemType)
Expand Down
17 changes: 14 additions & 3 deletions uSync8.ContentEdition/Serializers/ContentSerializerBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public abstract class ContentSerializerBase<TObject> : SyncTreeSerializerBase<TO
protected SyncValueMapperCollection syncMappers;

public ContentSerializerBase(
IEntityService entityService,
ILogger logger,
IEntityService entityService,
ILogger logger,
UmbracoObjectTypes umbracoObjectType,
SyncValueMapperCollection syncMappers)
: base(entityService, logger)
Expand Down Expand Up @@ -208,6 +208,15 @@ protected Attempt<TObject> DeserializeProperties(TObject item, XElement node)
return Attempt.Succeed(item);
}

protected void HandleSortOrder(TObject item, int sortOrder)
{
if (sortOrder != -1)
item.SortOrder = sortOrder;
}

protected abstract void HandleTrashedState(TObject item, bool trashed);


protected string GetExportValue(object value, PropertyType propertyType, string culture, string segment)
{
// this is where the mapping magic will happen.
Expand Down Expand Up @@ -256,7 +265,7 @@ protected override TObject FindOrCreate(XElement node)
}

var contentTypeAlias = node.Element("Info").Element("ContentType").ValueOrDefault(node.Name.LocalName);

// var contentTypeAlias = node.Name.LocalName;

return CreateItem(alias, parent, contentTypeAlias);
Expand Down Expand Up @@ -395,6 +404,8 @@ protected TObject FindByPath(IEnumerable<string> folders)

}



#endregion
}
}
39 changes: 18 additions & 21 deletions uSync8.ContentEdition/Serializers/MediaSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,39 +66,35 @@ public override SyncAttempt<IMedia> DeserializeSecondPass(IMedia item, XElement
var info = node.Element("Info");

var sortOrder = info.Element("SortOrder").ValueOrDefault(-1);
if (sortOrder != -1)
{
item.SortOrder = sortOrder;
}

HandleSortOrder(item, sortOrder);

var trashed = info.Element("Trashed").ValueOrDefault(false);
if (trashed)
{
if (!item.Trashed)
{
mediaService.MoveToRecycleBin(item);
}
return SyncAttempt<IMedia>.Succeed(item.Name, ChangeType.Import);
}

if (item.Trashed)
{
// remove from bin.
// ?
}

HandleTrashedState(item, trashed);

var attempt = mediaService.Save(item);
if (!attempt.Success)
return SyncAttempt<IMedia>.Fail(item.Name, ChangeType.Fail, "");

// we return no-change so we don't trigger the second save
return SyncAttempt<IMedia>.Succeed(item.Name, ChangeType.NoChange );
}


protected override void HandleTrashedState(IMedia item, bool trashed)
{
if (!trashed && item.Trashed)
{
// if the item is trashed, then moving it back to the parent value
// restores it.
mediaService.Move(item, item.ParentId);
}
else if (trashed && !item.Trashed)
{
// move to the recycle bin
mediaService.MoveToRecycleBin(item);
}
}


protected override SyncAttempt<XElement> SerializeCore(IMedia item)
{
var node = InitializeNode(item, item.ContentType.Alias);
Expand Down Expand Up @@ -203,6 +199,7 @@ protected override void SaveItem(IMedia item)

protected override void DeleteItem(IMedia item)
=> mediaService.Delete(item);

}

}

0 comments on commit a6028aa

Please sign in to comment.