Skip to content

Commit

Permalink
Tweak: Delete - Return deleted ids, do not delete child if its a pare…
Browse files Browse the repository at this point in the history
…nt (#11)
  • Loading branch information
iNewLegend authored Sep 7, 2023
1 parent 31294d0 commit 087a668
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 19 deletions.
14 changes: 8 additions & 6 deletions crawler-backend/app/Http/Middleware/CrawlerNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@ public function handle(Request $request, Closure $next): Response
*/
$context = $response->getOriginalContent();

if (!$context instanceof \Illuminate\Support\Collection) {
if ( $context instanceof \App\Models\Url ) {
$stringify = json_encode($this->mapper($context));

return new Response($stringify, $response->getStatusCode(), $response->headers->all());
}

// Map the collection to a new collection
$result = $context->map(function ($item) {
return $this->mapper($item);
});
if ( $context instanceof \Illuminate\Support\Collection ) {
// Map the collection to a new collection
$result = $context->map(function ($item) {
return $this->mapper($item);
});

return new Response($result, $response->getStatusCode(), $response->headers->all());
return new Response($result, $response->getStatusCode(), $response->headers->all());
}
}

return $response;
Expand Down
73 changes: 63 additions & 10 deletions crawler-backend/app/Services/CrawlerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,18 @@ public function update($id, $depth)
return $urlModel;
}

public function delete($id)
/**
* Delete a URL and its associated child URLs.
*
* Deletes a URL by its ID and, if it has child URLs, removes them as well.
* If the URL is not found, it returns a 404 Response.
*
* @param string $id The ID of the URL to delete.
*
* @return array|Response An array containing the deleted URL ID and the IDs of its deleted child URLs,
* or a 404 Response if the URL was not found.
*/
public function delete(string $id): array|Response
{
$urlModel = $this->url->where('_id', $id)->first();

Expand All @@ -161,32 +172,74 @@ public function delete($id)
return new Response('Not found', 404);
}

// Delete all children.
$children = $this->getChildrenOf($id)->get();
$result = [];

$owner = new class ($this->getChildrenOf($id)) {
private int $initialCount;

private Collection $children;

private Collection $deletedChildren;

public function __construct($builder)
{
$this->children = $builder->get();

$this->initialCount = $builder->count();

$this->deletedChildren = new Collection();
}

public function delete(Url $child): void
{
$this->deletedChildren->push($child);

$child->delete();
}

public function getChildren(): Collection
{
return $this->children;
}

foreach ($children as $child) {
public function getDeletedChildren(): Collection
{
return $this->deletedChildren;
}

public function hasChildren(): bool
{
return $this->initialCount <> $this->deletedChildren->count();
}
};

// Delete all children.
foreach ($owner->getChildren() as $child) {
// Remove owner from child, manually.
$child->owner_ids = array_diff($child->owner_ids, [$urlModel->id]);

// Reindex array - will be object in db without.
$child->owner_ids = array_values($child->owner_ids);

// If no owners, delete.
if (!count($child->owner_ids)) {
$child->delete();
// If the child is not a parent(index link) and there are no owners, delete.
if ((!isset($child->depth) || $child->depth < 0) && !count($child->owner_ids)) {
$owner->delete($child);
continue;
}

$child->save();
}

// TODO - Use memory - If no children, remove depth - lazy no time.
if (!$this->getChildrenOf($id)->count()) {
if (!$owner->hasChildren()) {
$result[] = $id;

$urlModel->depth = -1;
$urlModel->save();
}

return $urlModel;
return array_merge($result,
$owner->getDeletedChildren()->pluck('_id')->toArray()
);
}

private function getChildrenOf($id): Url|Builder
Expand Down
4 changes: 2 additions & 2 deletions crawler-frontend/src/api/api.crawler.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class ApiCrawlerService {
return this.http.put( '/crawler/' + id + "?depth=" + depth, {} ) as Observable<CrawlerInterfaceWithChildren>;
}

deleteCrawler( id: string ): Observable<CrawlerInterfaceWithChildren> {
return this.http.delete( '/crawler/' + id ) as Observable<CrawlerInterfaceWithChildren>;
deleteCrawler( id: string ): Observable<string[]> {
return this.http.delete( '/crawler/' + id ) as Observable<string[]>;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export class CrawlerDisplayIndexComponent {

onDeleteClick( item: CrawlerInterfaceWithChildCountable ) {
this.crawler.deleteCrawler( item.id ).subscribe(
() => this.deleteItemEvent.emit( item )
( deletedItemsId ) => {
if ( deletedItemsId.includes( item.id ) ) {
this.deleteItemEvent.emit( item );
}
},
)
}

Expand Down

0 comments on commit 087a668

Please sign in to comment.