Skip to content

Commit 8509c36

Browse files
committed
Update schema
1 parent f421281 commit 8509c36

File tree

11 files changed

+336
-223
lines changed

11 files changed

+336
-223
lines changed

app/Http/Controllers/BarController.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use Illuminate\Http\Resources\Json\JsonResource;
2424
use Kami\Cocktail\Http\Resources\BarMembershipResource;
2525
use Kami\Cocktail\OpenAPI\Schemas\BarRequest as SchemasBarRequest;
26+
use Throwable;
2627

2728
class BarController extends Controller
2829
{
@@ -87,7 +88,7 @@ public function show(Request $request, int $id): JsonResource
8788
public function store(BarRequest $request): JsonResponse
8889
{
8990
if ($request->user()->cannot('create', Bar::class)) {
90-
abort(403, 'You can not create anymore bars');
91+
abort(403, 'You can not create any more bars.');
9192
}
9293

9394
Cache::forget('metrics_bass_total_bars');
@@ -106,7 +107,7 @@ public function store(BarRequest $request): JsonResponse
106107
try {
107108
$imageModels = Image::findOrFail($barRequest->images);
108109
$bar->attachImages($imageModels);
109-
} catch (\Throwable $e) {
110+
} catch (Throwable $e) {
110111
abort(500, $e->getMessage());
111112
}
112113
}
@@ -167,7 +168,7 @@ public function update(int $id, BarRequest $request): JsonResource
167168
try {
168169
$imageModels = Image::findOrFail($barRequest->images);
169170
$bar->attachImages($imageModels);
170-
} catch (\Throwable $e) {
171+
} catch (Throwable $e) {
171172
abort(500, $e->getMessage());
172173
}
173174
}

app/Http/Controllers/Public/BarController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@
1212

1313
class BarController extends Controller
1414
{
15-
#[OAT\Get(path: '/public/{barId}', tags: ['Public'], operationId: 'showPublicBar', description: 'Show public information about a single bar. To access this endpoint the bar must be marked as public.', summary: 'Show bar', parameters: [
16-
new OAT\Parameter(name: 'barId', in: 'path', required: true, description: 'Database id of bar', schema: new OAT\Schema(type: 'number')),
15+
#[OAT\Get(path: '/public/{slugOrId}', tags: ['Public'], operationId: 'showPublicBar', description: 'Show public information about a single bar. To access this endpoint the bar must be marked as public.', summary: 'Show bar', parameters: [
16+
new OAT\Parameter(name: 'slugOrId', in: 'path', required: true, description: 'Database id of bar', schema: new OAT\Schema(type: 'string')),
1717
new BAO\Parameters\PageParameter(),
1818
], security: [])]
1919
#[BAO\SuccessfulResponse(content: [
2020
new BAO\WrapObjectWithData(BarResource::class),
2121
])]
2222
#[BAO\NotFoundResponse]
23-
public function show(int $barId): BarResource
23+
public function show(string $slugOrId): BarResource
2424
{
25-
$bar = Bar::findOrFail($barId);
25+
$bar = Bar::where('slug', $slugOrId)->orWhere('id', $slugOrId)->firstOrFail();
2626
if (!$bar->is_public) {
2727
abort(404);
2828
}

app/Http/Controllers/Public/CocktailController.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
class CocktailController extends Controller
2020
{
21-
#[OAT\Get(path: '/public/{barId}/cocktails', tags: ['Public'], operationId: 'listPublicBarCocktails', description: 'List and filter bar cocktails. To access this endpoint the bar must be marked as public.', summary: 'List cocktails', parameters: [
22-
new OAT\Parameter(name: 'barId', in: 'path', required: true, description: 'Database id of bar', schema: new OAT\Schema(type: 'number')),
21+
#[OAT\Get(path: '/public/{slugOrId}/cocktails', tags: ['Public'], operationId: 'listPublicBarCocktails', description: 'List and filter bar cocktails. To access this endpoint the bar must be marked as public.', summary: 'List cocktails', parameters: [
22+
new OAT\Parameter(name: 'slugOrId', in: 'path', required: true, description: 'Database id or slug of bar', schema: new OAT\Schema(type: 'string')),
2323
new BAO\Parameters\PageParameter(),
2424
new OAT\Parameter(name: 'filter', in: 'query', description: 'Filter by attributes. You can specify multiple matching filter values by passing a comma separated list of values.', explode: true, style: 'deepObject', schema: new OAT\Schema(type: 'object', properties: [
2525
new OAT\Property(property: 'name', type: 'string', description: 'Filter by cocktail names(s) (fuzzy search)'),
@@ -36,16 +36,16 @@ class CocktailController extends Controller
3636
new BAO\PaginateData(CocktailResource::class),
3737
])]
3838
#[BAO\NotFoundResponse]
39-
public function index(Request $request, int $barId): JsonResource
39+
public function index(Request $request, string $slugOrId): JsonResource
4040
{
41-
$bar = Bar::findOrFail($barId);
41+
$bar = Bar::where('slug', $slugOrId)->orWhere('id', $slugOrId)->firstOrFail();
4242
if (!$bar->is_public) {
4343
abort(404);
4444
}
4545

4646
$queryParams = $request->only(['filter', 'sort', 'page']);
4747
ksort($queryParams);
48-
$cacheKey = 'public_cocktails_index:' . $barId . ':' . sha1(http_build_query($queryParams));
48+
$cacheKey = 'public_cocktails_index:' . $bar->id . ':' . sha1(http_build_query($queryParams));
4949

5050
if (Cache::has($cacheKey)) {
5151
$cocktails = Cache::get($cacheKey);
@@ -66,31 +66,26 @@ public function index(Request $request, int $barId): JsonResource
6666
return CocktailResource::collection($cocktails->withQueryString());
6767
}
6868

69-
#[OAT\Get(path: '/public/{barId}/cocktails/{slugOrPublicId}', tags: ['Public'], operationId: 'showPublicBarCocktail', description: 'Show public information about cocktail. If valid public ID is provided it will used, if not it will use cocktail slug.', summary: 'Show cocktail', parameters: [
70-
new OAT\Parameter(name: 'barId', in: 'path', required: true, description: 'Database id of bar', schema: new OAT\Schema(type: 'number')),
69+
#[OAT\Get(path: '/public/{slugOrId}/cocktails/{slugOrPublicId}', tags: ['Public'], operationId: 'showPublicBarCocktail', description: 'Show public information about cocktail. If valid public ID is provided it will used, if not it will use cocktail slug.', summary: 'Show cocktail', parameters: [
70+
new OAT\Parameter(name: 'slugOrId', in: 'path', required: true, description: 'Database id of bar', schema: new OAT\Schema(type: 'string')),
7171
new OAT\Parameter(name: 'slugOrPublicId', in: 'path', required: true, description: 'Cocktail slug or public id (ULID)', schema: new OAT\Schema(type: 'string')),
7272
], security: [])]
7373
#[BAO\SuccessfulResponse(content: [
7474
new BAO\WrapObjectWithData(CocktailResource::class),
7575
])]
7676
#[BAO\NotFoundResponse]
77-
public function show(int $barId, string $slugOrPublicId): CocktailResource
77+
public function show(string $barId, string $slugOrPublicId): CocktailResource
7878
{
79-
$cocktail = Cocktail::where('bar_id', $barId)
80-
->where('public_id', $slugOrPublicId)
81-
->orWhere('slug', $slugOrPublicId)
82-
->with('ingredients.ingredient', 'ingredients.substitutes.ingredient', 'images', 'tags', 'utensils')
83-
->firstOrFail();
84-
85-
if ($cocktail->public_id === $slugOrPublicId) {
86-
return new CocktailResource($cocktail);
87-
}
88-
89-
$bar = Bar::findOrFail($barId);
79+
$bar = Bar::where('slug', $barId)->orWhere('id', $barId)->firstOrFail();
9080
if (!$bar->is_public) {
9181
abort(404);
9282
}
9383

84+
$cocktail = Cocktail::where('public_id', $slugOrPublicId)
85+
->orWhere('slug', $slugOrPublicId)
86+
->with('ingredients.ingredient', 'ingredients.substitutes.ingredient', 'images', 'tags', 'utensils')
87+
->firstOrFail();
88+
9489
return new CocktailResource($cocktail);
9590
}
9691
}

app/Http/Controllers/ShoppingListController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ShoppingListController extends Controller
3030
public function index(Request $request, int $id): JsonResource
3131
{
3232
$user = User::findOrFail($id);
33-
if ($request->user()->id !== $user->id && $request->user()->cannot('show', $user)) {
33+
if ($request->user()->id !== $user->id || $request->user()->cannot('show', $user)) {
3434
abort(403);
3535
}
3636

@@ -54,7 +54,7 @@ public function index(Request $request, int $id): JsonResource
5454
public function batchStore(IngredientsBatchRequest $request, int $id): Response
5555
{
5656
$user = User::findOrFail($id);
57-
if ($request->user()->id !== $user->id && $request->user()->cannot('show', $user)) {
57+
if ($request->user()->id !== $user->id || $request->user()->cannot('show', $user)) {
5858
abort(403);
5959
}
6060

@@ -111,7 +111,7 @@ public function batchStore(IngredientsBatchRequest $request, int $id): Response
111111
public function batchDelete(IngredientsBatchRequest $request, int $id): Response
112112
{
113113
$user = User::findOrFail($id);
114-
if ($request->user()->id !== $user->id && $request->user()->cannot('show', $user)) {
114+
if ($request->user()->id !== $user->id || $request->user()->cannot('show', $user)) {
115115
abort(403);
116116
}
117117

@@ -150,7 +150,7 @@ public function batchDelete(IngredientsBatchRequest $request, int $id): Response
150150
public function share(Request $request, int $id): JsonResponse
151151
{
152152
$user = User::findOrFail($id);
153-
if ($request->user()->id !== $user->id && $request->user()->cannot('show', $user)) {
153+
if ($request->user()->id !== $user->id || $request->user()->cannot('show', $user)) {
154154
abort(403);
155155
}
156156

app/Http/Controllers/StatsController.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ class StatsController extends Controller
3131
public function index(CocktailService $cocktailRepo, Request $request, int $id): JsonResponse
3232
{
3333
$bar = Bar::findOrFail($id);
34-
$barMembership = $request->user()->getBarMembership($bar->id)->load('userIngredients');
34+
$barMembership = $request->user()->getBarMembership($bar->id);
35+
36+
if ($barMembership === null) {
37+
abort(403);
38+
}
39+
40+
$barMembership->load('userIngredients');
41+
3542
$limit = 5;
3643
$stats = [];
3744

app/Http/Resources/Public/BarResource.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
new OAT\Property(property: 'subtitle', type: 'string', nullable: true, example: 'A short subtitle of a bar', description: 'Optional short quip about the bar'),
2121
new OAT\Property(property: 'description', type: 'string', nullable: true, example: 'Bar description', description: 'Description of the bar'),
2222
new OAT\Property(property: 'images', type: 'array', items: new OAT\Items(type: ImageResource::class), description: 'Images associated with the bar'),
23+
new OAT\Property(property: 'is_menu_enabled', type: 'boolean', example: true, description: 'Whether the bar has enabled its menu for public viewing'),
2324
],
24-
required: ['id', 'slug', 'name', 'subtitle', 'description', 'images'],
25+
required: ['id', 'slug', 'name', 'subtitle', 'description', 'images', 'is_menu_enabled'],
2526
)]
2627
class BarResource extends JsonResource
2728
{
@@ -40,6 +41,7 @@ public function toArray($request)
4041
'subtitle' => $this->subtitle,
4142
'description' => $this->description,
4243
'images' => ImageResource::collection($this->images),
44+
'is_menu_enabled' => $this->menu?->is_enabled ?? false,
4345
];
4446
}
4547
}

app/Http/Resources/Public/CocktailResource.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ public function toArray($request)
9090
'description' => e($this->description),
9191
'source' => $this->source,
9292
'public_id' => $this->public_id,
93-
'bar_id' => $this->bar_id,
9493
'public_at' => $this->public_at?->toAtomString() ?? null,
9594
'images' => ImageResource::collection($this->images),
9695
'tags' => $this->when(

app/Models/Bar.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Illuminate\Database\Eloquent\Relations\HasMany;
1919
use Illuminate\Database\Eloquent\Factories\HasFactory;
2020
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
21+
use Illuminate\Database\Eloquent\Relations\HasOne;
2122

2223
class Bar extends Model implements UploadableInterface
2324
{
@@ -133,6 +134,14 @@ public function exports(): HasMany
133134
return $this->hasMany(Export::class);
134135
}
135136

137+
/**
138+
* @return HasOne<Menu, $this>
139+
*/
140+
public function menu(): HasOne
141+
{
142+
return $this->hasOne(Menu::class);
143+
}
144+
136145
public function owner(): User
137146
{
138147
return $this->createdUser;

0 commit comments

Comments
 (0)