Skip to content

Commit e55b1a4

Browse files
committed
reduce duplication of recycle methods
1 parent a02d054 commit e55b1a4

File tree

6 files changed

+99
-271
lines changed

6 files changed

+99
-271
lines changed

src/sdk/PnP.Core/Model/Base/ExpandoBaseDataModel.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using PnP.Core.Model.SharePoint;
2+
using System;
23
using System.Collections.Generic;
34
using System.Dynamic;
45
using System.Linq;
@@ -16,7 +17,7 @@ namespace PnP.Core.Model
1617
/// https://weblog.west-wind.com/posts/2012/feb/08/creating-a-dynamic-extensible-c-expando-object
1718
/// </remarks>
1819
/// <typeparam name="TModel">The actual type of the entity of the Domain Model</typeparam>
19-
internal class ExpandoBaseDataModel<TModel> : BaseDataModel<TModel>, IExpandoDataModel
20+
internal class ExpandoBaseDataModel<TModel> : RecyclableBaseDataModel<TModel>, IExpandoDataModel
2021
{
2122
/// <summary>
2223
/// Type of the instance object

src/sdk/PnP.Core/Model/SharePoint/Core/Internal/File.cs

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace PnP.Core.Model.SharePoint
2020
[SharePointType("SP.File", Target = typeof(Web), Uri = "_api/Web/getFileById('{Id}')")]
2121
[SharePointType("SP.File", Target = typeof(ListItem), Uri = "_api/Web/lists(guid'{List.Id}')/items({Parent.Id})/file")]
2222

23-
internal sealed class File : BaseDataModel<IFile>, IFile
23+
internal sealed class File : RecyclableBaseDataModel<IFile>, IFile
2424
{
2525
internal const string AddFileContentAdditionalInformationKey = "Content";
2626
internal const string AddFileOverwriteAdditionalInformationKey = "Overwrite";
@@ -613,80 +613,6 @@ public async Task ApproveBatchAsync(Batch batch, string comment = null)
613613

614614
#endregion
615615

616-
#region Recycle
617-
618-
public Guid Recycle()
619-
{
620-
return RecycleAsync().GetAwaiter().GetResult();
621-
}
622-
623-
public async Task<Guid> RecycleAsync()
624-
{
625-
var entity = EntityManager.GetClassInfo(GetType(), this);
626-
string recycleEndpointUrl = $"{entity.SharePointUri}/recycle";
627-
628-
var apiCall = new ApiCall(recycleEndpointUrl, ApiType.SPORest)
629-
{
630-
RemoveFromModel = true
631-
};
632-
633-
var response = await RawRequestAsync(apiCall, HttpMethod.Post).ConfigureAwait(false);
634-
635-
if (!string.IsNullOrEmpty(response.Json))
636-
{
637-
return ProcessRecycleResponse(response.Json);
638-
}
639-
640-
return Guid.Empty;
641-
}
642-
643-
private static Guid ProcessRecycleResponse(string json)
644-
{
645-
var document = JsonSerializer.Deserialize<JsonElement>(json);
646-
if (document.TryGetProperty("value", out JsonElement recycleBinItemId))
647-
{
648-
// return the recyclebin item id
649-
return recycleBinItemId.GetGuid();
650-
}
651-
652-
return Guid.Empty;
653-
}
654-
655-
public IBatchSingleResult<BatchResultValue<Guid>> RecycleBatch()
656-
{
657-
return RecycleBatchAsync().GetAwaiter().GetResult();
658-
}
659-
660-
public IBatchSingleResult<BatchResultValue<Guid>> RecycleBatch(Batch batch)
661-
{
662-
return RecycleBatchAsync(batch).GetAwaiter().GetResult();
663-
}
664-
665-
public async Task<IBatchSingleResult<BatchResultValue<Guid>>> RecycleBatchAsync()
666-
{
667-
return await RecycleBatchAsync(PnPContext.CurrentBatch).ConfigureAwait(false);
668-
}
669-
670-
public async Task<IBatchSingleResult<BatchResultValue<Guid>>> RecycleBatchAsync(Batch batch)
671-
{
672-
var entity = EntityManager.GetClassInfo(GetType(), this);
673-
string recycleEndpointUrl = $"{entity.SharePointUri}/recycle";
674-
675-
var apiCall = new ApiCall(recycleEndpointUrl, ApiType.SPORest)
676-
{
677-
RemoveFromModel = true,
678-
RawSingleResult = new BatchResultValue<Guid>(Guid.Empty),
679-
RawResultsHandler = (json, apiCall) =>
680-
{
681-
(apiCall.RawSingleResult as BatchResultValue<Guid>).Value = ProcessRecycleResponse(json);
682-
}
683-
};
684-
685-
var batchRequest = await RawRequestBatchAsync(batch, apiCall, HttpMethod.Post).ConfigureAwait(false);
686-
return new BatchSingleResult<BatchResultValue<Guid>>(batch, batchRequest.Id, apiCall.RawSingleResult as BatchResultValue<Guid>);
687-
}
688-
#endregion
689-
690616
#region CopyTo
691617
private ApiCall GetCopyToCrossSiteApiCall(string destinationUrl, bool overwrite, MoveCopyOptions options)
692618
{

src/sdk/PnP.Core/Model/SharePoint/Core/Internal/Folder.cs

Lines changed: 1 addition & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace PnP.Core.Model.SharePoint
2222
[SharePointType("SP.Folder", Target = typeof(Folder), Uri = "_api/Web/getFolderById('{Id}')", Get = "_api/Web/getFolderById('{Parent.Id}')", LinqGet = "_api/Web/getFolderById('{Parent.Id}')/Folders")]
2323
[SharePointType("SP.Folder", Target = typeof(List), Uri = "_api/Web/Lists(guid'{Parent.Id}')/rootFolder", LinqGet = "_api/Web/Lists(guid'{Parent.Id}')/Folders")]
2424
[SharePointType("SP.Folder", Target = typeof(ListItem), Uri = "_api/Web/Lists(guid'{List.Id}')/Items({Parent.Id})/Folder")]
25-
internal sealed class Folder : BaseDataModel<IFolder>, IFolder
25+
internal sealed class Folder : RecyclableBaseDataModel<IFolder>, IFolder
2626
{
2727
#region Construction
2828
public Folder()
@@ -160,80 +160,6 @@ public IFolder AddFolderBatch(string name)
160160
}
161161
#endregion
162162

163-
#region Recycle
164-
165-
public Guid Recycle()
166-
{
167-
return RecycleAsync().GetAwaiter().GetResult();
168-
}
169-
170-
public async Task<Guid> RecycleAsync()
171-
{
172-
var entity = EntityManager.GetClassInfo(GetType(), this);
173-
string recycleEndpointUrl = $"{entity.SharePointUri}/recycle";
174-
175-
var apiCall = new ApiCall(recycleEndpointUrl, ApiType.SPORest)
176-
{
177-
RemoveFromModel = true
178-
};
179-
180-
var response = await RawRequestAsync(apiCall, HttpMethod.Post).ConfigureAwait(false);
181-
182-
if (!string.IsNullOrEmpty(response.Json))
183-
{
184-
return ProcessRecycleResponse(response.Json);
185-
}
186-
187-
return Guid.Empty;
188-
}
189-
190-
private static Guid ProcessRecycleResponse(string json)
191-
{
192-
var document = JsonSerializer.Deserialize<JsonElement>(json);
193-
if (document.TryGetProperty("value", out JsonElement recycleBinItemId))
194-
{
195-
// return the recyclebin item id
196-
return recycleBinItemId.GetGuid();
197-
}
198-
199-
return Guid.Empty;
200-
}
201-
202-
public IBatchSingleResult<BatchResultValue<Guid>> RecycleBatch()
203-
{
204-
return RecycleBatchAsync().GetAwaiter().GetResult();
205-
}
206-
207-
public IBatchSingleResult<BatchResultValue<Guid>> RecycleBatch(Batch batch)
208-
{
209-
return RecycleBatchAsync(batch).GetAwaiter().GetResult();
210-
}
211-
212-
public async Task<IBatchSingleResult<BatchResultValue<Guid>>> RecycleBatchAsync()
213-
{
214-
return await RecycleBatchAsync(PnPContext.CurrentBatch).ConfigureAwait(false);
215-
}
216-
217-
public async Task<IBatchSingleResult<BatchResultValue<Guid>>> RecycleBatchAsync(Batch batch)
218-
{
219-
var entity = EntityManager.GetClassInfo(GetType(), this);
220-
string recycleEndpointUrl = $"{entity.SharePointUri}/recycle";
221-
222-
var apiCall = new ApiCall(recycleEndpointUrl, ApiType.SPORest)
223-
{
224-
RemoveFromModel = true,
225-
RawSingleResult = new BatchResultValue<Guid>(Guid.Empty),
226-
RawResultsHandler = (json, apiCall) =>
227-
{
228-
(apiCall.RawSingleResult as BatchResultValue<Guid>).Value = ProcessRecycleResponse(json);
229-
}
230-
};
231-
232-
var batchRequest = await RawRequestBatchAsync(batch, apiCall, HttpMethod.Post).ConfigureAwait(false);
233-
return new BatchSingleResult<BatchResultValue<Guid>>(batch, batchRequest.Id, apiCall.RawSingleResult as BatchResultValue<Guid>);
234-
}
235-
#endregion
236-
237163
#region Copy To
238164
private ApiCall GetCopyToApiCall(string destinationUrl, MoveCopyOptions options)
239165
{

src/sdk/PnP.Core/Model/SharePoint/Core/Internal/List.cs

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ namespace PnP.Core.Model.SharePoint
2323
/// </summary>
2424
[SharePointType("SP.List", Uri = "_api/Web/Lists(guid'{Id}')", Update = "_api/web/lists/getbyid(guid'{Id}')", LinqGet = "_api/web/lists")]
2525
//[GraphType(Get = "sites/{Parent.GraphId}/lists/{GraphId}", LinqGet = "sites/{Parent.GraphId}/lists")]
26-
internal sealed class List : BaseDataModel<IList>, IList
26+
internal sealed class List : RecyclableBaseDataModel<IList>, IList
2727
{
2828
// List of fields that loaded when the Lists collection is requested. This approach is needed as
2929
// Graph requires the "system" field to be loaded as trigger to return all lists
@@ -262,71 +262,15 @@ internal async override Task GraphToRestMetadataAsync()
262262
#region Extension methods
263263

264264
#region Recycle
265-
public Guid Recycle()
266-
{
267-
return RecycleAsync().GetAwaiter().GetResult();
268-
}
269-
270-
public async Task<Guid> RecycleAsync()
271-
{
272-
ApiCall apiCall = GetRecycleApiCall();
273-
274-
var response = await RawRequestAsync(apiCall, HttpMethod.Post).ConfigureAwait(false);
275-
276-
if (!string.IsNullOrEmpty(response.Json))
277-
{
278-
return ParseRecycleResponse(response.Json);
279-
}
280-
281-
return Guid.Empty;
282-
}
283-
284-
private static Guid ParseRecycleResponse(string json)
285-
{
286-
var document = JsonSerializer.Deserialize<JsonElement>(json);
287-
if (document.TryGetProperty("value", out JsonElement recycleBinItemId))
288-
{
289-
// return the recyclebin item id
290-
return recycleBinItemId.GetGuid();
291-
}
292-
return Guid.Empty;
293-
}
294-
295-
public IBatchSingleResult<BatchResultValue<Guid>> RecycleBatch()
296-
{
297-
return RecycleBatchAsync().GetAwaiter().GetResult();
298-
}
299-
300-
public async Task<IBatchSingleResult<BatchResultValue<Guid>>> RecycleBatchAsync()
301-
{
302-
return await RecycleBatchAsync(PnPContext.CurrentBatch).ConfigureAwait(false);
303-
}
304-
305-
public IBatchSingleResult<BatchResultValue<Guid>> RecycleBatch(Batch batch)
306-
{
307-
return RecycleBatchAsync(batch).GetAwaiter().GetResult();
308-
}
309-
310-
public async Task<IBatchSingleResult<BatchResultValue<Guid>>> RecycleBatchAsync(Batch batch)
311-
{
312-
ApiCall apiCall = GetRecycleApiCall();
313-
apiCall.RawSingleResult = new BatchResultValue<Guid>(Guid.Empty);
314-
apiCall.RawResultsHandler = (json, apiCall) =>
315-
{
316-
(apiCall.RawSingleResult as BatchResultValue<Guid>).Value = ParseRecycleResponse(json);
317-
};
318-
319-
var batchRequest = await RawRequestBatchAsync(batch, apiCall, HttpMethod.Post).ConfigureAwait(false);
320-
return new BatchSingleResult<BatchResultValue<Guid>>(batch, batchRequest.Id, apiCall.RawSingleResult as BatchResultValue<Guid>);
321-
}
322-
323-
private ApiCall GetRecycleApiCall()
265+
266+
protected override ApiCall BuildRecycleApiCall()
324267
{
325268
return new ApiCall($"_api/Web/Lists(guid'{Id}')/recycle", ApiType.SPORest)
326269
{
327270
RemoveFromModel = true
328271
};
329272
}
273+
330274
#endregion
331275

332276
#region GetItemsByCamlQuery

src/sdk/PnP.Core/Model/SharePoint/Core/Internal/ListItem.cs

Lines changed: 2 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -987,72 +987,15 @@ private ApiCall SetComplianceTagApiCall(string complianceTag, bool isTagPolicyHo
987987
#endregion
988988

989989
#region Recycle
990-
public Guid Recycle()
991-
{
992-
return RecycleAsync().GetAwaiter().GetResult();
993-
}
994-
995-
public async Task<Guid> RecycleAsync()
996-
{
997-
ApiCall apiCall = BuildRecycleApiCall();
998-
999-
var response = await RawRequestAsync(apiCall, HttpMethod.Post).ConfigureAwait(false);
1000-
1001-
if (!string.IsNullOrEmpty(response.Json))
1002-
{
1003-
return ProcessRecyleResponse(response.Json);
1004-
}
1005-
1006-
return Guid.Empty;
1007-
}
1008-
1009-
private static Guid ProcessRecyleResponse(string json)
1010-
{
1011-
var document = JsonSerializer.Deserialize<JsonElement>(json);
1012-
if (document.TryGetProperty("value", out JsonElement recycleBinItemId))
1013-
{
1014-
// return the recyclebin item id
1015-
return recycleBinItemId.GetGuid();
1016-
}
1017-
1018-
return Guid.Empty;
1019-
}
1020-
1021-
public IBatchSingleResult<BatchResultValue<Guid>> RecycleBatch()
1022-
{
1023-
return RecycleBatchAsync().GetAwaiter().GetResult();
1024-
}
1025-
1026-
public async Task<IBatchSingleResult<BatchResultValue<Guid>>> RecycleBatchAsync()
1027-
{
1028-
return await RecycleBatchAsync(PnPContext.CurrentBatch).ConfigureAwait(false);
1029-
}
1030-
1031-
public IBatchSingleResult<BatchResultValue<Guid>> RecycleBatch(Batch batch)
1032-
{
1033-
return RecycleBatchAsync(batch).GetAwaiter().GetResult();
1034-
}
1035990

1036-
public async Task<IBatchSingleResult<BatchResultValue<Guid>>> RecycleBatchAsync(Batch batch)
1037-
{
1038-
ApiCall apiCall = BuildRecycleApiCall();
1039-
apiCall.RawSingleResult = new BatchResultValue<Guid>(Guid.Empty);
1040-
apiCall.RawResultsHandler = (json, apiCall) =>
1041-
{
1042-
(apiCall.RawSingleResult as BatchResultValue<Guid>).Value = ProcessRecyleResponse(json);
1043-
};
1044-
1045-
var batchRequest = await RawRequestBatchAsync(batch, apiCall, HttpMethod.Post).ConfigureAwait(false);
1046-
return new BatchSingleResult<BatchResultValue<Guid>>(batch, batchRequest.Id, apiCall.RawSingleResult as BatchResultValue<Guid>);
1047-
}
1048-
1049-
private ApiCall BuildRecycleApiCall()
991+
protected override ApiCall BuildRecycleApiCall()
1050992
{
1051993
return new ApiCall($"{GetItemUri()}/recycle", ApiType.SPORest)
1052994
{
1053995
RemoveFromModel = true
1054996
};
1055997
}
998+
1056999
#endregion
10571000

10581001
#region Graph/Rest interoperability overrides

0 commit comments

Comments
 (0)