last ...
This commit is contained in:
@@ -307,8 +307,8 @@ namespace xFileService.Providers
|
||||
|
||||
//
|
||||
// Retrieve Last Index ...
|
||||
var referencesCount = await CountReferences(providedForId);
|
||||
if (forIndex > referencesCount - 1)
|
||||
var maxIndex = (await GetReferencingIndexes(providedForId)).Max();
|
||||
if (forIndex > maxIndex)
|
||||
{
|
||||
XException.NotFound.Throw();
|
||||
}
|
||||
@@ -393,6 +393,7 @@ namespace xFileService.Providers
|
||||
.AsQueryable()
|
||||
.Where(te => te.References.Contains(identifier))
|
||||
.Select(te => te.References)
|
||||
.ToList()
|
||||
.SelectMany(tr => tr.ParseXReferenceList<TKey>())
|
||||
.Where(tr => tr.ProvidedFor == Provider && $"{tr.ReferencedTo}" == $"{providedForId}")
|
||||
.OrderBy(tr => tr.Index)
|
||||
@@ -631,10 +632,12 @@ namespace xFileService.Providers
|
||||
{
|
||||
//
|
||||
// Validate ...
|
||||
if (model.IsNull() ||
|
||||
providedForId.IsNull() ||
|
||||
model.Id.IsNull() ||
|
||||
model.Id.IsDefaultGuid())
|
||||
var result =
|
||||
!model.Id.IsNull() &&
|
||||
!providedForId.IsNull() &&
|
||||
!model.IsNullOrDefault() &&
|
||||
!model.Id.IsDefaultGuid();
|
||||
if (!result)
|
||||
{
|
||||
XException.InvalidArgs.Throw();
|
||||
}
|
||||
@@ -648,30 +651,98 @@ namespace xFileService.Providers
|
||||
}
|
||||
|
||||
//
|
||||
// Check Model Reference to ID ...
|
||||
var isReferenced = model.References
|
||||
.Any(r => r.ProvidedFor == Provider &&
|
||||
r.ReferencedTo == $"{providedForId}");
|
||||
// Reading Referencing Indexes ...
|
||||
var indexes = await GetReferencingIndexes(providedForId);
|
||||
|
||||
//
|
||||
var result = false;
|
||||
if (isReferenced)
|
||||
// Check Model Reference to ID ...
|
||||
var referencesCount = await CountReferences(providedForId);
|
||||
result = referencesCount == 0;
|
||||
if (result)
|
||||
{
|
||||
//
|
||||
model.References = model.References
|
||||
.Where(r => r.ProvidedFor != Provider ||
|
||||
(r.ProvidedFor == Provider &&
|
||||
r.ReferencedTo != $"{providedForId}"))
|
||||
.ToList();
|
||||
// There is not any Reference to Remove ...
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Check Model is Trully Referenced to ProvidedForId or not ...
|
||||
var reference = model.References
|
||||
.FirstOrDefault(r => r.ProvidedFor == Provider &&
|
||||
r.ReferencedTo == $"{providedForId}");
|
||||
result = !model.IsNullOrDefault();
|
||||
if (!result)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Remove Referenced Item from Model ...
|
||||
model.References = model.References
|
||||
.Where(r => !(r.ProvidedFor == Provider &&
|
||||
r.ReferencedTo == $"{providedForId}"))
|
||||
.ToList();
|
||||
|
||||
//
|
||||
// Update DataBase ...
|
||||
model = await FileProvider.Update(
|
||||
id: model.Id,
|
||||
item: model,
|
||||
userInfo: userInfo,
|
||||
connectionId: connectionId
|
||||
);
|
||||
|
||||
//
|
||||
result = !model.IsNullOrDefault();
|
||||
if (!result)
|
||||
{
|
||||
XException.ActionFailed.Throw();
|
||||
}
|
||||
|
||||
//
|
||||
// Get Max Exists Indexes ...
|
||||
var maxIndex = indexes.Max();
|
||||
|
||||
//
|
||||
// Here We Have to Re Arrange Indexed ...
|
||||
if (reference.Index < maxIndex)
|
||||
{
|
||||
//
|
||||
model = await FileProvider.Update(
|
||||
item: model,
|
||||
id: model.Id,
|
||||
userInfo: userInfo,
|
||||
connectionId: connectionId
|
||||
);
|
||||
result = !model.IsNullOrDefault();
|
||||
var startIndex = reference.Index + 1;
|
||||
for (int i = startIndex; i <= maxIndex; i++)
|
||||
{
|
||||
//
|
||||
var dto = await GetReference(
|
||||
forIndex: i,
|
||||
providedForId: providedForId
|
||||
);
|
||||
if (!dto.IsNullOrDefault())
|
||||
{
|
||||
//
|
||||
dto.References
|
||||
.ToList()
|
||||
.ForEach(iref =>
|
||||
{
|
||||
//
|
||||
if (
|
||||
iref.Index == i &&
|
||||
iref.ProvidedFor == Provider &&
|
||||
iref.ReferencedTo == reference.ReferencedTo
|
||||
)
|
||||
{
|
||||
iref.Index = i - 1;
|
||||
}
|
||||
});
|
||||
|
||||
//
|
||||
dto = await FileProvider.Update(
|
||||
item: dto,
|
||||
id: dto.Id,
|
||||
userInfo: userInfo,
|
||||
connectionId: connectionId
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user