-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from nghenhan/feat/get-approver-from-database
feat: get approver from database
- Loading branch information
Showing
7 changed files
with
149 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package repo | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
) | ||
|
||
var DB *gorm.DB | ||
|
||
func ConnectDatabase() { | ||
dbURL := os.Getenv("DB_URL") | ||
if dbURL == "" { | ||
log.Fatal("DB_URL environment variable is not set") | ||
} | ||
|
||
database, err := gorm.Open(postgres.Open(dbURL), &gorm.Config{}) | ||
if err != nil { | ||
log.Fatal("Failed to connect to database: ", err) | ||
} | ||
|
||
DB = database | ||
fmt.Println("Successfully connected to database!") | ||
} | ||
|
||
func GetDB() *gorm.DB { | ||
return DB | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// user.go | ||
package repo | ||
|
||
// User represents the user table schema | ||
type User struct { | ||
TlpUsername string `gorm:"column:tlp_username"` | ||
DiscordID string `gorm:"column:discord_id;primaryKey"` | ||
DiscordName string `gorm:"column:discord_name"` | ||
IsApprover bool `gorm:"column:is_approver"` | ||
} | ||
|
||
// TableName overrides the table name | ||
func (User) TableName() string { | ||
return "user" | ||
} | ||
|
||
// GetApprovers returns all users with is_approver = true | ||
func GetApprovers() ([]User, error) { | ||
var approvers []User | ||
result := DB.Where("is_approver = ?", true).Find(&approvers) | ||
if result.Error != nil { | ||
return nil, result.Error | ||
} | ||
return approvers, nil | ||
} | ||
|
||
// GetUserByTlpUsername returns a user by their TLP username | ||
func GetUserByTlpUsername(tlpUsername string) (*User, error) { | ||
var user User | ||
result := DB.Where("tlp_username = ?", tlpUsername).First(&user) | ||
if result.Error != nil { | ||
return nil, result.Error | ||
} | ||
return &user, nil | ||
} |