Skip to content

Commit

Permalink
♻️ Add the TransactionType
Browse files Browse the repository at this point in the history
  • Loading branch information
wjuniorbh92 committed Aug 23, 2023
1 parent 756d2ac commit 8670ad3
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 12 deletions.
7 changes: 7 additions & 0 deletions backend/docs/docs.go

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

7 changes: 7 additions & 0 deletions backend/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,13 @@
"in": "path",
"required": true
},
{
"type": "string",
"description": "Transaction type (e,g, '0', '1')",
"name": "transaction_type",
"in": "path",
"required": true
},
{
"type": "string",
"description": "Time range for the query (e.g., '24h', ",
Expand Down
5 changes: 5 additions & 0 deletions backend/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,11 @@ paths:
name: asset_id
required: true
type: integer
- description: Transaction type (e,g, '0', '1')
in: path
name: transaction_type
required: true
type: string
- description: 'Time range for the query (e.g., ''24h'', '
in: path
name: time_range
Expand Down
16 changes: 14 additions & 2 deletions backend/internal/controller/http/v1/log_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func newLogTransactionsRoutes(handler *gin.RouterGroup, w usecase.WalletUseCase,
h.GET("/assets/:asset_id/:time_range", r.getLogTransactionsByAssetID)
h.GET("/user/:user_id/:time_range", r.getLogTransactionsByUserID)
h.GET("/transaction_type/:transaction_type_id/:time_range", r.getLogTransactionsByTransactionTypeID)
h.GET("/assets/:asset_id/sum/:time_range/:time_frame", r.sumAmountsByAssetID)
h.GET("/assets/:asset_id/type/:transaction_type_id/sum/:time_range/:time_frame", r.sumAmountsByAssetID)
h.GET("/assets/sum/:time_range/:time_frame", r.sumAmountsForAllAssets)
}
}
Expand Down Expand Up @@ -142,6 +142,7 @@ func (r *logTransactionsRoutes) getLogTransactionsByTransactionTypeID(c *gin.Con
// @Accept json
// @Produce json
// @Param asset_id path int true "Asset ID"
// @Param transaction_type path string true "Transaction type (e,g, '0', '1')"
// @Param time_range path string true "Time range for the query (e.g., '24h', "7d")"
// @Param time_frame path string false "Time frame for the query (e.g., '1h')".
// @Security ApiKeyAuth
Expand All @@ -151,9 +152,20 @@ func (r *logTransactionsRoutes) getLogTransactionsByTransactionTypeID(c *gin.Con
// @Router /log_transactions/asset/{asset_id}/sum/{time_range}/{time_frame} [get]
func (r *logTransactionsRoutes) sumAmountsByAssetID(c *gin.Context) {
assetIDStr := c.Param("asset_id")
transactionTypeStr := c.Param("transaction_type_id")
timeRange := c.Param("time_range")
timeFrame := c.Param("time_frame")

if transactionTypeStr == "" {
transactionTypeStr = "0"
}

// Convert transactionTypeStr to integer
transactionType, err := strconv.Atoi(transactionTypeStr)
if err != nil {
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid transaction type: %s", err.Error()), err)
return
}
duration, err := time.ParseDuration(timeFrame)
if err != nil {
errorResponse(c, http.StatusBadRequest, "Invalid time_frame format", err)
Expand All @@ -166,7 +178,7 @@ func (r *logTransactionsRoutes) sumAmountsByAssetID(c *gin.Context) {
return
}

sum, err := r.l.SumLogTransactionsByAssetID(assetID, timeRange, duration)
sum, err := r.l.SumLogTransactionsByAssetID(assetID, timeRange, duration, transactionType)
if err != nil {
errorResponse(c, http.StatusInternalServerError, fmt.Sprintf("error getting log transactions: %s", err.Error()), err)
return
Expand Down
2 changes: 1 addition & 1 deletion backend/internal/usecase/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ type (
GetLogTransactionsByAssetID(assetID int, timeRange string) ([]entity.LogTransaction, error)
GetLogTransactionsByUserID(userID int, timeRange string) ([]entity.LogTransaction, error)
GetLogTransactionsByTransactionTypeID(transactionTypeID int, timeRange string) ([]entity.LogTransaction, error)
SumLogTransactionsByAssetID(assetID int, timeRange string, timeFrame time.Duration) (entity.SumLogTransaction, error)
SumLogTransactionsByAssetID(assetID int, timeRange string, timeFrame time.Duration, transactionType int) (entity.SumLogTransaction, error)
SumLogTransactions(timeRange string, timeFrame time.Duration) ([]entity.SumLogTransaction, error)
}
)
4 changes: 2 additions & 2 deletions backend/internal/usecase/log_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func (l *LogTransactionUseCase) SumLogTransactions(timeRange string, timeFrame t
return sum, nil
}

func (l *LogTransactionUseCase) SumLogTransactionsByAssetID(assetID int, timeRange string, duration time.Duration) (entity.SumLogTransaction, error) {
sum, err := l.lRepo.SumLogTransactionsByAssetID(assetID, timeRange, duration)
func (l *LogTransactionUseCase) SumLogTransactionsByAssetID(assetID int, timeRange string, duration time.Duration, transactionType int) (entity.SumLogTransaction, error) {
sum, err := l.lRepo.SumLogTransactionsByAssetID(assetID, timeRange, duration, transactionType)
if err != nil {
return entity.SumLogTransaction{}, err
}
Expand Down
29 changes: 22 additions & 7 deletions backend/internal/usecase/repo/log_transaction_postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (repo *LogTransactionRepo) GetLogTransactionsByTransactionTypeID(transactio
return getLogTransactions(repo, timeRange, "WHERE transaction_type_id = $2")
}

func (repo *LogTransactionRepo) SumLogTransactionsByAssetID(assetID int, timeRange string, timeFrame time.Duration) (entity.SumLogTransaction, error) {
func (repo *LogTransactionRepo) SumLogTransactionsByAssetID(assetID int, timeRange string, timeFrame time.Duration, transactionType int) (entity.SumLogTransaction, error) {
dateFilter, err := getDateFilter(timeRange)
if err != nil {
return entity.SumLogTransaction{}, err
Expand All @@ -53,18 +53,33 @@ func (repo *LogTransactionRepo) SumLogTransactionsByAssetID(assetID int, timeRan
timeFrameUnit := getTimeFrame(timeFrame)
timeFrameSeconds := timeFrame.Seconds()

query := `
baseQuery := `
SELECT
a.id, a.name, a.code, a.asset_type, SUM(lt.amount),
DATE_TRUNC($3, TIMESTAMP 'epoch' + INTERVAL '1 second' * floor(EXTRACT(EPOCH FROM lt.date)/$4) * $4) as dateFrame
FROM logtransactions AS lt
JOIN asset AS a ON lt.asset_id = a.id
WHERE lt.date >= $1 AND lt.asset_id = $2
GROUP BY a.id, dateFrame
`

var whereClause string
var queryArgs []interface{}

if transactionType == 0 {
whereClause = "WHERE lt.date >= $1 AND lt.asset_id = $2"
queryArgs = append(queryArgs, dateFilter, assetID, timeFrameUnit, timeFrameSeconds)
} else {
whereClause = "WHERE lt.date >= $1 AND lt.asset_id = $2 AND lt.transaction_type_id = $5"
queryArgs = append(queryArgs, dateFilter, assetID, timeFrameUnit, timeFrameSeconds, transactionType)
}

groupAndOrderClause := `
GROUP BY a.id, dateFrame
ORDER BY a.id, dateFrame;
`
`

query := baseQuery + whereClause + groupAndOrderClause

rows, err := repo.Db.Query(query, dateFilter, assetID, timeFrameUnit, timeFrameSeconds)
rows, err := repo.Db.Query(query, queryArgs...)
if err != nil {
return entity.SumLogTransaction{}, err
}
Expand Down Expand Up @@ -198,7 +213,7 @@ func getDateFilter(timeRange string) (time.Time, error) {
if value == 1 {
return time.Date(currentTime.Year(), currentTime.Month(), currentTime.Day(), 0, 0, 0, 0, currentTime.Location()), nil
}
// For other values, adjust the date accordingly.

targetDate := currentTime.AddDate(0, 0, -value+1)
return time.Date(targetDate.Year(), targetDate.Month(), targetDate.Day(), 0, 0, 0, 0, targetDate.Location()), nil
default:
Expand Down

0 comments on commit 8670ad3

Please sign in to comment.