-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from IT-Academy-BCN/feature/student-crud
Feat(Routes file & Student CRUD's first mehods)
- Loading branch information
Showing
13 changed files
with
312 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\api; | ||
|
||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\StoreStudentRequest; | ||
use App\Http\Resources\StudentCreateResource; | ||
use App\Http\Resources\StudentListResource; | ||
use App\Models\Student; | ||
use App\Models\User; | ||
use Illuminate\Http\Exceptions\HttpResponseException; | ||
use Illuminate\Support\Facades\DB; | ||
|
||
class StudentController extends Controller | ||
{ | ||
public function index() | ||
{ | ||
|
||
$studentsList = Student::all(); | ||
|
||
if (! $studentsList) { | ||
throw new HttpResponseException(response()->json(['message' => 'Something went wrong. Please try again.'], 404)); | ||
} | ||
|
||
return response()->json([ | ||
'data' => StudentListResource::collection($studentsList), | ||
'status' => 200, | ||
]); | ||
|
||
} | ||
|
||
public function store(StoreStudentRequest $request) | ||
{ | ||
|
||
$user = DB::transaction(function () use ($request) { | ||
|
||
$user = User::create([ | ||
'name' => $request->name, | ||
'surname' => $request->surname, | ||
'dni' => $request->dni, | ||
'email' => $request->email, | ||
'password' => bcrypt($request->password), | ||
'active' => true, | ||
]); | ||
|
||
$user->student()->create([ | ||
'subtitle' => $request->subtitle, | ||
'bootcamp' => $request->bootcamp, | ||
]); | ||
|
||
return $user; | ||
|
||
}); | ||
|
||
if (! $user) { | ||
throw new HttpResponseException(response()->json(['message' => __('Registre no efectuat. Si-us-plau, torna-ho a provar.')], 404)); | ||
} | ||
|
||
$student = $user->student; | ||
|
||
return response()->json([ | ||
'data' => StudentCreateResource::make($student), | ||
'status' => 201, | ||
]); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
class StoreStudentRequest extends UserRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
$array = [ | ||
'subtitle' => 'required', | ||
'bootcamp' => 'required|in:Front end Developer,PHP Developer,Java Developer,Nodejs Developer', | ||
]; | ||
|
||
return array_merge(parent::rules(), $array); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use Illuminate\Contracts\Validation\Validator; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Http\Exceptions\HttpResponseException; | ||
|
||
abstract class UserRequest extends FormRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string> | ||
*/ | ||
public function rules(): array | ||
{ | ||
|
||
return [ | ||
'name' => 'required|string', | ||
'surname' => 'required|string', | ||
'dni' => 'required|string|unique:users', | ||
'email' => 'required|string|email|max:255|unique:users', | ||
'password' => 'required|string|min:8', | ||
]; | ||
} | ||
|
||
/** | ||
* If validator fails returns the exception in json form | ||
* | ||
* @return array | ||
*/ | ||
protected function failedValidation(Validator $validator) | ||
{ | ||
|
||
throw new HttpResponseException(response()->json(['errors' => $validator->errors()], 422)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class StudentCreateResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return [ | ||
'name' => $this->user->name, | ||
'surname' => $this->user->surname, | ||
'subtitle' => $this->subtitle, | ||
'bootcamp' => $this->bootcamp, | ||
//endDate | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
namespace App\Http\Resources; | ||
|
||
use Illuminate\Http\Request; | ||
use Illuminate\Http\Resources\Json\JsonResource; | ||
|
||
class StudentListResource extends JsonResource | ||
{ | ||
/** | ||
* Transform the resource into an array. | ||
* | ||
* @return array<string, mixed> | ||
*/ | ||
public function toArray(Request $request): array | ||
{ | ||
return [ | ||
'name' => $this->user->name, | ||
'surname' => $this->user->surname, | ||
'subtitle' => $this->subtitle, | ||
'about ' => $this->about, | ||
'cv' => $this->cv, | ||
'bootcamp' => $this->bootcamp, | ||
'endDate' => $this->endDate, | ||
'linkedin' => $this->linkedin, | ||
'github ' => $this->github, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Student extends Model | ||
{ | ||
use HasFactory; | ||
|
||
/** | ||
* The attributes that are mass assignable. | ||
* | ||
* @var array<int, string> | ||
*/ | ||
protected $fillable = [ | ||
'subtitle', | ||
'about', | ||
'cv', | ||
'bootcamp', | ||
'endDate', | ||
'linkedin', | ||
'github', | ||
]; | ||
|
||
public function user() | ||
{ | ||
return $this->belongsTo(User::class); | ||
} | ||
|
||
/*public function tags() { | ||
$this->hasMany(Tag::class); | ||
} | ||
public function projects(){ | ||
$this->hasMany(Project::class); | ||
}*/ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.