We can add and list groups now let's goooo
This commit is contained in:
		
							parent
							
								
									bdd03f96f7
								
							
						
					
					
						commit
						fc074d83c3
					
				
					 3 changed files with 34 additions and 14 deletions
				
			
		| 
						 | 
					@ -20,6 +20,7 @@ func main() {
 | 
				
			||||||
	app.GET("/auth/logout", api.AuthLogoutRedirect)
 | 
						app.GET("/auth/logout", api.AuthLogoutRedirect)
 | 
				
			||||||
	// Create & Update
 | 
						// Create & Update
 | 
				
			||||||
	app.POST("/post/user/update", api.CreateOrUpdateUser)
 | 
						app.POST("/post/user/update", api.CreateOrUpdateUser)
 | 
				
			||||||
 | 
						app.POST("/post/group", api.CreateDatabaseGroup)
 | 
				
			||||||
	// 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)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -103,15 +103,16 @@ func CreateOrUpdateUser(context *gin.Context) {
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	if databasecommands.GetDatabaseUserExists(GlobalDatabase, currentDiscordUser.Id) {
 | 
						if databasecommands.GetDatabaseUserExists(GlobalDatabase, currentDiscordUser.Id) {
 | 
				
			||||||
		dbOAuthToken := databasecommands.GetDatabaseUserToken(GlobalDatabase, currentDiscordUser.Id)
 | 
							dbOAuthToken := databasecommands.GetDatabaseUserToken(GlobalDatabase, currentDiscordUser.Id)
 | 
				
			||||||
		if dbOAuthToken == "" {
 | 
							if databasecommands.GetDatabaseUserLoggedIn(GlobalDatabase, dbOAuthToken) {
 | 
				
			||||||
			context.SetCookie("discord-oauthtoken", string(oauthTokenJSON), 0, "", GlobalConfig.API.Domain, false, false)
 | 
					 | 
				
			||||||
			err := databasecommands.UpdateDatabaseUser(GlobalDatabase, updatedDatabaseUser)
 | 
					 | 
				
			||||||
			if err != nil {
 | 
					 | 
				
			||||||
				log.Println(err)
 | 
					 | 
				
			||||||
				context.AbortWithStatus(http.StatusInternalServerError)
 | 
					 | 
				
			||||||
			}
 | 
					 | 
				
			||||||
		} else {
 | 
					 | 
				
			||||||
			context.SetCookie("discord-oauthtoken", dbOAuthToken, 0, "", GlobalConfig.API.Domain, false, false)
 | 
								context.SetCookie("discord-oauthtoken", dbOAuthToken, 0, "", GlobalConfig.API.Domain, false, false)
 | 
				
			||||||
 | 
								updatedDatabaseUser.LoginToken = string(oauthTokenJSON)
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								context.SetCookie("discord-oauthtoken", string(oauthTokenJSON), 0, "", GlobalConfig.API.Domain, false, false)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							err := databasecommands.UpdateDatabaseUser(GlobalDatabase, updatedDatabaseUser)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								log.Println(err)
 | 
				
			||||||
 | 
								context.AbortWithStatus(http.StatusInternalServerError)
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
		err := databasecommands.CreateDatabaseUser(GlobalDatabase, updatedDatabaseUser)
 | 
							err := databasecommands.CreateDatabaseUser(GlobalDatabase, updatedDatabaseUser)
 | 
				
			||||||
| 
						 | 
					@ -123,7 +124,22 @@ func CreateOrUpdateUser(context *gin.Context) {
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func CreateDatabaseGroup(context *gin.Context) {
 | 
					func CreateDatabaseGroup(context *gin.Context) {
 | 
				
			||||||
 | 
						GetIsUserAuthorized(context)
 | 
				
			||||||
 | 
						isAuthorized := context.GetBool("is-authorized")
 | 
				
			||||||
 | 
						log.Println(isAuthorized)
 | 
				
			||||||
 | 
						if isAuthorized {
 | 
				
			||||||
 | 
							name := context.Query("name")
 | 
				
			||||||
 | 
							if name != "" {
 | 
				
			||||||
 | 
								databasecommands.CreateDatabaseGroup(GlobalDatabase, databasemodels.Group{
 | 
				
			||||||
 | 
									Name: name,
 | 
				
			||||||
 | 
								})
 | 
				
			||||||
 | 
								context.Status(http.StatusOK)
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								context.AbortWithStatus(http.StatusBadRequest)
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							context.AbortWithStatus(http.StatusUnauthorized)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Read Endpoints (get/)
 | 
					// Read Endpoints (get/)
 | 
				
			||||||
| 
						 | 
					@ -159,6 +175,7 @@ func GetUserInfo(context *gin.Context) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func GetIsUserAuthorized(context *gin.Context) {
 | 
					func GetIsUserAuthorized(context *gin.Context) {
 | 
				
			||||||
	oauthTokenJSON, err := context.Cookie("discord-oauthtoken")
 | 
						oauthTokenJSON, err := context.Cookie("discord-oauthtoken")
 | 
				
			||||||
 | 
						log.Println(oauthTokenJSON)
 | 
				
			||||||
	if err == nil {
 | 
						if err == nil {
 | 
				
			||||||
		var oauthToken *oauth2.Token
 | 
							var oauthToken *oauth2.Token
 | 
				
			||||||
		err := json.Unmarshal([]byte(oauthTokenJSON), &oauthToken)
 | 
							err := json.Unmarshal([]byte(oauthTokenJSON), &oauthToken)
 | 
				
			||||||
| 
						 | 
					@ -168,18 +185,17 @@ func GetIsUserAuthorized(context *gin.Context) {
 | 
				
			||||||
					context.JSON(http.StatusOK, gin.H{
 | 
										context.JSON(http.StatusOK, gin.H{
 | 
				
			||||||
						"message": true,
 | 
											"message": true,
 | 
				
			||||||
					})
 | 
										})
 | 
				
			||||||
				} else {
 | 
										context.Set("is-authorized", true)
 | 
				
			||||||
					context.JSON(http.StatusUnauthorized, gin.H{
 | 
										log.Println("yessss")
 | 
				
			||||||
						"message": true,
 | 
										return
 | 
				
			||||||
					})
 | 
					 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				return
 | 
					 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	context.JSON(http.StatusUnauthorized, gin.H{
 | 
						context.JSON(http.StatusUnauthorized, gin.H{
 | 
				
			||||||
		"message": false,
 | 
							"message": false,
 | 
				
			||||||
	})
 | 
						})
 | 
				
			||||||
 | 
						context.Set("is-authorized", false)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func GetDatabaseGroup(context *gin.Context) {
 | 
					func GetDatabaseGroup(context *gin.Context) {
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -16,6 +16,7 @@ func InitializeDatabase() *gorm.DB {
 | 
				
			||||||
		log.Fatal("Failed to connect to database.")
 | 
							log.Fatal("Failed to connect to database.")
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	db.AutoMigrate(&databasemodels.User{})
 | 
						db.AutoMigrate(&databasemodels.User{})
 | 
				
			||||||
 | 
						db.AutoMigrate(&databasemodels.Group{})
 | 
				
			||||||
	return db
 | 
						return db
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -45,6 +46,7 @@ func GetDatabaseUserLoggedIn(db *gorm.DB, oauthTokenJSON string) bool {
 | 
				
			||||||
	if errors.Is(result.Error, gorm.ErrRecordNotFound) {
 | 
						if errors.Is(result.Error, gorm.ErrRecordNotFound) {
 | 
				
			||||||
		return false
 | 
							return false
 | 
				
			||||||
	} else {
 | 
						} else {
 | 
				
			||||||
 | 
							log.Println(queryUser)
 | 
				
			||||||
		return queryUser.LoggedIn
 | 
							return queryUser.LoggedIn
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -71,6 +73,7 @@ func LogoutDatabaseUser(db *gorm.DB, oauthToken string) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func CreateDatabaseGroup(db *gorm.DB, group databasemodels.Group) error {
 | 
					func CreateDatabaseGroup(db *gorm.DB, group databasemodels.Group) error {
 | 
				
			||||||
	result := db.Create(&group)
 | 
						result := db.Create(&group)
 | 
				
			||||||
 | 
						log.Println(group)
 | 
				
			||||||
	if result.Error != nil {
 | 
						if result.Error != nil {
 | 
				
			||||||
		return result.Error
 | 
							return result.Error
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue