PLATFORM
  • Tails

    Create websites with TailwindCSS

  • Blocks

    Design blocks for your website

  • Wave

    Start building the next great SAAS

  • Pines

    Alpine & Tailwind UI Library

  • Auth

    Plug'n Play Authentication for Laravel

  • Designer comingsoon

    Create website designs with AI

  • DevBlog comingsoon

    Blog platform for developers

  • Static

    Build a simple static website

  • SaaS Adventure

    21-day program to build a SAAS

Question By
Solved

Adding endpoints to the API

Solved
kaprinkey1

Apr 3rd, 2024 11:19 AM

Hey Bobby or whomever,

After developing our shop4charities publishing website for advertisers, I realized that I needed a better way to manage the stores, products, categories, and links. I have gone through several processes thus far ranging from filling out a form to uploading the csv file cj.com gives but it does not contain all the information we use on our website.

As you know I am using Laravel Wave for this. I am in the process of adding endpoints for it right now.

I looked at the API controller out of the box and it does use the BREAD operations.

namespace Wave\Http\Controllers\API;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use TCG\Voyager\Http\Controllers\ContentTypes\Text;
use TCG\Voyager\Http\Controllers\Controller;
use TCG\Voyager\Http\Controllers\Traits\BreadRelationshipParser;
use TCG\Voyager\Models\DataType;

    //*********************************************
    //                _____
    //               |  __ \
    //               | |  | |
    //               | |  | |
    //               | |__| |
    //               |_____/
    //
    //         API Delete an item BREA(D)
    //
    //*********************************************

    public function delete(Request $request, $slug, $id)
    {
        $slug = $this->getSlug($request);

        $dataType = Datatype::where('slug', '=', $slug)->first();

        // Check permission
        $this->authorize('delete', app($dataType->model_name));
        $data = call_user_func([$dataType->model_name, 'findOrFail'], $id);

        $res = $data->delete($id);

        if ($res) {
            return response()->json(['success' => true, 'message' => 'Successfully deleted']);
        }

    }

    public function getSlug(Request $request)
    {
        if (isset($this->slug)) {
            $slug = $this->slug;
        } else {
            $slug = $request->segment(2);
        }

        return $slug;
    }

    public function insertUpdateData($request, $slug, $rows, $data)
    {

        foreach ($rows as $row) {

            $options = $row->details;

            $content = $this->getContentBasedOnType($request, $slug, $row, $options);

            if(isset($request->{$row->field})){
                $data->{$row->field} = $content;
            }
        }

        $data->save();

        return $data;
    }

    public function getContentBasedOnType(Request $request, $slug, $row, $options = null)
    {
        return (new Text($request, $slug, $row, $options))->handle();
    }

    private function isValidJson($string) {
     json_decode($string);
     return (json_last_error() == JSON_ERROR_NONE);
    }

}

My question is, should I extend the api controller namespace you guys created or go with my own namespace. Currently, I have added methods to my StoreController and CategoryController for the api endpoints. I am wondering if I should just make a new controller and go the BREAD route which would be better coherence to what is already in place.

  public function apiShow($programName)
    {
        $store = Store::where('program_name', $programName)
                      ->with('category') // Ensure the relationship is defined in the Store model
                      ->firstOrFail();

        return response()->json($store);
    }

-----------------------------------------------------------------------------------
 public function apiShow($slug)
    {
        $category = Category::where('slug', $slug)->with('stores')->firstOrFail();
        return response()->json($category);
    }
kaprinkey1

Apr 3rd, 2024 11:38 AM

Bobby,

Nevermind I got the endpoints working without issue and have 8 pages of store data. Matter of fact almost every single bread I created in voyager is accessible via the API, I just have to change the endpoint URL.

However, I did discover one issue. There is a categories and category_id tables. I never altered the voyager category controller and just made my own for the category_id table. When I hit the endpoints it is returning the voyager categories. We need the blog so I cannot delete the categories table. Can you think of a way around this?

Screenshot 2024-04-03 133822.png

bobbyiliev

Apr 3rd, 2024 01:23 PM

Hey!

Happy to hear that you've got this sorted out!

Regarding the categories question, as far as I can tell from the output that you've shared the category and category_id are columns in your store table rather than separate tables.

If this is the case, and if you don't need them you should be able to drop those columns from the store table. Or if you need them, you can leave them as they are.

kaprinkey1

Apr 3rd, 2024 06:52 PM

Yes the stores table has a category_id column tied to the category_id table. The stores table also has a category column but that is from the csv file upload I created.

The CSV file had the column so I wrote some logic that depending on the category name, it will insert the category into the category_id table. It does work and what it's purpose is for.

However, thinking about this further, our categories do not change often, if at all except maybe for the Rakuten categories we plan to add. That would mean I don't necessarily need to see the categories but rather update, delete the categories.

I also think I forgot to add the category endpoint to the json file.

I am wondering if adding the endpoint to the json file returns the store categories instead of the post categories as when I hit the categories endpoint it returns the post categories when I need the store categories.

I can already run BREAD on the stores table via API. Need the same for categories and the controller logic is already present in the store category controller.

kaprinkey1

Apr 3rd, 2024 08:11 PM

Best Answer

Bobby,

I figured I would let you know that I got the stores endpoint working entirely including all BREAD operations. The interesting thing I found was that since the API endpoints and logic was in place already, I merely had to create BREAD for the stores table in voyager admin panel, without adding any logic to my store controller or model for the API endpoints and it worked lol. Originally I had my own API routes defined with methods in my controllers for stores and categories and at that point PUT was being denied bc I did not define them in my custom api.php file. After removing the methods and routes, I was able to then use the PUT method with my stores endpoint.

Mind you, I have not added the stores route to the endpoints.json file. Yes I did add them in Insomnia and defined the methods there so basically it is the same thing but I didn't have to touch the JSON file is my point.

I was not sure if you guys knew all of this already when someone extends the API but figured I would let you know in the event Wave is released again. It would give you better insight to what the docs should have regarding extending the API endpoints.

Knowing this, I have to give you guys props for that. To make adding endpoints that easy? Shitttt lol.

Thinking about that, does Foundation have the API out of the box?

bobbyiliev

Apr 5th, 2024 02:04 AM

Hey!

Happy to hear that you've got it working so far!

Regarding Foundation, we are planning to start working on Wave v3 first and a REST API will still be available out of the box!

Any other feature requests would also be appreciated!

kaprinkey1

Apr 7th, 2024 09:47 AM

I would like to see Teams integrated via JetStream.

If I heard correctly and that voyager is being passed on, JetStream is the next best alternative and it also comes with Fortify so you get the Teams but also API, user auth, team settings and more as you probably already know with your familiarity with Laravel and her packages. It is super easy to extend as well. I am already working with it my WeS project. It also comes with your choice of Livewire or Vue. I personally picked Livewire for the WeS app.

I read that Filament is being used? Is this solely for the admin panel or you plan to extend that into users, roles and etc? I have never worked with Filament as I stayed with the DevDojo product line. I really do not like jumping around communities so when I found you guys, I stuck with it.