Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markdown and Recipe Manager #6

Merged
merged 1 commit into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,14 @@ docker-compose up -d php
### Admin section
- [x] Label Manager
- [x] Category Manager
- [ ] Recipe Manager
- [x] Recipe Manager
- [ ] Image uploads
- [ ] Paginate Recipe Manager
- [ ] User Management
### Other
- [ ] Customization of intro blurb
- [ ] Recent recipes component
- [ ] Customizable intro blurb and logo
- [x] Recent recipes on homepage
- [ ] Infinite scroll for recipe lists
### Possible future features
- [ ] "I made it" counter
- [ ] Reviews/ratings
Expand Down
3 changes: 3 additions & 0 deletions app/Actions/Fortify/UpdateUserProfileInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
use Illuminate\Validation\Rule;
use Laravel\Fortify\Contracts\UpdatesUserProfileInformation;

/**
* @codeCoverageIgnore
*/
class UpdateUserProfileInformation implements UpdatesUserProfileInformation
{
/**
Expand Down
131 changes: 131 additions & 0 deletions app/Http/Controllers/Admin/RecipeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Enums\BannerTypeEnum;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Label;
use App\Models\Recipe;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use Inertia\Inertia;

class RecipeController extends Controller
{
public function index()
{
return Inertia::render('Admin/Recipe/RecipeIndex')->with([
'recipes' => fn () => Recipe::withTrashed()
->with('labels', 'categories')
->orderBy('id', 'desc')
->get()
->makeVisible('id'),
]);
}

public function store(Request $request): RedirectResponse
{
$validated = $request->validate([
'name' => ['required', 'max:140', 'unique:recipes,name'],
'serving' => ['required', 'string'],
'cook_time' => ['required', 'integer'],
'prep_time' => ['required', 'integer'],
'ingredients' => ['required', 'string', 'min:10'],
'instructions' => ['required', 'string', 'min:10'],
'description' => ['nullable', 'string'],
'labelsSelected' => ['array'],
'categoriesSelected' => ['array'],
]) + ['user_id' => $request->user()->id];

unset($validated['categoriesSelected'], $validated['labelsSelected']);

$recipe = Recipe::create($validated);

$recipe->labels()->sync(
Label::whereIn('slug', $request->get('labelsSelected'))->pluck('id')->toArray()
);

$recipe->categories()->sync(
Category::whereIn('slug', $request->get('categoriesSelected'))->pluck('id')->toArray()
);

return redirect()->route('admin.recipes.index')->withBanner("Successfully created {$recipe->name}");
}

public function edit(Recipe $recipe)
{
return Inertia::render('Admin/Recipe/RecipeEdit')->with([
'recipe' => fn () => $recipe->makeVisible('id')->loadMissing('labels', 'categories'),
'labels' => fn () => Label::withTrashed()
->orderBy('order_column')
->get()
->makeVisible('id'),
'categories' => fn () => Category::withTrashed()
->orderBy('order_column')
->get()
->makeVisible('id'),
]);
}

public function update(Recipe $recipe, Request $request)
{
$validated = $request->validate([
'name' => ['required', 'max:140', Rule::unique('recipes')->ignore($recipe->id)],
'serving' => ['required', 'string'],
'cook_time' => ['required', 'integer'],
'prep_time' => ['required', 'integer'],
'ingredients' => ['required', 'string', 'min:10'],
'instructions' => ['required', 'string', 'min:10'],
'description' => ['nullable', 'string'],
'labelsSelected' => ['array'],
'categoriesSelected' => ['array'],
]);

$recipe->labels()->sync(
Label::whereIn('slug', $request->get('labelsSelected'))->pluck('id')->toArray()
);
$recipe->categories()->sync(
Category::whereIn('slug', $request->get('categoriesSelected'))->pluck('id')->toArray()
);

unset($validated['categoriesSelected'], $validated['labelsSelected']);
$recipe->update($validated);

return redirect()->back()->withBanner("Successfully updated {$recipe->name}");
}

public function create()
{
return Inertia::render('Admin/Recipe/RecipeEdit')->with([
'recipe' => fn () => new Recipe(),
'labels' => fn () => Label::withTrashed()
->orderBy('order_column')
->get()
->makeVisible('id'),
'categories' => fn () => Category::withTrashed()
->orderBy('order_column')
->get()
->makeVisible('id'),
]);
}

public function destroy(Recipe $recipe)
{
$name = $recipe->name;
$recipe->delete();

return redirect()->route('admin.recipes.index')
->withBanner("Deleted {$name}", BannerTypeEnum::danger);
}

public function restore(Recipe $recipe)
{
$name = $recipe->name;
$recipe->restore();

return redirect()->route('admin.recipes.index')
->withBanner("Restored {$name}");
}
}
21 changes: 21 additions & 0 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Http\Controllers;

use App\Models\Recipe;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;

class HomeController extends Controller
{
public function __invoke(Request $request): Response
{
return Inertia::render('Dashboard')->with([
'most_recent_recipes' => Inertia::lazy(fn () => Recipe::with('images', 'labels', 'categories')
->orderBy('id', 'desc')
->limit(3)
->get()),
]);
}
}
2 changes: 1 addition & 1 deletion app/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ public function getSlugOptions(): SlugOptions

public function recipes(): BelongsToMany
{
return $this->belongsToMany(Recipe::class);
return $this->belongsToMany(Recipe::class)->orderBy('id', 'desc');
}
}
2 changes: 1 addition & 1 deletion app/Models/Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,6 @@ public function getSlugOptions(): SlugOptions

public function recipes(): BelongsToMany
{
return $this->belongsToMany(Recipe::class);
return $this->belongsToMany(Recipe::class)->orderBy('id', 'desc');
}
}
5 changes: 4 additions & 1 deletion app/Models/Recipe.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class Recipe extends Model
'serving',
'ingredients',
'instructions',
'description',
'cook_time',
'prep_time',
'published_at',
'user_id',
];
Expand Down Expand Up @@ -58,7 +61,7 @@ public function toSearchableArray(): array
{
return [
'name' => $this->name,
'ingredients' => $this->ingredients,
// 'ingredients' => $this->ingredients,
];
}

Expand Down
2 changes: 2 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ class AppServiceProvider extends ServiceProvider
*/
public function register(): void
{
// @codeCoverageIgnoreStart
if ($this->app->isLocal()) {
$this->app->register(IdeHelperServiceProvider::class);
}
// @codeCoverageIgnoreEnd

RedirectResponse::macro('withBanner', function (string $message, ?BannerTypeEnum $bannerType = BannerTypeEnum::success) {
// @phpstan-ignore-next-line
Expand Down
2 changes: 2 additions & 0 deletions app/Providers/FortifyServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function register(): void

/**
* Bootstrap any application services.
*
* @codeCoverageIgnore
*/
public function boot(): void
{
Expand Down
Binary file modified bun.lockb
Binary file not shown.
3 changes: 3 additions & 0 deletions database/factories/RecipeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public function definition(): array
'serving' => $this->faker->word(),
'ingredients' => $this->faker->text(),
'instructions' => $this->faker->text(),
'description' => $this->faker->text(),
'cook_time' => $this->faker->numberBetween(30, 90),
'prep_time' => $this->faker->numberBetween(5, 30),
'published_at' => $this->faker->dateTime(),
'user_id' => User::factory(),
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public function up(): void
$table->string('name');
$table->string('slug')->index();
$table->string('serving');
$table->integer('prep_time')->default(0);
$table->integer('cook_time')->default(0);
$table->integer('total_time')->storedAs('cook_time + prep_time');
$table->longText('ingredients');
$table->longText('instructions');
$table->longText('description')->nullable();
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"vue": "^3.3.13"
},
"dependencies": {
"marked": "^12.0.1",
"primeicons": "^6.0.1",
"primevue": "^3.49.1"
}
Expand Down
49 changes: 45 additions & 4 deletions resources/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,51 @@
@tailwind components;
@tailwind utilities;

@layer base {
body {
@apply text-surface-700 dark:text-white/70
}

h1 {
@apply text-4xl text-primary-200;
.primary {
@apply text-primary-400;
}
}
h2 {
@apply text-3xl text-primary-200;
.primary {
@apply text-primary-400;
}
}
h3 {
@apply text-2xl text-primary-200;
.primary {
@apply text-primary-400;
}
}
h4 {
@apply text-xl text-primary-200;
.primary {
@apply text-primary-400;
}
}
h5 {
@apply text-lg text-primary-200;
.primary {
@apply text-primary-400;
}
}
ul {
list-style: inside;
}

ol {
list-style: decimal;
@apply ml-4;
}
}

:root {
--primary-50: 255 247 237;
--primary-100: 255 237 213;
Expand Down Expand Up @@ -29,10 +74,6 @@
--surface-950: 8 8 8;
}

h1, h2 {
color: var(--surface-0);
}

[x-cloak] {
display: none;
}
28 changes: 28 additions & 0 deletions resources/js/Components/Markdown.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script setup>
import {computed} from "vue";
import {marked} from "marked";

const props = defineProps({
body: {type: [String, null], required: false, default: ""}
});

const markdownToHtml = computed(() => marked.parse(props.body || "") )
</script>

<template>
<div class="markdown-container">
<div v-html="markdownToHtml"></div>
</div>
</template>

<style lang="scss" scoped>
.markdown-container::v-deep {
p {
@apply mb-2;
}

h1, h2, h3, h4, h5 {
@apply mb-3;
}
}
</style>
Loading