Skip to content

Commit

Permalink
Add StoryboardService
Browse files Browse the repository at this point in the history
  • Loading branch information
StevenWeathers committed Jun 24, 2023
1 parent 01d5a1e commit 8dbf2f2
Show file tree
Hide file tree
Showing 14 changed files with 195 additions and 136 deletions.
29 changes: 15 additions & 14 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,20 @@ type Config struct {
}

type Service struct {
Config *Config
Router *mux.Router
Email *email.Email
Cookie *securecookie.SecureCookie
DB *db.Database
Logger *otelzap.Logger
UserService thunderdome.UserService
APIKeyService thunderdome.APIKeyService
AlertService thunderdome.AlertService
AuthService thunderdome.AuthService
BattleService thunderdome.BattleService
CheckinService thunderdome.CheckinService
RetroService thunderdome.RetroService
Config *Config
Router *mux.Router
Email *email.Email
Cookie *securecookie.SecureCookie
DB *db.Database
Logger *otelzap.Logger
UserService thunderdome.UserService
APIKeyService thunderdome.APIKeyService
AlertService thunderdome.AlertService
AuthService thunderdome.AuthService
BattleService thunderdome.BattleService
CheckinService thunderdome.CheckinService
RetroService thunderdome.RetroService
StoryboardService thunderdome.StoryboardService
}

// standardJsonResponse structure used for all restful APIs response body
Expand Down Expand Up @@ -114,7 +115,7 @@ func Init(apiService Service) *Service {
var a = &apiService
b := battle.New(a.DB, a.Logger, a.validateSessionCookie, a.validateUserCookie, a.UserService, a.AuthService, a.BattleService)
rs := retro.New(a.DB, a.Logger, a.validateSessionCookie, a.validateUserCookie, a.UserService, a.AuthService, a.RetroService)
sb := storyboard.New(a.DB, a.Logger, a.validateSessionCookie, a.validateUserCookie, a.UserService, a.AuthService)
sb := storyboard.New(a.DB, a.Logger, a.validateSessionCookie, a.validateUserCookie, a.UserService, a.AuthService, a.StoryboardService)
tc := checkin.New(a.DB, a.Logger, a.validateSessionCookie, a.validateUserCookie, a.UserService, a.AuthService, a.CheckinService)
swaggerJsonPath := "/" + a.Config.PathPrefix + "swagger/doc.json"
validate = validator.New()
Expand Down
2 changes: 1 addition & 1 deletion api/maintenance.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (a *Service) handleCleanStoryboards() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
DaysOld := viper.GetInt("config.cleanup_storyboards_days_old")

err := a.DB.CleanStoryboards(r.Context(), DaysOld)
err := a.StoryboardService.CleanStoryboards(r.Context(), DaysOld)
if err != nil {
a.Failure(w, r, http.StatusInternalServerError, err)
return
Expand Down
14 changes: 7 additions & 7 deletions api/storyboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (a *Service) handleStoryboardCreate() http.HandlerFunc {
// if storyboard created with team association
if teamIdExists {
if isTeamUserOrAnAdmin(r) {
newStoryboard, err = a.DB.TeamCreateStoryboard(ctx, TeamID, UserID, s.StoryboardName, s.JoinCode, s.FacilitatorCode)
newStoryboard, err = a.StoryboardService.TeamCreateStoryboard(ctx, TeamID, UserID, s.StoryboardName, s.JoinCode, s.FacilitatorCode)

if err != nil {
a.Failure(w, r, http.StatusInternalServerError, err)
Expand All @@ -83,7 +83,7 @@ func (a *Service) handleStoryboardCreate() http.HandlerFunc {
return
}
} else {
newStoryboard, err = a.DB.CreateStoryboard(ctx, UserID, s.StoryboardName, s.JoinCode, s.FacilitatorCode)
newStoryboard, err = a.StoryboardService.CreateStoryboard(ctx, UserID, s.StoryboardName, s.JoinCode, s.FacilitatorCode)
if err != nil {
a.Failure(w, r, http.StatusInternalServerError, err)
return
Expand Down Expand Up @@ -117,15 +117,15 @@ func (a *Service) handleStoryboardGet() http.HandlerFunc {
UserId := r.Context().Value(contextKeyUserID).(string)
UserType := r.Context().Value(contextKeyUserType).(string)

storyboard, err := a.DB.GetStoryboard(StoryboardID, UserId)
storyboard, err := a.StoryboardService.GetStoryboard(StoryboardID, UserId)
if err != nil {
a.Failure(w, r, http.StatusNotFound, Errorf(ENOTFOUND, "STORYBOARD_NOT_FOUND"))
return
}

// don't allow retrieving storyboard details if storyboard has JoinCode and user hasn't joined yet
if storyboard.JoinCode != "" {
UserErr := a.DB.GetStoryboardUserActiveStatus(StoryboardID, UserId)
UserErr := a.StoryboardService.GetStoryboardUserActiveStatus(StoryboardID, UserId)
if UserErr != nil && UserType != adminUserType {
a.Failure(w, r, http.StatusForbidden, Errorf(EUNAUTHORIZED, "USER_MUST_JOIN_STORYBOARD"))
return
Expand Down Expand Up @@ -155,7 +155,7 @@ func (a *Service) handleGetUserStoryboards() http.HandlerFunc {
vars := mux.Vars(r)
UserID := vars["userId"]

storyboards, Count, err := a.DB.GetStoryboardsByUser(UserID)
storyboards, Count, err := a.StoryboardService.GetStoryboardsByUser(UserID)
if err != nil {
a.Failure(w, r, http.StatusNotFound, Errorf(ENOTFOUND, "STORYBOARDS_NOT_FOUND"))
return
Expand Down Expand Up @@ -193,9 +193,9 @@ func (a *Service) handleGetStoryboards() http.HandlerFunc {
Active, _ := strconv.ParseBool(query.Get("active"))

if Active {
storyboards, Count, err = a.DB.GetActiveStoryboards(Limit, Offset)
storyboards, Count, err = a.StoryboardService.GetActiveStoryboards(Limit, Offset)
} else {
storyboards, Count, err = a.DB.GetStoryboards(Limit, Offset)
storyboards, Count, err = a.StoryboardService.GetStoryboards(Limit, Offset)
}

if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions api/storyboard/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (sub subscription) readPump(b *Service, ctx context.Context) {
StoryboardID := sub.arena

defer func() {
Users := b.DB.RetreatStoryboardUser(StoryboardID, UserID)
Users := b.StoryboardService.RetreatStoryboardUser(StoryboardID, UserID)
UpdatedUsers, _ := json.Marshal(Users)

retreatEvent := createSocketEvent("user_left", string(UpdatedUsers), UserID)
Expand Down Expand Up @@ -104,7 +104,7 @@ func (sub subscription) readPump(b *Service, ctx context.Context) {

// confirm owner for any operation that requires it
if _, ok := ownerOnlyOperations[eventType]; ok && !badEvent {
err := b.DB.ConfirmStoryboardFacilitator(StoryboardID, UserID)
err := b.StoryboardService.ConfirmStoryboardFacilitator(StoryboardID, UserID)
if err != nil {
badEvent = true
}
Expand Down Expand Up @@ -223,14 +223,14 @@ func (b *Service) ServeWs() http.HandlerFunc {
}

// make sure storyboard is legit
storyboard, storyboardErr := b.DB.GetStoryboard(storyboardID, User.Id)
storyboard, storyboardErr := b.StoryboardService.GetStoryboard(storyboardID, User.Id)
if storyboardErr != nil {
b.handleSocketClose(ctx, ws, 4004, "storyboard not found")
return
}

// check users storyboard active status
UserErr := b.DB.GetStoryboardUserActiveStatus(storyboardID, User.Id)
UserErr := b.StoryboardService.GetStoryboardUserActiveStatus(storyboardID, User.Id)
if UserErr != nil && !errors.Is(UserErr, sql.ErrNoRows) {
usrErrMsg := UserErr.Error()

Expand Down Expand Up @@ -278,7 +278,7 @@ func (b *Service) ServeWs() http.HandlerFunc {
ss := subscription{c, storyboardID, User.Id}
h.register <- ss

Users, _ := b.DB.AddUserToStoryboard(ss.arena, User.Id)
Users, _ := b.StoryboardService.AddUserToStoryboard(ss.arena, User.Id)
UpdatedUsers, _ := json.Marshal(Users)

Storyboard, _ := json.Marshal(storyboard)
Expand All @@ -299,7 +299,7 @@ func (b *Service) ServeWs() http.HandlerFunc {
func (b *Service) APIEvent(ctx context.Context, arenaID string, UserID, eventType string, eventValue string) error {
// confirm leader for any operation that requires it
if _, ok := ownerOnlyOperations[eventType]; ok {
err := b.DB.ConfirmStoryboardFacilitator(arenaID, UserID)
err := b.StoryboardService.ConfirmStoryboardFacilitator(arenaID, UserID)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit 8dbf2f2

Please sign in to comment.