Skip to content

Commit

Permalink
[feat] create divisions api
Browse files Browse the repository at this point in the history
  • Loading branch information
hikahana committed Jan 14, 2025
1 parent 8827803 commit db76c49
Show file tree
Hide file tree
Showing 5 changed files with 384 additions and 0 deletions.
82 changes: 82 additions & 0 deletions api/externals/controller/division_controller.go
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 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")
var divisionDetails divisionDetails

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
)
142 changes: 142 additions & 0 deletions api/externals/repository/division_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
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 := baseGetQuery

if year != "" {
ds = ds.Where(goqu.C("years.year").Eq(year))
}

if financialRecordId != "" {
ds = ds.Where(goqu.C("financial_records.id").Eq(financialRecordId))
}

// クエリを構築し、SQLを生成
query, _, err := ds.GroupBy("divisions.id").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 := baseGetQuery.
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 := baseGetQuery
query, _, err := ds.Limit(1).ToSQL()

if err != nil {
return nil, err
}
return dr.crud.ReadByID(c, query)
}

// Note: この中のみで呼ぶために小文字宣言
type division = generated.Division

// NOTE: getの共通部分抜き出し
var baseGetQuery = 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.COALESCE(goqu.L("SUM(item_budgets.amount) - 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())
4 changes: 4 additions & 0 deletions api/internals/di/di.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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)
financialRecordRepository := repository.NewFinancialRecordRepository(client, crud)
fundInformationRepository := repository.NewFundInformationRepository(client, crud)
Expand Down Expand Up @@ -57,6 +58,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)
financialRecordUseCase := usecase.NewFinancialRecordUseCase(financialRecordRepository)
fundInformationUseCase := usecase.NewFundInformationUseCase(fundInformationRepository)
Expand Down Expand Up @@ -91,6 +93,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)
financialRecordController := controller.NewFinancialRecordController(financialRecordUseCase)
fundInformationController := controller.NewFundInformationController(fundInformationUseCase)
Expand Down Expand Up @@ -119,6 +122,7 @@ func InitializeServer() db.Client {
budgetController,
bureauController,
departmentController,
divisionController,
expenseController,
financialRecordController,
fundInformationController,
Expand Down
Loading

0 comments on commit db76c49

Please sign in to comment.