Skip to content

Commit

Permalink
Merge pull request #144 from Kovah/dev
Browse files Browse the repository at this point in the history
v0.0.41
  • Loading branch information
Kovah committed Aug 12, 2020
2 parents 23cdc11 + ba94fb8 commit 97a222a
Show file tree
Hide file tree
Showing 14 changed files with 702 additions and 463 deletions.
9 changes: 2 additions & 7 deletions app/Http/Requests/Models/LinkStoreRequest.php
Expand Up @@ -13,18 +13,13 @@
*/
class LinkStoreRequest extends FormRequest
{
/** @var bool */
private $isApiRequest;

/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(Request $request): bool
{
$this->isApiRequest = $request->isJson();

return true;
}

Expand All @@ -45,8 +40,8 @@ public function rules(): array
],
'title' => 'nullable|string',
'description' => 'nullable|string',
'lists' => $this->isApiRequest ? 'array' : 'nullable|string',
'tags' => $this->isApiRequest ? 'array' : 'nullable|string',
'lists' => 'nullable',
'tags' => 'nullable',
'is_private' => 'sometimes|boolean',
'check_disabled' => 'sometimes|boolean',
];
Expand Down
9 changes: 2 additions & 7 deletions app/Http/Requests/Models/LinkUpdateRequest.php
Expand Up @@ -17,9 +17,6 @@ class LinkUpdateRequest extends FormRequest
/** @var bool */
private $requireUniqueUrl;

/** @var bool */
private $isApiRequest;

/**
* Determine if the user is authorized to make this request.
*
Expand All @@ -28,8 +25,6 @@ class LinkUpdateRequest extends FormRequest
*/
public function authorize(Request $request)
{
$this->isApiRequest = $request->isJson();

if ($request->input('url') !== null) {
$this->requireUniqueUrl = Link::urlHasChanged(
$request->route('link'),
Expand All @@ -51,8 +46,8 @@ public function rules()
'url' => 'required|string',
'title' => 'nullable|string',
'description' => 'nullable|string',
'lists' => $this->isApiRequest ? 'array' : 'nullable|string',
'tags' => $this->isApiRequest ? 'array' : 'nullable|string',
'lists' => 'nullable',
'tags' => 'nullable',
'is_private' => 'sometimes|boolean',
'check_disabled' => 'sometimes|boolean',
];
Expand Down
78 changes: 33 additions & 45 deletions app/Repositories/LinkRepository.php
Expand Up @@ -128,6 +128,8 @@ protected static function processLinkTaxonmies(Link $link, array $data)

/**
* Create or get the tags from the input and attach them to the link.
* If links or tags are provided as strings, they are converted into
* an array of entries, separated by commas.
*
* @param Link $link
* @param array|string $tags
Expand All @@ -136,12 +138,12 @@ protected static function updateTagsForLink(Link $link, $tags): void
{
$oldTags = $link->tags->pluck('id');

if (is_array($tags)) {
$newTags = self::processTaxonomyAsArray(Tag::class, $tags);
} else {
$newTags = self::processTaxonomyAsString(Tag::class, $tags);
if (!is_array($tags)) {
$tags = explode(',', $tags);
}

$newTags = self::processTaxonomy(Tag::class, $tags);

$link->tags()->sync($newTags);

if ($oldTags->isEmpty() || $oldTags->diff($newTags)->isNotEmpty()) {
Expand All @@ -164,12 +166,12 @@ protected static function updateListsForLink(Link $link, $lists): void
{
$oldLists = $link->lists->pluck('id');

if (is_array($lists)) {
$newLists = self::processTaxonomyAsArray(LinkList::class, $lists);
} else {
$newLists = self::processTaxonomyAsString(LinkList::class, $lists);
if (!is_array($lists)) {
$lists = explode(',', $lists);
}

$newLists = self::processTaxonomy(LinkList::class, $lists);

$link->lists()->sync($newLists);

if ($oldLists->isEmpty() || $oldLists->diff($newLists)->isNotEmpty()) {
Expand All @@ -183,51 +185,37 @@ protected static function updateListsForLink(Link $link, $lists): void
}

/**
* Tags or lists are passed as comma-delimited string in the LinkAce
* frontend. Parsing the string also creates the corresponding tag or list
* if it does not exist already.
* Tags or lists are passed as comma-delimited strings or integers.
* If integers are passed we assume that the tags or lists are referenced
* by their ID. n that case we try to reetrieve the tag or list by the
* provided ID.
* If tags or lists are passed as strings, we create them and pass the new
* entity to the taxonomy list.
*
* @param string $model
* @param string $tags
* @param array $entries
* @return Collection
*/
protected static function processTaxonomyAsString(string $model, string $tags): Collection
protected static function processTaxonomy(string $model, array $entries): Collection
{
$parsedTags = explode(',', $tags);
$newTags = collect();

foreach ($parsedTags as $tag) {
$newTag = $model::firstOrCreate([
'user_id' => auth()->user()->id,
'name' => $tag,
]);

$newTags->push($newTag->id);
}

return $newTags;
}

/**
* Tags or lists are passed as arrays containing the model IDs in API
* calls. The passed IDs are first checked for existence before allowing
* them to be synced with the link.
*
* @param string $model
* @param array $data
* @return Collection
*/
protected static function processTaxonomyAsArray(string $model, array $data): Collection
{
$entries = collect();
$newEntries = collect();

foreach ($entries as $entry) {
if (is_int($entry)) {
$newEntry = Tag::find($entry);
} else {
$newEntry = $model::firstOrCreate([
'user_id' => auth()->id(),
'name' => trim($entry),
]);
}

foreach ($data as $entry) {
if ($model::find($entry)) {
$entries->push($entry);
if ($newEntry !== null) {
$newEntries->push($newEntry->id);
}
}

return $entries;
return $newEntries;
}

/**
Expand All @@ -240,7 +228,7 @@ protected static function processTaxonomyAsArray(string $model, array $data): Co
* @param mixed $oldData
* @param mixed $newData
*/
protected static function createRelationshipRevision(Link $link, string $key, $oldData, $newData)
protected static function createRelationshipRevision(Link $link, string $key, $oldData, $newData): void
{
$revision = [
'revisionable_type' => $link->getMorphClass(),
Expand Down

0 comments on commit 97a222a

Please sign in to comment.