-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathInvestmentController.php
More file actions
121 lines (108 loc) · 4.21 KB
/
Copy pathInvestmentController.php
File metadata and controls
121 lines (108 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
namespace App\Http\Controllers;
use App\Http\Requests\InvestmentRequest;
use App\Models\Investment;
use App\Models\Owner;
use App\Repositories\InvestmentRepository;
use App\Repositories\OwnerRepository;
use App\Traits\ApiResponser;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\Validator;
class InvestmentController extends Controller
{
use ApiResponser;
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
try {
$investmentRepo = new InvestmentRepository;
$allInvestments = $investmentRepo->getInvestmentByOwner($request->owner_id);
return $this->success($allInvestments, "Investmento criado com sucesso");
} catch (\Exception $e) {
return $this->error("Houve um erro interno ao buscar a lista de todos os investimentos.", 500, ["detail" => $e->getMessage()]);
}
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
try {
$ownerRepo = new OwnerRepository;
$ownerId = $ownerRepo->setOwner($request->owner_id, $request->owner_name);
$investmentRepo = new InvestmentRepository;
$investmentRepo->setInvestment($request->invesment, $ownerId);
return $this->success($request->input(), "Investmento criado com sucesso");
} catch (\Exception $e) {
return $this->error("Houve um erro interno ao criar um investimento.", 500, ["detail" => $e->getMessage()]);
}
}
/**
* Display the specified resource.
*
* @param int $ownerId
* @return \Illuminate\Http\Response
*/
public function show(int $investmentId)
{
try {
$validator = Validator::make(["investmentId" => $investmentId], [
'investmentId' => 'required|numeric',
]);
if ($validator->fails()) {
return $this->error(
'Falha ao realizar ao consultar um investmento.',
Response::HTTP_BAD_REQUEST,
$validator->errors()
);
}
$investmentRepo = new InvestmentRepository;
$investment = $investmentRepo->getInvestmentById($investmentId);
$investmentGain = $investmentRepo->getValueWithGain($investment);
return $this->success($investmentGain, "Investmento consultado com sucesso");
} catch (\Exception $e) {
return $this->error("Houve um erro interno ao buscar um investimento.", Response::HTTP_INTERNAL_SERVER_ERROR, ["detail" => $e->getMessage()]);
}
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Models\Investment $investment
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Investment $investment)
{
try {
$validator = Validator::make(
[
"investmentId" => $investment->id,
"date" => $request->date
],
[
'investmentId' => 'required|numeric|exists:investments,id',
'date' => 'required'
]);
if ($validator->fails()) {
return $this->error(
'Falha ao realizar a retirada de um investmento.',
Response::HTTP_BAD_REQUEST,
$validator->errors()
);
}
$investmentRepo = new InvestmentRepository;
$investment = $investmentRepo->withdrawalInvestment($investment, $request->date);
return $this->success($investment, "Investmento retirado com sucesso");
} catch (\Exception $e) {
return $this->error("Houve um erro interno ao retirar um investimento.", Response::HTTP_INTERNAL_SERVER_ERROR, ["detail" => $e->getMessage()]);
}
}
}