feat: worry box feature implemented
This commit is contained in:
@@ -13,11 +13,12 @@ public function storeUserMood(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'mood_id' => 'required|exists:moods,id',
|
||||
'note' => 'nullable|string'
|
||||
]);
|
||||
|
||||
$user = auth()->user();
|
||||
|
||||
$mood = UserMood::updateOrCreate(
|
||||
$moodEntry = UserMood::updateOrCreate(
|
||||
[
|
||||
'user_id' => $user->id,
|
||||
'date' => now()->toDateString(),
|
||||
@@ -26,13 +27,20 @@ public function storeUserMood(Request $request)
|
||||
'mood_id' => $data['mood_id'],
|
||||
]
|
||||
);
|
||||
// If a note is provided, create or update it
|
||||
if (!empty($data['note'])) {
|
||||
$moodEntry->notes()->updateOrCreate(
|
||||
['user_id' => $user->id],
|
||||
['content' => $data['note']]
|
||||
);
|
||||
}
|
||||
|
||||
$user->increment('xp', 10);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Mood saved successfully',
|
||||
'xp' => $user->xp,
|
||||
'mood' => $mood->load('mood')
|
||||
'mood' => $moodEntry->load(['mood', 'notes'])
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -42,7 +50,7 @@ public function getUserMoods()
|
||||
$user = auth()->user();
|
||||
|
||||
$moods = UserMood::where('user_id', $user->id)
|
||||
->with('mood')
|
||||
->with(['mood', 'notes'])
|
||||
->orderBy('date', 'desc')
|
||||
->get();
|
||||
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
use App\Models\Worry;
|
||||
use App\Models\Note;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class WorryController extends Controller
|
||||
{
|
||||
public function store(Request $request)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'title' => 'required|string|max:255',
|
||||
'note' => 'nullable|string'
|
||||
]);
|
||||
|
||||
$worry = Worry::create([
|
||||
'user_id' => auth()->id(),
|
||||
'title' => $data['title'],
|
||||
]);
|
||||
|
||||
if (!empty($data['note'])) {
|
||||
$worry->notes()->create([
|
||||
'user_id' => auth()->id(),
|
||||
'content' => $data['note']
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json($worry->load('notes'));
|
||||
}
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$data = $request->validate([
|
||||
'title' => 'nullable|string|max:255',
|
||||
'note' => 'nullable|string'
|
||||
]);
|
||||
|
||||
$worry = Worry::where('user_id', auth()->id())->findOrFail($id);
|
||||
|
||||
if (!empty($data['title'])) {
|
||||
$worry->update(['title' => $data['title']]);
|
||||
}
|
||||
|
||||
if (isset($data['note'])) {
|
||||
$note = $worry->notes()->where('user_id', auth()->id())->first();
|
||||
|
||||
if ($note) {
|
||||
$note->update(['content' => $data['note']]);
|
||||
} else {
|
||||
$worry->notes()->create([
|
||||
'user_id' => auth()->id(),
|
||||
'content' => $data['note']
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
return response()->json($worry->load('notes'));
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$worries = Worry::where('user_id', auth()->id())
|
||||
->with('notes')
|
||||
->orderBy('created_at', 'desc')
|
||||
->get();
|
||||
|
||||
return response()->json($worries);
|
||||
}
|
||||
|
||||
public function toggleSolved($id)
|
||||
{
|
||||
$worry = Worry::where('user_id', auth()->id())->findOrFail($id);
|
||||
$worry->update(['is_solved' => !$worry->is_solved]);
|
||||
|
||||
return response()->json($worry);
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$worry = Worry::where('user_id', auth()->id())->findOrFail($id);
|
||||
$worry->delete();
|
||||
|
||||
return response()->json(['message' => 'Worry deleted successfully']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Note extends Model
|
||||
{
|
||||
protected $fillable = ['user_id', 'content', 'noteable_id', 'noteable_type'];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function noteable()
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
}
|
||||
@@ -16,4 +16,8 @@ public function mood()
|
||||
{
|
||||
return $this->belongsTo(Mood::class);
|
||||
}
|
||||
public function notes()
|
||||
{
|
||||
return $this->morphMany(Note::class, 'noteable');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Worry extends Model
|
||||
{
|
||||
protected $fillable = ['user_id', 'title', 'is_solved'];
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function notes()
|
||||
{
|
||||
return $this->morphMany(Note::class, 'noteable');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('notes', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('user_id')->constrained()->onDelete('cascade');
|
||||
$table->text('content');
|
||||
$table->nullableMorphs('noteable'); // noteable_id & noteable_type
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('notes');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('worries', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('user_id')->constrained()->onDelete('cascade');
|
||||
$table->string('title');
|
||||
$table->boolean('is_solved')->default(false);
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('worries');
|
||||
}
|
||||
};
|
||||
@@ -10,7 +10,7 @@ class DatabaseSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$this->call(MoodSeeder::class);
|
||||
$this->call(BreathingTemplateSeeder::class);
|
||||
}
|
||||
/**
|
||||
* Seed the application's database.
|
||||
|
||||
+11
-4
@@ -8,6 +8,7 @@
|
||||
use App\Http\Controllers\ProductController;
|
||||
use App\Http\Controllers\BreathingTemplate;
|
||||
use App\Http\Controllers\MoodController;
|
||||
use App\Http\Controllers\WorryController;
|
||||
|
||||
Route::get('package-names/{name}/products', [ProductController::class, 'index']);
|
||||
Route::prefix('auth')->controller(UserController::class)->group(function () {
|
||||
@@ -36,10 +37,6 @@
|
||||
Route::middleware('abilities:admin')->patch('/{name}/products/{id}', [ProductController::class, 'update']);
|
||||
Route::middleware('abilities:admin')->delete('/{name}/products/{id}', [ProductController::class, 'delete']);
|
||||
Route::put('/{name}/products/{id}/subscribe', [ProductController::class, 'subscribe']);
|
||||
|
||||
|
||||
|
||||
|
||||
});
|
||||
// --------- user profile
|
||||
Route::post('/profile', [UserController::class, 'updateProfile']);
|
||||
@@ -66,4 +63,14 @@
|
||||
Route::put('breathing-templates/{id}', [BreathingExerciseController::class, 'updateTemplate']);
|
||||
Route::delete('breathing-templates/{id}', [BreathingExerciseController::class, 'deleteTemplate']);
|
||||
Route::get('user-templates', [BreathingExerciseController::class, 'getUserTemplates']);
|
||||
|
||||
/// worry box feature
|
||||
Route::prefix('worries')->controller(WorryController::class)->group(function () {
|
||||
Route::post('/', 'store'); // Create worry + note
|
||||
Route::get('/', 'index'); // Get all worries
|
||||
Route::put('/{id}', 'update'); // Edit worry & note
|
||||
Route::patch('/{id}/toggle', 'toggleSolved'); // Mark solved / unsolved
|
||||
Route::delete('/{id}', 'destroy'); // Delete worry
|
||||
});
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user