-
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
局divisionAPIの作成 #909
Merged
Merged
局divisionAPIの作成 #909
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c3b1f7c
Merge branch 'feat/kubosaka/create-api-financial-record' of github:NU…
hikahana 4dc99d0
Merge branch 'develop' of github:NUTFes/FinanSu into feat/hikahana/90…
hikahana aaea9a1
[fix] divisions post parameter
hikahana e6ebc8e
[fix] generated
hikahana 8827803
[fix] go.mod
hikahana db76c49
[feat] create divisions api
hikahana 422b9d7
[fix] 型名の修正&row.Scan修正
hikahana 62d483c
[fix] 型名修正
hikahana e9985c8
[fix] query修正
hikahana 96b7e67
[fix] 不要な宣言の削除
hikahana 1b4fcc3
[fix] ログの削除
hikahana 827a0c4
[fix] クエリ修正&アッパーキャメルに統一
hikahana ef31134
[fix] タイポ修正
hikahana a1d20f1
[fix] division parameterをキャメルケースに修正
hikahana 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,81 @@ | ||
package controller | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/NUTFes/FinanSu/api/generated" | ||
"github.com/NUTFes/FinanSu/api/internals/usecase" | ||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
type divisionController struct { | ||
u usecase.DivisionUseCase | ||
} | ||
|
||
type DivisionController interface { | ||
IndexDivisions(echo.Context) error | ||
CreateDivision(echo.Context) error | ||
UpdateDivision(echo.Context) error | ||
DestroyDivision(echo.Context) error | ||
} | ||
|
||
func NewDivisionController(u usecase.DivisionUseCase) DivisionController { | ||
return &divisionController{u} | ||
} | ||
|
||
func (d *divisionController) IndexDivisions(c echo.Context) error { | ||
ctx := c.Request().Context() | ||
year := c.QueryParam("year") | ||
financialRecordId := c.QueryParam("financial_record_id") | ||
|
||
divisionDetails, err := d.u.GetDivisions(ctx, year, financialRecordId) | ||
if err != nil { | ||
return err | ||
} | ||
return c.JSON(http.StatusOK, divisionDetails) | ||
} | ||
|
||
func (d *divisionController) CreateDivision(c echo.Context) error { | ||
ctx := c.Request().Context() | ||
division := new(Division) | ||
|
||
if err := c.Bind(division); err != nil { | ||
return c.String(http.StatusBadRequest, "Bad Request") | ||
} | ||
latestDivision, err := d.u.CreateDivision(ctx, *division) | ||
if err != nil { | ||
return err | ||
} | ||
return c.JSON(http.StatusOK, latestDivision) | ||
} | ||
|
||
func (d *divisionController) UpdateDivision(c echo.Context) error { | ||
ctx := c.Request().Context() | ||
id := c.Param("id") | ||
division := new(Division) | ||
|
||
if err := c.Bind(division); err != nil { | ||
return c.String(http.StatusBadRequest, "Bad Request") | ||
} | ||
updatedDivision, err := d.u.UpdateDivision(ctx, id, *division) | ||
if err != nil { | ||
return err | ||
} | ||
return c.JSON(http.StatusOK, updatedDivision) | ||
} | ||
|
||
func (d *divisionController) DestroyDivision(c echo.Context) error { | ||
ctx := c.Request().Context() | ||
id := c.Param("id") | ||
|
||
err := d.u.DestroyDivision(ctx, id) | ||
if err != nil { | ||
return err | ||
} | ||
return c.String(http.StatusOK, "Destroy Division") | ||
} | ||
|
||
type ( | ||
Division = generated.Division | ||
DivisionDetails = generated.DivisionDetails | ||
) |
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,140 @@ | ||
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 divisionRepository struct { | ||
client db.Client | ||
crud abstract.Crud | ||
} | ||
|
||
type DivisionRepository interface { | ||
AllByPeriodAndFinancialRecord(context.Context, string, string) (*sql.Rows, error) | ||
GetById(context.Context, string) (*sql.Row, error) | ||
Create(context.Context, Division) error | ||
Update(context.Context, string, Division) error | ||
Delete(context.Context, string) error | ||
FindLatestRecord(context.Context) (*sql.Row, error) | ||
} | ||
|
||
func NewDivisionRepository(c db.Client, ac abstract.Crud) DivisionRepository { | ||
return &divisionRepository{c, ac} | ||
} | ||
|
||
// 年度別と財務記録で取得 | ||
func (dr *divisionRepository) AllByPeriodAndFinancialRecord( | ||
c context.Context, | ||
year string, | ||
financialRecordId string, | ||
) (*sql.Rows, error) { | ||
|
||
ds := selectDivisionQuery | ||
|
||
if year != "" { | ||
ds = ds.Where(goqu.Ex{"years.year": year}) | ||
} | ||
if financialRecordId != "" { | ||
ds = ds.Where(goqu.Ex{"financial_records.id": financialRecordId}) | ||
} | ||
|
||
// クエリを構築し、SQLを生成 | ||
query, _, err := ds.ToSQL() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return dr.crud.Read(c, query) | ||
} | ||
|
||
// IDで取得 | ||
func (dr *divisionRepository) GetById( | ||
c context.Context, | ||
id string, | ||
) (*sql.Row, error) { | ||
ds, _, err := selectDivisionQuery. | ||
Where(goqu.Ex{"divisions.id": id}). | ||
ToSQL() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return dr.crud.ReadByID(c, ds) | ||
} | ||
|
||
// 部門作成 | ||
func (dr *divisionRepository) Create( | ||
c context.Context, | ||
division Division, | ||
) error { | ||
ds := dialect.Insert("divisions"). | ||
Rows(goqu.Record{"name": division.Name, "financial_record_id": division.FinancialRecordID}) | ||
query, _, err := ds.ToSQL() | ||
if err != nil { | ||
return err | ||
} | ||
return dr.crud.UpdateDB(c, query) | ||
} | ||
|
||
// 部門更新 | ||
func (dr *divisionRepository) Update( | ||
c context.Context, | ||
id string, | ||
division Division, | ||
) error { | ||
ds := dialect.Update("divisions"). | ||
Set(goqu.Record{"name": division.Name, "financial_record_id": division.FinancialRecordID}). | ||
Where(goqu.Ex{"id": id}) | ||
query, _, err := ds.ToSQL() | ||
if err != nil { | ||
return err | ||
} | ||
return dr.crud.UpdateDB(c, query) | ||
} | ||
|
||
// 部門削除 | ||
func (dr *divisionRepository) Delete( | ||
c context.Context, | ||
id string, | ||
) error { | ||
ds := dialect.Delete("divisions").Where(goqu.Ex{"id": id}) | ||
query, _, err := ds.ToSQL() | ||
if err != nil { | ||
return err | ||
} | ||
return dr.crud.UpdateDB(c, query) | ||
} | ||
|
||
// 最新の部門を取得する | ||
func (dr *divisionRepository) FindLatestRecord(c context.Context) (*sql.Row, error) { | ||
ds := selectDivisionQuery | ||
query, _, err := ds.Limit(1).ToSQL() | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
return dr.crud.ReadByID(c, query) | ||
} | ||
|
||
type Division = generated.Division | ||
|
||
// NOTE: getの共通部分抜き出し | ||
var selectDivisionQuery = dialect.From("divisions"). | ||
Select( | ||
"divisions.id", | ||
"divisions.name", | ||
"financial_records.name", | ||
goqu.COALESCE(goqu.SUM("item_budgets.amount"), 0).As("budget"), | ||
goqu.COALESCE(goqu.SUM("buy_reports.amount"), 0).As("expense"), | ||
goqu.L("COALESCE(SUM(item_budgets.amount), 0) - COALESCE(SUM(buy_reports.amount), 0)").As("balance")). | ||
InnerJoin(goqu.I("financial_records"), goqu.On(goqu.I("financial_records.id").Eq(goqu.I("divisions.financial_record_id")))). | ||
InnerJoin(goqu.I("years"), goqu.On(goqu.I("financial_records.year_id").Eq(goqu.I("years.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(goqu.I("divisions.id")). | ||
Order(goqu.I("divisions.id").Desc()) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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.
[must]これopenapiとクエリパラメータとどっちか揃えてください!