-
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
Merged
Merged
[予算管理]局APIの作成 #907
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
8f4b4cd
openapi修正
Kubosaka 93c033c
コントローラー・ユースケース・リポジトリ作成
Kubosaka 1bf1280
post作成
Kubosaka 0e05c57
put作成
Kubosaka a6b1389
delete作成
Kubosaka 3b8e9bb
get全件データの取得
Kubosaka 2cb678e
postとputに金額カラム追加
Kubosaka f22f3f9
不要なコード削除
Kubosaka 08658b9
型名省略
Kubosaka 38c277b
変数名修正financialRecordList
Kubosaka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package controller | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/NUTFes/FinanSu/api/generated" | ||
"github.com/NUTFes/FinanSu/api/internals/usecase" | ||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
type financialRecordController struct { | ||
u usecase.FinancialRecordUseCase | ||
} | ||
|
||
type FinancialRecordController interface { | ||
IndexFinancialRecords(echo.Context) error | ||
CreateFinancialRecord(echo.Context) error | ||
UpdateFinancialRecord(echo.Context) error | ||
DestroyFinancialRecord(echo.Context) error | ||
} | ||
|
||
func NewFinancialRecordController(u usecase.FinancialRecordUseCase) FinancialRecordController { | ||
return &financialRecordController{u} | ||
} | ||
|
||
func (f *financialRecordController) IndexFinancialRecords(c echo.Context) error { | ||
year := c.QueryParam("year") | ||
var financialRecordDetails generated.FinancialRecordDetails | ||
var err error | ||
|
||
if year != "" { | ||
financialRecordDetails, err = f.u.GetFinancialRecordsByYears(c.Request().Context(), year) | ||
if err != nil { | ||
return err | ||
} | ||
return c.JSON(http.StatusOK, financialRecordDetails) | ||
} | ||
|
||
financialRecordDetails, err = f.u.GetFinancialRecords(c.Request().Context()) | ||
if err != nil { | ||
return err | ||
} | ||
return c.JSON(http.StatusOK, financialRecordDetails) | ||
} | ||
|
||
func (f *financialRecordController) CreateFinancialRecord(c echo.Context) error { | ||
financialRecord := new(generated.FinancialRecord) | ||
if err := c.Bind(financialRecord); err != nil { | ||
return c.String(http.StatusBadRequest, "Bad Request") | ||
} | ||
latastFinancialRecord, err := f.u.CreateFinancialRecord(c.Request().Context(), *financialRecord) | ||
if err != nil { | ||
return err | ||
} | ||
return c.JSON(http.StatusOK, latastFinancialRecord) | ||
} | ||
|
||
func (f *financialRecordController) UpdateFinancialRecord(c echo.Context) error { | ||
id := c.Param("id") | ||
financialRecord := new(generated.FinancialRecord) | ||
if err := c.Bind(financialRecord); err != nil { | ||
return c.String(http.StatusBadRequest, "Bad Request") | ||
} | ||
updatedFinancialRecord, err := f.u.UpdateFinancialRecord( | ||
c.Request().Context(), | ||
id, | ||
*financialRecord, | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
return c.JSON(http.StatusOK, updatedFinancialRecord) | ||
} | ||
|
||
func (f *financialRecordController) DestroyFinancialRecord(c echo.Context) error { | ||
id := c.Param("id") | ||
err := f.u.DestroyFinancialRecord(c.Request().Context(), id) | ||
if err != nil { | ||
return err | ||
} | ||
return c.String(http.StatusOK, "Destroy FinancialRecord") | ||
} |
175 changes: 175 additions & 0 deletions
175
api/externals/repository/financial_record_repository.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
package repository | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
|
||
"github.com/NUTFes/FinanSu/api/drivers/db" | ||
"github.com/NUTFes/FinanSu/api/externals/repository/abstract" | ||
"github.com/NUTFes/FinanSu/api/generated" | ||
goqu "github.com/doug-martin/goqu/v9" | ||
) | ||
|
||
type financialRecordRepository struct { | ||
client db.Client | ||
crud abstract.Crud | ||
} | ||
|
||
type FinancialRecordRepository interface { | ||
All(context.Context) (*sql.Rows, error) | ||
AllByPeriod(context.Context, string) (*sql.Rows, error) | ||
GetById(context.Context, string) (*sql.Row, error) | ||
Create(context.Context, generated.FinancialRecord) error | ||
Update(context.Context, string, generated.FinancialRecord) error | ||
Delete(context.Context, string) error | ||
FindLatestRecord(context.Context) (*sql.Row, error) | ||
} | ||
|
||
func NewFinancialRecordRepository(c db.Client, ac abstract.Crud) FinancialRecordRepository { | ||
return &financialRecordRepository{c, ac} | ||
} | ||
|
||
// 年度別に取得 | ||
func (frr *financialRecordRepository) All( | ||
c context.Context, | ||
) (*sql.Rows, error) { | ||
query, _, err := dialect.Select( | ||
"financial_records.id", | ||
"financial_records.name", "years.year", | ||
goqu.COALESCE(goqu.SUM("item_budgets.amount"), 0).As("budget"), | ||
goqu.COALESCE(goqu.SUM("buy_reports.amount"), 0).As("expense"), | ||
goqu.COALESCE(goqu.L("SUM(item_budgets.amount) - SUM(buy_reports.amount)"), 0).As("balance")). | ||
From("financial_records"). | ||
InnerJoin(goqu.I("years"), goqu.On(goqu.I("financial_records.year_id").Eq(goqu.I("years.id")))). | ||
LeftJoin(goqu.I("divisions"), goqu.On(goqu.I("financial_records.id").Eq(goqu.I("divisions.financial_record_id")))). | ||
LeftJoin(goqu.I("festival_items"), goqu.On(goqu.I("divisions.id").Eq(goqu.I("festival_items.division_id")))). | ||
LeftJoin(goqu.I("item_budgets"), goqu.On(goqu.I("festival_items.id").Eq(goqu.I("item_budgets.festival_item_id")))). | ||
LeftJoin(goqu.I("buy_reports"), goqu.On(goqu.I("festival_items.id").Eq(goqu.I("buy_reports.festival_item_id")))). | ||
GroupBy("financial_records.id"). | ||
ToSQL() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
return frr.crud.Read(c, query) | ||
} | ||
|
||
// 年度別に取得 | ||
func (frr *financialRecordRepository) AllByPeriod( | ||
c context.Context, | ||
year string, | ||
) (*sql.Rows, error) { | ||
query, _, err := dialect.Select( | ||
"financial_records.id", | ||
"financial_records.name", "years.year", | ||
goqu.COALESCE(goqu.SUM("item_budgets.amount"), 0).As("budget"), | ||
goqu.COALESCE(goqu.SUM("buy_reports.amount"), 0).As("expense"), | ||
goqu.COALESCE(goqu.L("SUM(item_budgets.amount) - SUM(buy_reports.amount)"), 0).As("balance")). | ||
From("financial_records"). | ||
InnerJoin(goqu.I("years"), goqu.On(goqu.I("financial_records.year_id").Eq(goqu.I("years.id")))). | ||
LeftJoin(goqu.I("divisions"), goqu.On(goqu.I("financial_records.id").Eq(goqu.I("divisions.financial_record_id")))). | ||
LeftJoin(goqu.I("festival_items"), goqu.On(goqu.I("divisions.id").Eq(goqu.I("festival_items.division_id")))). | ||
LeftJoin(goqu.I("item_budgets"), goqu.On(goqu.I("festival_items.id").Eq(goqu.I("item_budgets.festival_item_id")))). | ||
LeftJoin(goqu.I("buy_reports"), goqu.On(goqu.I("festival_items.id").Eq(goqu.I("buy_reports.festival_item_id")))). | ||
GroupBy("financial_records.id"). | ||
Where(goqu.Ex{"years.year": year}). | ||
ToSQL() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return frr.crud.Read(c, query) | ||
} | ||
|
||
// IDで取得 | ||
func (frr *financialRecordRepository) GetById( | ||
c context.Context, | ||
id string, | ||
) (*sql.Row, error) { | ||
query, _, err := dialect.Select( | ||
"financial_records.id", | ||
"financial_records.name", "years.year", | ||
goqu.COALESCE(goqu.SUM("item_budgets.amount"), 0).As("budget"), | ||
goqu.COALESCE(goqu.SUM("buy_reports.amount"), 0).As("expense"), | ||
goqu.COALESCE(goqu.L("SUM(item_budgets.amount) - SUM(buy_reports.amount)"), 0).As("balance")). | ||
From("financial_records"). | ||
InnerJoin(goqu.I("years"), goqu.On(goqu.I("financial_records.year_id").Eq(goqu.I("years.id")))). | ||
LeftJoin(goqu.I("divisions"), goqu.On(goqu.I("financial_records.id").Eq(goqu.I("divisions.financial_record_id")))). | ||
LeftJoin(goqu.I("festival_items"), goqu.On(goqu.I("divisions.id").Eq(goqu.I("festival_items.division_id")))). | ||
LeftJoin(goqu.I("item_budgets"), goqu.On(goqu.I("festival_items.id").Eq(goqu.I("item_budgets.festival_item_id")))). | ||
LeftJoin(goqu.I("buy_reports"), goqu.On(goqu.I("festival_items.id").Eq(goqu.I("buy_reports.festival_item_id")))). | ||
GroupBy("financial_records.id"). | ||
Where(goqu.Ex{"financial_records.id": id}). | ||
ToSQL() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return frr.crud.ReadByID(c, query) | ||
} | ||
|
||
// 作成 | ||
func (frr *financialRecordRepository) Create( | ||
c context.Context, | ||
financialRecord generated.FinancialRecord, | ||
) error { | ||
ds := dialect.Insert("financial_records"). | ||
Rows(goqu.Record{"name": financialRecord.Name, "year_id": financialRecord.YearId}) | ||
query, _, err := ds.ToSQL() | ||
if err != nil { | ||
return err | ||
} | ||
return frr.crud.UpdateDB(c, query) | ||
} | ||
|
||
// 編集 | ||
func (frr *financialRecordRepository) Update( | ||
c context.Context, | ||
id string, | ||
financialRecord generated.FinancialRecord, | ||
) error { | ||
ds := dialect.Update("financial_records"). | ||
Set(goqu.Record{"name": financialRecord.Name, "year_id": financialRecord.YearId}). | ||
Where(goqu.Ex{"id": id}) | ||
query, _, err := ds.ToSQL() | ||
if err != nil { | ||
return err | ||
} | ||
return frr.crud.UpdateDB(c, query) | ||
} | ||
|
||
// 削除 | ||
func (frr *financialRecordRepository) Delete( | ||
c context.Context, | ||
id string, | ||
) error { | ||
ds := dialect.Delete("financial_records").Where(goqu.Ex{"id": id}) | ||
query, _, err := ds.ToSQL() | ||
if err != nil { | ||
return err | ||
} | ||
return frr.crud.UpdateDB(c, query) | ||
|
||
} | ||
|
||
// 最新のsponcerを取得する | ||
func (frr *financialRecordRepository) FindLatestRecord(c context.Context) (*sql.Row, error) { | ||
query, _, err := dialect.Select( | ||
"financial_records.id", | ||
"financial_records.name", "years.year", | ||
goqu.COALESCE(goqu.SUM("item_budgets.amount"), 0).As("budget"), | ||
goqu.COALESCE(goqu.SUM("buy_reports.amount"), 0).As("expense"), | ||
goqu.COALESCE(goqu.L("SUM(item_budgets.amount) - SUM(buy_reports.amount)"), 0).As("balance")). | ||
From("financial_records"). | ||
InnerJoin(goqu.I("years"), goqu.On(goqu.I("financial_records.year_id").Eq(goqu.I("years.id")))). | ||
LeftJoin(goqu.I("divisions"), goqu.On(goqu.I("financial_records.id").Eq(goqu.I("divisions.financial_record_id")))). | ||
LeftJoin(goqu.I("festival_items"), goqu.On(goqu.I("divisions.id").Eq(goqu.I("festival_items.division_id")))). | ||
LeftJoin(goqu.I("item_budgets"), goqu.On(goqu.I("festival_items.id").Eq(goqu.I("item_budgets.festival_item_id")))). | ||
LeftJoin(goqu.I("buy_reports"), goqu.On(goqu.I("festival_items.id").Eq(goqu.I("buy_reports.festival_item_id")))). | ||
GroupBy("financial_records.id"). | ||
Order(goqu.I("id").Desc()). | ||
Limit(1). | ||
ToSQL() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return frr.crud.ReadByID(c, query) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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を使いました