Skip to content

Commit

Permalink
Merge pull request #98 from Crudzaso/feature/use-lotteries-api
Browse files Browse the repository at this point in the history
Feature/use lotteries api
  • Loading branch information
DiegoAndresRamirez authored Dec 9, 2024
2 parents 44474dc + ea97454 commit 300d8c4
Show file tree
Hide file tree
Showing 6 changed files with 633 additions and 170 deletions.
65 changes: 50 additions & 15 deletions Modules/Raffle/app/Http/Controllers/RaffleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,18 @@ public function store(Request $request)
'description' => 'nullable|string',
'start_date' => 'required|date',
'end_date' => 'required|date|after:start_date',
'image' => 'nullable|image|max:2048', // Validar si es una imagen
]);


$imagePath = null;

// Verificar si se subió una imagen
if ($request->hasFile('image')) {
// Guardar la imagen en el disco 'public' y obtener la ruta
$imagePath = $request->file('image')->store('raffle_images', 'public');
}

// Crear la rifa
Raffle::create([
'name' => $request->name,
'organizer_id' => $request->organizer_id,
Expand All @@ -51,11 +61,13 @@ public function store(Request $request)
'description' => $request->description,
'start_date' => $request->start_date,
'end_date' => $request->end_date,
'total_sales' => 0, // valor predeterminado de total_sales
'total_sales' => 0, // Valor predeterminado
'image' => $imagePath, // Guardar la ruta de la imagen en la base de datos
]);

return redirect()->route('raffles.index')->with('success', 'Rifa creada exitosamente.');
}


public function edit($id)
{
Expand Down Expand Up @@ -102,22 +114,32 @@ public function destroy($id)

public function getRaffles(Request $request)
{
$query = Raffle::with('organizer', 'lottery');

// Comenzamos la consulta con las relaciones
$query = Raffle::with('organizer', 'lottery')
// Filtramos por rifas activas, aquellas cuyo end_date aún no ha pasado
->where('end_date', '>=', now())
// Ordenamos por la fecha de creación o fin, de más reciente a más antiguo
->orderBy('created_at', 'desc');

// Filtros de precio mínimo y máximo
if ($request->has('min_price')) {
$query->where('ticket_price', '>=', $request->input('min_price'));
}
if ($request->has('max_price')) {
$query->where('ticket_price', '<=', $request->input('max_price'));
}


// Filtramos por la fecha de fin si se proporciona
if ($request->has('end_date')) {
$query->where('end_date', '<=', $request->input('end_date'));
}


// Paginamos los resultados (6 resultados por página)
$raffles = $query->paginate(6);

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


public function getLastChanceRaffles()
{
Expand All @@ -142,29 +164,42 @@ public function getFilteredRaffles(Request $request)
$filter = $request->input('filter');
$date = $request->input('date');
$query = Raffle::with('organizer', 'lottery');

// Aplicar filtro adicional por fecha
// Filtro adicional por fecha específica
if ($date) {
$query->whereDate('end_date', '>', $date);
}

switch ($filter) {
case 'popular':
// Rifas populares con más de 50 tickets vendidos y activas
$query->where('tickets_sold', '>=', 50)
->where('end_date', '>', now()->addDays(10));
->where('end_date', '>', now())
->orderBy('tickets_sold', 'desc');
break;

case 'last_chance':
$query->where('end_date', '<', now()->addDays(3));
// Rifas activas ordenadas por el end_date más próximo
$query->where('end_date', '>', now())
->orderBy('end_date', 'asc');
break;

case 'flash':
$query->where('total_tickets', '<=', 50);
// Rifas con menos de 50 tickets y activas, ordenadas por creación más reciente
$query->where('end_date', '>', now())
->orderBy('created_at', 'desc');
break;

default:
$query->where('end_date', '>', now());
// Por defecto, todas las rifas activas
$query->where('end_date', '>', now())
->orderBy('end_date', 'asc');
break;
}

$raffles = $query->paginate(6);
return response()->json($raffles);
}


}
33 changes: 33 additions & 0 deletions database/migrations/2024_12_09_080953_update_lotteries_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('lotteries', function (Blueprint $table) {
$table->text('description')->nullable()->change();
$table->string('url_imagen')->nullable()->change();

$table->string('slug')->unique()->after('name');
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('lotteries', function (Blueprint $table) {
$table->text('description')->nullable(false)->change();
$table->string('url_imagen')->nullable(false)->change();
$table->dropColumn('slug');
});
}
};
2 changes: 2 additions & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ class DatabaseSeeder extends Seeder
public function run(): void
{

$this->call(LotterySeeder::class);

$this->call([
RolesPermissionsSeeder::class,
]);
Expand Down
135 changes: 135 additions & 0 deletions database/seeders/LotterySeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Modules\Lottery\Models\Lottery;

class LotterySeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$lotteries = [
["name" => "ANTIOQUEÑITA MAÑANA FESTIVO", "slug" => "antioquenita-manana-festivo"],
["name" => "ANTIOQUEÑITA TARDE FESTIVO", "slug" => "antioquenita-tarde-festivo"],
["name" => "CARIBEÑA DIA FESTIVO", "slug" => "caribena-dia-festivo"],
["name" => "CHONTICO DIA FESTIVO", "slug" => "chontico-dia-festivo"],
["name" => "CULONA DIA FESTIVO", "slug" => "culona-dia-festivo"],
["name" => "MOTILON DIA", "slug" => "motilon-dia"],
["name" => "PAISITA DIA FESTIVO", "slug" => "paisita-dia-festivo"],
["name" => "SAMAN FESTIVO", "slug" => "saman-festivo"],
["name" => "SINUANO DIA FESTIVO", "slug" => "sinuano-dia-festivo"],
["name" => "ANTIOQUEÑITA FESTIVA MAÑANA", "slug" => "antioqueaita-festiva-maaana"],
["name" => "ANTIOQUEÑITA FESTIVA TARDE", "slug" => "antioqueaita-festiva-tarde"],
["name" => "ANTIOQUEÑITA MAÑANA", "slug" => "antioqueaita-maaana"],
["name" => "ANTIOQUEÑITA TARDE", "slug" => "antioqueaita-tarde"],
["name" => "ANTIOQUEÑITA FESTIVA MAÑANA", "slug" => "antioquenita-festiva-manana"],
["name" => "ANTIOQUEÑITA FESTIVA TARDE", "slug" => "antioquenita-festiva-tarde"],
["name" => "ANTIOQUEÑITA MAÑANA", "slug" => "antioquenita-manana"],
["name" => "ANTIOQUEÑITA TARDE", "slug" => "antioquenita-tarde"],
["name" => "AST-LUN 21:50", "slug" => "ast-lun-2150"],
["name" => "AST-SOL 13:50", "slug" => "ast-sol-1350"],
["name" => "ASTRO LUNA", "slug" => "astro-luna"],
["name" => "ASTRO SOL", "slug" => "astro-sol"],
["name" => "BOGOTA", "slug" => "bogota"],
["name" => "BOYACA", "slug" => "boyaca"],
["name" => "CAFETERITO FESTIVO", "slug" => "cafeterito-festivo"],
["name" => "CAFETERITO NOCHE", "slug" => "cafeterito-noche"],
["name" => "CAFETERITO NOCHE FESTIVO", "slug" => "cafeterito-noche-festivo"],
["name" => "CAFETERO", "slug" => "cafetero"],
["name" => "CAFETERO DIA", "slug" => "cafetero-dia"],
["name" => "CAFETERO NOCHE", "slug" => "cafetero-noche"],
["name" => "CAFETERO NOCHE FESTIVO", "slug" => "cafetero-noche-festivo"],
["name" => "CARIBEÑA DIA", "slug" => "caribeaa-dia"],
["name" => "CARIBEÑA FESTIVO DIA", "slug" => "caribeaa-festivo-dia"],
["name" => "CARIBEÑA NOCHE", "slug" => "caribeaa-noche"],
["name" => "CARIBEÑA DIA", "slug" => "caribena-dia"],
["name" => "CARIBEÑA FESTIVA NOCHE", "slug" => "caribena-festiva-noche"],
["name" => "CARIBEÑA FESTIVO DIA", "slug" => "caribena-festivo-dia"],
["name" => "CARIBEÑA NOCHE", "slug" => "caribena-noche"],
["name" => "CARIBEÑA NOCHE FESTIVO", "slug" => "caribena-noche-festivo"],
["name" => "CAUCA", "slug" => "cauca"],
["name" => "CHONTICO", "slug" => "chontico"],
["name" => "CHONTICO DIA", "slug" => "chontico-dia"],
["name" => "CHONTICO FESTIVO", "slug" => "chontico-festivo"],
["name" => "CHONTICO FESTIVO DIA", "slug" => "chontico-festivo-dia"],
["name" => "CHONTICO NOCHE", "slug" => "chontico-noche"],
["name" => "CHONTICO NOCHE FESTIVO", "slug" => "chontico-noche-festivo"],
["name" => "CHONTICO NOCHE JUEVES", "slug" => "chontico-noche-jueves"],
["name" => "CRUZ ROJA", "slug" => "cruz-roja"],
["name" => "CULONA", "slug" => "culona"],
["name" => "CULONA DIA", "slug" => "culona-dia"],
["name" => "CULONA FESTIVO", "slug" => "culona-festivo"],
["name" => "CULONA FESTIVO DIA", "slug" => "culona-festivo-dia"],
["name" => "CULONA FESTIVO NOCHE", "slug" => "culona-festivo-noche"],
["name" => "CULONA NOCHE", "slug" => "culona-noche"],
["name" => "CULONA NOCHE FESTIVO", "slug" => "culona-noche-festivo"],
["name" => "CUNDINAMARCA", "slug" => "cundinamarca"],
["name" => "DORADO", "slug" => "dorado"],
["name" => "DORADO FESTIVO", "slug" => "dorado-festivo"],
["name" => "DORADO MAÑANA", "slug" => "dorado-manana"],
["name" => "DORADO NOCHE", "slug" => "dorado-noche"],
["name" => "DORADO NOCHE FESTIVO", "slug" => "dorado-noche-festivo"],
["name" => "DORADO TARDE", "slug" => "dorado-tarde"],
["name" => "EXTRA CRUZ ROJA", "slug" => "extra-cruz-roja"],
["name" => "EXTRA DE COLOMBIA", "slug" => "extra-de-colombia"],
["name" => "FANTASTICA DIA", "slug" => "fantastica-dia"],
["name" => "FANTASTICA NOCHE", "slug" => "fantastica-noche"],
["name" => "FANTASTICA NOCHE FESTIVO", "slug" => "fantastica-noche-festivo"],
["name" => "HUILA", "slug" => "huila"],
["name" => "LA FANTASTICA DIA", "slug" => "la-fantastica-dia"],
["name" => "LA FANTASTICA FESTIVO", "slug" => "la-fantastica-festivo"],
["name" => "LA FANTASTICA NOCHE", "slug" => "la-fantastica-noche"],
["name" => "LA FANTASTICA NOCHE FESTIVO", "slug" => "la-fantastica-noche-festivo"],
["name" => "MANIZALES", "slug" => "manizales"],
["name" => "MEDELLIN", "slug" => "medellin"],
["name" => "META", "slug" => "meta"],
["name" => "MOTILON", "slug" => "motilon"],
["name" => "MOTILON FESTIVO", "slug" => "motilon-festivo"],
["name" => "MOTILON NOCHE", "slug" => "motilon-noche"],
["name" => "PAISITA 3 SABADOS", "slug" => "paisita-3-sabados"],
["name" => "PAISITA DIA", "slug" => "paisita-dia"],
["name" => "PAISITA FESTIVO", "slug" => "paisita-festivo"],
["name" => "PAISITA FESTIVO DIA", "slug" => "paisita-festivo-dia"],
["name" => "PAISITA FESTIVO NOCHE", "slug" => "paisita-festivo-noche"],
["name" => "PAISITA NOCHE", "slug" => "paisita-noche"],
["name" => "PAISITA NOCHE FESTIVO", "slug" => "paisita-noche-festivo"],
["name" => "PIJAO", "slug" => "pijao"],
["name" => "PIJAO FESTIVO", "slug" => "pijao-festivo"],
["name" => "PIJAO NOCHE FESTIVO", "slug" => "pijao-noche-festivo"],
["name" => "QUINDIO", "slug" => "quindio"],
["name" => "RISARALDA", "slug" => "risaralda"],
["name" => "SAMAN", "slug" => "saman"],
["name" => "SAMANFESTIVO", "slug" => "samanfestivo"],
["name" => "SANTANDER", "slug" => "santander"],
["name" => "SINUANO DIA", "slug" => "sinuano-dia"],
["name" => "SINUANO FESTIVO DIA", "slug" => "sinuano-festivo-dia"],
["name" => "SINUANO FESTIVO NOCHE", "slug" => "sinuano-festivo-noche"],
["name" => "SINUANO NOCHE", "slug" => "sinuano-noche"],
["name" => "SINUANO NOCHE FESTIVO", "slug" => "sinuano-noche-festivo"],
["name" => "SORTEO EXTRA CAUCA", "slug" => "sorteo-extra-cauca"],
["name" => "SORTEO EXTRA CRUZROJA-VALLE", "slug" => "sorteo-extra-cruzroja-valle"],
["name" => "SORTEO EXTRA DE BOGOTA", "slug" => "sorteo-extra-de-bogota"],
["name" => "SORTEO EXTRA DE BOYACA", "slug" => "sorteo-extra-de-boyaca"],
["name" => "SORTEO EXTRA DE MANIZALES", "slug" => "sorteo-extra-de-manizales"],
["name" => "SORTEO EXTRA DEL TOLIMA", "slug" => "sorteo-extra-del-tolima"],
["name" => "SORTEO EXTRA SANTANDER", "slug" => "sorteo-extra-santander"],
["name" => "SUPER EXTRA MEDELLIN", "slug" => "super-extra-medellin"],
["name" => "SUPER EXTRA NAVIDAD MEDELLIN", "slug" => "super-extra-navidad-medellin"],
["name" => "TOLIMA", "slug" => "tolima"],
["name" => "VALLE", "slug" => "valle"]
];

foreach ($lotteries as $lottery) {
Lottery::create([
'name' => $lottery['name'],
'slug' => $lottery['slug'],
'description' => 'Descripción predeterminada para la lotería ' . $lottery['name'],
'url_imagen' => 'https://via.placeholder.com/150', // URL de imagen predeterminada
]);
}
}
}
2 changes: 1 addition & 1 deletion resources/js/Components/Dashboard/ActiveRaffles.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const redirectToRaffles = () => {
const fetchRaffles = async () => {
try {
const response = await fetch('/raffles-actives');
const response = await fetch('/raffles-actives'); // Llama al endpoint correcto
if (!response.ok) {
throw new Error('Error al obtener las rifas activas');
}
Expand Down
Loading

0 comments on commit 300d8c4

Please sign in to comment.