Skip to content

Commit

Permalink
[feat]購入報告の年度取得api作成
Browse files Browse the repository at this point in the history
  • Loading branch information
Kubosaka committed Dec 12, 2023
1 parent 7307a06 commit c43d4c6
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 0 deletions.
20 changes: 20 additions & 0 deletions api/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,26 @@ const docTemplate = `{
}
},
},
"/purchasereports/details/{year}": {
"get": {
tags: ["purchase_report"],
"description": "年度で指定されたpurchase_reportsに紐づくデータを取得",
"parameters": [
{
"name": "year",
"in": "path",
"description": "year",
"required": true,
"type": "integer"
}
],
"responses": {
"200": {
"description": "年度で指定されたpurchase_reportsに紐づくデータを取得",
}
}
},
},
"/sources": {
"get": {
tags: ["source"],
Expand Down
10 changes: 10 additions & 0 deletions api/externals/controller/purchase_report_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type PurchaseReportController interface {
DestroyPurchaseReport(echo.Context) error
IndexPurchaseReportDetails(echo.Context) error
ShowPurchaseReportDetail(echo.Context) error
IndexPurchaseReportDetailsByYear(echo.Context) error
}

func NewPurchaseReportController(u usecase.PurchaseReportUseCase) PurchaseReportController {
Expand Down Expand Up @@ -99,3 +100,12 @@ func (p *purchaseReportController) ShowPurchaseReportDetail(c echo.Context) erro
}
return c.JSON(http.StatusOK, purchaseReportDetail)
}

func (p *purchaseReportController) IndexPurchaseReportDetailsByYear(c echo.Context) error {
year := c.Param("year")
purchaseReportDetails, err := p.u.GetPurchaseReportDetailsByYear(c.Request().Context(), year)
if err != nil {
return err
}
return c.JSON(http.StatusOK, purchaseReportDetails)
}
69 changes: 69 additions & 0 deletions api/externals/repository/purchase_report_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type PurchaseReportRepository interface {
FindDetail(context.Context, string) (*sql.Row, error)
AllItemInfo(context.Context, string) (*sql.Rows, error)
FindNewRecord(context.Context) (*sql.Row, error)
AllDetailsForPeriods(context.Context, string) (*sql.Rows, error)
}

func NewPurchaseReportRepository(c db.Client, ac abstract.Crud) PurchaseReportRepository {
Expand Down Expand Up @@ -159,3 +160,71 @@ func (ppr *purchaseReportRepository) FindNewRecord(c context.Context) (*sql.Row,
query := "SELECT * FROM purchase_reports ORDER BY id DESC LIMIT 1"
return ppr.crud.ReadByID(c, query)
}


// Purchase_reportに紐づく、Purchase_orderからPurchase_itemsの取得
func (ppr *purchaseReportRepository) AllDetailsForPeriods(c context.Context, year string) (*sql.Rows, error) {
query := `
SELECT
purchase_reports.id,
purchase_reports.user_id,
purchase_reports.discount,
purchase_reports.addition,
purchase_reports.finance_check,
purchase_reports.purchase_order_id,
purchase_reports.remark,
purchase_reports.buyer,
purchase_reports.created_at,
purchase_reports.updated_at,
report_user.id,
report_user.name,
report_user.bureau_id,
report_user.role_id,
report_user.created_at,
report_user.updated_at,
purchase_orders.id,
purchase_orders.deadline,
purchase_orders.user_id,
purchase_orders.expense_id,
purchase_orders.finance_check,
purchase_orders.created_at,
purchase_orders.updated_at,
order_user.id,
order_user.name,
order_user.bureau_id,
order_user.role_id,
order_user.created_at,
order_user.updated_at
FROM
purchase_reports
INNER JOIN
users
AS
report_user
ON
purchase_reports.user_id = report_user.id
INNER JOIN
purchase_orders
ON
purchase_reports.purchase_order_id = purchase_orders.id
INNER JOIN
users
AS
order_user
ON
purchase_orders.user_id = order_user.id
INNER JOIN
year_periods
ON
purchase_reports.created_at > year_periods.started_at
AND
purchase_reports.created_at < year_periods.ended_at
INNER JOIN
years
ON
year_periods.year_id = years.id
WHERE
years.year = ` + year +
" ORDER BY purchase_reports.id"
return ppr.crud.Read(c, query)
}
71 changes: 71 additions & 0 deletions api/internals/usecase/purchase_report_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type PurchaseReportUseCase interface {
DestroyPurchaseReport(context.Context, string) error
GetPurchaseReportDetails(context.Context) ([]domain.PurchaseReportDetails, error)
GetPurchaseReportDetailByID(context.Context, string) (domain.PurchaseReportDetails, error)
GetPurchaseReportDetailsByYear(context.Context, string) ([]domain.PurchaseReportDetails, error)
}

func NewPurchaseReportUseCase(rep rep.PurchaseReportRepository) PurchaseReportUseCase {
Expand Down Expand Up @@ -285,3 +286,73 @@ func (p *purchaseReportUseCase) GetPurchaseReportDetailByID(c context.Context, i
return purchaseReportDetail, nil
}

//Purchase_reportに紐づく、Purchase_orderからPurchase_itemsの取得(GETS)
func (p *purchaseReportUseCase) GetPurchaseReportDetailsByYear(c context.Context, year string) ([]domain.PurchaseReportDetails, error) {
purchaseReportDetail:= domain.PurchaseReportDetails{}
var purchaseReportDetails []domain.PurchaseReportDetails
purchaseItem := domain.PurchaseItem{}
var purchaseItems []domain.PurchaseItem
rows, err := p.rep.AllDetailsForPeriods(c, year)
if err != nil {
return nil, err
}
for rows.Next() {
err := rows.Scan(
&purchaseReportDetail.PurchaseReport.ID,
&purchaseReportDetail.PurchaseReport.UserID,
&purchaseReportDetail.PurchaseReport.Discount,
&purchaseReportDetail.PurchaseReport.Addition,
&purchaseReportDetail.PurchaseReport.FinanceCheck,
&purchaseReportDetail.PurchaseReport.PurchaseOrderID,
&purchaseReportDetail.PurchaseReport.Remark,
&purchaseReportDetail.PurchaseReport.Buyer,
&purchaseReportDetail.PurchaseReport.CreatedAt,
&purchaseReportDetail.PurchaseReport.UpdatedAt,
&purchaseReportDetail.ReportUser.ID,
&purchaseReportDetail.ReportUser.Name,
&purchaseReportDetail.ReportUser.BureauID,
&purchaseReportDetail.ReportUser.RoleID,
&purchaseReportDetail.ReportUser.CreatedAt,
&purchaseReportDetail.ReportUser.UpdatedAt,
&purchaseReportDetail.PurchaseOrder.ID,
&purchaseReportDetail.PurchaseOrder.DeadLine,
&purchaseReportDetail.PurchaseOrder.UserID,
&purchaseReportDetail.PurchaseOrder.ExpenseID,
&purchaseReportDetail.PurchaseOrder.FinanceCheck,
&purchaseReportDetail.PurchaseOrder.CreatedAt,
&purchaseReportDetail.PurchaseOrder.UpdatedAt,
&purchaseReportDetail.OrderUser.ID,
&purchaseReportDetail.OrderUser.Name,
&purchaseReportDetail.OrderUser.BureauID,
&purchaseReportDetail.OrderUser.RoleID,
&purchaseReportDetail.OrderUser.CreatedAt,
&purchaseReportDetail.OrderUser.UpdatedAt,
)
if err != nil {
return nil, err
}
rows, err := p.rep.AllItemInfo(c, strconv.Itoa(int(purchaseReportDetail.PurchaseReport.PurchaseOrderID)))
for rows.Next() {
err := rows.Scan(
&purchaseItem.ID,
&purchaseItem.Item,
&purchaseItem.Price,
&purchaseItem.Quantity,
&purchaseItem.Detail,
&purchaseItem.Url,
&purchaseItem.PurchaseOrderID,
&purchaseItem.FinanceCheck,
&purchaseItem.CreatedAt,
&purchaseItem.UpdatedAt,
)
if err != nil {
return nil, err
}
purchaseItems = append(purchaseItems, purchaseItem)
}
purchaseReportDetail.PurchaseItems = purchaseItems
purchaseReportDetails = append(purchaseReportDetails, purchaseReportDetail)
purchaseItems = nil
}
return purchaseReportDetails, nil
}
1 change: 1 addition & 0 deletions api/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func (r router) ProvideRouter(e *echo.Echo) {
e.DELETE("/purchasereports/:id", r.purchaseReportController.DestroyPurchaseReport)
e.GET("/purchasereports/details", r.purchaseReportController.IndexPurchaseReportDetails)
e.GET("/purchasereports/:id/details", r.purchaseReportController.ShowPurchaseReportDetail)
e.GET("/purchasereports/details/:year", r.purchaseReportController.IndexPurchaseReportDetailsByYear)

// sources
e.GET("/sources", r.sourceController.IndexSource)
Expand Down

0 comments on commit c43d4c6

Please sign in to comment.