Skip to content

Commit

Permalink
open the sky
Browse files Browse the repository at this point in the history
  • Loading branch information
ecojuntak committed Jul 20, 2019
1 parent e9207c5 commit dad6265
Show file tree
Hide file tree
Showing 14 changed files with 203 additions and 21 deletions.
2 changes: 1 addition & 1 deletion app/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

class Group extends Model
{
//
protected $fillable = ["user_id", "name", "target_amount", "target_date"];
}
46 changes: 43 additions & 3 deletions app/Http/Controllers/GroupController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
namespace App\Http\Controllers;

use App\Group;
use App\Session;
use App\Transaction;
use App\UserGroup;
use Illuminate\Http\Request;

class GroupController extends Controller
Expand All @@ -12,9 +15,23 @@ class GroupController extends Controller
*
* @return \Illuminate\Http\Response
*/
public function index()
public function index(Request $request)
{
$groups = Group::all();

if($request->limit != null) {
$groupIds = UserGroup::take($request->limit)->where('user_id', $request->user_id)->pluck('group_id');
error_log($groupIds);
$groups = Group::whereIn("id", $groupIds)->get();
} else {
$groups = Group::all();
}

foreach ($groups as $group) {
$total = $this->collectCurrentAmount($group->id);
$group->current_balance = $total;
$group->percentage = number_format((float)$total / $group->target_amount, 2, '.', '');

}

return response()->json($groups);
}
Expand All @@ -37,7 +54,30 @@ public function create()
*/
public function store(Request $request)
{
//
$group = Group::create($request->all());

UserGroup::create([
"user_id" => $request->user_id,
"group_id" => $group->id
]);

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

private function collectCurrentAmount($groupId) {
$sessions = Session::where("group_id", $groupId)->get();

$total = 0;

foreach ($sessions as $session) {
$transactions = Transaction::where('session_id', $session->id)->get();

foreach ($transactions as $transaction) {
$total += $transaction->amount;
}
}

return $total;
}

/**
Expand Down
14 changes: 12 additions & 2 deletions app/Http/Controllers/TransactionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Transaction;
use Illuminate\Http\Request;
use DB;

class TransactionController extends Controller
{
Expand All @@ -12,9 +13,18 @@ class TransactionController extends Controller
*
* @return \Illuminate\Http\Response
*/
public function index()
public function index(Request $request)
{
//
$transaction = DB::table('transactions')
->join('users', 'users.id', '=', 'transactions.user_id')
->join('sessions', 'sessions.id', '=', 'transactions.session_id')
->join('groups', 'groups.id', '=', 'sessions.group_id')
->whereMonth('transactions.created_at', '=', $request->month)
->take($request->limit)
->select('users.name as username', 'transactions.amount', 'transactions.created_at', "groups.name", "transactions.isOut as is_out")
->get();

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

/**
Expand Down
4 changes: 3 additions & 1 deletion app/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@

class Transaction extends Model
{
//
public function user() {
return $this->hasOne("users");
}
}
2 changes: 1 addition & 1 deletion app/UserGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

class UserGroup extends Model
{
//
protected $fillable = ["user_id", "group_id"];
}
8 changes: 4 additions & 4 deletions database/migrations/2014_10_12_000000_create_users_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public function up()
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
// $table->string('email')->unique();
// $table->timestamp('email_verified_at')->nullable();
// $table->string('password');
// $table->rememberToken();
$table->timestamps();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function up()
$table->unsignedBigInteger("user_id");
$table->timestamps();

$table->foreign('user_id')->references('id')->on('users');
// $table->foreign('user_id')->references('id')->on('users');
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function up()
$table->string("status")->default("pending");
$table->timestamps();

$table->foreign('user_id')->references('id')->on('users');
// $table->foreign('user_id')->references('id')->on('users');
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public function up()
$table->unsignedBigInteger("group_id");
$table->timestamps();

$table->foreign('user_id')->references('id')->on('users');
$table->foreign('group_id')->references('id')->on('groups');
// $table->foreign('user_id')->references('id')->on('users');
// $table->foreign('group_id')->references('id')->on('groups');
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ public function up()
{
Schema::create('sessions', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string("name");
$table->unsignedBigInteger("group_id");
$table->timestamps();

$table->foreign('group_id')->references('id')->on('groups');
// $table->foreign('group_id')->references('id')->on('groups');
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public function up()
$table->unsignedBigInteger("session_id");
$table->timestamps();

$table->foreign('user_id')->references('id')->on('users');
$table->foreign('session_id')->references('id')->on('sessions');
// $table->foreign('user_id')->references('id')->on('users');
// $table->foreign('session_id')->references('id')->on('sessions');
});
}

Expand Down
126 changes: 126 additions & 0 deletions database/seeds/AllSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php

use Illuminate\Database\Seeder;

class AllSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$faker = Faker\Factory::create();

$dreams = [
"Trip to Japan 2020",
"Buy Motor Bike",
"Buy New Phone",
"Going Umrah",
"Arisan Gang Damai"
];

$amounts = [
32000000,
18000000,
5000000,
40000000,
3000000,
];

$transactions = [
100000,
200000,
300000,
400000,
500000,
600000,
700000,
800000,
900000,
110000,
120000,
130000,
140000,
150000,
160000,
170000,
180000,
190000,
200000,
210000,
220000,
230000,
240000,
250000,
260000,
270000
];

for($i=1; $i<=25; $i++) {
App\User::create([
"name" => $faker->name,
]);

App\Account::create([
"number" => str_random(),
"balance" => 10000000,
"user_id" => $i
]);

if($i % 5 == 0) {
App\Group::create([
"name" => $dreams[$i%4],
"user_id" => $i,
"target_amount" => $amounts[$i%4],
"target_date" => $i . " Oktober 2020",
]);
}
}

$j = 1;
$k = 1;

for($i=1; $i<=25; $i++, $k++) {
App\Session::create([
"name" => "Session " . $k,
"group_id" => $j,
]);

if($i % 5 == 0) {
$j++;
$k = 0;
}
}

$j = 1;

for($i=1; $i<=25; $i++) {
App\UserGroup::create([
"user_id" => $i,
"group_id" => $j,
]);

if($i % 5 == 0) {
$j++;
}
}

$j = 1;

for($i=1; $i<=25; $i++) {
$isOut = $i % 2;
App\Transaction::create([
"user_id" => $j,
"amount" => $transactions[$i],
"isOut" => $isOut,
"session_id" => $i,
]);

if($i % 5 == 0) {
$j++;
}
}
}
}
2 changes: 1 addition & 1 deletion database/seeds/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ class DatabaseSeeder extends Seeder
*/
public function run()
{
// $this->call(UsersTableSeeder::class);
$this->call(AllSeeder::class);
}
}
5 changes: 4 additions & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,7 @@
return $request->user();
});

Route::get("/groups", "GroupController@index");
Route::get("/groups", "GroupController@index");
Route::post("/groups", "GroupController@store");

Route::get('/transactions', "TransactionController@index");

0 comments on commit dad6265

Please sign in to comment.