145 lines
4.0 KiB
PHP
145 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\User;
|
|
|
|
use App\Models\Contact;
|
|
use App\Models\User;
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class UserContactController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
$contacts = Contact::where('pemilik_kontak', Auth::user()->email)->get();
|
|
return view('user.contact.index', ['contacts' => $contacts]);
|
|
// dd($contacts);
|
|
}
|
|
|
|
public function getContact()
|
|
{
|
|
$data = DB::table('contacts')
|
|
->select('contacts.relasi_kontak', 'users.nama_depan', 'users.nama_belakang')
|
|
->join('users', 'contacts.relasi_kontak', '=', 'users.email')
|
|
->where('contacts.pemilik_kontak','=',Auth::user()->email)
|
|
->paginate(10);
|
|
return response()->json($data);
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$email_relasi = $request->input('email');
|
|
if ($email_relasi == Auth::user()->email) {
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => 'Kontak yang ingin didaftarkan tidak boleh sama',
|
|
]);
|
|
} else {
|
|
$result = Contact::create([
|
|
'pemilik_kontak' => Auth::user()->email,
|
|
'relasi_kontak' => $request->input('email'),
|
|
]);
|
|
|
|
if ($result) {
|
|
return response()->json([
|
|
'status' => true,
|
|
'message' => 'Akun berhasil masuk ke kontak',
|
|
]);
|
|
} else {
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => 'Akun gagal masuk ke kontak',
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(Contact $contact)
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(Contact $contact)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(Request $request, Contact $contact)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(Contact $contact, $id)
|
|
{
|
|
try {
|
|
$result = Contact::destroy($id);
|
|
if ($result) {
|
|
return response()->json([
|
|
'message' => 'Berhasil hapus data',
|
|
'status' => true,
|
|
]);
|
|
} else {
|
|
return response()->json([
|
|
'message' => 'Gagal hapus data karena ' . $result,
|
|
'status' => false,
|
|
]);
|
|
}
|
|
} catch (\Exception $e) {
|
|
return response()->json([
|
|
'message' => 'Gagal hapus data, karena ' . $e,
|
|
'status' => false,
|
|
]);
|
|
}
|
|
}
|
|
|
|
public function cekEmail($email)
|
|
{
|
|
$result = User::where('email', $email)->get();
|
|
if ($result->isNotEmpty() && $result[0]->role == 'User' && $result[0]->status != 'Rejected') {
|
|
if ($result[0]->status == 'Finished') {
|
|
return response()->json([
|
|
'status' => true,
|
|
'message' => $result,
|
|
]);
|
|
} else {
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => 'Akun dengen email ' . $email . ' tersedia dan belum diverifikasi',
|
|
]);
|
|
}
|
|
} else {
|
|
return response()->json([
|
|
'status' => false,
|
|
'message' => 'Akun dengen email ' . $email . ' tidak tersedia atau ditolak',
|
|
]);
|
|
}
|
|
}
|
|
}
|