Skip to content

Commit

Permalink
add download, sum and total
Browse files Browse the repository at this point in the history
  • Loading branch information
efortin committed Jan 31, 2022
1 parent 01c8755 commit 7b45381
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 0 deletions.
104 changes: 104 additions & 0 deletions controllers/statistics_controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package controllers

import (
"context"
"go.mongodb.org/mongo-driver/mongo"
"hackathon-api/configs"
"hackathon-api/responses"
"net/http"
"time"

"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson"
)

//https://www.mongodb.com/blog/post/quick-start-golang--mongodb--data-aggregation-pipeline

var statsCollection = configs.GetCollection(configs.DB, "donations")

func CountDonationByMoney() gin.HandlerFunc {
return func(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
money := c.Param("money")
defer cancel()

count, err := donationCollection.CountDocuments(ctx, bson.M{"moneyType": money})

if err != nil {
c.JSON(http.StatusInternalServerError, responses.DonationResponse{Status: http.StatusInternalServerError, Message: "error", Data: map[string]interface{}{"data": err.Error()}})
return
}

if err != nil {
c.JSON(http.StatusInternalServerError, responses.DonationResponse{Status: http.StatusInternalServerError, Message: "error", Data: map[string]interface{}{"data": err.Error()}})
return
}

c.JSON(http.StatusOK,
responses.DonationResponse{Status: http.StatusOK, Message: "success", Data: map[string]interface{}{"data": count}},
)

}
}

func SumDonationsByMoney() gin.HandlerFunc {
return func(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
//money := c.Param("money")
defer cancel()

//matchStage := bson.D{{"$match", bson.D{{"moneyType", money}}}}
groupStage := bson.D{{"$group", bson.D{{"_id", "$moneyType"}, {"total", bson.D{{"$sum", "$amount"}}}}}}

resultCursor, err := statsCollection.Aggregate(ctx, mongo.Pipeline{ /*matchStage,*/ groupStage})
if err != nil {
println(err)
}

var resultData []bson.M
resultCursor.All(ctx, resultData)

if err = resultCursor.All(ctx, &resultData); err != nil {
panic(err)
}

if err != nil {
c.JSON(http.StatusInternalServerError, responses.DonationResponse{Status: http.StatusInternalServerError, Message: "error", Data: map[string]interface{}{"data": err.Error()}})
return
}

c.JSON(http.StatusOK, resultData)

}

}

func SumDonations() gin.HandlerFunc {
return func(c *gin.Context) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

groupStage := bson.D{{"$group", bson.D{{"_id", ""}, {"total", bson.D{{"$sum", "$amount"}}}}}}

resultCursor, err := statsCollection.Aggregate(ctx, mongo.Pipeline{ /*matchStage,*/ groupStage})
if err != nil {
println(err)
}

var resultData []bson.M
resultCursor.All(ctx, resultData)

if err = resultCursor.All(ctx, &resultData); err != nil {
panic(err)
}

if err != nil {
c.JSON(http.StatusInternalServerError, responses.DonationResponse{Status: http.StatusInternalServerError, Message: "error", Data: map[string]interface{}{"data": err.Error()}})
return
}

c.JSON(http.StatusOK, resultData)

}

}
1 change: 1 addition & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func main() {

//routes
routes.DonationRoute(router)
routes.StatisticsRoute(router)

router.Run("0.0.0.0:8080")
}
7 changes: 7 additions & 0 deletions models/stats_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package models

// Stats type
type Stats struct {
ID string `json:"_id,omitempty" bson:"_id,omitempty"`
total string `json:"total,omitempty" bson:"total,omitempty"`
}
13 changes: 13 additions & 0 deletions routes/statistics_route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package routes

import (
"hackathon-api/controllers"

"github.com/gin-gonic/gin"
)

func StatisticsRoute(router *gin.Engine) {
router.GET("/statistics/:money", controllers.CountDonationByMoney())
router.GET("/statistics/sum", controllers.SumDonationsByMoney())
router.GET("/statistics/total", controllers.SumDonations())
}

0 comments on commit 7b45381

Please sign in to comment.