Skip to content

Commit

Permalink
🎨 DB周りを整理
Browse files Browse the repository at this point in the history
  • Loading branch information
pikachu0310 committed Jan 30, 2024
1 parent c26092c commit 65ca521
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
15 changes: 15 additions & 0 deletions internal/domains/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package domains

import (
"github.com/google/uuid"
"time"
)

type Session struct {
ID *uuid.UUID `db:"id"`
Redirect *string `db:"redirect"`
AccessToken *string `db:"access_token"`
RefreshToken *string `db:"refresh_token"`
ExpiresIn *int `db:"expires_in"`
CreatedAt *time.Time `db:"created_at"`
}
5 changes: 3 additions & 2 deletions internal/handler/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/traPtitech/game3-back/internal/api/models"
"github.com/traPtitech/game3-back/internal/domains"
"github.com/traPtitech/game3-back/internal/repository"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -127,7 +128,7 @@ func (h *Handler) OauthCallback(c echo.Context, params models.OauthCallbackParam
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
}

CreateSessionParams := &repository.Session{
CreateSessionParams := &domains.Session{
ID: &sessionToken,
AccessToken: tokenResponse.AccessToken,
RefreshToken: tokenResponse.RefreshToken,
Expand Down Expand Up @@ -159,7 +160,7 @@ func (h *Handler) Login(c echo.Context) error {
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate session token")
}

CreateSessionParams := &repository.Session{
CreateSessionParams := &domains.Session{
ID: &sessionToken,
Redirect: req.Redirect,
}
Expand Down
32 changes: 32 additions & 0 deletions internal/repository/session.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package repository

import (
"github.com/traPtitech/game3-back/internal/domains"
)

func (r *Repository) CreateSession(session *domains.Session) error {
_, err := r.db.Exec("INSERT INTO session (id, redirect) VALUES (?, ?)", *session.ID, *session.Redirect)
if err != nil {
return err
}

return nil
}

func (r *Repository) UpdateSession(session *domains.Session) error {
_, err := r.db.Exec("UPDATE session SET access_token = ?, refresh_token = ?, expires_in = ? WHERE id = ?", *session.AccessToken, *session.RefreshToken, *session.ExpiresIn, *session.ID)
if err != nil {
return err
}

return nil
}

func (r *Repository) GetSession(sessionID string) (*domains.Session, error) {
session := &domains.Session{}
if err := r.db.Get(session, "SELECT * FROM session WHERE id = ?", sessionID); err != nil {
return nil, err
}

return session, nil
}

0 comments on commit 65ca521

Please sign in to comment.