diff --git a/app/Http/Controllers/AssignmentController.php b/app/Http/Controllers/AssignmentController.php new file mode 100644 index 0000000..424604d --- /dev/null +++ b/app/Http/Controllers/AssignmentController.php @@ -0,0 +1,50 @@ +validated())){ + return redirect()->route('assignment.index')->with('success', 'Assignment is successfully created'); + } + return redirect()->route('assignment.index')->with('error', 'Failed to create assignment'); + } + + + public function show(Assignment $assignment) + { + return redirect()->route('assignment.index'); + } + + + + public function update(Request $request,Assignment $assignment){ + + if($assigment->update($request->validated())){ + return redirect()->route('assignment.index')->with('success', 'Assignment is successfully updated'); + } + return redirect()->route('assignment.index')->with('error', 'Failed to update assignment'); + } + + + + public function destroy(Assignment $assignment){ + $assignment -> delete(); + + return redirect()->route('assignment.index'); + } +} diff --git a/app/Http/Controllers/CourseController.php b/app/Http/Controllers/CourseController.php new file mode 100644 index 0000000..0a7c5a1 --- /dev/null +++ b/app/Http/Controllers/CourseController.php @@ -0,0 +1,40 @@ +validated())){ + return redirect()->route('course.index')->with('success', 'Course is successfully created'); + } + return redirect()->route('course.index')->with('error', 'Failed to create course'); + } + + + + public function update(Request $request,Course $course) { + if($course->update($request->validated())){ + return redirect()->route('course.index')->with('success', 'Course is successfully updated'); + } + return redirect()->route('course.index')->with('error', 'Failed to update course'); + } + + + public function destroy(Course $course){ + $course -> delete(); + return redirect()->route('course.index'); + } +} diff --git a/app/Http/Controllers/CourseworkController.php b/app/Http/Controllers/CourseworkController.php deleted file mode 100644 index a1ca183..0000000 --- a/app/Http/Controllers/CourseworkController.php +++ /dev/null @@ -1,11 +0,0 @@ - + */ + public function rules() + { + return [ + 'name' => 'required|max:255', + 'due_at' => 'required', + ]; + } + +} diff --git a/app/Http/Requests/CourseRequest.php b/app/Http/Requests/CourseRequest.php new file mode 100644 index 0000000..0e37197 --- /dev/null +++ b/app/Http/Requests/CourseRequest.php @@ -0,0 +1,30 @@ + + */ + public function rules() + { + return [ + 'name' => 'required|max:255', + ]; + } +} diff --git a/app/Models/Assignment.php b/app/Models/Assignment.php index 6af4e9b..b13916b 100644 --- a/app/Models/Assignment.php +++ b/app/Models/Assignment.php @@ -9,6 +9,7 @@ class Assignment extends Model { use HasFactory; protected $fillable = [ + 'user_id', 'course_id', 'name', 'due_at' @@ -18,6 +19,7 @@ class Assignment extends Model { 'due_at' => 'datetime:Y-m-d H:i:s' ]; + public function course() { return $this->belongsTo(Course::class); } diff --git a/app/Models/Course.php b/app/Models/Course.php index e8eaf0d..af36711 100644 --- a/app/Models/Course.php +++ b/app/Models/Course.php @@ -15,4 +15,8 @@ class Course extends Model { public function assignments() { return $this->hasMany(Assignment::class); } + + public function user(){ + return $this->belongsTo(User::class); + } } diff --git a/app/Models/User.php b/app/Models/User.php index 7ef6b8a..34f9446 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -43,4 +43,13 @@ class User extends Authenticatable { 'email_verified_at' => 'datetime', 'is_admin' => 'boolean' ]; + + + public function courses(){ + return $this->hasMany(Course::class); + } + + public function assignments(){ + return $this->hasManyThrough(Course::class,Assignment::class); + } } diff --git a/app/Policies/AssignmentPolicy.php b/app/Policies/AssignmentPolicy.php new file mode 100644 index 0000000..d54416c --- /dev/null +++ b/app/Policies/AssignmentPolicy.php @@ -0,0 +1,85 @@ +is_admin + ? Response::allow() + : Response::deny(); + } + + /** + * Determine whether the user can view the model. + * + * @param \App\Models\User $user + * @param \App\Models\Assignment $assignment + * @return \Illuminate\Auth\Access\Response|bool + */ + public function view(User $user, Course $course, Assignment $assignment) + { + return (($user->courses()->where('course_id', $course->id)->exist()) + &&($user->assignments()->where('assignment_id',$assigment->id)->exist())) + ? Response::allow() + : Response::deny(); + } + + /** + * Determine whether the user can create models. + * + * @param \App\Models\User $user + * @return \Illuminate\Auth\Access\Response|bool + */ + public function create() + { + return true; + } + + /** + * Determine whether the user can update the model. + * + * @param \App\Models\User $user + * @param \App\Models\Assignment $assignment + * @return \Illuminate\Auth\Access\Response|bool + */ + public function update(User $user, Course $course, Assignment $assignment) + { + return (($user->courses()->where('course_id', $course->id)->exist()) + &&($user->assignments()->where('assignment_id',$assigment->id)->exist())) + ? Response::allow() + : Response::deny(); + } + + /** + * Determine whether the user can delete the model. + * + * @param \App\Models\User $user + * @param \App\Models\Assignment $assignment + * @return \Illuminate\Auth\Access\Response|bool + */ + public function delete(User $user, Course $course, Assignment $assignment) + { + return (($user->courses()->where('course_id', $course->id)->exist()) + &&($user->assignments()->where('assignment_id',$assigment->id)->exist())) + ? Response::allow() + : Response::deny(); + } + +} diff --git a/app/Policies/CoursePolicy.php b/app/Policies/CoursePolicy.php new file mode 100644 index 0000000..d853c21 --- /dev/null +++ b/app/Policies/CoursePolicy.php @@ -0,0 +1,81 @@ +is_admin + ? Response::allow() + : Response::deny(); + } + + /** + * Determine whether the user can view the model. + * + * @param \App\Models\User $user + * @param \App\Models\Course $course + * @return \Illuminate\Auth\Access\Response|bool + */ + public function view(User $user, Course $course) + { + return ($user->courses()->where('course_id', $course->id)->exist()) + ? Response::allow() + : Response::deny(); + } + + /** + * Determine whether the user can create models. + * + * @param \App\Models\User $user + * @return \Illuminate\Auth\Access\Response|bool + */ + public function create(User $user) + { + return $user->is_admin + ? Response::allow() + : Response::deny(); + } + + /** + * Determine whether the user can update the model. + * + * @param \App\Models\User $user + * @param \App\Models\Course $course + * @return \Illuminate\Auth\Access\Response|bool + */ + public function update(User $user, Course $course) + { + return ($user->courses()->where('course_id', $course->id)->exist()) + ? Response::allow() + : Response::deny(); + } + + /** + * Determine whether the user can delete the model. + * + * @param \App\Models\User $user + * @param \App\Models\Course $course + * @return \Illuminate\Auth\Access\Response|bool + */ + public function delete(User $user, Course $course) + { + return ($user->courses()->where('course_id', $course->id)->exist()) + ? Response::allow() + : Response::deny(); + } + +} diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 7424418..f7b1ba5 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -1,7 +1,9 @@ registerPolicies(); - // } } diff --git a/package-lock.json b/package-lock.json index f298815..0b147b0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "html", + "name": "ready", "lockfileVersion": 2, "requires": true, "packages": { diff --git a/routes/web.php b/routes/web.php index 6cb6909..0361e45 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,6 +1,7 @@ name('schedule.index'); Route::get('/import', [ImportCourseController::class, 'index'])->name('import.index'); - Route::get('/coursework', [CourseworkController::class, 'index'])->name('coursework.index'); Route::get('/logout', [LoginController::class, 'logout'])->name('logout'); + + Route::resource('/course',CourseController::class); + Route::resource('/assignment',AssignmentController::class); + }); Route::middleware(['auth', 'admin'])->prefix('/admin')->group(function () { @@ -46,4 +50,6 @@ Route::get('/register', [RegisterController::class, 'index'])->name('register.index'); Route::post('/register', [RegisterController::class, 'store'])->name('register.store'); + + });