美文网首页
Recipe-box 控制器设计

Recipe-box 控制器设计

作者: 三仕贰號 | 来源:发表于2018-01-05 16:50 被阅读0次

创建控制器(创建完成之后生成的文件在 app/Http/Controllers/ 下)

>php artisan make:controller AuthController
>php artisan make:controller RecipeController

添加路由 (路径:routes/api.php)

<?php
Route::post('/register', 'AuthController@register');
Route::post('/login', 'AuthController@login');
Route::post('/logout', 'AuthController@logout');

Route::resource('recipes', 'RecipeController');

修改 AuthController.php 控制器

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use Hash;
class AuthController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:api')->only('logout');
    }
    public function register(Request $request){
        $this->validate($request, [
            'name' => 'required|max:255',
            'email' => 'required|email|unique:users',
            'password' => 'required|between:6,25|confirmed'
        ]);
        $user = new User($request->all());
        $user->password = bcrypt($request->password);
        $user->save();
        return response()->json([
            'registered' => true
        ]);
    }
    public function login(Request $request){
        $this->validate($request, [
            'email' => 'required|email',
            'password' => 'required|between:6,25'
        ]);
        $user = User::where('email', $request->email)->first();
        if($user && Hash::check($request->password, $user->password)){
            $user->api_token = str_random(60);
            $user->save();
            return response()->json([
                'authenticated' => true,
                'api_token' => $user->api_token,
                'user_id' => $user->id
            ]);
        }
        return response()->json([
            'email' => '提供的电子邮件和密码不匹配!'
        ], 422);
    }
    public function logout(Request $request){
        $user = $request->user();
        $user->api_token = null;
        $user->save();
        return response()->json(['logged_out' => true]);
    }
}

修改RecipeController.php 控制器

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Recipe;
use App\RecipeIngredient;
use App\RecipeDirection;
use File;
class RecipeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:api')->except('index', 'show');
    }
    public function index(){
        $recipes = Recipe::orderBy('created_at','desc')->get(['name','image','id']);
        return response()->json(['recipes' => $recipes]);
    }
    public function create(){
        $form = Recipe::form();
        return response()->json(['form' => $form]);
    }
    public function store(Request $request){
        $this->validate($request, [
           'name' => 'required|max:255',
           'description' => 'required|max:3000',
           'image' => 'required|image',
           'ingredients' => 'required|array|min:1',
           'ingredients.*.name' => 'required|max:255',
           'ingredients.*.qty' => 'required|max:255',
           'directions' => 'required|array|min:1',
           'directions.*.description' => 'required|max:3000'
        ]);
        $ingredients = [];
        foreach ($request->ingredients as $ingredient){
            $ingredients[] = new RecipeIngredient($ingredient);
        }
        $directions = [];
        foreach ($request->directions as $direction){
            $directions[] = new RecipeDirection($direction);
        }
        if(!$request->hasFile('image') && !$request->file('image')->isValid()){
            return abort(404, '图片没有上传');
        }
        $filename = $this->getFileName($request->image);
        $request->image->move(base_path('public/images'), $filename);
        $recipe = new Recipe($request->all());
        $recipe->image = $filename;
        $request->user()->recipes()->save($recipe);
        $recipe->directions()->saveMany($directions);
        $recipe->ingredients()->saveMany($ingredients);
        return response()->json([
            'saved' =>true,
            'id' => $recipe->id,
            'filename' => $filename,
            'message' => '食谱创建成功!'
        ]);
    }
    protected function getFileName($file){
        return str_random(32).'.'.$file->extension();
    }
    public function show($id){
        $recipe = Recipe::with(['user','ingredients', 'directions'])->findOrFail($id);
        return response()->json(['recipe' => $recipe]);
    }
    public function edit($id, Request $request){
        $form = $request->user()->recipes()
            ->with(['ingredients' => function($query){
                $query->get(['id','name','qty']);
            }, 'directions' => function($query){
                $query->get(['id','description']);
            }])
            ->findOrFail($id, [
                'id','name','description','image'
            ]);
        return response()->json(['form' => $form]);
    }
    public function update($id, Request $request){
        $this->validate($request, [
            'name' => 'required|max:255',
            'description' => 'required|max:3000',
            'image' => 'image',
            'ingredients' => 'required|array|min:1',
            'ingredients.*.id' => 'integer|exists:recipe_ingredients',
            'ingredients.*.name' => 'required|max:255',
            'ingredients.*.qty' => 'required|max:255',
            'directions' => 'required|array|min:1',
            'directions.*.id' => 'integer|exists:recipe_directions',
            'directions.*.description' => 'required|max:3000'
        ]);
        $recipe = $request->user()->recipes()->findOrFail($id);
        $ingredients = [];
        $ingredientsUpdated = [];
        foreach ($request->ingredients as $ingredient){
            if(isset($ingredient['id'])){
                RecipeIngredient::where('recipe_id', $recipe->id)
                    ->where('id', $ingredient['id'])
                    ->update($ingredient);
                $ingredientsUpdated[] = $ingredient['id'];
            }else{
                $ingredients[] = new RecipeIngredient($ingredient);
            }
        }
        $directions = [];
        $directionsUpdated = [];
        foreach ($request->directions as $direction){
            if(isset($direction['id'])){
                RecipeDirection::where('recipe_id', $recipe->id)
                    ->where('id', $direction['id'])
                    ->update($direction);
                $directionsUpdated[] = $direction['id'];
            }else{
                $directions[] = new RecipeDirection($direction);
            }
        }
        $recipe->name = $request->name;
        $recipe->description = $request->description;
        if($request->hasFile('image') && $request->file('image')->isValid()){
            $filename = $this->getFileName($request->image);
            $request->image->move(base_path('public/images'),$filename);
            File::delete(base_path('public/images/'.$recipe->image));
            $recipe->image = $filename;
        }
        $recipe->save();
        RecipeIngredient::whereNotIn('id', $ingredientsUpdated)
            ->where('recipe_id', $recipe->id)->delete();
        RecipeDirection::whereNotIn('id', $directionsUpdated)
            ->where('recipe_id', $recipe->id)->delete();
        if(count($ingredients)){
            $recipe->ingredients()->saveMany($ingredients);
        }
        if(count($ingredients)){
            $recipe->directions()->saveMany($directions);
        }
        return response()->json([
            'saved' => true,
            'id' => $recipe->id,
            'message' => '更新食谱成功!'
        ]);
    }
    public function destroy($id, Request $request){
        $recipe = $request->user()->recipes()->findOrFail($id);
        RecipeIngredient::where('recipe_id', $recipe->id)->delete();
        RecipeDirection::where('recipe_id', $recipe->id)->delete();
        File::delete(base_path('public/images/'.$recipe->image));
        $recipe->delete();
        return response()->json([
            'deleted' => true
        ]);
    }
}

相关文章

  • Recipe-box 控制器设计

    创建控制器(创建完成之后生成的文件在 app/Http/Controllers/ 下) 添加路由 (路径:rout...

  • Designing a Controller-Setting U

    设计控制器建立控制器要设计一个控制器,单击窗口左侧的控制器文件夹树状图。如果菜单上没有控制器,单击New按钮并给控...

  • 父子控制器相关

    父子控制器:导航控制器,UITabBarViewController等。 父子控制器的设计原理:a控制器的View...

  • Recipe-box 模型设计(laravel&vue)

    创建数据库模型(创建完成之后可在 database/migrations 下看到创建的文档) >php artis...

  • A3. Overview of Building Blocks-

    控制器设计与仿真这是一个基于Windows的软件包,与AIDAPro集成,用于设计,测试和构建Exasmoc控制器...

  • 设计原理-控制器

    1.设计原理如果A控制器的view要成为B控制器的子控件, A控制器一定要成为B控制器的子控件 防止控制器被释放,...

  • ios开发- UIViewController

    UIViewController(视图控制器),顾名思义,是MVC设计模式中的控制器部分。UIViewContro...

  • AngularJS 1.x简单介绍及常用套路

    MVC设计模式 M:model 数据模型V:view 视图C:controller 控制器由控制器创建 数据 到数...

  • MVC设计模式

    MVC设计模式MVC设计模式 M:Model 数据模型 V:View 视图 C: Controller 控制器 (...

  • MVCN设计模式

    MVCN设计模式 Model ( 模型 ) View ( 视图 ) Controller ( 控制器 ) N...

网友评论

      本文标题:Recipe-box 控制器设计

      本文链接:https://www.haomeiwen.com/subject/zxxknxtx.html