Skip to content

Commit

Permalink
Feat:add user preference filter data and fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
mehdi-fathi committed Dec 5, 2023
1 parent ef3a4fe commit e48efcc
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 41 deletions.
69 changes: 61 additions & 8 deletions app/Http/Controllers/Api/UserPreferenceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,36 @@ public function list(Request $request)
* mediaType="multipart/form-data",
* @OA\Schema(
* type="object",
* @OA\Property(property="preferences[source]", type="string"),
* @OA\Property(property="preferences[published_at][from]", type="string"),
* @OA\Property(property="preferences[published_at][to]", type="string"),
* @OA\Property(property="preferences[category]", type="string"),
* @OA\Property(
* property="preferences[authors][0]",
* description="Author 1",
* type="string"
* ),
* @OA\Property(
* property="preferences[authors][1]",
* description="Author 2",
* type="string"
* ),
* @OA\Property(
* property="preferences[categories][0]",
* description="sport",
* type="string"
* ),
* @OA\Property(
* property="preferences[categories][1]",
* description="art",
* type="string"
* ),
* @OA\Property(
* property="preferences[sources][0]",
* description="bbc-news",
* type="string"
* ),
* @OA\Property(
* property="preferences[sources][1]",
* description="bbc-news",
* type="string"
* ),
* @OA\Property(property="name", type="string")
* )
* )
Expand Down Expand Up @@ -67,10 +93,37 @@ public function store(UserPreferenceStoreRequest $request)
* @OA\MediaType(
* mediaType="application/x-www-form-urlencoded",
* @OA\Schema(
* type="object",
* @OA\Property(property="preferences[source]", type="string"),
* @OA\Property(property="preferences[published_at][from]", type="string"),
* @OA\Property(property="preferences[category]", type="string"),
* type="object",
* @OA\Property(
* property="preferences[authors][0]",
* description="Author 1",
* type="string"
* ),
* @OA\Property(
* property="preferences[authors][1]",
* description="Author 2",
* type="string"
* ),
* @OA\Property(
* property="preferences[categories][0]",
* description="sport",
* type="string"
* ),
* @OA\Property(
* property="preferences[categories][1]",
* description="art",
* type="string"
* ),
* @OA\Property(
* property="preferences[sources][0]",
* description="bbc-news",
* type="string"
* ),
* @OA\Property(
* property="preferences[sources][1]",
* description="bbc-news",
* type="string"
* ),
* @OA\Property(property="name", type="string")
* )
* )
Expand Down
8 changes: 3 additions & 5 deletions app/Http/Requests/UserPreferenceStoreRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ public function authorize(): bool
public function rules(): array
{
return [
'preferences.source' => 'required_without_all:preferences.published_at,preferences.category',
'preferences.published_at' => [
'from' => 'required_without_all:preferences.source,preferences.category',
],
'preferences.category' => 'required_without_all:preferences.source,preferences.published_at',
'preferences.sources' => 'required_without_all:preferences.categories,preferences.authors',
'preferences.categories' => 'required_without_all:preferences.source,preferences.authors',
'preferences.authors' => 'required_without_all:preferences.source,preferences.categories',
'name' => 'required|string|unique:user_preferences,name',
];
}
Expand Down
8 changes: 3 additions & 5 deletions app/Http/Requests/UserPreferenceUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ public function authorize(): bool
public function rules(): array
{
return [
'preferences.source' => 'required_without_all:preferences.published_at,preferences.category',
'preferences.published_at' => [
'from' => 'required_without_all:preferences.source,preferences.category',
],
'preferences.category' => 'required_without_all:preferences.source,preferences.published_at',
'preferences.sources' => 'required_without_all:preferences.categories,preferences.authors',
'preferences.categories' => 'required_without_all:preferences.source,preferences.authors',
'preferences.authors' => 'required_without_all:preferences.source,preferences.categories',
'name' => 'required|string',
];
}
Expand Down
22 changes: 20 additions & 2 deletions app/Logic/Service/NewsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,32 @@ public function getFilteredNews($data, int $limit = 10): LengthAwarePaginator
if (!empty($data['preference'])) {
$UserPreferenceData = $this->UserPreferenceService->findOrFailByName($data['preference']);
$data = $UserPreferenceData['preferences'];
}

$this->filterNewsByData($data);
$this->filterNewsByPreferenceData($data);

} else {
$this->filterNewsByData($data);
}

$out = $this->newsRepo->getFilteredDataPaginate(['Source.DataSource'], $limit);
return $out;
}

/**
* @param mixed $data
* @return void
*/
private function filterNewsByPreferenceData(mixed $data): void
{
collect($data)->filter()->each(function ($item, $key) {
match ($key) {
'sources' => $this->newsRepo->getFilteredBySource($item),
'categories' => $this->newsRepo->getFilteredByCategory($item),
'authors' => $this->newsRepo->getFilteredByAuthor($item),
};
});
}

/**
* @param mixed $data
* @return void
Expand Down
17 changes: 13 additions & 4 deletions app/Models/News.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function Source()
public function scopeGetSource($query, $value)
{
return $query->whereHas('Source', function ($q) use ($value) {
$q->where('name', $value);
$q->whereIn('name', $value);
});
}

Expand All @@ -62,15 +62,24 @@ public function scopeGetPublishedAt($query, string $fromDate, ?string $toDate =

}

/**
* @param $query
* @param $category
* @return mixed
*/
public function scopeGetCategory($query, $category)
{
return $query->whereIn('category', $category);
}

/**
* @param $query
* @param string|null $category
* @param $author
* @return mixed
*/
public function scopeGetCategory($query, ?string $category = null)
public function scopeGetAuthor($query, $author)
{
return $query->where('category', $category);
return $query->whereIn('author', $author);
}

/**
Expand Down
19 changes: 14 additions & 5 deletions app/Repositories/News/EloquentNewsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,30 @@ public function getFilteredByPublishedAt($from_published_at, $to_published_at):
}

/**
* @param $category
* @param string $category
* @return void
*/
public function getFilteredByCategory($category): void
public function getFilteredByCategory(string $category): void
{
$this->setBuilder($this->getBuilder()->getCategory($category));
}

/**
* @param $category
* @param $author
* @return void
*/
public function searchByText($category): void
public function getFilteredByAuthor($author): void
{
$this->setBuilder($this->getBuilder()->search($category));
$this->setBuilder($this->getBuilder()->getAuthor($author));
}

/**
* @param string $text
* @return void
*/
public function searchByText(string $text): void
{
$this->setBuilder($this->getBuilder()->search($text));
}

/**
Expand Down
14 changes: 10 additions & 4 deletions app/Repositories/News/NewsRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,22 @@ public function getFilteredBySource($source);
public function getFilteredByPublishedAt($from_published_at, $to_published_at): void;

/**
* @param $category
* @param string $category
* @return void
*/
public function getFilteredByCategory($category): void;
public function getFilteredByCategory(string $category): void;

/**
* @param $category
* @param string $author
* @return void
*/
public function searchByText($category): void;
public function getFilteredByAuthor(string $author): void;

/**
* @param string $text
* @return void
*/
public function searchByText(string $text): void;

/**
* @param array $relations
Expand Down
43 changes: 35 additions & 8 deletions storage/api-docs/api-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"name": "published_at[to]",
"in": "query",
"description": "published_at to",
"example": "2023-12-04"
"example": "2023-12-05"
},
{
"name": "category",
Expand Down Expand Up @@ -89,16 +89,28 @@
"multipart/form-data": {
"schema": {
"properties": {
"preferences[source]": {
"preferences[authors][0]": {
"description": "Author 1",
"type": "string"
},
"preferences[published_at][from]": {
"preferences[authors][1]": {
"description": "Author 2",
"type": "string"
},
"preferences[published_at][to]": {
"preferences[categories][0]": {
"description": "sport",
"type": "string"
},
"preferences[category]": {
"preferences[categories][1]": {
"description": "art",
"type": "string"
},
"preferences[sources][0]": {
"description": "bbc-news",
"type": "string"
},
"preferences[sources][1]": {
"description": "bbc-news",
"type": "string"
},
"name": {
Expand Down Expand Up @@ -139,13 +151,28 @@
"application/x-www-form-urlencoded": {
"schema": {
"properties": {
"preferences[source]": {
"preferences[authors][0]": {
"description": "Author 1",
"type": "string"
},
"preferences[authors][1]": {
"description": "Author 2",
"type": "string"
},
"preferences[categories][0]": {
"description": "sport",
"type": "string"
},
"preferences[categories][1]": {
"description": "art",
"type": "string"
},
"preferences[published_at][from]": {
"preferences[sources][0]": {
"description": "bbc-news",
"type": "string"
},
"preferences[category]": {
"preferences[sources][1]": {
"description": "bbc-news",
"type": "string"
},
"name": {
Expand Down

0 comments on commit e48efcc

Please sign in to comment.