Skip to content

Commit

Permalink
implement start heat request and send broadcast notification
Browse files Browse the repository at this point in the history
  • Loading branch information
konrad2002 committed Dec 3, 2024
1 parent b65b9ca commit 4e3f699
Show file tree
Hide file tree
Showing 10 changed files with 118 additions and 7 deletions.
2 changes: 1 addition & 1 deletion client/disq_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func (c *DisqualificationClient) ImportDisqualification(start model.Start, reaso
},
}

res, err := client.Post(c.apiUrl, "disqualification/import", request)
res, err := client.Post(c.apiUrl, "disqualification/import", request, nil)
if err != nil {
return nil, false, err
}
Expand Down
2 changes: 1 addition & 1 deletion client/heat_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (c *HeatClient) ImportHeat(heat model.Heat) (*model.Heat, bool, error) {
Heat: heat,
}

res, err := client.Post(c.apiUrl, "heat/import", request)
res, err := client.Post(c.apiUrl, "heat/import", request, nil)
if err != nil {
return nil, false, err
}
Expand Down
6 changes: 3 additions & 3 deletions client/start_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func NewStartClient(url string) *StartClient {
}

func (c *StartClient) Actuator() (string, error) {
res, err := client.Get(c.apiUrl, "actuator", nil)
res, err := client.Get(c.apiUrl, "actuator", nil, nil)
if err != nil {
return "", err
}
Expand All @@ -37,7 +37,7 @@ func (c *StartClient) ImportStart(start model.Start) (*model.Start, bool, error)
Start: start,
}

res, err := client.Post(c.apiUrl, "start/import", request)
res, err := client.Post(c.apiUrl, "start/import", request, nil)
if err != nil {
return nil, false, err
}
Expand All @@ -61,7 +61,7 @@ func (c *StartClient) ImportResult(start model.Start, result model.Result) (*mod
Result: result,
}

res, err := client.Post(c.apiUrl, "result/import", request)
res, err := client.Post(c.apiUrl, "result/import", request, nil)
if err != nil {
return nil, false, err
}
Expand Down
30 changes: 30 additions & 0 deletions controller/heat_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func heatController() {
router.POST("/heat", addHeat)
router.POST("/heat/import", importHeat)
router.POST("/heat/meet/:meet_id/events/start_estimation_date", updateHeatsStartEstimationDate)
router.POST("/heat/meet/:meet_id/event/:event_id/heat/:heat/start", triggerHeatStart)
router.POST("/heat/:id/time", updateHeatTime)

router.PUT("/heat", updateHeat)
Expand Down Expand Up @@ -329,3 +330,32 @@ func updateHeatsStartEstimationDate(c *gin.Context) {

c.IndentedJSON(http.StatusOK, info)
}

func triggerHeatStart(c *gin.Context) {
meeting := c.Param("meet_id")

if meeting == "" {
c.String(http.StatusBadRequest, "no meeting id given")
return
}

event, err1 := strconv.Atoi(c.Param("event_id"))
if err1 != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "given event_id is not of type number"})
return
}

number, err2 := strconv.Atoi(c.Param("heat"))
if err2 != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{"message": "given heat is not of type number"})
return
}

heat, err := service.SetHeatStartToNowByNumber(meeting, event, number)
if err != nil {
c.IndentedJSON(http.StatusNotFound, gin.H{"message": err.Error()})
return
}

c.IndentedJSON(http.StatusOK, heat)
}
7 changes: 5 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,24 @@ require (
github.com/gin-gonic/gin v1.9.1
github.com/sirupsen/logrus v1.9.3
github.com/swimresults/athlete-service v1.5.0
github.com/swimresults/meeting-service v0.5.0
github.com/swimresults/service-core v0.5.0
github.com/swimresults/meeting-service v0.6.3
github.com/swimresults/service-core v0.6.1
go.mongodb.org/mongo-driver v1.13.0
)

require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
Expand All @@ -32,6 +34,7 @@ require (
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/swimresults/user-service v0.1.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
Expand Down
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583j
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE=
Expand All @@ -31,6 +33,8 @@ github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/compress v1.13.6 h1:P76CopJELS0TiO2mebmnzgWaajssP/EszplttgQxcgc=
Expand Down Expand Up @@ -84,8 +88,14 @@ github.com/swimresults/meeting-service v0.4.0 h1:FUXkHbFomZXvB6zr4WMFn/Q0ikft483
github.com/swimresults/meeting-service v0.4.0/go.mod h1:60kU5gbAV253krD6q5nlLsyvFLTRcbA7zLHOXRSfkRA=
github.com/swimresults/meeting-service v0.5.0 h1:RtSuEBVI5kSyl1aequJHGwa9BuoPnJ5MXeN3jUvCIy0=
github.com/swimresults/meeting-service v0.5.0/go.mod h1:60kU5gbAV253krD6q5nlLsyvFLTRcbA7zLHOXRSfkRA=
github.com/swimresults/meeting-service v0.6.3 h1:KOsAKlZgr98gc+g09+thdsMJujFHKyKKrL6IsWcuo/E=
github.com/swimresults/meeting-service v0.6.3/go.mod h1:60kU5gbAV253krD6q5nlLsyvFLTRcbA7zLHOXRSfkRA=
github.com/swimresults/service-core v0.5.0 h1:zF+/X9KWsVXgXdDfNFEKeryMGAxiXhstnKwL7Je/Rvw=
github.com/swimresults/service-core v0.5.0/go.mod h1:PnaIA/xlyf4GpsYHE7yj5HDFdbiq/VSEvqgj27VfqTQ=
github.com/swimresults/service-core v0.6.1 h1:f5HLfmvHRMBMKACpTZ/CgatHlIVAc2xWracwmLMnKb0=
github.com/swimresults/service-core v0.6.1/go.mod h1:PnaIA/xlyf4GpsYHE7yj5HDFdbiq/VSEvqgj27VfqTQ=
github.com/swimresults/user-service v0.1.0 h1:agSnBI3qNy7nHKwIppA5aiZ1iZQqn0wlwrHbgm08jYg=
github.com/swimresults/user-service v0.1.0/go.mod h1:qMrOwT8+xtRR0ikyLNieXzIyGBYL2c45OU0uR7vhLag=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
Expand Down
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
log "github.com/sirupsen/logrus"
"github.com/swimresults/start-service/controller"
"github.com/swimresults/start-service/notification"
"github.com/swimresults/start-service/service"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
Expand Down Expand Up @@ -39,6 +40,7 @@ func main() {
}
defer file.Close()
service.Init(client)
notification.Init()
controller.Run()

if err := client.Disconnect(ctx); err != nil {
Expand Down
26 changes: 26 additions & 0 deletions notification/heat_start_broadcast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package notification

import (
"fmt"
"strconv"
)

func BroadcastHeatStart(meeting string, event int, heat int, delay int) {
b := `
{
"status": {
"event": ` + strconv.Itoa(event) + `,
"heat": ` + strconv.Itoa(heat) + `,
"over": false,
"delay": ` + strconv.Itoa(delay) + `
}
}
`
notification, err := notificationClient.SendMeetingBroadcastNotification(serviceKey, meeting, b)
if err != nil {
fmt.Printf("failed sending meeting broadcast notification: %s", err.Error())
return
}

println(notification)
}
25 changes: 25 additions & 0 deletions notification/notification.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package notification

import (
"fmt"
userClient "github.com/swimresults/user-service/client"
"os"
)

var notificationClient *userClient.NotificationClient
var serviceKey string

func Init() {

serviceKey = os.Getenv("SR_SERVICE_KEY")

if serviceKey == "" {
fmt.Println("no security for inter-service communication given! Please set SR_SERVICE_KEY.")
return
}

userServiceUrl := os.Getenv("SR_START_USER_URL")
if userServiceUrl != "" {
notificationClient = userClient.NewNotificationClient(userServiceUrl)
}
}
15 changes: 15 additions & 0 deletions service/heat_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"context"
"errors"
"fmt"
"github.com/swimresults/service-core/misc"
"github.com/swimresults/start-service/dto"
"github.com/swimresults/start-service/model"
"github.com/swimresults/start-service/notification"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
Expand Down Expand Up @@ -460,6 +462,19 @@ func UpdateHeatTimes(id primitive.ObjectID, time time.Time, timeType string) (mo
return UpdateHeat(heat)
}

func SetHeatStartToNowByNumber(meeting string, event int, number int) (model.Heat, error) {
heat, err := GetHeatByNumber(meeting, event, number)
if err != nil {
return model.Heat{}, err
}

heat.StartAt = misc.TimeNow()

go notification.BroadcastHeatStart(meeting, event, number, int(heat.StartEstimation.Sub(heat.StartAt).Seconds()))

return UpdateHeat(heat)
}

func UpdateHeatsEstimationDateByMeetingAndEvent(meeting string, events []int, t time.Time, updateTimeZone bool) ([]model.Heat, error) {
var heats []model.Heat
var err error
Expand Down

0 comments on commit 4e3f699

Please sign in to comment.