Skip to content

Commit

Permalink
Integrate firestore
Browse files Browse the repository at this point in the history
  • Loading branch information
eyad-hussein committed Jan 6, 2024
1 parent edbb615 commit d88e4fe
Show file tree
Hide file tree
Showing 19 changed files with 1,203 additions and 23 deletions.
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use an official PHP runtime as a parent image
FROM php:8.1-fpm

WORKDIR /app

RUN apt-get update && apt-get install -y \
libzip-dev \
unzip

RUN docker-php-ext-install pdo pdo_mysql zip

COPY . /app

RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

RUN composer install --no-interaction --optimize-autoloader

EXPOSE 9000

CMD ["php", "artisan", "serve", "--host=0.0.0.0", "--port=9000"]
27 changes: 27 additions & 0 deletions app/Http/Controllers/ImageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Services\FirestoreService;

class ImageController extends Controller
{
protected $firestoreService;

public function __construct(FirestoreService $firestoreService)
{
$this->firestoreService = $firestoreService;
}

public function index()
{
// return "hi";
$imageUrls = $this->firestoreService->getImageUrlsFromStorage("robotics-website-4a145.appspot.com");

return response()->json([
'data' => $imageUrls
]);
}
}
91 changes: 91 additions & 0 deletions app/Http/Controllers/WorkshopController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace App\Http\Controllers;

use App\Models\Workshop;
use Illuminate\Http\Request;

class WorkshopController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}

/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$workshop = Workshop::create([
'title' => $request->title,
'description' => $request->description,
'start_date' => $request->start_date,
'location' => $request->location,
'duration' => $request->duration,
]);

return response()->json(['workshop' => $workshop], 200);
}

/**
* Display the specified resource.
*/
public function show(Request $request, Workshop $workshop)
{
$workshop = Workshop::find($request->id);

return response(
[
'workshop' => $workshop,
],
200
);
}

public function showActive()
{
$workshops = Workshop::whereBetween('id', [1, 3])->get();
return response(
[
'workshops' => $workshops,
],
200
);
}

/**
* Show the form for editing the specified resource.
*/
public function edit(Workshop $workshop)
{
//
}

/**
* Update the specified resource in storage.
*/
public function update(Request $request, Workshop $workshop)
{
//
}

/**
* Remove the specified resource from storage.
*/
public function destroy(Workshop $workshop)
{
//
}
}
13 changes: 13 additions & 0 deletions app/Models/Image.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Image extends Model
{
use HasFactory;

protected $fillable = ['url'];
}
31 changes: 31 additions & 0 deletions app/Models/Workshop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace App\Models;

use App\Models\Image;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Workshop extends Model
{
use HasFactory;

protected $fillable = [
'title',
'description',
'start_date',
'location',
'duration',
];


protected $casts = [
// 'start_date' => 'datetime',
'duration' => 'integer',
];

public function images()
{
return $this->belongsToMany(Image::class);
}
}
41 changes: 41 additions & 0 deletions app/Services/FirestoreService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace App\Services;

use Google\Cloud\Firestore\FirestoreClient;
use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Core\Exception\GoogleException;

class FirestoreService
{
protected $firestore;
protected $storage;

public function __construct()
{
$this->firestore = new FirestoreClient();

try {
// Replace 'your-project-id' with your actual Google Cloud Project ID
$this->storage = new StorageClient(['projectId' => 'robotics-website-4a145']);
} catch (GoogleException $e) {
// Handle exception, e.g., log or throw
}
}

public function getImageUrlsFromStorage($bucketName)
{
$bucket = $this->storage->bucket($bucketName);

$objects = $bucket->objects();

$imageUrls = [];

foreach ($objects as $object) {
// Assuming all objects in the bucket are images
$imageUrls[] = $object->signedUrl(strtotime('+1 hour'));
}

return $imageUrls;
}
}
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@
"name": "laravel/laravel",
"type": "project",
"description": "The skeleton application for the Laravel framework.",
"keywords": ["laravel", "framework"],
"keywords": [
"laravel",
"framework"
],
"license": "MIT",
"require": {
"php": "^8.1",
"google/cloud-firestore": "^1.40",
"google/cloud-storage": "^1.36",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^10.10",
"laravel/sanctum": "^3.3",
"laravel/tinker": "^2.8"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",
"fakerphp/faker": "^1.23",
"laravel/pint": "^1.0",
"laravel/sail": "^1.18",
"mockery/mockery": "^1.4.4",
Expand All @@ -24,7 +29,8 @@
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
"Database\\Seeders\\": "database/seeders/",
"App\\Services\\": "app/Services/"
}
},
"autoload-dev": {
Expand Down
Loading

0 comments on commit d88e4fe

Please sign in to comment.