Starting to implement database model interfaces.
This commit is contained in:
		
							parent
							
								
									10db069eea
								
							
						
					
					
						commit
						bdd03f96f7
					
				
					 4 changed files with 95 additions and 33 deletions
				
			
		| 
						 | 
					@ -23,6 +23,8 @@ func main() {
 | 
				
			||||||
	// Read
 | 
						// Read
 | 
				
			||||||
	app.GET("/get/user/info", api.GetUserInfo)
 | 
						app.GET("/get/user/info", api.GetUserInfo)
 | 
				
			||||||
	app.GET("/get/user/authorized", api.GetIsUserAuthorized)
 | 
						app.GET("/get/user/authorized", api.GetIsUserAuthorized)
 | 
				
			||||||
 | 
						app.GET("/get/group", api.GetDatabaseGroup)
 | 
				
			||||||
 | 
						app.GET("/get/groups", api.GetDatabaseGroups)
 | 
				
			||||||
	// Delete
 | 
						// Delete
 | 
				
			||||||
	app.Run(":31337")
 | 
						app.Run(":31337")
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -21,6 +21,31 @@ var GlobalDatabase *gorm.DB
 | 
				
			||||||
var GlobalConfig configserver.AppConfig
 | 
					var GlobalConfig configserver.AppConfig
 | 
				
			||||||
var GlobalOAuth *oauth2.Config
 | 
					var GlobalOAuth *oauth2.Config
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Private Functions
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func getDiscordUser(context *gin.Context, oauthToken *oauth2.Token) authdiscord.DiscordUser {
 | 
				
			||||||
 | 
						response, err := GlobalOAuth.Client(context, oauthToken).Get("https://discord.com/api/users/@me")
 | 
				
			||||||
 | 
						if err != nil || response.StatusCode != 200 {
 | 
				
			||||||
 | 
							responseMessage := ""
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								responseMessage = err.Error()
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								responseMessage = response.Status
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							log.Println(responseMessage)
 | 
				
			||||||
 | 
							log.Println("Assuming the Discord OAuth Key has expired.")
 | 
				
			||||||
 | 
							context.Redirect(http.StatusUnauthorized, "https://localhost:15995/logout")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						defer response.Body.Close()
 | 
				
			||||||
 | 
						body, err := io.ReadAll(response.Body)
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							log.Println(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						var user authdiscord.DiscordUser
 | 
				
			||||||
 | 
						json.Unmarshal(body, &user)
 | 
				
			||||||
 | 
						return user
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Authentication Workflow
 | 
					// Authentication Workflow
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func AuthCallback(context *gin.Context) {
 | 
					func AuthCallback(context *gin.Context) {
 | 
				
			||||||
| 
						 | 
					@ -76,7 +101,7 @@ func CreateOrUpdateUser(context *gin.Context) {
 | 
				
			||||||
		LoginToken:       string(oauthTokenJSON),
 | 
							LoginToken:       string(oauthTokenJSON),
 | 
				
			||||||
		LoggedIn:         true,
 | 
							LoggedIn:         true,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if databasecommands.DatabaseUserExists(GlobalDatabase, currentDiscordUser.Id) {
 | 
						if databasecommands.GetDatabaseUserExists(GlobalDatabase, currentDiscordUser.Id) {
 | 
				
			||||||
		dbOAuthToken := databasecommands.GetDatabaseUserToken(GlobalDatabase, currentDiscordUser.Id)
 | 
							dbOAuthToken := databasecommands.GetDatabaseUserToken(GlobalDatabase, currentDiscordUser.Id)
 | 
				
			||||||
		if dbOAuthToken == "" {
 | 
							if dbOAuthToken == "" {
 | 
				
			||||||
			context.SetCookie("discord-oauthtoken", string(oauthTokenJSON), 0, "", GlobalConfig.API.Domain, false, false)
 | 
								context.SetCookie("discord-oauthtoken", string(oauthTokenJSON), 0, "", GlobalConfig.API.Domain, false, false)
 | 
				
			||||||
| 
						 | 
					@ -97,33 +122,12 @@ func CreateOrUpdateUser(context *gin.Context) {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Read Endpoints (get/)
 | 
					func CreateDatabaseGroup(context *gin.Context) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func getDiscordUser(context *gin.Context, oauthToken *oauth2.Token) authdiscord.DiscordUser {
 | 
					 | 
				
			||||||
	response, err := GlobalOAuth.Client(context.Request.Context(), oauthToken).Get("https://discord.com/api/users/@me")
 | 
					 | 
				
			||||||
	if err != nil || response.StatusCode != 200 {
 | 
					 | 
				
			||||||
		responseMessage := ""
 | 
					 | 
				
			||||||
		if err != nil {
 | 
					 | 
				
			||||||
			responseMessage = err.Error()
 | 
					 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			responseMessage = response.Status
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
		context.JSON(http.StatusInternalServerError, gin.H{
 | 
					 | 
				
			||||||
			"message": responseMessage,
 | 
					 | 
				
			||||||
		})
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	defer response.Body.Close()
 | 
					 | 
				
			||||||
	body, err := io.ReadAll(response.Body)
 | 
					 | 
				
			||||||
	if err != nil {
 | 
					 | 
				
			||||||
		context.JSON(http.StatusInternalServerError, gin.H{
 | 
					 | 
				
			||||||
			"message": err.Error(),
 | 
					 | 
				
			||||||
		})
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	var user authdiscord.DiscordUser
 | 
					 | 
				
			||||||
	json.Unmarshal(body, &user)
 | 
					 | 
				
			||||||
	return user
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Read Endpoints (get/)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func GetUserInfo(context *gin.Context) {
 | 
					func GetUserInfo(context *gin.Context) {
 | 
				
			||||||
	oauthTokenJSON, err := context.Cookie("discord-oauthtoken")
 | 
						oauthTokenJSON, err := context.Cookie("discord-oauthtoken")
 | 
				
			||||||
	if err == nil {
 | 
						if err == nil {
 | 
				
			||||||
| 
						 | 
					@ -132,7 +136,7 @@ func GetUserInfo(context *gin.Context) {
 | 
				
			||||||
		if err == nil {
 | 
							if err == nil {
 | 
				
			||||||
			if oauthToken.Valid() {
 | 
								if oauthToken.Valid() {
 | 
				
			||||||
				user := getDiscordUser(context, oauthToken)
 | 
									user := getDiscordUser(context, oauthToken)
 | 
				
			||||||
				if databasecommands.DatabaseUserLoggedIn(GlobalDatabase, user.Id) {
 | 
									if databasecommands.GetDatabaseUserLoggedIn(GlobalDatabase, oauthTokenJSON) {
 | 
				
			||||||
					context.JSON(http.StatusOK, user)
 | 
										context.JSON(http.StatusOK, user)
 | 
				
			||||||
				} else {
 | 
									} else {
 | 
				
			||||||
					context.Redirect(http.StatusTemporaryRedirect, "http://localhost:31337/auth/logout")
 | 
										context.Redirect(http.StatusTemporaryRedirect, "http://localhost:31337/auth/logout")
 | 
				
			||||||
| 
						 | 
					@ -160,8 +164,7 @@ func GetIsUserAuthorized(context *gin.Context) {
 | 
				
			||||||
		err := json.Unmarshal([]byte(oauthTokenJSON), &oauthToken)
 | 
							err := json.Unmarshal([]byte(oauthTokenJSON), &oauthToken)
 | 
				
			||||||
		if err == nil {
 | 
							if err == nil {
 | 
				
			||||||
			if oauthToken.Valid() {
 | 
								if oauthToken.Valid() {
 | 
				
			||||||
				user := getDiscordUser(context, oauthToken)
 | 
									if databasecommands.GetDatabaseUserLoggedIn(GlobalDatabase, oauthTokenJSON) {
 | 
				
			||||||
				if databasecommands.DatabaseUserLoggedIn(GlobalDatabase, user.Id) {
 | 
					 | 
				
			||||||
					context.JSON(http.StatusOK, gin.H{
 | 
										context.JSON(http.StatusOK, gin.H{
 | 
				
			||||||
						"message": true,
 | 
											"message": true,
 | 
				
			||||||
					})
 | 
										})
 | 
				
			||||||
| 
						 | 
					@ -179,4 +182,23 @@ func GetIsUserAuthorized(context *gin.Context) {
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func GetDatabaseGroup(context *gin.Context) {
 | 
				
			||||||
 | 
						groupName := context.Query("group")
 | 
				
			||||||
 | 
						if groupName != "" {
 | 
				
			||||||
 | 
							group := databasecommands.GetDatabaseGroup(GlobalDatabase, groupName)
 | 
				
			||||||
 | 
							context.JSON(http.StatusOK, group)
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							context.JSON(http.StatusBadRequest, gin.H{
 | 
				
			||||||
 | 
								"name": "",
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func GetDatabaseGroups(context *gin.Context) {
 | 
				
			||||||
 | 
						groups := databasecommands.GetDatabaseGroups(GlobalDatabase)
 | 
				
			||||||
 | 
						context.JSON(http.StatusOK, gin.H{
 | 
				
			||||||
 | 
							"groups": groups,
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Delete Endpoints (delete/)
 | 
					// Delete Endpoints (delete/)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -29,7 +29,7 @@ func GetDatabaseUserToken(db *gorm.DB, id string) string {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func DatabaseUserExists(db *gorm.DB, id string) bool {
 | 
					func GetDatabaseUserExists(db *gorm.DB, id string) bool {
 | 
				
			||||||
	var queryUser databasemodels.User
 | 
						var queryUser databasemodels.User
 | 
				
			||||||
	result := db.Where("id = ?", id).Take(&queryUser)
 | 
						result := db.Where("id = ?", id).Take(&queryUser)
 | 
				
			||||||
	if errors.Is(result.Error, gorm.ErrRecordNotFound) {
 | 
						if errors.Is(result.Error, gorm.ErrRecordNotFound) {
 | 
				
			||||||
| 
						 | 
					@ -39,9 +39,9 @@ func DatabaseUserExists(db *gorm.DB, id string) bool {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func DatabaseUserLoggedIn(db *gorm.DB, id string) bool {
 | 
					func GetDatabaseUserLoggedIn(db *gorm.DB, oauthTokenJSON string) bool {
 | 
				
			||||||
	var queryUser databasemodels.User
 | 
						var queryUser databasemodels.User
 | 
				
			||||||
	result := db.Where("id = ?", id).Take(&queryUser)
 | 
						result := db.Where("login_token = ?", oauthTokenJSON).Take(&queryUser)
 | 
				
			||||||
	if errors.Is(result.Error, gorm.ErrRecordNotFound) {
 | 
						if errors.Is(result.Error, gorm.ErrRecordNotFound) {
 | 
				
			||||||
		return false
 | 
							return false
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
| 
						 | 
					@ -67,5 +67,30 @@ func CreateDatabaseUser(db *gorm.DB, user databasemodels.User) error {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func LogoutDatabaseUser(db *gorm.DB, oauthToken string) {
 | 
					func LogoutDatabaseUser(db *gorm.DB, oauthToken string) {
 | 
				
			||||||
	db.Model(&databasemodels.User{}).Where("login_token = ?", oauthToken).Update("logged_in", false)
 | 
						db.Model(&databasemodels.User{}).Where("login_token = ?", oauthToken).Update("logged_in", false)
 | 
				
			||||||
	db.Model(&databasemodels.User{}).Where("login_token = ?", oauthToken).Update("login_token", "")
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func CreateDatabaseGroup(db *gorm.DB, group databasemodels.Group) error {
 | 
				
			||||||
 | 
						result := db.Create(&group)
 | 
				
			||||||
 | 
						if result.Error != nil {
 | 
				
			||||||
 | 
							return result.Error
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func GetDatabaseGroup(db *gorm.DB, inputGroup string) databasemodels.Group {
 | 
				
			||||||
 | 
						var outputGroup databasemodels.Group
 | 
				
			||||||
 | 
						result := db.Model(&databasemodels.Group{}).Where("name = ?", inputGroup).Take(&outputGroup)
 | 
				
			||||||
 | 
						if result.Error != nil {
 | 
				
			||||||
 | 
							return databasemodels.Group{}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return outputGroup
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func GetDatabaseGroups(db *gorm.DB) []databasemodels.Group {
 | 
				
			||||||
 | 
						var outputGroup []databasemodels.Group
 | 
				
			||||||
 | 
						result := db.Find(&outputGroup)
 | 
				
			||||||
 | 
						if result.Error != nil {
 | 
				
			||||||
 | 
							return []databasemodels.Group{}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return outputGroup
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -14,15 +14,18 @@ type User struct {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Person struct {
 | 
					type Person struct {
 | 
				
			||||||
 | 
						gorm.Model
 | 
				
			||||||
	Name   string
 | 
						Name   string
 | 
				
			||||||
	Groups []Group // Unique
 | 
						Groups []Group // Unique
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Group struct {
 | 
					type Group struct {
 | 
				
			||||||
	Name string
 | 
						gorm.Model
 | 
				
			||||||
 | 
						Name string `json:"name"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Character struct {
 | 
					type Character struct {
 | 
				
			||||||
 | 
						gorm.Model
 | 
				
			||||||
	Name         string
 | 
						Name         string
 | 
				
			||||||
	Owners       []Person // Unique
 | 
						Owners       []Person // Unique
 | 
				
			||||||
	Roles        []Role   // Unique
 | 
						Roles        []Role   // Unique
 | 
				
			||||||
| 
						 | 
					@ -31,35 +34,42 @@ type Character struct {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Role struct {
 | 
					type Role struct {
 | 
				
			||||||
 | 
						gorm.Model
 | 
				
			||||||
	Name       string
 | 
						Name       string
 | 
				
			||||||
	Tiers      []Tier
 | 
						Tiers      []Tier
 | 
				
			||||||
	Visibility []Group // Unique
 | 
						Visibility []Group // Unique
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Tier struct {
 | 
					type Tier struct {
 | 
				
			||||||
 | 
						gorm.Model
 | 
				
			||||||
	FunctionSets []FunctionSet
 | 
						FunctionSets []FunctionSet
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type FunctionSet struct {
 | 
					type FunctionSet struct {
 | 
				
			||||||
 | 
						gorm.Model
 | 
				
			||||||
	Functions []Function
 | 
						Functions []Function
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Function struct {
 | 
					type Function struct {
 | 
				
			||||||
 | 
						gorm.Model
 | 
				
			||||||
	Name         string
 | 
						Name         string
 | 
				
			||||||
	Tags         FunctionTag
 | 
						Tags         FunctionTag
 | 
				
			||||||
	Requirements []Function
 | 
						Requirements []Function
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type FunctionTag struct {
 | 
					type FunctionTag struct {
 | 
				
			||||||
 | 
						gorm.Model
 | 
				
			||||||
	Name string
 | 
						Name string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type InventorySlot struct {
 | 
					type InventorySlot struct {
 | 
				
			||||||
 | 
						gorm.Model
 | 
				
			||||||
	Item     Item
 | 
						Item     Item
 | 
				
			||||||
	Quantity int64 // Positive
 | 
						Quantity int64 // Positive
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Item struct {
 | 
					type Item struct {
 | 
				
			||||||
 | 
						gorm.Model
 | 
				
			||||||
	Name                string
 | 
						Name                string
 | 
				
			||||||
	Functions           []Function
 | 
						Functions           []Function
 | 
				
			||||||
	FlavorText          string
 | 
						FlavorText          string
 | 
				
			||||||
| 
						 | 
					@ -71,10 +81,12 @@ type Item struct {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type ItemTag struct {
 | 
					type ItemTag struct {
 | 
				
			||||||
 | 
						gorm.Model
 | 
				
			||||||
	Naem string
 | 
						Naem string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Customization struct {
 | 
					type Customization struct {
 | 
				
			||||||
 | 
						gorm.Model
 | 
				
			||||||
	Name                string
 | 
						Name                string
 | 
				
			||||||
	Functions           []Function
 | 
						Functions           []Function
 | 
				
			||||||
	FlavorText          string
 | 
						FlavorText          string
 | 
				
			||||||
| 
						 | 
					@ -85,6 +97,7 @@ type Customization struct {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Schematic struct {
 | 
					type Schematic struct {
 | 
				
			||||||
 | 
						gorm.Model
 | 
				
			||||||
	Material     []InventorySlot
 | 
						Material     []InventorySlot
 | 
				
			||||||
	Tools        []InventorySlot
 | 
						Tools        []InventorySlot
 | 
				
			||||||
	Requirements []Function
 | 
						Requirements []Function
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue