Skip to content

Commit

Permalink
Merge branch 'dev' into generate-toml
Browse files Browse the repository at this point in the history
  • Loading branch information
wjuniorbh92 committed Sep 28, 2023
2 parents 90a1ba2 + 03fb0dc commit 56a3f16
Show file tree
Hide file tree
Showing 101 changed files with 5,405 additions and 771 deletions.
764 changes: 731 additions & 33 deletions backend/docs/docs.go

Large diffs are not rendered by default.

764 changes: 731 additions & 33 deletions backend/docs/swagger.json

Large diffs are not rendered by default.

504 changes: 488 additions & 16 deletions backend/docs/swagger.yaml

Large diffs are not rendered by default.

54 changes: 38 additions & 16 deletions backend/internal/controller/http/v1/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,31 +65,39 @@ type CreateAssetRequest struct {
}

type BurnAssetRequest struct {
Id string `json:"id" binding:"required" example:"001"`
SponsorId int `json:"sponsor_id" example:"2"`
Amount string `json:"amount" binding:"required" example:"1000"`
Id string `json:"id" binding:"required" example:"001"`
SponsorId int `json:"sponsor_id" example:"2"`
Amount string `json:"amount" binding:"required" example:"1000"`
CurrentSupply float64 `json:"current_supply" example:"1000"`
CurrentMainVault float64 `json:"current_main_vault" example:"1000"`
}

type MintAssetRequest struct {
Id string `json:"id" binding:"required" example:"12"`
SponsorId int `json:"sponsor_id" example:"2"`
Code string `json:"code" binding:"required" example:"USDC"`
Amount string `json:"amount" binding:"required" example:"1000"`
Id string `json:"id" binding:"required" example:"12"`
SponsorId int `json:"sponsor_id" example:"2"`
Code string `json:"code" binding:"required" example:"USDC"`
Amount string `json:"amount" binding:"required" example:"1000"`
CurrentSupply float64 `json:"current_supply" example:"1000"`
CurrentMainVault float64 `json:"current_main_vault" example:"1000"`
}

type ClawbackAssetRequest struct {
SponsorId int `json:"sponsor_id" example:"2"`
Code string `json:"code" binding:"required" example:"USDC"`
Amount string `json:"amount" binding:"required" example:"1000"`
From string `json:"from" binding:"required" example:"GDKIJJIKXLOM2NRMPNQZUUYK24ZPVFC6426GZAICZ6E5PQG2MIPIMB2L"`
SponsorId int `json:"sponsor_id" example:"2"`
Code string `json:"code" binding:"required" example:"USDC"`
Amount string `json:"amount" binding:"required" example:"1000"`
From string `json:"from" binding:"required" example:"GDKIJJIKXLOM2NRMPNQZUUYK24ZPVFC6426GZAICZ6E5PQG2MIPIMB2L"`
CurrentSupply float64 `json:"current_supply" example:"1000"`
CurrentMainVault float64 `json:"current_main_vault" example:"1000"`
}

type TransferAssetRequest struct {
SourceWalletID int `json:"source_wallet_id" binding:"required" example:"1"`
SponsorId int `json:"sponsor_id" example:"2"`
DestinationWalletPK string `json:"destination_wallet_pk" binding:"required" example:"GABCD...."`
AssetID string `json:"asset_id" binding:"required" example:"12"`
Amount string `json:"amount" binding:"required" example:"12"`
SourceWalletID int `json:"source_wallet_id" binding:"required" example:"1"`
SponsorId int `json:"sponsor_id" example:"2"`
DestinationWalletPK string `json:"destination_wallet_pk" binding:"required" example:"GABCD...."`
AssetID string `json:"asset_id" binding:"required" example:"12"`
Amount string `json:"amount" binding:"required" example:"12"`
CurrentSupply float64 `json:"current_supply" example:"1000"`
CurrentMainVault float64 `json:"current_main_vault" example:"1000"`
}

type UpdateAuthFlagsRequest struct {
Expand Down Expand Up @@ -374,12 +382,15 @@ func (r *assetsRoutes) mintAsset(c *gin.Context) {
if err != nil {
amount = 0
}

err = r.l.CreateLogTransaction(entity.LogTransaction{
Asset: asset,
Amount: amount,
TransactionTypeID: entity.MintAsset,
UserID: userID,
Description: createLogDescription(entity.MintAsset, asset.Code, nil, nil),
CurrentSupply: &request.CurrentSupply,
CurrentMainVault: &request.CurrentMainVault,
})
if err != nil {
errorResponse(c, http.StatusNotFound, "error to create log transaction", err)
Expand Down Expand Up @@ -468,6 +479,8 @@ func (r *assetsRoutes) burnAsset(c *gin.Context) {
Amount: amount,
UserID: userID,
Description: createLogDescription(entity.BurnAsset, asset.Code, nil, nil),
CurrentSupply: &request.CurrentSupply,
CurrentMainVault: &request.CurrentMainVault,
})
if err != nil {
errorResponse(c, http.StatusNotFound, "error to create log transaction", err)
Expand Down Expand Up @@ -538,6 +551,7 @@ func (r *assetsRoutes) transferAsset(c *gin.Context) {
})
if err != nil {
errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err)
return
}

token := c.Request.Header.Get("Authorization")
Expand All @@ -562,6 +576,10 @@ func (r *assetsRoutes) transferAsset(c *gin.Context) {
TransactionTypeID: entity.TransferAsset,
UserID: userID,
Description: createLogDescription(entity.TransferAsset, asset.Code, nil, nil),
OriginPK: &sourceWallet.Key.PublicKey,
DestinationPK: &request.DestinationWalletPK,
CurrentSupply: &request.CurrentSupply,
CurrentMainVault: &request.CurrentMainVault,
})
if err != nil {
errorResponse(c, http.StatusNotFound, "error to create log transaction", err)
Expand Down Expand Up @@ -626,6 +644,7 @@ func (r *assetsRoutes) clawbackAsset(c *gin.Context) {
})
if err != nil {
errorResponse(c, http.StatusInternalServerError, "starlabs messaging problems", err)
return
}

token := c.Request.Header.Get("Authorization")
Expand All @@ -638,6 +657,7 @@ func (r *assetsRoutes) clawbackAsset(c *gin.Context) {
userID, err := strconv.Atoi(user.ID)
if err != nil {
errorResponse(c, http.StatusNotFound, "error to parse user id", err)
return
}

amount, err := strconv.ParseFloat(request.Amount, 64)
Expand All @@ -650,6 +670,8 @@ func (r *assetsRoutes) clawbackAsset(c *gin.Context) {
Amount: amount,
UserID: userID,
Description: createLogDescription(entity.ClawbackAsset, asset.Code, nil, nil),
CurrentSupply: &request.CurrentSupply,
CurrentMainVault: &request.CurrentMainVault,
})
if err != nil {
errorResponse(c, http.StatusNotFound, "error to create log transaction", err)
Expand Down
73 changes: 73 additions & 0 deletions backend/internal/controller/http/v1/log_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,18 @@ func newLogTransactionsRoutes(handler *gin.RouterGroup, w usecase.WalletUseCase,
h.GET("/transaction_type/:transaction_type_id/:time_range", r.getLogTransactionsByTransactionTypeID)
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)
h.GET("/last-transactions/:transaction_type_id", r.getLastLogTransactions)
h.POST("/supply/:asset_id", r.supplyByAssetID)

}
}

type FiltersRequest struct {
TimeRange string `json:"time_range" example:"hour"`
PeriodInitial string `json:"period_initial" example:"24 hours"`
Interval string `json:"interval" example:"1 hour"`
}

// @Summary Get all transactions logs
// @Description Get all transactions logs within a specific time range
// @Tags Log Transactions
Expand Down Expand Up @@ -216,3 +225,67 @@ func (r *logTransactionsRoutes) sumAmountsForAllAssets(c *gin.Context) {

c.JSON(http.StatusOK, sum)
}

// @Summary Get last transactions
// @Description Get last transactions logs for a specific transaction type
// @Tags Log Transactions
// @Accept json
// @Produce json
// @Param transaction_type_id path int true "Transaction Type ID"
// @Security ApiKeyAuth
// @Success 200 {object} entity.LogTransaction
// @Router /log_transactions/last_transactions/{transaction_type_id} [get]
func (r *logTransactionsRoutes) getLastLogTransactions(c *gin.Context) {
transactionTypeIDStr := c.Param("transaction_type_id")

transactionTypeID, err := strconv.Atoi(transactionTypeIDStr)
if err != nil {
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid asset ID: %s", err.Error()), err)
return
}

logTransactions, err := r.l.GetLastLogTransactions(transactionTypeID)
if err != nil {
errorResponse(c, http.StatusInternalServerError, fmt.Sprintf("error getting last log transactions: %s", err.Error()), err)
return
}

c.JSON(http.StatusOK, logTransactions)
}

// @Summary Get sum of supply by Asset ID within a specific time frame
// @Description Get sum of supply for a specific asset, grouped by a specified time frame (e.g., '1h' for 1 hour)
// @Tags Log Transactions
// @Accept json
// @Produce json
// @Param asset_id path int true "Asset ID"
// @Param time_range path string true "Time range for the query (e.g., '24h' or '1d' '7d' '30d')"
// @Param time_frame path string true "Time frame for the query (e.g., '1h' '2h' '24h' '36h')"
// @Security ApiKeyAuth
// @Success 200 {object} entity.SumLogTransaction
// @Failure 400 {string} string "Invalid time_frame format"
// @Failure 500 {string} string "Internal server error"
// @Router /log_transactions/supply/{asset_id}/{time_range}/{time_frame} [get]
func (r *logTransactionsRoutes) supplyByAssetID(c *gin.Context) {
assetIDStr := c.Param("asset_id")

var request FiltersRequest
if err := c.ShouldBindJSON(&request); err != nil {
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err)
return
}

assetID, err := strconv.Atoi(assetIDStr)
if err != nil {
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid asset ID: %s", err.Error()), err)
return
}

sum, err := r.l.LogTransactionsSupplyByAssetID(assetID, request.TimeRange, request.PeriodInitial, request.Interval)
if err != nil {
errorResponse(c, http.StatusInternalServerError, fmt.Sprintf("error getting log transactions: %s", err.Error()), err)
return
}

c.JSON(http.StatusOK, sum)
}
140 changes: 139 additions & 1 deletion backend/internal/controller/http/v1/role.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package v1

import (
"fmt"
"net/http"
"strconv"

"github.com/CheesecakeLabs/token-factory-v2/backend/internal/entity"
"github.com/CheesecakeLabs/token-factory-v2/backend/internal/usecase"
"github.com/gin-gonic/gin"
)
Expand All @@ -18,6 +21,9 @@ func newRoleRoutes(handler *gin.RouterGroup, roleUseCase usecase.RoleUseCase, me
h := handler.Group("/role")
{
h.GET("", r.list)
h.POST("", r.createRole)
h.PUT("/:id", r.updateRole)
h.POST("/delete/:id", r.deleteRole)
}
}

Expand All @@ -27,7 +33,7 @@ func newRoleRoutes(handler *gin.RouterGroup, roleUseCase usecase.RoleUseCase, me
// @Tags Role
// @Accept json
// @Produce json
// @Param type query string true "Type"
// @Param type query string true "Type"
// @Success 200 {object} []entity.Role
// @Router /role [get]
func (r *role) list(c *gin.Context) {
Expand All @@ -37,3 +43,135 @@ func (r *role) list(c *gin.Context) {
}
c.JSON(http.StatusOK, roles)
}

// @Summary Create a new role
// @Description Create a new role
// @Tags Role
// @Accept json
// @Produce json
// @Param request body entity.RoleRequest true "Role info"
// @Success 200 {object} entity.RoleRequest
// @Failure 400 {object} response
// @Failure 404 {object} response
// @Failure 500 {object} response
// @Router /role [post]
func (r *role) createRole(c *gin.Context) {
var request entity.RoleRequest
var err error
var roleCreated entity.RoleRequest

if err := c.ShouldBindJSON(&request); err != nil {
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err)
return
}

roleCreated, err = r.roleUseCase.CreateRole(request)
if err != nil {
errorResponse(c, http.StatusNotFound, fmt.Sprintf("error: %s", err.Error()), err)
return
}

c.JSON(http.StatusOK, roleCreated)
}

// @Summary Update a role
// @Description Update a role by providing the Role ID and the updated information.
// @Tags Role
// @Accept json
// @Produce json
// @Param id path string true "Role ID" Format(uuid)
// @Param request body entity.RoleRequest true "Role info"
// @Success 200 {object} entity.Role "Updated role information"
// @Failure 400 {object} response "Bad Request: Invalid input data"
// @Failure 404 {object} response "Not Found: Role not found"
// @Failure 500 {object} response "Internal Server Error: Failed to update role"
// @Router /role/{id} [put]
func (r *role) updateRole(c *gin.Context) {
// Get the ID of the role to be updated from the URL parameters
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
errorResponse(c, http.StatusBadRequest, "invalid role ID", err)
return
}

var request entity.RoleRequest
if err := c.ShouldBindJSON(&request); err != nil {
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err)
return
}

// Find the existing role by its ID
existingRole, err := r.roleUseCase.GetRoleById(id)
if err != nil {
if err.Error() == "RoleRepo - GetRoleById - role not found" {
errorResponse(c, http.StatusNotFound, "role not found", err)
return
}
errorResponse(c, http.StatusInternalServerError, "error finding role", err)
return
}

// Update the role data
existingRole.Name = request.Name

updatedRole, err := r.roleUseCase.UpdateRole(existingRole)
if err != nil {
errorResponse(c, http.StatusInternalServerError, "error updating role", err)
return
}

c.JSON(http.StatusOK, updatedRole)
}

// @Summary Delete a role
// @Description Delete a role by providing the Role ID and the updated information.
// @Tags Role
// @Accept json
// @Produce json
// @Param id path string true "Role ID" Format(uuid)
// @Param request body entity.RoleRequest true "Role info"
// @Success 200 {object} entity.RoleDelete "Updated role information"
// @Failure 400 {object} response "Bad Request: Invalid input data"
// @Failure 404 {object} response "Not Found: Role not found"
// @Failure 500 {object} response "Internal Server Error: Failed to delete role"
// @Router /role/{id} [delete]
func (r *role) deleteRole(c *gin.Context) {
// Get the ID of the role to be delete from the URL parameters
idStr := c.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil {
errorResponse(c, http.StatusBadRequest, "invalid role ID", err)
return
}

var request entity.RoleDeleteRequest
if err := c.ShouldBindJSON(&request); err != nil {
errorResponse(c, http.StatusBadRequest, fmt.Sprintf("invalid request body: %s", err.Error()), err)
return
}

// Find the existing role by its ID
existingRole, err := r.roleUseCase.GetRoleById(id)
if err != nil {
if err.Error() == "RoleRepo - GetRoleById - role not found" {
errorResponse(c, http.StatusNotFound, "role not found", err)
return
}
errorResponse(c, http.StatusInternalServerError, "error finding role", err)
return
}

roleToDelete := entity.RoleDelete{
Id: existingRole.Id,
NewUsersRoleId: request.NewUsersRoleId,
}

deletedRole, err := r.roleUseCase.DeleteRole(roleToDelete)
if err != nil {
errorResponse(c, http.StatusInternalServerError, "error deleting role", err)
return
}

c.JSON(http.StatusOK, deletedRole)
}
Loading

0 comments on commit 56a3f16

Please sign in to comment.