Skip to content

Commit db4d594

Browse files
committed
Add admin flag, refactor user-related code
- Introduce is_admin flag in member table and user-related functions - Update createOrReturnID to return is_admin status - Automatically set first user as admin - Refactor User struct to include IsAdmin field - Update GetUser function to handle new IsAdmin field - Modify UserMiddleware to set is_admin in context - Remove unused model structs (Favorite, MemberPref, Message, etc.) - Update CreateOrReturnID query to return both ID and is_admin status - Adjust tests to accommodate new is_admin functionality - Update mock functions and test cases for user-related operations - Replace os.Stderr with io.Discard in test logger for clean test output Change-Id: I6238f1e736e6924d32f95c0f41b48578f86104f6 Signed-off-by: Ian Meyer <[email protected]>
1 parent 3672b88 commit db4d594

File tree

7 files changed

+135
-118
lines changed

7 files changed

+135
-118
lines changed

models.go

Lines changed: 0 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

querier.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

queries.sql.go

Lines changed: 10 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

server.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@ type ExtendedQuerier interface {
4141
}
4242

4343
type User struct {
44-
ID int64
45-
Email string
44+
ID int64
45+
Email string
46+
IsAdmin bool
4647
}
4748

4849
type QueriesWrapper struct {
@@ -396,7 +397,7 @@ func (s *DiscussService) CreateThreadPost(w http.ResponseWriter, r *http.Request
396397

397398
user, err := GetUser(r)
398399
if err != nil {
399-
s.logger.DebugContext(r.Context(), "CreateThreadPost", slog.String("user", user.Email), slog.String("user_id", strconv.FormatInt(user.ID, 10)))
400+
s.logger.DebugContext(r.Context(), "CreateThreadPost", slog.String("user", user.Email), slog.Int64("user_id", user.ID))
400401
s.renderError(w, http.StatusInternalServerError)
401402
return
402403
}
@@ -1024,16 +1025,17 @@ func UserMiddleware(s *DiscussService, next http.Handler) http.HandlerFunc {
10241025

10251026
ctx := context.WithValue(r.Context(), "email", email)
10261027

1027-
id, err := s.queries.CreateOrReturnID(ctx, email)
1028+
user, err := s.queries.CreateOrReturnID(ctx, email)
10281029
if err != nil {
10291030
s.logger.ErrorContext(ctx, err.Error())
10301031
s.renderError(w, http.StatusInternalServerError)
10311032
return
10321033
}
10331034

1034-
ctx = context.WithValue(ctx, "user_id", id)
1035+
ctx = context.WithValue(ctx, "user_id", user.ID)
1036+
ctx = context.WithValue(ctx, "is_admin", user.IsAdmin)
10351037

1036-
s.logger.DebugContext(ctx, "UserMiddleware", slog.String("email", email), slog.Int64("user_id", id))
1038+
s.logger.DebugContext(ctx, "UserMiddleware", slog.String("email", email), slog.Int64("user_id", user.ID), slog.Bool("is_admin", user.IsAdmin))
10371039

10381040
r = r.WithContext(ctx)
10391041

@@ -1049,13 +1051,19 @@ func GetUser(r *http.Request) (User, error) {
10491051
if uid, ok := ctx.Value("user_id").(int64); ok {
10501052
u.ID = uid
10511053
} else {
1052-
return User{}, errors.New("user ID not found in context or invalid type")
1054+
return User{}, errors.New("user_id not found in context or invalid type")
10531055
}
10541056

10551057
if email, ok := ctx.Value("email").(string); ok {
10561058
u.Email = email
10571059
} else {
1058-
return User{}, errors.New("user email not found in context or invalid type")
1060+
return User{}, errors.New("email not found in context or invalid type")
1061+
}
1062+
1063+
if isAdmin, ok := ctx.Value("is_admin").(bool); ok {
1064+
u.IsAdmin = isAdmin
1065+
} else {
1066+
return User{}, errors.New("is_admin not found in context or invalid type")
10591067
}
10601068

10611069
return u, nil

0 commit comments

Comments
 (0)