Skip to content

Commit

Permalink
Merge pull request #25 from IT-Academy-BCN/feature/student-crud
Browse files Browse the repository at this point in the history
Feat(Routes file & Student CRUD's first mehods)
  • Loading branch information
CloudSalander authored Nov 2, 2023
2 parents 912ea26 + 5e6731a commit d19a68b
Show file tree
Hide file tree
Showing 13 changed files with 312 additions and 30 deletions.
Binary file added .rnd
Binary file not shown.
67 changes: 67 additions & 0 deletions app/Http/Controllers/api/StudentController.php
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,
]);

}
}
29 changes: 29 additions & 0 deletions app/Http/Requests/StoreStudentRequest.php
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);
}
}
46 changes: 46 additions & 0 deletions app/Http/Requests/UserRequest.php
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));
}
}
25 changes: 25 additions & 0 deletions app/Http/Resources/StudentCreateResource.php
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
];
}
}
29 changes: 29 additions & 0 deletions app/Http/Resources/StudentListResource.php
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,
];
}
}
41 changes: 41 additions & 0 deletions app/Models/Student.php
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);
}*/

}
7 changes: 7 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class User extends Authenticatable
*/
protected $fillable = [
'name',
'surname',
'dni',
'email',
'password',
];
Expand All @@ -43,4 +45,9 @@ class User extends Authenticatable
'email_verified_at' => 'datetime',
'password' => 'hashed',
];

public function student()
{
return $this->hasOne(Student::class);
}
}
4 changes: 2 additions & 2 deletions app/Providers/RouteServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public function boot(): void

$this->routes(function () {
Route::middleware('api')
->prefix('api')
->group(base_path('routes/api.php'));
->prefix('api/v1')
->group(base_path('routes/api/v1.php'));

Route::middleware('web')
->group(base_path('routes/web.php'));
Expand Down
Loading

0 comments on commit d19a68b

Please sign in to comment.