Skip to content

Commit

Permalink
Prepare UserGroup models
Browse files Browse the repository at this point in the history
  • Loading branch information
slhmy committed Jun 23, 2024
1 parent ad8f7c7 commit 50d672a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
14 changes: 14 additions & 0 deletions cmd/init_db/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"fmt"

judge_model "github.com/oj-lab/oj-lab-platform/models/judge"
problem_model "github.com/oj-lab/oj-lab-platform/models/problem"
user_model "github.com/oj-lab/oj-lab-platform/models/user"
Expand All @@ -13,6 +15,9 @@ func main() {
db := gorm_agent.GetDefaultDB()
err := db.AutoMigrate(
&user_model.User{},
&user_model.Role{},
&user_model.UserGroup{},
&user_model.UserGroupMember{},
&problem_model.Problem{},
&judge_model.Judge{},
&judge_model.JudgeResult{},
Expand All @@ -32,6 +37,15 @@ func main() {
if err != nil {
panic("failed to create admin user")
}
err = user_model.CreateUserGroup(db, user_model.UserGroup{
OwnerAccount: "admin",
Members: []user_model.UserGroupMember{
{User: user_model.User{Account: "admin"}, Role: "admin"},
},
})
if err != nil {
panic(fmt.Sprintf("failed to create admin user group: %v", err))
}

err = user_model.CreateUser(db, user_model.User{
Name: "anonymous",
Expand Down
22 changes: 22 additions & 0 deletions models/user/user_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package user_model

import (
"github.com/google/uuid"
"github.com/oj-lab/oj-lab-platform/models"
)

type UserGroup struct {
models.MetaFields
UID uuid.UUID `json:"uid" gorm:"primaryKey"`
OwnerAccount string `json:"ownerAccount"`
Owner User `json:"owner" gorm:"foreignKey:OwnerAccount"`
Members []UserGroupMember `json:"members" gorm:"foreignKey:GroupUID"`
}

type UserGroupMember struct {
models.MetaFields
GroupUID uuid.UUID `json:"groupUID" gorm:"primaryKey"`
UserAccount string `json:"userAccount" gorm:"primaryKey"`
User User `json:"user"`
Role string `json:"role"`
}
16 changes: 16 additions & 0 deletions models/user/user_group_db.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package user_model

import (
"github.com/google/uuid"
"gorm.io/gorm"
)

func CreateUserGroup(tx *gorm.DB, group UserGroup) error {
group.UID = uuid.New()

return tx.Create(&group).Error
}

func CreateUserGroupMember(tx *gorm.DB, member UserGroupMember) error {
return tx.Create(&member).Error
}

0 comments on commit 50d672a

Please sign in to comment.