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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6f2c1b5
add simple models and migrations for course and assignment
Jun 17, 2022
8cf4beb
Merge branch 'master' of https://github.com/ready-app/ready
Jun 17, 2022
58109d3
Merge branch 'master' of https://github.com/ready-app/ready
Jun 21, 2022
c07c74c
add more things for models and migrations for course and assignment
Jun 21, 2022
545b382
fix a casting problem
Jun 21, 2022
0c65323
Merge branch 'master' of https://github.com/ready-app/ready
Jun 21, 2022
48a58bc
using = true to attempt generating created_at and update_at
Jun 24, 2022
0b4cdd3
Merge branch 'master' of https://github.com/ready-app/ready
Jun 24, 2022
1a1e1c1
delete manully creating of 'created_at' attribute for Assignment migr…
Jun 24, 2022
17c7944
adding relationships between Assignment and Course class, setting up …
Jun 24, 2022
0608652
delete unnecessary attribute in Assignment model
Jun 28, 2022
3ba87e5
Merge branch 'master' of https://github.com/ready-app/ready
Jun 28, 2022
91c0c8f
Merge branch 'AssignmentModelAndMigration'
Jun 28, 2022
b7d5f04
foreign key changes
Jun 28, 2022
90ebb8c
basic controller for Course
Jul 15, 2022
71c660b
adding some policies and gates
Jul 15, 2022
01bdcba
single change to routes
Jul 22, 2022
e73eccb
add assignment controller
Jul 26, 2022
32d1648
Merge branch 'master' into ControllerRoutes
Jul 29, 2022
cee750c
rewirte the update and destroy function so that it could only be chag…
Jul 29, 2022
c021397
delete the original CourseWork Controller
Jul 29, 2022
b89da19
minor changes to controllers
Aug 2, 2022
a56539b
delete unused functions
Aug 2, 2022
4e05200
rewrite functions with is_admin attribute
Aug 2, 2022
e3d8823
minor changes
Aug 2, 2022
e80233b
changes in policies
Aug 2, 2022
b94a8df
minor changes to delete unused functions
Aug 5, 2022
5028d01
change to the relations between Models
Aug 12, 2022
1307f0a
add validations
Aug 14, 2022
2203483
change to resource controller
Aug 14, 2022
c47f35d
minor changes
Aug 14, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions app/Http/Controllers/AssignmentController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class AssignmentController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
//
}


public function create(Request $request){
if(Assignment::create($request->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');
}
}
40 changes: 40 additions & 0 deletions app/Http/Controllers/CourseController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CourseController extends Controller
{

/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
Copy link
Collaborator

Choose a reason for hiding this comment

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

All the comments I gave to AssignmentController apply here too

//
}

public function create(Request $request) {
if(Course::create($request->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');
}
}
11 changes: 0 additions & 11 deletions app/Http/Controllers/CourseworkController.php

This file was deleted.

32 changes: 32 additions & 0 deletions app/Http/Requests/AssignmentRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class AssignmentRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'name' => 'required|max:255',
'due_at' => 'required',
];
}

}
30 changes: 30 additions & 0 deletions app/Http/Requests/CourseRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CourseRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
'name' => 'required|max:255',
];
}
}
2 changes: 2 additions & 0 deletions app/Models/Assignment.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Assignment extends Model {
use HasFactory;

protected $fillable = [
'user_id',
'course_id',
'name',
'due_at'
Expand All @@ -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);
}
Expand Down
4 changes: 4 additions & 0 deletions app/Models/Course.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,8 @@ class Course extends Model {
public function assignments() {
return $this->hasMany(Assignment::class);
}

public function user(){
return $this->belongsTo(User::class);
}
}
9 changes: 9 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
85 changes: 85 additions & 0 deletions app/Policies/AssignmentPolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

namespace App\Policies;

use App\Models\Assignment;
use App\Models\User;
use App\Models\Course;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Auth\Access\Response;

class AssignmentPolicy
{
use HandlesAuthorization;


/**
* Determine whether the user can view any models.
*
* @param \App\Models\User $user
* @return \Illuminate\Auth\Access\Response|bool
*/
public function viewAny(User $user)
{
return $user->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();
}

}
81 changes: 81 additions & 0 deletions app/Policies/CoursePolicy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace App\Policies;

use App\Models\Course;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;

class CoursePolicy
{
use HandlesAuthorization;

/**
* Determine whether the user can view any models.
*
* @param \App\Models\User $user
* @return \Illuminate\Auth\Access\Response|bool
*/
public function viewAny(User $user)
{
return $user->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();
}

}
5 changes: 3 additions & 2 deletions app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

namespace App\Providers;

use App\Models\User;
use App\Models\Assignment;
use App\Models\Course;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;

Expand All @@ -23,6 +25,5 @@ class AuthServiceProvider extends ServiceProvider {
public function boot() {
$this->registerPolicies();

//
}
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading