Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor public user model #354

Merged
merged 5 commits into from
Oct 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions backend/handlers/admin/adduser.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ func AddUserHandler(loadEconConfig setup.EconConfigLoader) func(http.ResponseWri

appConfig := loadEconConfig()
user := models.User{
Username: req.Username,
DisplayName: util.UniqueDisplayName(db),
UserType: "REGULAR",
InitialAccountBalance: appConfig.Economics.User.InitialAccountBalance,
AccountBalance: appConfig.Economics.User.InitialAccountBalance,
PersonalEmoji: randomEmoji(),
PublicUser: models.PublicUser{
Username: req.Username,
DisplayName: util.UniqueDisplayName(db),
UserType: "REGULAR",
InitialAccountBalance: appConfig.Economics.User.InitialAccountBalance,
AccountBalance: appConfig.Economics.User.InitialAccountBalance,
PersonalEmoji: randomEmoji(),
},
PrivateUser: models.PrivateUser{
Email: util.UniqueEmail(db),
APIKey: util.GenerateUniqueApiKey(db),
Expand Down
9 changes: 7 additions & 2 deletions backend/handlers/bets/betutils/feeutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ func TestGetUserInitialBetFee(t *testing.T) {
}

appConfig = setuptesting.MockEconomicConfig()
user := &models.User{Username: "testuser", AccountBalance: 1000}
user := &models.User{
PublicUser: models.PublicUser{
Username: "testuser",
AccountBalance: 1000,
},
}
if err := db.Create(user).Error; err != nil {
t.Fatalf("Failed to save user to database: %v", err)
}
Expand Down Expand Up @@ -85,7 +90,7 @@ func TestGetSumBetFees(t *testing.T) {
appConfig = setuptesting.MockEconomicConfig()

// Create a test user
user := &models.User{Username: "testuser"}
user := &models.User{PublicUser: models.PublicUser{Username: "testuser"}}

// Scenario 1: User has no bets, buys shares, gets initial fee
buyBet := models.Bet{MarketID: 1, Amount: 100}
Expand Down
2 changes: 1 addition & 1 deletion backend/handlers/markets/listmarkets.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type ListMarketsResponse struct {

type MarketOverview struct {
Market marketpublicresponse.PublicResponseMarket `json:"market"`
Creator usersHandlers.PublicUserType `json:"creator"`
Creator models.PublicUser `json:"creator"`
LastProbability float64 `json:"lastProbability"`
NumUsers int `json:"numUsers"`
TotalVolume int64 `json:"totalVolume"`
Expand Down
6 changes: 1 addition & 5 deletions backend/handlers/markets/marketdetailshandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// MarketDetailResponse defines the structure for the market detail response
type MarketDetailHandlerResponse struct {
Market marketpublicresponse.PublicResponseMarket `json:"market"`
Creator usersHandlers.PublicUserType `json:"creator"`
Creator models.PublicUser `json:"creator"`
ProbabilityChanges []wpam.ProbabilityChange `json:"probabilityChanges"`
NumUsers int `json:"numUsers"`
TotalVolume int64 `json:"totalVolume"`
Expand Down Expand Up @@ -55,10 +55,6 @@ func MarketDetailsHandler(w http.ResponseWriter, r *http.Request) {

// find the number of users on the market
numUsers := models.GetNumMarketUsers(bets)
if err != nil {
http.Error(w, "Error retrieving number of users.", http.StatusInternalServerError)
return
}

// market volume is equivalent to the sum of all bets
marketVolume := marketmath.GetMarketVolume(bets)
Expand Down
34 changes: 3 additions & 31 deletions backend/handlers/users/publicuser.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,6 @@ import (
"gorm.io/gorm"
)

// PublicUserType is a struct for user data that is safe to send to the client for Profiles
type PublicUserType struct {
Username string `json:"username"`
DisplayName string `json:"displayname" gorm:"unique;not null"`
UserType string `json:"usertype"`
InitialAccountBalance int64 `json:"initialAccountBalance"`
AccountBalance int64 `json:"accountBalance"`
PersonalEmoji string `json:"personalEmoji,omitempty"`
Description string `json:"description,omitempty"`
PersonalLink1 string `json:"personalink1,omitempty"`
PersonalLink2 string `json:"personalink2,omitempty"`
PersonalLink3 string `json:"personalink3,omitempty"`
PersonalLink4 string `json:"personalink4,omitempty"`
}

func GetPublicUserResponse(w http.ResponseWriter, r *http.Request) {
// Extract the username from the URL
vars := mux.Vars(r)
Expand All @@ -38,23 +23,10 @@ func GetPublicUserResponse(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(response)
}

// Function to get the Info From the Database
func GetPublicUserInfo(db *gorm.DB, username string) PublicUserType {

// Function to get the users public info From the Database
func GetPublicUserInfo(db *gorm.DB, username string) models.PublicUser {
var user models.User
db.Where("username = ?", username).First(&user)

return PublicUserType{
Username: user.Username,
DisplayName: user.DisplayName,
UserType: user.UserType,
InitialAccountBalance: user.InitialAccountBalance,
AccountBalance: user.AccountBalance,
PersonalEmoji: user.PersonalEmoji,
Description: user.Description,
PersonalLink1: user.PersonalLink1,
PersonalLink2: user.PersonalLink2,
PersonalLink3: user.PersonalLink3,
PersonalLink4: user.PersonalLink4,
}
return user.PublicUser
}
10 changes: 7 additions & 3 deletions backend/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ import (

type User struct {
gorm.Model
ID int64 `json:"id" gorm:"primary_key"`
ID int64 `json:"id" gorm:"primary_key"`
PublicUser
PrivateUser
MustChangePassword bool `json:"mustChangePassword" gorm:"default:true"`
}

type PublicUser struct {
Username string `json:"username" gorm:"unique;not null"`
DisplayName string `json:"displayname" gorm:"unique;not null"`
Password string `json:"password,omitempty" gorm:"not null"`
Expand All @@ -20,8 +26,6 @@ type User struct {
PersonalLink2 string `json:"personalink2,omitempty"`
PersonalLink3 string `json:"personalink3,omitempty"`
PersonalLink4 string `json:"personalink4,omitempty"`
PrivateUser
MustChangePassword bool `json:"mustChangePassword" gorm:"default:true"`
}

type PrivateUser struct {
Expand Down
16 changes: 9 additions & 7 deletions backend/seed/seed.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,15 @@ func SeedUsers(db *gorm.DB) {
if count == 0 {
// No admin user found, create one
adminUser := models.User{
Username: "admin",
DisplayName: "Administrator",
UserType: "ADMIN",
InitialAccountBalance: config.Economics.User.InitialAccountBalance,
AccountBalance: config.Economics.User.InitialAccountBalance,
PersonalEmoji: "NONE",
Description: "Administrator",
PublicUser: models.PublicUser{
Username: "admin",
DisplayName: "Administrator",
UserType: "ADMIN",
InitialAccountBalance: config.Economics.User.InitialAccountBalance,
AccountBalance: config.Economics.User.InitialAccountBalance,
PersonalEmoji: "NONE",
Description: "Administrator",
},
PrivateUser: models.PrivateUser{
Email: "[email protected]",
APIKey: "NONE",
Expand Down
Loading