-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadingProgressController.php
More file actions
124 lines (103 loc) · 3.59 KB
/
Copy pathReadingProgressController.php
File metadata and controls
124 lines (103 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace App\Http\Controllers;
use App\Models\Book;
use App\Models\ReadingProgress;
use App\Models\ReadingStreak;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class ReadingProgressController extends Controller
{
public function getReadingProgress(Request $request)
{
$user = $request->user();
$readingProgress = $user->readingProgress()
->with('book')
->get();
return response()->json([
'reading_progress' => $readingProgress
]);
}
public function readToday(Request $request)
{
$user = $request->user();
$streak = $user->readingStreak()->latest()->first();
// If there is no existing streak, create a new one
if (!$streak) {
$streak = new ReadingStreak([
'last_reading_day' => Carbon::now()->toDateString(),
'streak' => 1,
'longest_streak' => 1
]);
$user->readingStreak()->save($streak);
return response()->json([
'message' => 'New streak created!',
'streak' => $streak->streak
]);
}
// Check if the user has already read today
$today = Carbon::now()->toDateString();
if ($streak->last_reading_day === $today) {
return response()->json([
'message' => 'You have already read today!'
]);
}
// Check if the last reading day was yesterday
$yesterday = Carbon::yesterday()->toDateString();
if ($streak->last_reading_day === $yesterday) {
$streak->increment('streak');
} else {
$streak->streak = 1;
}
// Update the last_reading_day field
$streak->last_reading_day = $today;
$streak->save();
// Update the streak field in the user table
$user->update([
'current_reading_streak' => $streak->streak,
'longest_reading_streak' => max($streak->streak, $user->longest_reading_streak)
]);
return response()->json([
'message' => 'Last reading day updated!',
'streak' => $streak->streak
]);
}
public function start_reading(Request $request)
{
$user = $request->user();
$started_at = now();
$bookId = $request->input('book_id');
$book = Book::find($bookId);
$user->books()->syncWithoutDetaching([
$book->id => ['started_reading_at' => $started_at]
]);
$user->increaseReadingRankOnStart($book);
return response()->json([
'message' => 'Started reading book',
'book' => $book->title,
'started_reading_at' => $started_at
]);
}
public function updateFinishedReading(Request $request)
{
$user = $request->user();
$bookId = $request->input('book_id');
$book = Book::find($bookId);
if (!$book) {
return response()->json(['error' => 'Book not found.'], 404);
}
// Update finished_reading_at timestamp
$finishedAt = Carbon::now()->toDateString(); // Get the date instance in YYYY-MM-DD format
$user->books()->syncWithoutDetaching([
$book->id => ['finished_reading_at' => $finishedAt]
]);
// Increase reading rank
$user->increaseReadingRankOnFinish($book);
return response()->json([
'message' => 'Finished reading book',
'book' => $book->title,
'finished_reading_at' => $finishedAt
]);
}
}