-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
180 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,26 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Models\Offer; | ||
use App\Models\Listing; | ||
use Illuminate\Http\Request; | ||
|
||
class ListingOfferController extends Controller | ||
{ | ||
public function store(Listing $listing, Request $request) | ||
{ | ||
$listing->offers()->save( | ||
Offer::make( | ||
$request->validate([ | ||
'amount' => 'required|integer|min:1|max:20000000' | ||
]) | ||
)->bidder()->associate($request->user()) | ||
); | ||
|
||
return redirect()->back()->with( | ||
'success', | ||
'Offer was made!' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,26 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Illuminate\Database\Eloquent\Factories\HasFactory; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
|
||
class Offer extends Model | ||
{ | ||
|
||
protected $fillable = [ | ||
'amount', | ||
'accepted_at', | ||
'rejected_at' | ||
]; | ||
|
||
public function listing():BelongsTo{ | ||
return $this->belongsTo(Listing::class, 'listing_id'); | ||
} | ||
|
||
public function bidder():BelongsTo{ | ||
return $this->belongsTo(User::class, 'bidder_id'); | ||
} | ||
use HasFactory; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
database/migrations/2024_05_09_093328_create_offers_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,44 @@ | ||
<?php | ||
|
||
use App\Models\Listing; | ||
use App\Models\User; | ||
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::create('offers', function (Blueprint $table) { | ||
$table->id(); | ||
$table->timestamps(); | ||
$table->timestamp('accepted_at')->nullable(); | ||
$table->timestamp('rejected_at')->nullable(); | ||
|
||
$table->foreignIdFor( | ||
Listing::class, | ||
'listing_id' | ||
)->constrained('listings'); | ||
|
||
|
||
$table->foreignIdFor( | ||
User::class, | ||
'bidder_id' | ||
)->constrained('users'); | ||
|
||
$table->unsignedInteger('amount'); | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
*/ | ||
public function down(): void | ||
{ | ||
Schema::dropIfExists('offers'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 1,58 @@ | ||
<template> | ||
<Box> | ||
<template #header>Make an Offer</template> | ||
<div> | ||
<form @submit.prevent="makeOffer"> | ||
<input v-model.number="form.amount" type="text" class="input" /> | ||
<input | ||
v-model.number="form.amount" | ||
type="range" :min="min" | ||
:max="max" step="10000" | ||
class="mt-2 w-full h-4 bg-gray-200 rounded-lg appearance-none cursor-pointer dark:bg-gray-700" | ||
/> | ||
|
||
<button type="submit" class="btn-outline w-full mt-2 text-sm"> | ||
Make an Offer | ||
</button> | ||
</form> | ||
</div> | ||
<div class="flex justify-between text-gray-500 mt-2"> | ||
<div>Difference</div> | ||
<div> | ||
<Price :price="difference" /> | ||
</div> | ||
</div> | ||
</Box> | ||
</template> | ||
|
||
<script setup> | ||
import Price from '@/Components/price.vue' | ||
import Box from '@/Components/UI/box.vue' | ||
import { useForm } from "@inertiajs/vue3"; | ||
import { computed, watch } from 'vue' | ||
const props = defineProps({ | ||
listingId: Number, | ||
price: Number, | ||
}) | ||
const form = useForm({ | ||
amount: props.price | ||
}) | ||
const makeOffer = () => form.post( | ||
route('listing.offer.store', | ||
{ listing: props.listingId }, | ||
), | ||
{ | ||
preserveScroll: true, | ||
preserveState: true, | ||
}, | ||
) | ||
const difference = computed(() => form.amount - props.price) | ||
const min = computed(() => Math.round(props.price / 2)) | ||
const max = computed(() => Math.round(props.price * 2)) | ||
const emit = defineEmits(['offerUpdated']); | ||
watch(() => form.amount, () => emit('offerUpdated')) | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters