Skip to content
This repository has been archived by the owner on Apr 12, 2020. It is now read-only.

Entrega Proyecto 2 #17

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 5 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"plugins": [
"@babel/plugin-proposal-class-properties"
]
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class LoginController extends Controller
*
* @var string
*/
protected $redirectTo = '/home';
protected $redirectTo = '/';

/**
* Create a new controller instance.
Expand Down
4 changes: 3 additions & 1 deletion app/Http/Controllers/Auth/RegisterController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Support\Str;

class RegisterController extends Controller
{
Expand All @@ -28,7 +29,7 @@ class RegisterController extends Controller
*
* @var string
*/
protected $redirectTo = '/home';
protected $redirectTo = '/';

/**
* Create a new controller instance.
Expand Down Expand Up @@ -67,6 +68,7 @@ protected function create(array $data)
'name' => $data['name'],
'email' => $data['email'],
'password' => Hash::make($data['password']),
'api_token' => Str::random(60),
]);
}
}
83 changes: 83 additions & 0 deletions app/Http/Controllers/ShirtController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use File;
use Illuminate\Support\Facades\Response;
use App\Shirt;
use App\User;
use App\Http\Resources\Shirt as ShirtResource;

class ShirtController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(User $user)
{
return ShirtResource::collection($user->shirts);
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(User $user)
{
$shirt = $user->addShirt([
"user_id" => $user->id,
"color" => "FFFFFF",
"type" => "tshirt",
"design_name" => "Your design",
"decoration" => null,
]);
return new ShirtResource($shirt);
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$shirt = Shirt::findOrFail($id);
return new ShirtResource($shirt);
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Shirt $shirt, Request $request)
{
$shirt->update([
'design_name' => $request['design_name'],
'color' => $request['color'],
'type' => $request['type'],
'decoration' => $request['decoration'],
]);
return new ShirtResource($shirt);
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Shirt $shirt)
{
$shirt->delete();
return new ShirtResource($shirt);
}
}
75 changes: 75 additions & 0 deletions app/Http/Controllers/StaticImageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\Http\Controllers;

use File;
use Illuminate\Support\Facades\Response;

class StaticImageController extends Controller
{

public function getAllShirts()
{
return Response::json(array(
'tshirt' => $this->getTShirts(),
'longsleeve' => $this->getLongsleeves(),
));
}

protected function getTShirts()
{
$TshirtsFilepath = public_path() . '/img/shirts/tshirt/';
$tshirts = [
'FFFFFF' => base64_encode(File::get($TshirtsFilepath . 'FFFFFF.png')),
'1B998B' => base64_encode(File::get($TshirtsFilepath . '1B998B.png')),
'ED217C' => base64_encode(File::get($TshirtsFilepath . 'ED217C.png')),
'FF9B71' => base64_encode(File::get($TshirtsFilepath . 'FF9B71.png')),
'55DDFF' => base64_encode(File::get($TshirtsFilepath . '55DDFF.png')),
];
return $tshirts;
}

protected function getLongsleeves()
{
$LongsleeveFilepath = public_path() . '/img/shirts/longsleeve/';
$longsleeve = array(
'FFFFFF' => base64_encode(File::get($LongsleeveFilepath . 'FFFFFF.png')),
'1B998B' => base64_encode(File::get($LongsleeveFilepath . '1B998B.png')),
'ED217C' => base64_encode(File::get($LongsleeveFilepath . 'ED217C.png')),
'FF9B71' => base64_encode(File::get($LongsleeveFilepath . 'FF9B71.png')),
'55DDFF' => base64_encode(File::get($LongsleeveFilepath . '55DDFF.png')),
);
return $longsleeve;
}

public function getShirtImage($type, $color)
{
$filepath = public_path() . '/img/shirts/' . $type . '/' . $color . '.png';
if (file_exists($filepath)) {
return Response::json(array(
'content' => base64_encode(File::get($filepath)),
));
}
}

public function getAllDecorations()
{
$filepath = public_path() . '/img/decorations';
$count = sizeof(File::files($filepath));
$decorations = [];
for ($i = 0; $i < $count; $i++) {
array_push($decorations, ['id' => $i, 'content' => base64_encode(File::get($filepath . '/' . $i . '.png'))]);
}
return Response::json($decorations);
}

public function getDecorationImage($id)
{
$filepath = public_path() . '/img/decorations/' . $id . '.png';
if (file_exists($filepath)) {
return Response::json(array(
'decoration' => base64_encode(File::get($filepath)),
));
}
}
}
14 changes: 14 additions & 0 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Resources\User as UserResource;

class UserController extends Controller
{
public function getLoggedInUser(Request $request)
{
return new UserResource($request->user());
}
}
2 changes: 1 addition & 1 deletion app/Http/Middleware/RedirectIfAuthenticated.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class RedirectIfAuthenticated
public function handle($request, Closure $next, $guard = null)
{
if (Auth::guard($guard)->check()) {
return redirect('/home');
return redirect('/');
}

return $next($request);
Expand Down
26 changes: 26 additions & 0 deletions app/Http/Resources/Shirt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class Shirt extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'color' => $this->color,
'type' => $this->type,
'design_name' => $this->design_name,
'user_id' => $this->user_id,
'decoration' => $this->decoration,
];
}
}
25 changes: 25 additions & 0 deletions app/Http/Resources/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class User extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'api_token' => $this->api_token,
'isLoggedIn' => true,
];
}
}
2 changes: 2 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Providers;

use Illuminate\Http\Resources\Json\Resource;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

Expand All @@ -24,6 +25,7 @@ public function register()
*/
public function boot()
{
Resource::withoutWrapping();
Schema::defaultStringLength(191);
}
}
22 changes: 22 additions & 0 deletions app/Shirt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Shirt extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'color', 'type', 'user_id', 'design_name', 'decoration'
];

public function user()
{
return $this->belongsTo(User::class);
}
}
12 changes: 11 additions & 1 deletion app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class User extends Authenticatable
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
'name', 'email', 'password', 'api_token'
];

/**
Expand All @@ -36,4 +36,14 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];

public function shirts()
{
return $this->hasMany(Shirt::class);
}

public function addShirt($Shirt)
{
return $this->shirts()->create($Shirt);
}
}
4 changes: 2 additions & 2 deletions config/filesystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@

'public' => [
'driver' => 'local',
'root' => storage_path('app/public'),
'url' => env('APP_URL').'/storage',
'root' => public_path(), //storage_path('app/public'),
'url' => env('APP_URL') . '/img',
'visibility' => 'public',
],

Expand Down
13 changes: 13 additions & 0 deletions database/factories/ShirtFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

/* @var $factory \Illuminate\Database\Eloquent\Factory */

use App\Shirt;
use Faker\Generator as Faker;

$factory->define(Shirt::class, function (Faker $faker) {
return [
'user_id' => rand(1,4),
'design_name' => Str::random(10),
];
});
Loading