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

Controller routes #89

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open

Controller routes #89

wants to merge 31 commits into from

Conversation

Kelisnor
Copy link
Collaborator

@Kelisnor Kelisnor commented Aug 5, 2022

//There's no specific page for course and assignment, so I use AdminPage instead

Kelisnor added 27 commits June 17, 2022 14:44
Comment on lines 20 to 25
public function create(User $user): RedirectResponse {
if ($user->is_admin) {
return redirect()->route('assignment.index')->with('success', 'Assignment created');
}
return redirect()->route('assignment.index')->with('error', 'User is not an admin');
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't make an assignment which it should. Also any user should be able to make an assignment for their class rather than just admins. Your policies should control whether a user can access or not.

Comment on lines 14 to 18
public function index()
{
return inertia("AdminPage");
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just leave this function empty for now rather than render a page that is unrelated.

Comment on lines 39 to 52
public function update(User $user): RedirectResponse {
if ($user->is_admin) {
return redirect()->route('assignment.index')->with('success', 'Assignment updated');
}
return redirect()->route('assignment.index')->with('error', 'User is not an admin');
}


public function destroy(User $user): RedirectResponse {
if ($user->is_admin) {
return redirect()->route('assignment.index')->with('success', 'Assignment deleted');
}
return redirect()->route('assignment.index')->with('error', 'User is not an admin');
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These don't make any database changes and shouldn't check for admin.

Comment on lines 28 to 35
public function findAssignmentByCourse($course_id)
{
$assignments_list = Assignment::where('course_id', $course_id)->get();
foreach ($assignment as $assignments_list) {
$assignment->name=$assignments_list->assignment->name;
}
return response()->json(['data'=>$assignment]);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't seem to be used?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed since still not used

Comment on lines 15 to 17
public function index() {
return inertia("AdminPage");
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be left empty.

Comment on lines 71 to 90
public function update(User $user, Assignment $assignment)
{
return $use->is_admin
? 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, Assignment $assignment)
{
return $use->is_admin
? Response::allow()
: Response::deny();
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should check if the user owns the assignment instead.

Comment on lines 31 to 75
public function view(User $user, Course $course)
{
return true;
}

/**
* Determine whether the user can create models.
*
* @param \App\Models\User $user
* @return \Illuminate\Auth\Access\Response|bool
*/
public function create(User $user)
{
return $use->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 $use->is_admin
? 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 $use->is_admin
? Response::allow()
: Response::deny();
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should check if user owns the course

Comment on lines 18 to 19
Assignment::class => AssignmentPolicy::class,
Course::class => CoursePolicy::class,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Laravel should automatically register these. Was it not doing so?

Comment on lines 30 to 35
Gate::define('update-post', function (User $user,
Course $course, Assignment $assignment) {
//?check if user is an admin
return ($assignment->course_id === $course->id);
});

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this gate for?

routes/web.php Outdated
Comment on lines 41 to 42
Route::apiResource('users', CourseController::class)->only(['create','update', 'destroy']);
Route::apiResource('users', AssignmnetController::class)->only(['create','update', 'destroy']);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The names should change and these shouldn't be in the admin middleware

Copy link
Collaborator

@cjreed121 cjreed121 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some review comments. Also if you can add some tests for these that would be great.

Comment on lines 20 to 23
public function create(){
return Redirect::to('assignment.index');
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should actually create the assignment here not just redirect.

return response()->json(['data'=>$assignment]);
}

public function findAssignmentByUser($user_id)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not used so can be removed

Comment on lines 28 to 35
public function findAssignmentByCourse($course_id)
{
$assignments_list = Assignment::where('course_id', $course_id)->get();
foreach ($assignment as $assignments_list) {
$assignment->name=$assignments_list->assignment->name;
}
return response()->json(['data'=>$assignment]);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be removed since still not used

Comment on lines 44 to 49
public function show($id)
{
$assignment = Assignment::find($id);

//return View::make('')->with('assignment', $assignment);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is properly type hinted with an Assignment model then Laravel will find the model and you can just handle returning an inertia view with that model.

Comment on lines 53 to 58
public function update($id){
$assignment = Assignment::find($id);
$assignment->name = Input::get('name');
$assignment->due_at = Input::get('due_at');

return Redirect::to('assignment.index');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should really have validation here. You can make a request class to separate this logic as well.

Comment on lines 22 to 24
public function user(){
return $this->belongsTo(User::class);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This likely won't be too useful so it can be removed

Comment on lines 52 to 54
public function assignments(){
return $this->hasMany(Assignment::class);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be hasManyThrough with the Assignment and Course passed in

@@ -14,6 +14,7 @@ public function up() {
Schema::create('courses', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->foreignId('user_id')->constrained()->onDelete('cascade');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to edit existing migrations

@@ -13,6 +13,7 @@
public function up() {
Schema::create('assignments', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->onDelete('cascade');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't want to edit existing migrations

routes/web.php Outdated
Comment on lines 33 to 34
Route::get('/course',[CourseController::class,'index'])->name('course.index');
Route::get('/assignment',[AssignmentController::class,'index'])->name('assignment.index');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should be resource instead of get so that all of the routes become available

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants