diff --git a/app/Ejaculation.php b/app/Ejaculation.php index 80fd2b7..399783a 100644 --- a/app/Ejaculation.php +++ b/app/Ejaculation.php @@ -22,4 +22,9 @@ class Ejaculation extends Model { return $this->belongsTo('App\User'); } + + public function tags() + { + return $this->belongsToMany('App\Tag')->withTimestamps(); + } } diff --git a/app/Http/Controllers/EjaculationController.php b/app/Http/Controllers/EjaculationController.php index ecba43f..ca731c5 100644 --- a/app/Http/Controllers/EjaculationController.php +++ b/app/Http/Controllers/EjaculationController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Tag; use App\User; use Carbon\Carbon; use Validator; @@ -28,6 +29,7 @@ class EjaculationController extends Controller 'time' => 'required|date_format:H:i', 'note' => 'nullable|string|max:500', 'link' => 'nullable|url', + 'tags' => 'nullable|string', ])->after(function ($validator) use ($request, $inputs) { // 日時の重複チェック if (!$validator->errors()->hasAny(['date', 'time'])) { @@ -46,6 +48,15 @@ class EjaculationController extends Controller 'is_private' => $request->has('is_private') ?? false ]); + $tags = explode(' ', $inputs['tags']); + $tagIds = []; + foreach ($tags as $tag) { + $tag = Tag::firstOrCreate(['name' => $tag]); + $tagIds[] = $tag->id; + } + + $ejaculation->tags()->sync($tagIds); + return redirect()->route('checkin.show', ['id' => $ejaculation->id])->with('status', 'チェックインしました!'); } @@ -115,6 +126,7 @@ class EjaculationController extends Controller { $ejaculation = Ejaculation::findOrFail($id); $user = User::findOrFail($ejaculation->user_id); + $ejaculation->tags()->detach(); $ejaculation->delete(); return redirect()->route('user.profile', ['name' => $user->name])->with('status', '削除しました。'); } diff --git a/app/Tag.php b/app/Tag.php new file mode 100644 index 0000000..7d4cb9a --- /dev/null +++ b/app/Tag.php @@ -0,0 +1,19 @@ +belongsToMany('App\Ejaculation')->withTimestamps(); + } +} diff --git a/database/migrations/2018_01_08_075844_create_tags_table.php b/database/migrations/2018_01_08_075844_create_tags_table.php new file mode 100644 index 0000000..3693b75 --- /dev/null +++ b/database/migrations/2018_01_08_075844_create_tags_table.php @@ -0,0 +1,40 @@ +increments('id'); + $table->string('name'); + $table->timestamps(); + }); + + Schema::create('ejaculation_tag', function (Blueprint $table) { + $table->increments('id'); + $table->integer('ejaculation_id')->index(); + $table->integer('tag_id')->index(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('tags'); + Schema::dropIfExists('ejaculation_tag'); + } +} diff --git a/resources/views/ejaculation/checkin.blade.php b/resources/views/ejaculation/checkin.blade.php index 06eb09b..81fe4e2 100644 --- a/resources/views/ejaculation/checkin.blade.php +++ b/resources/views/ejaculation/checkin.blade.php @@ -36,7 +36,7 @@
- +
    @@ -112,6 +112,16 @@ .join(' ') ); } + function insertTag(value) { + $('#tags').append('
  • ' + value + ' | x
  • '); + } + var initTags = $('input[name=tags]').val(); + if (initTags.trim() !== '') { + initTags.split(' ').forEach(function (value) { + insertTag(value); + }); + } + $('#tagInput').on('keydown', function (ev) { var $this = $(this); if ($this.val().trim() !== '') { @@ -119,7 +129,7 @@ case 'Tab': case 'Enter': case ' ': - $('#tags').append('
  • ' + $this.val().trim() + ' | x
  • '); + insertTag($this.val().trim()); $this.val(''); updateTags(); ev.preventDefault(); diff --git a/resources/views/ejaculation/show.blade.php b/resources/views/ejaculation/show.blade.php index e50e3c1..56429d0 100644 --- a/resources/views/ejaculation/show.blade.php +++ b/resources/views/ejaculation/show.blade.php @@ -34,15 +34,14 @@ @endif
    - @if ($ejaculation->is_private) {{-- TODO: タグを付けたら、タグが空じゃないかも判定に加える --}} + @if ($ejaculation->is_private || $ejaculation->tags->isNotEmpty())

    @if ($ejaculation->is_private) 非公開 @endif - {{-- - 催眠音声 - 適当なタグ - --}} + @foreach ($ejaculation->tags as $tag) + {{ $tag->name }} + @endforeach

    @endif diff --git a/resources/views/user/profile.blade.php b/resources/views/user/profile.blade.php index ff9402a..1cd5ed4 100644 --- a/resources/views/user/profile.blade.php +++ b/resources/views/user/profile.blade.php @@ -20,15 +20,14 @@ @endif
    - @if ($ejaculation->is_private) {{-- TODO: タグを付けたら、タグが空じゃないかも判定に加える --}} + @if ($ejaculation->is_private || $ejaculation->tags->isNotEmpty())

    @if ($ejaculation->is_private) 非公開 @endif - {{-- - 催眠音声 - 適当なタグ - --}} + @foreach ($ejaculation->tags as $tag) + {{ $tag->name }} + @endforeach

    @endif