aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2022-10-30 01:24:43 +0300
committerArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2022-10-30 01:24:43 +0300
commiteede38cb4f2905c2c3568620fd78a4174091fcdf (patch)
tree4d75a9a8fefdfe3871caab7d512bab1318837a3a
parentseperate user controller / work on (#4) (diff)
downloadlphub-eede38cb4f2905c2c3568620fd78a4174091fcdf.tar.gz
lphub-eede38cb4f2905c2c3568620fd78a4174091fcdf.tar.bz2
lphub-eede38cb4f2905c2c3568620fd78a4174091fcdf.zip
seperate user controller
Diffstat (limited to '')
-rw-r--r--backend/controllers/controllers.go71
1 files changed, 0 insertions, 71 deletions
diff --git a/backend/controllers/controllers.go b/backend/controllers/controllers.go
index 712b33c..ec973eb 100644
--- a/backend/controllers/controllers.go
+++ b/backend/controllers/controllers.go
@@ -4,13 +4,11 @@ import (
4 "log" 4 "log"
5 "net/http" 5 "net/http"
6 "os" 6 "os"
7 "regexp"
8 "time" 7 "time"
9 8
10 "github.com/gin-gonic/gin" 9 "github.com/gin-gonic/gin"
11 "github.com/golang-jwt/jwt/v4" 10 "github.com/golang-jwt/jwt/v4"
12 "github.com/pektezol/leastportals/backend/database" 11 "github.com/pektezol/leastportals/backend/database"
13 "github.com/pektezol/leastportals/backend/models"
14 "github.com/solovev/steam_go" 12 "github.com/solovev/steam_go"
15) 13)
16 14
@@ -88,72 +86,3 @@ func Logout(c *gin.Context) {
88 }) 86 })
89 } 87 }
90} 88}
91
92func Profile(c *gin.Context) {
93 // Check if user exists
94 user, exists := c.Get("user")
95 if !exists {
96 c.JSON(http.StatusUnauthorized, gin.H{
97 "code": http.StatusUnauthorized,
98 "output": gin.H{
99 "error": "User not logged in. Could be invalid token.",
100 },
101 })
102 } else {
103 user := user.(models.User)
104 c.JSON(http.StatusOK, gin.H{
105 "code": http.StatusOK,
106 "output": gin.H{
107 "username": user.Username,
108 "avatar": user.AvatarLink,
109 "types": user.TypeToString(),
110 },
111 "profile": true,
112 })
113 }
114}
115
116func User(c *gin.Context) {
117 id := c.Param("id")
118 // Check if id is all numbers and 17 length
119 match, _ := regexp.MatchString("^[0-9]{17}$", id)
120 if !match {
121 c.JSON(http.StatusNotFound, gin.H{
122 "code": http.StatusNotFound,
123 "output": gin.H{
124 "error": "User not found.",
125 },
126 })
127 return
128 }
129 // Check if user exists
130 var targetUser models.User
131 database.DB.QueryRow(`SELECT * FROM users WHERE steam_id = $1;`, id).Scan(
132 &targetUser.SteamID, &targetUser.Username, &targetUser.AvatarLink, &targetUser.CountryCode,
133 &targetUser.CreatedAt, &targetUser.UpdatedAt, &targetUser.UserType)
134 if targetUser.SteamID == "" {
135 // User does not exist
136 c.JSON(http.StatusNotFound, gin.H{
137 "code": http.StatusNotFound,
138 "output": gin.H{
139 "error": "User not found.",
140 },
141 })
142 return
143 }
144 // Target user exists
145 _, exists := c.Get("user")
146 if exists {
147 c.Redirect(http.StatusFound, "/api/v1/profile")
148 return
149 }
150 c.JSON(http.StatusOK, gin.H{
151 "code": http.StatusOK,
152 "output": gin.H{
153 "username": targetUser.Username,
154 "avatar": targetUser.AvatarLink,
155 "types": targetUser.TypeToString(),
156 },
157 "profile": false,
158 })
159}