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

[feat]購入報告の年度取得api作成 #666

Merged
merged 2 commits into from
Jan 28, 2024
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
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)
}
44 changes: 44 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,46 @@ 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.*,
report_user.*,
purchase_orders.*,
order_user.*
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
Loading