Update backend #6
@@ -6,6 +6,7 @@ import (
|
|||||||
|
|
||||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/logger"
|
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/logger"
|
||||||
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/repositories"
|
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/repositories"
|
||||||
|
"gitea.d3m0k1d.ru/d3m0k1d/d3m0k1d.ru/backend/internal/storage"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"golang.org/x/oauth2"
|
"golang.org/x/oauth2"
|
||||||
"golang.org/x/oauth2/endpoints"
|
"golang.org/x/oauth2/endpoints"
|
||||||
@@ -89,28 +90,30 @@ func (h *AuthHandlers) CallbackGithub(c *gin.Context) {
|
|||||||
c.JSON(500, gin.H{"error": "get request failed to github", "details": err.Error()})
|
c.JSON(500, gin.H{"error": "get request failed to github", "details": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
var ghUser struct {
|
var ghUser storage.UserReg
|
||||||
ID int `json:"id"`
|
|
||||||
Login string `json:"login"`
|
|
||||||
Email string `json:"email"`
|
|
||||||
AvatarURL string `json:"avatar_url"`
|
|
||||||
}
|
|
||||||
err = json.NewDecoder(resp.Body).Decode(&ghUser)
|
err = json.NewDecoder(resp.Body).Decode(&ghUser)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
h.logger.Error("Decode failed: " + err.Error())
|
h.logger.Error("Decode failed: " + err.Error())
|
||||||
c.JSON(500, gin.H{"error": "decode failed", "details": err.Error()})
|
c.JSON(500, gin.H{"error": "decode failed", "details": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
isreg, err := h.repo.IsRegistered(c.Request.Context(), ghUser.ID)
|
isreg, err := h.repo.IsRegistered(c.Request.Context(), ghUser.GithubID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(500, gin.H{"error": "database error", "details": err.Error()})
|
c.JSON(500, gin.H{"error": "database error", "details": err.Error()})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if isreg {
|
if isreg {
|
||||||
c.JSON(200, gin.H{"user": ghUser})
|
c.JSON(200, gin.H{"user": ghUser})
|
||||||
return
|
|
||||||
|
reg := h.repo.Register(c.Request.Context(), ghUser)
|
||||||
|
if reg != nil {
|
||||||
|
c.JSON(500, gin.H{"error": "database error", "details": reg.Error()})
|
||||||
|
err := h.repo.Register(c.Request.Context(), ghUser)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(500, gin.H{"error": "database error", "details": err.Error()})
|
||||||
|
h.logger.Error("Database eer in gh callback handler")
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
h.logger.Info("200 OK - token received")
|
|
||||||
c.JSON(200, gin.H{"user": ghUser})
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,34 +1,34 @@
|
|||||||
package storage
|
package storage
|
||||||
|
|
||||||
type Post struct {
|
type Post struct {
|
||||||
ID int `db:"id"`
|
ID int `db:"id" json:"id"`
|
||||||
Title string `db:"title"`
|
Title string `db:"title" json:"title"`
|
||||||
Content string `db:"content"`
|
Content string `db:"content" json:"content"`
|
||||||
CreatedAt string `db:"created_at"`
|
CreatedAt string `db:"created_at" json:"created_at"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostReq struct {
|
type PostReq struct {
|
||||||
ID int `json:"id"`
|
ID int `db:"id" json:"id"`
|
||||||
Title string `json:"title"`
|
Title string `db:"title" json:"title"`
|
||||||
Content string `json:"content"`
|
Content string `db:"content" json:"content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type PostCreate struct {
|
type PostCreate struct {
|
||||||
Title string `json:"title"`
|
Title string `db:"title" json:"title"`
|
||||||
Content string `json:"content"`
|
Content string `db:"content" json:"content"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type User struct {
|
type User struct {
|
||||||
ID int `db:"id"`
|
ID int `db:"id" json:"id"`
|
||||||
Email string `db:"email"`
|
Email string `db:"email" json:"email"`
|
||||||
GithubID string `db:"github_id"`
|
GithubID int `db:"github_id" json:"github_id"`
|
||||||
GithubLogin string `db:"github_login"`
|
GithubLogin string `db:"github_login" json:"github_login"`
|
||||||
AvatarURL string `db:"avatar_url"`
|
AvatarURL string `db:"avatar_url" json:"avatar_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type UserReg struct {
|
type UserReg struct {
|
||||||
Email string `db:"email"`
|
Email string `db:"email" json:"email"`
|
||||||
GithubID string `db:"github_id"`
|
GithubID int `db:"github_id" json:"github_id"`
|
||||||
GithubLogin string `db:"github_login"`
|
GithubLogin string `db:"github_login" json:"github_login"`
|
||||||
AvatarURL string `db:"avatar_url"`
|
AvatarURL string `db:"avatar_url" json:"avatar_url"`
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user