Skip to content
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 10 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions api/externals/controller/financial_record_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 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 api/externals/repository/financial_record_repository.go
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")).
Comment on lines +39 to +41
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

データがNULLの時に0にしたいため、COALESCEを使いました

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)
}
71 changes: 43 additions & 28 deletions api/generated/openapi_gen.go

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

Loading
Loading