-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[予算管理]局APIの作成 #907
[予算管理]局APIの作成 #907
Changes from 1 commit
8f4b4cd
93c033c
1bf1280
0e05c57
a6b1389
3b8e9bb
2cb678e
f22f3f9
08658b9
38c277b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,7 +5,6 @@ import ( | |
|
||
rep "github.com/NUTFes/FinanSu/api/externals/repository" | ||
"github.com/NUTFes/FinanSu/api/generated" | ||
"github.com/NUTFes/FinanSu/api/internals/domain" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
|
@@ -14,7 +13,8 @@ type financialRecordUseCase struct { | |
} | ||
|
||
type FinancialRecordUseCase interface { | ||
GetFinancialRecords(context.Context) ([]domain.Bureau, error) | ||
GetFinancialRecords(context.Context) (generated.FinancialRecordDetails, error) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [imo]
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 確かに👍 |
||
GetFinancialRecordsByYears(context.Context, string) (generated.FinancialRecordDetails, error) | ||
CreateFinancialRecord( | ||
context.Context, | ||
generated.FinancialRecord, | ||
|
@@ -31,31 +31,111 @@ func NewFinancialRecordUseCase(rep rep.FinancialRecordRepository) FinancialRecor | |
return &financialRecordUseCase{rep} | ||
} | ||
|
||
func (fru *financialRecordUseCase) GetFinancialRecords(c context.Context) ([]domain.Bureau, error) { | ||
bureau := domain.Bureau{} | ||
var bureaus []domain.Bureau | ||
func (fru *financialRecordUseCase) GetFinancialRecords( | ||
c context.Context, | ||
) (generated.FinancialRecordDetails, error) { | ||
var financialRecordDetails generated.FinancialRecordDetails | ||
var financialRecordList []generated.FinancialRecordWithBalance | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
var total generated.Total | ||
|
||
rows, err := fru.rep.All(c) | ||
if err != nil { | ||
return financialRecordDetails, errors.Wrapf(err, "can not connect SQL") | ||
} | ||
|
||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
var financialRecord generated.FinancialRecordWithBalance | ||
err := rows.Scan( | ||
&financialRecord.Id, | ||
&financialRecord.Name, | ||
&financialRecord.Year, | ||
&financialRecord.Budget, | ||
&financialRecord.Expense, | ||
&financialRecord.Balance, | ||
) | ||
|
||
if err != nil { | ||
return financialRecordDetails, errors.Wrapf(err, "can not connect SQL") | ||
} | ||
financialRecordList = append(financialRecordList, financialRecord) | ||
} | ||
|
||
// totalを求める | ||
budgetTotal := 0 | ||
expenseTotal := 0 | ||
balanceTotal := 0 | ||
|
||
for _, financialRecord := range financialRecordList { | ||
budgetTotal += *financialRecord.Budget | ||
expenseTotal += *financialRecord.Expense | ||
balanceTotal += *financialRecord.Balance | ||
} | ||
|
||
total.Budget = &budgetTotal | ||
total.Expense = &expenseTotal | ||
total.Balance = &balanceTotal | ||
|
||
// financialRecordDetails.FinancialRecords = &financialRecordList | ||
financialRecordDetails.Total = &total | ||
financialRecordDetails.FinancialRecords = &financialRecordList | ||
|
||
return financialRecordDetails, err | ||
} | ||
|
||
func (fru *financialRecordUseCase) GetFinancialRecordsByYears( | ||
c context.Context, | ||
year string, | ||
) (generated.FinancialRecordDetails, error) { | ||
var financialRecordDetails generated.FinancialRecordDetails | ||
var financialRecordList []generated.FinancialRecordWithBalance | ||
var total generated.Total | ||
|
||
year := "2021" | ||
//クエリ実行 | ||
rows, err := fru.rep.AllByPeriod(c, year) | ||
if err != nil { | ||
return nil, err | ||
return financialRecordDetails, errors.Wrapf(err, "can not connect SQL") | ||
} | ||
|
||
defer rows.Close() | ||
|
||
for rows.Next() { | ||
var financialRecord generated.FinancialRecordWithBalance | ||
err := rows.Scan( | ||
&bureau.ID, | ||
&bureau.Name, | ||
&bureau.CreatedAt, | ||
&bureau.UpdatedAt, | ||
&financialRecord.Id, | ||
&financialRecord.Name, | ||
&financialRecord.Year, | ||
&financialRecord.Budget, | ||
&financialRecord.Expense, | ||
&financialRecord.Balance, | ||
) | ||
|
||
if err != nil { | ||
return nil, errors.Wrapf(err, "can not connect SQL") | ||
return financialRecordDetails, errors.Wrapf(err, "can not connect SQL") | ||
} | ||
bureaus = append(bureaus, bureau) | ||
financialRecordList = append(financialRecordList, financialRecord) | ||
} | ||
return bureaus, nil | ||
|
||
// totalを求める | ||
budgetTotal := 0 | ||
expenseTotal := 0 | ||
balanceTotal := 0 | ||
|
||
for _, financialRecord := range financialRecordList { | ||
budgetTotal += *financialRecord.Budget | ||
expenseTotal += *financialRecord.Expense | ||
balanceTotal += *financialRecord.Balance | ||
} | ||
|
||
total.Budget = &budgetTotal | ||
total.Expense = &expenseTotal | ||
total.Balance = &balanceTotal | ||
|
||
// financialRecordDetails.FinancialRecords = &financialRecordList | ||
financialRecordDetails.Total = &total | ||
financialRecordDetails.FinancialRecords = &financialRecordList | ||
|
||
return financialRecordDetails, err | ||
} | ||
|
||
func (fru *financialRecordUseCase) CreateFinancialRecord( | ||
|
@@ -113,3 +193,12 @@ func (fru *financialRecordUseCase) DestroyFinancialRecord(c context.Context, id | |
err := fru.rep.Delete(c, id) | ||
return err | ||
} | ||
|
||
type FinancialRecordDetailColumn struct { | ||
Id *string | ||
Name *string | ||
Year *string | ||
Budget *int | ||
Expense *int | ||
Balance *int | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
データがNULLの時に0にしたいため、COALESCEを使いました