Compare commits
No commits in common. "eb2ebff0ae54ba2dae5a0a5283aa86f5a05e03d2" and "217a9bf4167ed88466d3e169d9be78d24389b96d" have entirely different histories.
eb2ebff0ae
...
217a9bf416
@ -2,9 +2,6 @@ FROM golang:1.25-alpine
|
|||||||
|
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
|
|
||||||
# Solution t IP block?
|
|
||||||
ENV GOPROXY=https://goproxy.io,https://athens.azurefd.net,direct
|
|
||||||
|
|
||||||
COPY go.mod go.sum ./
|
COPY go.mod go.sum ./
|
||||||
RUN go mod download
|
RUN go mod download
|
||||||
|
|
||||||
|
|||||||
@ -237,10 +237,7 @@ func (r *RecipeRepository) GetRecipe(id int, userId *int) (*domain.Recipe, error
|
|||||||
&recipe.Created,
|
&recipe.Created,
|
||||||
&recipe.Deleted,
|
&recipe.Deleted,
|
||||||
); err != nil {
|
); err != nil {
|
||||||
if err == sql.ErrNoRows {
|
return nil, fmt.Errorf("Failed to location recipe in database: %s", err.Error())
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return nil, fmt.Errorf("Failed to location recipe (id: %d) in database: %s", id, err.Error())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse duration
|
// Parse duration
|
||||||
@ -303,7 +300,7 @@ func (r *RecipeRepository) GetRecipes(ids []int, userId *int) ([]domain.Recipe,
|
|||||||
|
|
||||||
for _, id := range ids {
|
for _, id := range ids {
|
||||||
recipe, err := r.GetRecipe(id, userId)
|
recipe, err := r.GetRecipe(id, userId)
|
||||||
if err != nil && err != sql.ErrNoRows {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,7 +4,6 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
sq "github.com/Masterminds/squirrel"
|
|
||||||
domain "github.com/haydenhargreaves/Potion/internal/domain/user"
|
domain "github.com/haydenhargreaves/Potion/internal/domain/user"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
_ "github.com/lib/pq"
|
_ "github.com/lib/pq"
|
||||||
@ -32,37 +31,42 @@ func NewUserRepository(db *sqlx.DB) domain.UserRepository {
|
|||||||
// best results, pair this function with the GetGoogleUser which will return the user if it can find
|
// best results, pair this function with the GetGoogleUser which will return the user if it can find
|
||||||
// it.
|
// it.
|
||||||
func (r *UserRepository) CreateGoogleUser(googleUserInfo *domain.GoogleUserInfo, googleRefreshToken string) (domain.User, error) {
|
func (r *UserRepository) CreateGoogleUser(googleUserInfo *domain.GoogleUserInfo, googleRefreshToken string) (domain.User, error) {
|
||||||
|
tx, err := r.db.Begin()
|
||||||
|
if err != nil {
|
||||||
|
return domain.User{}, err
|
||||||
|
}
|
||||||
|
|
||||||
if googleUserInfo == nil {
|
if googleUserInfo == nil {
|
||||||
return domain.User{}, fmt.Errorf("Google user info provided was nil")
|
return domain.User{}, fmt.Errorf("Google user info provided was nil")
|
||||||
}
|
}
|
||||||
|
|
||||||
psql := sq.StatementBuilder.PlaceholderFormat(sq.Dollar)
|
var user domain.User
|
||||||
|
query := `INSERT INTO users
|
||||||
|
(GoogleId, Name, Email, ImageUrl, GoogleRefreshToken)
|
||||||
|
VALUES ($1, $2, $3, $4, $5) RETURNING *;`
|
||||||
|
|
||||||
query := psql.
|
if err := tx.QueryRow(
|
||||||
Insert("users").
|
query,
|
||||||
Columns(
|
|
||||||
"googleid",
|
|
||||||
"name",
|
|
||||||
"email",
|
|
||||||
"imageurl",
|
|
||||||
"googlerefreshtoken").
|
|
||||||
Values(
|
|
||||||
googleUserInfo.Id,
|
googleUserInfo.Id,
|
||||||
googleUserInfo.Name,
|
googleUserInfo.Name,
|
||||||
googleUserInfo.Email,
|
googleUserInfo.Email,
|
||||||
googleUserInfo.Picture,
|
googleUserInfo.Picture,
|
||||||
googleRefreshToken,
|
googleRefreshToken,
|
||||||
).
|
).Scan(
|
||||||
Suffix("RETURNING *")
|
&user.Id,
|
||||||
|
&user.GoogleId,
|
||||||
_sql, args, err := query.ToSql()
|
&user.Name,
|
||||||
if err != nil {
|
&user.Email,
|
||||||
return domain.User{}, fmt.Errorf("Failed to construct sql query: %w", err)
|
&user.ImageUrl,
|
||||||
|
&user.GoogleRefreshToken,
|
||||||
|
&user.Created,
|
||||||
|
); err != nil {
|
||||||
|
tx.Rollback()
|
||||||
|
return domain.User{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
var user domain.User
|
if err := tx.Commit(); err != nil {
|
||||||
if err := r.db.Get(&user, _sql, args...); err != nil {
|
return domain.User{}, err
|
||||||
return domain.User{}, fmt.Errorf("Failed to create user: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return user, nil
|
return user, nil
|
||||||
@ -72,26 +76,23 @@ func (r *UserRepository) CreateGoogleUser(googleUserInfo *domain.GoogleUserInfo,
|
|||||||
// function is used when a user logs in with Google to prevent duplicate entries from being made. If
|
// function is used when a user logs in with Google to prevent duplicate entries from being made. If
|
||||||
// no user is found, this function will return a null pointer but not an error.
|
// no user is found, this function will return a null pointer but not an error.
|
||||||
func (r *UserRepository) GetGoogleUser(googleId string) (*domain.User, error) {
|
func (r *UserRepository) GetGoogleUser(googleId string) (*domain.User, error) {
|
||||||
psql := sq.StatementBuilder.PlaceholderFormat(sq.Dollar)
|
|
||||||
|
|
||||||
query := psql.
|
|
||||||
Select("*").
|
|
||||||
From("users").
|
|
||||||
Where(sq.Eq{
|
|
||||||
"GoogleId": googleId,
|
|
||||||
})
|
|
||||||
|
|
||||||
_sql, args, err := query.ToSql()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Failed to construct sql query: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var user domain.User
|
var user domain.User
|
||||||
if err := r.db.Get(&user, _sql, args...); err != nil {
|
query := `SELECT * FROM users WHERE GoogleId = $1`
|
||||||
|
|
||||||
|
if err := r.db.QueryRow(query, googleId).Scan(
|
||||||
|
&user.Id,
|
||||||
|
&user.GoogleId,
|
||||||
|
&user.Name,
|
||||||
|
&user.Email,
|
||||||
|
&user.ImageUrl,
|
||||||
|
&user.GoogleRefreshToken,
|
||||||
|
&user.Created,
|
||||||
|
); err != nil {
|
||||||
|
// If no user was found, don't error, just return
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("Failed to get Google user: %w", err)
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return &user, nil
|
return &user, nil
|
||||||
@ -102,19 +103,18 @@ func (r *UserRepository) GetGoogleUser(googleId string) (*domain.User, error) {
|
|||||||
// Callers are responsible for protecting against double nil results. Any errors will be bubbled
|
// Callers are responsible for protecting against double nil results. Any errors will be bubbled
|
||||||
// to the caller.
|
// to the caller.
|
||||||
func (r *UserRepository) GetUser(id int) (*domain.User, error) {
|
func (r *UserRepository) GetUser(id int) (*domain.User, error) {
|
||||||
psql := sq.StatementBuilder.PlaceholderFormat(sq.Dollar)
|
query := "SELECT * FROM users WHERE id = $1"
|
||||||
query := psql.
|
|
||||||
Select("*").
|
|
||||||
From("users").
|
|
||||||
Where(sq.Eq{"id": id})
|
|
||||||
|
|
||||||
_sql, args, err := query.ToSql()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Failed to construct sql query: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var user domain.User
|
var user domain.User
|
||||||
if err := r.db.Get(&user, _sql, args...); err != nil {
|
if err := r.db.QueryRow(query, id).Scan(
|
||||||
|
&user.Id,
|
||||||
|
&user.GoogleId,
|
||||||
|
&user.Name,
|
||||||
|
&user.Email,
|
||||||
|
&user.ImageUrl,
|
||||||
|
&user.GoogleRefreshToken,
|
||||||
|
&user.Created,
|
||||||
|
); err != nil {
|
||||||
// If no user was found, don't error, just return
|
// If no user was found, don't error, just return
|
||||||
if err == sql.ErrNoRows {
|
if err == sql.ErrNoRows {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user