Skip to content

Commit

Permalink
Вывод товаров по uri
Browse files Browse the repository at this point in the history
  • Loading branch information
bezumkin committed Aug 28, 2023
1 parent fce9782 commit ecd5d9b
Show file tree
Hide file tree
Showing 23 changed files with 249 additions and 137 deletions.
4 changes: 3 additions & 1 deletion .env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@ SMTP_USER=no-reply@localhost
SMTP_USER_NAME=no-reply
SMTP_PASS=
SMTP_PORT=25
SMTP_PROTO=
SMTP_PROTO=

PRODUCTS_PREFIX=products
7 changes: 4 additions & 3 deletions core/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ static function (RouteCollectorProxy $group) {
$group->group(
'/web',
static function (RouteCollectorProxy $group) {
$group->any('/categories[/{alias}]', App\Controllers\Web\Categories::class);
$group->any('/categories/{category}/products[/{alias}]', App\Controllers\Web\Category\Products::class);
$group->any('/products', App\Controllers\Web\Products::class);
$group->any('/resource/{uri:.+}', App\Controllers\Web\Resource::class);
$group->any('/categories[/{uri:.+}]', App\Controllers\Web\Categories::class);
$group->any('/category/{category_id}/products', App\Controllers\Web\Category\Products::class);
$group->any('/products[/{uri:.+}]', App\Controllers\Web\Products::class);
$group->any('/orders', App\Controllers\Web\Orders::class);
}
);
Expand Down
16 changes: 12 additions & 4 deletions core/src/Controllers/Web/Categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@

use App\Models\Category;
use Illuminate\Database\Eloquent\Builder;
use Vesp\Controllers\ModelController;
use Vesp\Controllers\ModelGetController;

class Categories extends ModelController
class Categories extends ModelGetController
{
protected $model = Category::class;

protected function beforeGet(Builder $c): Builder
{
$c->where('active', true);
$c->with('translations');

return $c;
}
Expand All @@ -22,10 +23,17 @@ protected function beforeCount(Builder $c): Builder
return $this->beforeGet($c);
}

protected function afterCount(Builder $c): Builder
{
$c->with('translations:category_id,lang,title');

return $c;
}

protected function getPrimaryKey(): ?array
{
if ($alias = $this->getProperty('alias')) {
return ['alias' => $alias];
if ($uri = $this->getProperty('uri')) {
return ['uri' => $uri];
}

return null;
Expand Down
23 changes: 2 additions & 21 deletions core/src/Controllers/Web/Category/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,18 @@

class Products extends \App\Controllers\Web\Products
{
/** @var Category $category */
protected $category;
protected ?Category $category;

public function checkScope(string $method): ?ResponseInterface
{
$c = Category::query()->where(['active' => true, 'uri' => $this->getProperty('category')]);
$c = Category::query()->where(['active' => true, 'id' => $this->getProperty('category_id')]);
if (!$this->category = $c->first()) {
return $this->failure('', 404);
}

return null;
}

protected function beforeGet(Builder $c): Builder
{
$c->where('active', true);
$c->with('category:id,uri,active', 'category.translations:category_id,lang,title');
$c->with('productFiles', static function (HasMany $c) {
$c->where('active', true);
$c->orderBy('rank');
$c->select('product_id', 'file_id');
$c->with('file:id,updated_at');
});

return $c;
}

protected function beforeCount(Builder $c): Builder
{
$c->where(['active' => true, 'category_id' => $this->category->id]);
Expand All @@ -45,10 +30,6 @@ protected function beforeCount(Builder $c): Builder

protected function getPrimaryKey(): ?array
{
if ($alias = $this->getProperty('alias')) {
return ['alias' => $alias, 'category_id' => $this->category->id];
}

return null;
}
}
26 changes: 23 additions & 3 deletions core/src/Controllers/Web/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@

use App\Models\Product;
use Illuminate\Database\Eloquent\Builder;
use Vesp\Controllers\ModelController;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Vesp\Controllers\ModelGetController;

class Products extends ModelController
class Products extends ModelGetController
{
protected $model = Product::class;

Expand All @@ -17,16 +18,31 @@ protected function beforeGet(Builder $c): Builder
$c->where('active', true);
});

$c->with('translations');
$c->with('category:id,uri,active', 'category.translations:category_id,lang,title');
$c->with('productFiles', static function (HasMany $c) {
$c->where('active', true);
$c->orderBy('rank');
$c->select('product_id', 'file_id');
$c->with('file:id,updated_at');
});

return $c;
}

protected function beforeCount(Builder $c): Builder
{
return $this->beforeGet($c);
$c->where('active', true);
$c->whereHas('category', static function (Builder $c) {
$c->where('active', true);
});

return $c;
}

protected function afterCount(Builder $c): Builder
{
$c->with('translations:product_id,lang,title');
$c->with('category:id,uri,active', 'category.translations:category_id,lang,title');
$c->with('firstFile');

Expand All @@ -35,6 +51,10 @@ protected function afterCount(Builder $c): Builder

protected function getPrimaryKey(): ?array
{
if ($uri = $this->getProperty('uri')) {
return ['uri' => $uri];
}

return null;
}
}
45 changes: 45 additions & 0 deletions core/src/Controllers/Web/Resource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace App\Controllers\Web;

use App\Models\Category;
use App\Models\Product;
use Psr\Http\Message\ResponseInterface;
use Slim\App;
use Slim\Psr7\Factory\ServerRequestFactory;
use Vesp\Controllers\Controller;
use Vesp\Services\Eloquent;

class Resource extends Controller
{
private App $app;

public function __construct(App $app, Eloquent $eloquent)
{
parent::__construct($eloquent);
$this->app = $app;
}

public function get(): ResponseInterface
{
$uri = trim($this->getProperty('uri', ''), '/');
$endpoint = trim($this->request->getRequestTarget(), '/');
$factory = new ServerRequestFactory();

if (Product::query()->where('uri', $uri)->count()) {
$endpoint = str_replace('/resource/', '/products/', $endpoint);
$request = $factory->createServerRequest('GET', $endpoint);
} elseif (Category::query()->where('uri', $uri)->count()) {
$endpoint = str_replace('/resource/', '/categories/', $endpoint);
$request = $factory->createServerRequest('GET', $endpoint);
} else {
return $this->failure('Not Found', 404);
}

foreach ($this->request->getHeaders() as $key => $value) {
$request = $request->withHeader($key, $value);
}

return $this->app->handle($request);
}
}
4 changes: 3 additions & 1 deletion docker/.env.dist
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ SMTP_USER=no-reply@shop.vesp.pro
SMTP_USER_NAME=no-reply
SMTP_PASS=
SMTP_PORT=1025
SMTP_PROTO=
SMTP_PROTO=

PRODUCTS_PREFIX=products
2 changes: 1 addition & 1 deletion frontend/src/admin/lexicons/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import localRu from './ru'

export default () => {
return {
fallbackLocale: 'en',
fallbackLocale: 'ru',
messages: {
en: merge(vespEn, localEn),
ru: merge(vespRu, localRu),
Expand Down
19 changes: 19 additions & 0 deletions frontend/src/site/assets/scss/_toasted.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.toasted-container {
.toasted {
padding: 0.75rem 1.25rem;
border-radius: $border-radius;
color: $white;

&.success {
background-color: rgba($success, 0.9);
}

&.info {
background-color: rgba($info, 0.9);
}

&.error {
background-color: rgba($danger, 0.9);
}
}
}
1 change: 1 addition & 0 deletions frontend/src/site/assets/scss/index.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@import 'variables';
@import 'toasted';

/* @import '~bootstrap/scss/bootstrap'; */
@import '~bootstrap/scss/reboot';
Expand Down
14 changes: 5 additions & 9 deletions frontend/src/site/components/breadcrumbs.vue
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
<template>
<b-breadcrumb>
<b-breadcrumb-item :to="{name: 'index'}">
<b-breadcrumb-item :to="{name: 'products'}">
<fa icon="home" />
</b-breadcrumb-item>
<b-breadcrumb-item :to="{name: 'category', params: {category: category.alias}}" :active="!product">
{{ category.title }}
<b-breadcrumb-item :to="$productLink(category)" :active="!product">
{{ $translate(category.translations) }}
</b-breadcrumb-item>
<b-breadcrumb-item
v-if="product"
:to="{name: 'category-product', params: {category: category.alias, product: product.alias}}"
active
>
{{ product.title }}
<b-breadcrumb-item v-if="product" active>
{{ $translate(product.translations) }}
</b-breadcrumb-item>
</b-breadcrumb>
</template>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<template>
<div>
<breadcrumbs :category="category" />
<breadcrumbs v-if="category" :category="category" />

<list-products-actions v-model="listView" :sort.sync="sort" :dir.sync="dir" />

<list-products
v-model="page"
:category="$route.params.category"
:category="category ? category.id : null"
:limit="limit"
:sort="sort"
:dir="dir"
Expand All @@ -19,31 +19,25 @@
</template>

<script>
import ListProducts from '../../components/list-products'
import ListProductsActions from '../../components/list-products-actions'
import ListProducts from '~/components/list-products'
import ListProductsActions from '~/components/list-products-actions'
import Breadcrumbs from '~/components/breadcrumbs'
export default {
components: {Breadcrumbs, ListProductsActions, ListProducts},
validate({params}) {
return Boolean(params.category)
},
async asyncData({app, params, error}) {
try {
const {data} = await app.$axios.get('web/categories/' + params.category)
return {category: data}
} catch (e) {
error({statusCode: e.response.status, message: e.message})
}
props: {
category: {
type: Object,
default: null,
},
},
data() {
return {
page: 1,
total: 0,
sort: 'title',
sort: 'created_at',
dir: 'asc',
listView: false,
category: {},
}
},
computed: {
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/site/components/list-products-actions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
<b-col md="8" class="d-flex flex-wrap align-items-center justify-content-center justify-content-md-start">
<div class="mr-2 ml-md-auto">Сортировка:</div>
<b-button-group>
<b-button :pressed="sort === 'title'" variant="outline-secondary" @click="onSort('title')">
По названию
<template v-if="sort === 'title' && dir === 'desc'">&uarr;</template>
<b-button :pressed="sort === 'created_at'" variant="outline-secondary" @click="onSort('created_at')">
По дате
<template v-if="sort === 'created_at' && dir === 'desc'">&uarr;</template>
<template v-else>&darr;</template>
</b-button>
<b-button :pressed="sort === 'price'" variant="outline-secondary" @click="onSort('price')">
Expand Down

0 comments on commit ecd5d9b

Please sign in to comment.