Skip to content

Commit

Permalink
Merge branch 'develop' into feat/kubosaka/intro-minio-client-go
Browse files Browse the repository at this point in the history
  • Loading branch information
Kubosaka committed Jan 19, 2025
2 parents cad27d3 + cc8d7d0 commit 5825ad2
Show file tree
Hide file tree
Showing 10 changed files with 442 additions and 12 deletions.
81 changes: 81 additions & 0 deletions api/externals/controller/division_controller.go
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
)
140 changes: 140 additions & 0 deletions api/externals/repository/division_repository.go
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())
29 changes: 27 additions & 2 deletions api/generated/openapi_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions api/go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53/go.mod h1:+3IMCy2vIlbG1XG/0ggNQv0SvxCAIpPM5b1nCz56Xno=
github.com/CloudyKit/jet/v6 v6.2.0/go.mod h1:d3ypHeIRNo2+XyqnGA8s+aphtcVpjP5hPwP/Lzo7Ro4=
github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60=
github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
Expand Down Expand Up @@ -48,6 +52,7 @@ github.com/labstack/echo/v4 v4.11.4 h1:vDZmA+qNeh1pd/cCkEicDMrjtrnMGQ1QFI9gWN1zG
github.com/labstack/echo/v4 v4.11.4/go.mod h1:noh7EvLwqDsmh/X/HWKPUl1AjzJrhyptRyEbQJfxen8=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/lib/pq v1.10.1 h1:6VXZrLU0jHBYyAqrSPa+MgPfnSvTPuMgK+k0o5kVFWo=
github.com/lib/pq v1.10.1/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
Expand All @@ -72,6 +77,8 @@ github.com/slack-go/slack v0.13.0 h1:7my/pR2ubZJ9912p9FtvALYpbt0cQPAqkRy2jaSI1PQ
github.com/slack-go/slack v0.13.0/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
Expand Down
4 changes: 4 additions & 0 deletions api/internals/di/di.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func InitializeServer() db.Client {
budgetRepository := repository.NewBudgetRepository(client, crud)
bureauRepository := repository.NewBureauRepository(client, crud)
departmentRepository := repository.NewDepartmentRepository(client, crud)
divisionRepository := repository.NewDivisionRepository(client, crud)
expenseRepository := repository.NewExpenseRepository(client, crud)
festivalItemRepository := repository.NewFestivalItemRepository(client, crud)
financialRecordRepository := repository.NewFinancialRecordRepository(client, crud)
Expand Down Expand Up @@ -68,6 +69,7 @@ func InitializeServer() db.Client {
budgetUseCase := usecase.NewBudgetUseCase(budgetRepository)
bureauUseCase := usecase.NewBureauUseCase(bureauRepository)
departmentUseCase := usecase.NewDepartmentUseCase(departmentRepository)
divisionUseCase := usecase.NewDivisionUseCase(divisionRepository)
expenseUseCase := usecase.NewExpenseUseCase(expenseRepository)
festivalUseCase := usecase.NewFestivalItemUseCase(festivalItemRepository)
financialRecordUseCase := usecase.NewFinancialRecordUseCase(financialRecordRepository)
Expand Down Expand Up @@ -104,6 +106,7 @@ func InitializeServer() db.Client {
budgetController := controller.NewBudgetController(budgetUseCase)
bureauController := controller.NewBureauController(bureauUseCase)
departmentController := controller.NewDepartmentController(departmentUseCase)
divisionController := controller.NewDivisionController(divisionUseCase)
expenseController := controller.NewExpenseController(expenseUseCase)
festivalItemController := controller.NewFestivalItemController(festivalUseCase)
financialRecordController := controller.NewFinancialRecordController(financialRecordUseCase)
Expand Down Expand Up @@ -134,6 +137,7 @@ func InitializeServer() db.Client {
budgetController,
bureauController,
departmentController,
divisionController,
expenseController,
festivalItemController,
financialRecordController,
Expand Down
Loading

0 comments on commit 5825ad2

Please sign in to comment.