Skip to content

Commit

Permalink
fix: fix gift api without passport (#5664)
Browse files Browse the repository at this point in the history
  • Loading branch information
ribounette authored Oct 30, 2021
1 parent 0354664 commit 7939a5f
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 6 deletions.
124 changes: 124 additions & 0 deletions app/Http/Controllers/Contacts/GiftController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

namespace App\Http\Controllers\Contacts;

use App\Models\Contact\Gift;
use Illuminate\Http\Request;
use App\Models\Account\Photo;
use App\Models\Contact\Contact;
use App\Http\Controllers\Controller;
use App\Traits\JsonRespondController;
use App\Services\Contact\Gift\CreateGift;
use App\Services\Contact\Gift\UpdateGift;
use App\Services\Contact\Gift\DestroyGift;
use App\Http\Resources\Gift\Gift as GiftResource;
use App\Services\Contact\Gift\AssociatePhotoToGift;

class GiftController extends Controller
{
use JsonRespondController;

/**
* Get the list of gifts for the given contact.
*
* @param Request $request
* @param Contact $contact
* @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection|\Illuminate\Http\JsonResponse
*/
public function index(Request $request, Contact $contact)
{
$gifts = $contact->gifts()
->orderBy('created_at', 'asc')
->paginate();

return GiftResource::collection($gifts);
}

/**
* Get the detail of a given gift.
*
* @param Request $request
* @param Gift $gift
* @return GiftResource|\Illuminate\Http\JsonResponse
*/
public function show(Request $request, Contact $contact, Gift $gift)
{
return new GiftResource($gift);
}

/**
* Store the gift.
*
* @param Request $request
* @return GiftResource|\Illuminate\Http\JsonResponse
*/
public function store(Request $request, Contact $contact)
{
$gift = app(CreateGift::class)->execute(
$request->except(['account_id', 'contact_id']) +
[
'account_id' => auth()->user()->account_id,
'contact_id' => $contact->id,
]
);

return new GiftResource($gift);
}

/**
* Update the gift.
*
* @param Request $request
* @param Gift $gift
* @return GiftResource|\Illuminate\Http\JsonResponse
*/
public function update(Request $request, Contact $contact, Gift $gift)
{
$gift = app(UpdateGift::class)->execute(
$request->except(['account_id', 'contact_id', 'gift_id']) +
[
'account_id' => auth()->user()->account_id,
'contact_id' => $contact->id,
'gift_id' => $gift->id,
]
);

return new GiftResource($gift);
}

/**
* Associate a photo to the gift.
*
* @param Request $request
* @param Gift $gift
* @param Photo $photo
* @return GiftResource|\Illuminate\Http\JsonResponse
*/
public function associate(Request $request, Contact $contact, Gift $gift, Photo $photo)
{
$gift = app(AssociatePhotoToGift::class)->execute([
'account_id' => auth()->user()->account_id,
'gift_id' => $gift->id,
'photo_id' => $photo->id,
]);

return new GiftResource($gift);
}

/**
* Delete a gift.
*
* @param Request $request
* @param Gift $gift
* @return \Illuminate\Http\JsonResponse
*/
public function destroy(Request $request, Contact $contact, Gift $gift)
{
app(DestroyGift::class)->execute([
'account_id' => auth()->user()->account_id,
'gift_id' => $gift->id,
]);

return $this->respondObjectDeleted($gift->id);
}
}
11 changes: 8 additions & 3 deletions resources/js/components/people/gifts/CreateGift.vue
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ export default {
},
props: {
hash: {
type: String,
default: '',
},
contactId: {
type: Number,
default: 0,
Expand Down Expand Up @@ -386,7 +390,8 @@ export default {
}
const method = this.gift ? 'put' : 'post';
const url = this.gift ? 'api/gifts/'+this.gift.id : 'api/gifts';
const url = `people/${this.hash}/gifts${this.gift ? '/'+this.gift.id : ''}`;
const vm = this;
axios[method](url, this.newGift)
Expand Down Expand Up @@ -417,7 +422,7 @@ export default {
return this.$refs.upload.forceFileUpload()
.then(photo => {
if (photo !== undefined) {
axios.put('api/gifts/'+response.data.data.id+'/photo/'+photo.id);
axios.put(`people/${this.hash}/gifts/${response.data.data.id}/photo/${photo.id}`);
response.data.data.photos.push(photo);
}
return response;
Expand All @@ -437,7 +442,7 @@ export default {
},
deletePhoto(photo) {
axios.delete('api/photos/' + photo.id)
axios.delete(`people/${this.hash}/photos/${photo.id}`)
.then(response => {
this.photos.splice(this.photos.indexOf(photo), 1);
if (this.photos.length == 0) {
Expand Down
8 changes: 5 additions & 3 deletions resources/js/components/people/gifts/Gifts.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<template v-if="displayCreateGift">
<create-gift
:hash="hash"
:contact-id="contactId"
:family-contacts="familyContacts"
:reach-limit="reachLimit"
Expand Down Expand Up @@ -80,6 +81,7 @@
</gift>
<create-gift
v-else
:hash="hash"
:gift="gift"
:contact-id="contactId"
:family-contacts="familyContacts"
Expand Down Expand Up @@ -193,7 +195,7 @@ export default {
},
getGifts() {
axios.get('api/contacts/' + this.contactId + '/gifts')
axios.get(`people/${this.hash}/gifts`)
.then(response => {
this.gifts = response.data.data;
});
Expand All @@ -208,7 +210,7 @@ export default {
gift.date = null;
}
gift.contact_id = this.contactId;
axios.put('api/gifts/' + gift.id, gift)
axios.put(`people/${this.hash}/gifts/${gift.id}`, gift)
.then(response => {
Vue.set(gift, 'status', response.data.data.status);
Vue.set(gift, 'date', response.data.data.date);
Expand All @@ -221,7 +223,7 @@ export default {
},
trash(gift) {
axios.delete('api/gifts/' + gift.id)
axios.delete(`people/${this.hash}/gifts/${gift.id}`)
.then(response => {
this.gifts.splice(this.gifts.indexOf(gift), 1);
this.closeDeleteModal();
Expand Down
6 changes: 6 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@
'index', 'store', 'update', 'destroy',
]);

// Gifts
Route::resource('people/{contact}/gifts', 'Contacts\\GiftController')->only([
'index', 'show', 'store', 'update', 'destroy',
]);
Route::put('people/{contact}/gifts/{gift}/photo/{photo}', 'Contacts\\GiftController@associate');

// Debt
Route::resource('people/{contact}/debts', 'Contacts\\DebtController')->except(['index', 'show']);

Expand Down

0 comments on commit 7939a5f

Please sign in to comment.