Skip to content
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
46 changes: 46 additions & 0 deletions internal/api/v1/invest_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package v1

import (
"net/http"

"github.com/Hello-Storage/hello-back/internal/db"
"github.com/Hello-Storage/hello-back/internal/entity"
"github.com/gin-gonic/gin"
)

func InvestGetDataByCode(router *gin.RouterGroup) {

router.GET("/invest/all", func(ctx *gin.Context) {

var investAccount []entity.InvestAccount
result := db.Db().Find(&investAccount)

if result.RowsAffected > 0 {

ctx.JSON(http.StatusOK, gin.H{
"isSuccess": "true",
"message": investAccount,
})
} else {

ctx.JSON(http.StatusNotFound, gin.H{
"isSuccess": "false",
"message": "No data found",
})
}

})

router.GET("/invest", func(ctx *gin.Context) {

var investCodes []entity.InvestCode
db.Db().Preload("InvestAccounts").Find(&investCodes)

ctx.JSON(http.StatusOK, gin.H{
"isSuccess": "true",
"message": investCodes,
})

})

}
59 changes: 59 additions & 0 deletions internal/api/v1/invest_post.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package v1

import (
"net/http"

"github.com/Hello-Storage/hello-back/internal/db"
"github.com/Hello-Storage/hello-back/internal/entity"
"github.com/gin-gonic/gin"
)

type InvestCodeRequest struct {
IP string `json:"ip"`
Email string `json:"email"`
SocialNetwork string `json:"social_network"`
}

func InvestPostData(router *gin.RouterGroup) {

router.POST("/invest", func(ctx *gin.Context) {

code := ctx.Query("code")

var request InvestCodeRequest
if err := ctx.ShouldBindJSON(&request); err != nil {
ctx.JSON(http.StatusBadRequest, gin.H{"error": "body request not found or incomplete"})
return
}

var investCode entity.InvestCode
result := db.Db().Preload("InvestAccounts").Where("code = ?", code).First(&investCode)

if result.RowsAffected > 0 {
newInvestAccount := entity.InvestAccount{IP: request.IP, Code: code}
db.Db().Create(&newInvestAccount)

ctx.JSON(http.StatusOK, gin.H{
"isSuccess": true,
"message": investCode,
})
} else {
//Creacion de Invest_codes

// newInvestCode := entity.InvestCode{Code: code, Email: request.Email, SocialNetwork: request.SocialNetwork}
// db.Db().Create(&newInvestCode)

//Creacion de Invest_account

// newInvestAccount := entity.InvestAccount{IP: request.IP, Code: code}
// db.Db().Create(&newInvestAccount)

ctx.JSON(http.StatusOK, gin.H{
"isSuccess": false,
"message": "The code is not found",
})
}

})

}
31 changes: 31 additions & 0 deletions internal/entity/invest_account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package entity

import (
"time"

"github.com/Hello-Storage/hello-back/internal/db"
"gorm.io/gorm"
)

type InvestAccount struct {
ID uint `gorm:"primarykey" json:"id"`
IP string `gorm:"type:varchar(16);unique" json:"ip"`
CreatedAt time.Time ` json:"created_at"`
UpdatedAt time.Time ` json:"updated_at"`
DeletedAt gorm.DeletedAt `gorm:"index" json:"deleted_at"`
Code string `gorm:"type:varchar(64);index" json:"code"`
InvestCode InvestCode `gorm:"foreignKey:Code;references:Code" json:"-"`
}

// TableName returns the entity table name.
func (InvestAccount) TableName() string {
return "invest_accounts"
}

func (m *InvestAccount) Create() error {
return db.Db().Create(m).Error
}

func (m *InvestAccount) Save() error {
return db.Db().Save(m).Error
}
24 changes: 24 additions & 0 deletions internal/entity/invest_code.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package entity

import (
"github.com/Hello-Storage/hello-back/internal/db"
)

type InvestCode struct {
Code string `gorm:"primarykey" json:"code"`
SocialNetwork string `gorm:"type:varchar(64)" json:"social_network"`
InvestAccounts []InvestAccount `gorm:"foreignKey:Code;references:Code"`
}

// TableName returns the entity table name.
func (InvestCode) TableName() string {
return "invest_codes"
}

func (m *InvestCode) Create() error {
return db.Db().Create(m).Error
}

func (m *InvestCode) Save() error {
return db.Db().Save(m).Error
}
8 changes: 7 additions & 1 deletion internal/server/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ func registerRoutes(router *gin.Engine) {
//api keys routes
api.ApiKey(AuthAPIv1, tokenMaker)

//api invest data
v1.InvestPostData(APIv1)
v1.InvestGetDataByCode(APIv1)

//statistics routes
api.GetStatistics(APIv1)
api.GetWeeklyPublicStats(APIv1)
Expand Down Expand Up @@ -67,7 +71,6 @@ func registerRoutes(router *gin.Engine) {
api.EncryptFile(FileRoutes)
api.UploadFileMultipart(FileRoutes)


api.GetPublishedFileName(router.Group("/api/file"))

// folder routes
Expand All @@ -93,6 +96,8 @@ func RegisterApiRoutes(router *gin.Engine) {
}

// Create router groups.

//Public route with apiKey
ApiKeyAPIv1 = router.Group("/public-api/v1")
ApiKeyAPIv1.Use(middlewares.APIKeyAuthMiddleware(tokenMaker))

Expand All @@ -103,4 +108,5 @@ func RegisterApiRoutes(router *gin.Engine) {
v1.FileUpdate(ApiKeyAPIv1)
v1.DeleteFile(ApiKeyAPIv1)
v1.DownloadFile(ApiKeyAPIv1)

}