Skip to content

Commit

Permalink
refactor(api-gql): use services for badges
Browse files Browse the repository at this point in the history
  • Loading branch information
Satont committed Dec 16, 2024
1 parent f427f74 commit 7926c6a
Show file tree
Hide file tree
Showing 20 changed files with 618 additions and 108 deletions.
20 changes: 20 additions & 0 deletions apps/api-gql/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
"github.com/twirapp/twir/apps/api-gql/internal/server/middlewares"
admin_actions "github.com/twirapp/twir/apps/api-gql/internal/services/admin-actions"
audit_logs "github.com/twirapp/twir/apps/api-gql/internal/services/audit-logs"
"github.com/twirapp/twir/apps/api-gql/internal/services/badges"
badges_users "github.com/twirapp/twir/apps/api-gql/internal/services/badges-users"
badges_with_users "github.com/twirapp/twir/apps/api-gql/internal/services/badges-with-users"
dashboard_widget_events "github.com/twirapp/twir/apps/api-gql/internal/services/dashboard-widget-events"
"github.com/twirapp/twir/apps/api-gql/internal/services/keywords"
"github.com/twirapp/twir/apps/api-gql/internal/services/timers"
Expand All @@ -42,6 +45,12 @@ import (

channelsrepository "github.com/twirapp/twir/libs/repositories/channels"
channelsrepositorypgx "github.com/twirapp/twir/libs/repositories/channels/pgx"

badgesrepository "github.com/twirapp/twir/libs/repositories/badges"
badgesrepositorypgx "github.com/twirapp/twir/libs/repositories/badges/pgx"

badgesusersrepository "github.com/twirapp/twir/libs/repositories/badges-users"
badgesusersrepositorypgx "github.com/twirapp/twir/libs/repositories/badges-users/pgx"
)

func main() {
Expand All @@ -59,6 +68,9 @@ func main() {
keywords.New,
audit_logs.New,
admin_actions.New,
badges.New,
badges_users.New,
badges_with_users.New,
),
// repositories
fx.Provide(
Expand All @@ -78,6 +90,14 @@ func main() {
channelsrepositorypgx.NewFx,
fx.As(new(channelsrepository.Repository)),
),
fx.Annotate(
badgesrepositorypgx.NewFx,
fx.As(new(badgesrepository.Repository)),
),
fx.Annotate(
badgesusersrepositorypgx.NewFx,
fx.As(new(badgesusersrepository.Repository)),
),
),
// grpc clients
fx.Provide(
Expand Down
17 changes: 17 additions & 0 deletions apps/api-gql/internal/delivery/gql/mappers/badges.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package mappers

import (
"github.com/twirapp/twir/apps/api-gql/internal/delivery/gql/gqlmodel"
"github.com/twirapp/twir/apps/api-gql/internal/entity"
)

func BadgeEntityToGql(m entity.Badge) gqlmodel.Badge {
return gqlmodel.Badge{
ID: m.ID.String(),
Name: m.Name,
CreatedAt: m.CreatedAt.String(),
FileURL: m.FileURL,
Enabled: m.Enabled,
FfzSlot: m.FFZSlot,
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions apps/api-gql/internal/delivery/gql/resolvers/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
twir_stats "github.com/twirapp/twir/apps/api-gql/internal/delivery/gql/twir-stats"
admin_actions "github.com/twirapp/twir/apps/api-gql/internal/services/admin-actions"
audit_logs "github.com/twirapp/twir/apps/api-gql/internal/services/audit-logs"
"github.com/twirapp/twir/apps/api-gql/internal/services/badges"
badges_users "github.com/twirapp/twir/apps/api-gql/internal/services/badges-users"
dashboard_widget_events "github.com/twirapp/twir/apps/api-gql/internal/services/dashboard-widget-events"
"github.com/twirapp/twir/apps/api-gql/internal/services/keywords"
"github.com/twirapp/twir/apps/api-gql/internal/services/timers"
Expand Down Expand Up @@ -47,12 +49,14 @@ type Resolver struct {
wsRouter wsrouter.WsRouter
twirStats *twir_stats.TwirStats

dashboardWidgetEventsService *dashboard_widget_events.DashboardWidgetsEvents
dashboardWidgetEventsService *dashboard_widget_events.Service
variablesService *variables.Service
timersService *timers.Service
keywordsService *keywords.Service
auditLogService *audit_logs.Service
adminActionsService *admin_actions.Service
badgesService *badges.Service
badgesUsersService *badges_users.Service
}

type Opts struct {
Expand All @@ -71,12 +75,14 @@ type Opts struct {
WsRouter wsrouter.WsRouter
TwirStats *twir_stats.TwirStats

DashboardWidgetEventsService *dashboard_widget_events.DashboardWidgetsEvents
DashboardWidgetEventsService *dashboard_widget_events.Service
VariablesService *variables.Service
TimersService *timers.Service
KeywordService *keywords.Service
UserAuditLogService *audit_logs.Service
AdminActionsService *admin_actions.Service
BadgesService *badges.Service
BadgesUsersService *badges_users.Service
}

func New(opts Opts) (*Resolver, error) {
Expand Down Expand Up @@ -105,6 +111,8 @@ func New(opts Opts) (*Resolver, error) {
keywordsService: opts.KeywordService,
auditLogService: opts.UserAuditLogService,
adminActionsService: opts.AdminActionsService,
badgesService: opts.BadgesService,
badgesUsersService: opts.BadgesUsersService,
}, nil
}

Expand Down
50 changes: 26 additions & 24 deletions apps/api-gql/internal/delivery/http-public/public/badges.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package public

import (
"github.com/gin-gonic/gin"
model "github.com/satont/twir/libs/gomodels"
"github.com/google/uuid"
badges_with_users "github.com/twirapp/twir/apps/api-gql/internal/services/badges-with-users"
)

func (p *Public) computeBadgeUrl(fileName string) string {
Expand All @@ -13,36 +14,37 @@ func (p *Public) computeBadgeUrl(fileName string) string {
return p.config.S3PublicUrl + "/badges/" + fileName
}

type badgeWithUsers struct {
ID uuid.UUID `json:"id"`
Name string `json:"name"`
FFZSlot int `json:"ffzSlot"`
URL string `json:"url"`
Users []string `json:"users"`
}

func (p *Public) HandleBadgesGet(c *gin.Context) {
var badges []model.Badge
if err := p.gorm.
WithContext(c.Request.Context()).
Preload("Users").
Find(&badges).
Error; err != nil {
entities, err := p.badgesWithUsersService.GetMany(
c.Request.Context(),
badges_with_users.GetManyInput{Enabled: true},
)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}

mappedBadges := make([]map[string]any, 0, len(badges))

for _, badge := range badges {
users := make([]string, 0, len(badge.Users))
for _, user := range badge.Users {
users = append(users, user.UserID)
}

mappedBadges = append(
mappedBadges,
map[string]any{
"id": badge.ID,
"name": badge.Name,
"url": p.computeBadgeUrl(badge.FileName),
"ffzSlot": badge.FFZSlot,
"users": users,
result := make([]badgeWithUsers, 0, len(entities))
for _, entity := range entities {
result = append(
result,
badgeWithUsers{
ID: entity.ID,
Name: entity.Name,
FFZSlot: entity.FFZSlot,
URL: entity.FileURL,
Users: entity.Users,
},
)
}

c.JSON(200, mappedBadges)
c.JSON(200, result)
}
Loading

0 comments on commit 7926c6a

Please sign in to comment.