Skip to content

Commit

Permalink
#10 Save volunteer
Browse files Browse the repository at this point in the history
  • Loading branch information
Jovana Marsilio committed Dec 3, 2020
1 parent 55ab955 commit b15808e
Show file tree
Hide file tree
Showing 16 changed files with 663 additions and 79 deletions.
29 changes: 21 additions & 8 deletions app/Http/Controllers/VolunteerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,63 @@

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\VolunteerRequest as Request;
use App\Repositories\ExpertiseAreaRepository;
use App\Repositories\WeekDayRepository;
use App\Repositories\CityRepository;
use App\Services\VolunteerService;

class VolunteerController extends Controller
{
public function __construct(
ExpertiseAreaRepository $expertiseAreaRepository,
WeekDayRepository $weekDayRepository,
CityRepository $cityRepository
CityRepository $cityRepository,
VolunteerService $volunteerService
) {
$this->expertiseAreaRepository = $expertiseAreaRepository;
$this->weekDayRepository = $weekDayRepository;
$this->cityRepository = $cityRepository;
$this->volunteerService = $volunteerService;
}

/**
* Show the form for creating a new resource.
* Show the form for creating a new volunteer.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
// Resgata os valores para popular o formulário.
$availableTimes = collect([1,2,3,4]);
$availableTimes = collect([
1 => 1,
2 => 2,
3 => 3,
4 => 4
]);
$weekDays = $this->weekDayRepository->allActive();
$cities = $this->cityRepository->allActive();
$expertiseAreas = $this->expertiseAreaRepository->allActive();

return view(
'website.volunteer.create',
compact('weekDays', 'availableTimes', 'cities', 'expertiseAreas')
compact('availableTimes', 'weekDays', 'cities', 'expertiseAreas')
);
}

/**
* Store a newly created resource in storage.
* Store a newly created volunteer.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
$response = $this->volunteerService->save($request);

if (!empty($response->volunteer)) {
return redirect()
->route('volunteer.create')
->with('volunteer_created', 'Dados salvos com sucesso!');
}
}
}
57 changes: 57 additions & 0 deletions app/Http/Requests/VolunteerRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Carbon\Carbon;

class VolunteerRequest 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
*/
public function rules()
{
$rules = [
'volunteer.name' => 'required|string|max:255',
'volunteer.birth_date' => 'required|string|size:10|date_format:d/m/Y',
'volunteer.cpf' => 'required|string|size:14',
'volunteer.cell_phone' => 'required|string|size:15',
'volunteer.email' => 'nullable|email',
'volunteer.responsible_name' => 'nullable|string|max:255',
'volunteer.responsible_cpf' => 'nullable|string|size:14',
'volunteer.occupation' => 'required|string|max:150',
'volunteer.city_id' => 'required|numeric',
'volunteer.available_time' => 'required|numeric',
'volunteer_week_day' => 'required|array',
'volunteer.expertise_area_id' => 'required|numeric',
'volunteer.confirmation' => 'required|numeric',
];

if (request()->input('volunteer.birth_date')) {
// Rescues the volunteer's date of birth.
$volunteerBirthDate = Carbon::createFromFormat('d/m/Y', request()->input('volunteer.birth_date'));

// Is under 18 years old?
if ($volunteerBirthDate->diffInYears(now()) < 18) {
// Responsible is required.
$rules['volunteer.responsible_name'] = 'required|string|max:255';
$rules['volunteer.responsible_cpf'] = 'required|string|size:14';
}
}

return $rules;
}
}
8 changes: 8 additions & 0 deletions app/Models/City.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ protected static function booted()
});
}

/**
* Volunteers.
*/
public function volunteers()
{
return $this->hasMany('App\Models\Volunteer', 'city_id', 'id');
}

/**
* Scope a query to only include active cities.
*
Expand Down
8 changes: 8 additions & 0 deletions app/Models/ExpertiseArea.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ protected static function booted()
});
}

/**
* Volunteers.
*/
public function volunteers()
{
return $this->hasMany('App\Models\Volunteer', 'expertise_area_id', 'id');
}

/**
* Scope a query to only include active areas.
*
Expand Down
74 changes: 74 additions & 0 deletions app/Models/Volunteer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class Volunteer extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'city_id',
'expertise_area_id',
'name',
'birth_date',
'cpf',
'cell_phone',
'email',
'responsible_name',
'responsible_cpf',
'occupation',
'available_time',
'ip',
'user_agent',
];

/**
* The attributes that should be cast.
*
* @var array
*/
// protected $casts = [
// 'birth_date' => 'datetime:Y-m-d',
// ];

/**
* Volunteer week days.
*/
public function volunteerWeekDays()
{
return $this->hasMany('App\Models\VolunteerWeekDay', 'volunteer_id', 'id');
}

/**
* City.
*/
public function city()
{
return $this->belongsTo('App\Models\City', 'city_id', 'id');
}

/**
* Expertise area.
*/
public function expertiseArea()
{
return $this->belongsTo('App\Models\ExpertiseArea', 'expertise_area_id', 'id');
}

/**
* Set the volunteer's birth date.
*
* @param string $birthDate
* @return void
*/
public function setBirthDateAttribute($birthDate)
{
$this->attributes['birth_date'] = Carbon::createFromFormat('d/m/Y', $birthDate)->format('Y-m-d');
}
}
34 changes: 34 additions & 0 deletions app/Models/VolunteerWeekDay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class VolunteerWeekDay extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'volunteer_id',
'week_day_id',
];

/**
* Volunteer.
*/
public function volunteer()
{
return $this->belongsTo('App\Models\Volunteer', 'volunteer_id', 'id');
}

/**
* Week day.
*/
public function weekDay()
{
return $this->belongsTo('App\Models\WeekDay', 'week_day_id', 'id');
}
}
8 changes: 8 additions & 0 deletions app/Models/WeekDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,14 @@ protected static function booted()
});
}

/**
* Volunteer week days.
*/
public function volunteerWeekDays()
{
return $this->hasMany('App\Models\VolunteerWeekDay', 'week_day_id', 'id');
}

/**
* Scope a query to only include active week days.
*
Expand Down
14 changes: 14 additions & 0 deletions app/Repositories/VolunteerRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Repositories;

use App\Repositories\Repository;
use App\Models\Volunteer;

class VolunteerRepository extends Repository
{
public function __construct(Volunteer $model)
{
$this->model = $model;
}
}
14 changes: 14 additions & 0 deletions app/Repositories/VolunteerWeekDayRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Repositories;

use App\Repositories\Repository;
use App\Models\VolunteerWeekDay;

class VolunteerWeekDayRepository extends Repository
{
public function __construct(VolunteerWeekDay $model)
{
$this->model = $model;
}
}
34 changes: 34 additions & 0 deletions app/Services/Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace App\Services;

abstract class Service
{
/**
* Construtor da classe.
*/
public function __construct()
{
//
}

/**
* Resposta padrão do serviço.
*
* @param array $data
* @return Object
*/
public function response($data = [])
{
// Por default o error é sempre vazio.
$default = ['error' => null];

// Concatena com os dados.
$data = array_merge($default, $data);

// Converte para objeto.
$data = (object) $data;

return $data;
}
}
Loading

0 comments on commit b15808e

Please sign in to comment.