feat: sqlc, nix formatting
This commit is contained in:
@ -11,26 +11,22 @@ import (
|
||||
_ "crypto/sha256" // Crypto
|
||||
|
||||
"connectrpc.com/connect"
|
||||
"github.com/aarondl/opt/omit"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
userv1 "github.com/spotdemo4/trevstack/server/internal/connect/user/v1"
|
||||
"github.com/spotdemo4/trevstack/server/internal/connect/user/v1/userv1connect"
|
||||
"github.com/spotdemo4/trevstack/server/internal/interceptors"
|
||||
"github.com/spotdemo4/trevstack/server/internal/models"
|
||||
userv1 "github.com/spotdemo4/trevstack/server/internal/services/user/v1"
|
||||
"github.com/spotdemo4/trevstack/server/internal/services/user/v1/userv1connect"
|
||||
"github.com/stephenafamo/bob"
|
||||
"github.com/spotdemo4/trevstack/server/internal/sqlc"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type AuthHandler struct {
|
||||
db *bob.DB
|
||||
db *sqlc.Queries
|
||||
key []byte
|
||||
}
|
||||
|
||||
func (h *AuthHandler) Login(ctx context.Context, req *connect.Request[userv1.LoginRequest]) (*connect.Response[userv1.LoginResponse], error) {
|
||||
// Get user
|
||||
user, err := models.Users.Query(
|
||||
models.SelectWhere.Users.Username.EQ(req.Msg.Username),
|
||||
).One(ctx, h.db)
|
||||
user, err := h.db.GetUserbyUsername(ctx, req.Msg.Username)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, connect.NewError(connect.CodePermissionDenied, err)
|
||||
@ -47,7 +43,7 @@ func (h *AuthHandler) Login(ctx context.Context, req *connect.Request[userv1.Log
|
||||
// Generate JWT
|
||||
t := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.RegisteredClaims{
|
||||
Issuer: "trevstack",
|
||||
Subject: strconv.FormatUint(uint64(user.ID), 10),
|
||||
Subject: strconv.FormatInt(user.ID, 10),
|
||||
IssuedAt: &jwt.NumericDate{
|
||||
Time: time.Now(),
|
||||
},
|
||||
@ -80,16 +76,13 @@ func (h *AuthHandler) Login(ctx context.Context, req *connect.Request[userv1.Log
|
||||
|
||||
func (h *AuthHandler) SignUp(ctx context.Context, req *connect.Request[userv1.SignUpRequest]) (*connect.Response[userv1.SignUpResponse], error) {
|
||||
// Get user
|
||||
user, err := models.Users.Query(
|
||||
models.SelectWhere.Users.Username.EQ(req.Msg.Username),
|
||||
).One(ctx, h.db)
|
||||
_, err := h.db.GetUserbyUsername(ctx, req.Msg.Username)
|
||||
if err != nil {
|
||||
if !errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, connect.NewError(connect.CodeInternal, err)
|
||||
}
|
||||
}
|
||||
if user != nil {
|
||||
return nil, connect.NewError(connect.CodeAlreadyExists, errors.New("username already exists"))
|
||||
} else {
|
||||
return nil, connect.NewError(connect.CodeAlreadyExists, err)
|
||||
}
|
||||
|
||||
// Check if confirmation passwords match
|
||||
@ -104,10 +97,10 @@ func (h *AuthHandler) SignUp(ctx context.Context, req *connect.Request[userv1.Si
|
||||
}
|
||||
|
||||
// Create user
|
||||
_, err = models.Users.Insert(&models.UserSetter{
|
||||
Username: omit.From(req.Msg.Username),
|
||||
Password: omit.From(string(hash)),
|
||||
}).One(ctx, h.db)
|
||||
_, err = h.db.InsertUser(ctx, sqlc.InsertUserParams{
|
||||
Username: req.Msg.Username,
|
||||
Password: string(hash),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, connect.NewError(connect.CodeInternal, err)
|
||||
}
|
||||
@ -221,7 +214,7 @@ func (h *AuthHandler) Logout(_ context.Context, _ *connect.Request[userv1.Logout
|
||||
// return res, nil
|
||||
// }
|
||||
|
||||
func NewAuthHandler(db *bob.DB, key string) (string, http.Handler) {
|
||||
func NewAuthHandler(db *sqlc.Queries, key string) (string, http.Handler) {
|
||||
interceptors := connect.WithInterceptors(interceptors.NewRateLimitInterceptor(key))
|
||||
|
||||
return userv1connect.NewAuthServiceHandler(
|
||||
|
@ -9,27 +9,25 @@ import (
|
||||
"time"
|
||||
|
||||
"connectrpc.com/connect"
|
||||
"github.com/aarondl/opt/omit"
|
||||
"github.com/aarondl/opt/omitnull"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
userv1 "github.com/spotdemo4/trevstack/server/internal/connect/user/v1"
|
||||
"github.com/spotdemo4/trevstack/server/internal/connect/user/v1/userv1connect"
|
||||
"github.com/spotdemo4/trevstack/server/internal/interceptors"
|
||||
"github.com/spotdemo4/trevstack/server/internal/models"
|
||||
userv1 "github.com/spotdemo4/trevstack/server/internal/services/user/v1"
|
||||
"github.com/spotdemo4/trevstack/server/internal/services/user/v1/userv1connect"
|
||||
"github.com/stephenafamo/bob"
|
||||
"github.com/spotdemo4/trevstack/server/internal/sqlc"
|
||||
"github.com/spotdemo4/trevstack/server/internal/util"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func userToConnect(item *models.User) *userv1.User {
|
||||
func userToConnect(item sqlc.User) *userv1.User {
|
||||
return &userv1.User{
|
||||
Id: item.ID,
|
||||
Username: item.Username,
|
||||
ProfilePictureId: item.ProfilePictureID.Ptr(),
|
||||
ProfilePictureId: item.ProfilePictureID,
|
||||
}
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
db *bob.DB
|
||||
db *sqlc.Queries
|
||||
key []byte
|
||||
}
|
||||
|
||||
@ -40,9 +38,7 @@ func (h *Handler) GetUser(ctx context.Context, _ *connect.Request[userv1.GetUser
|
||||
}
|
||||
|
||||
// Get user
|
||||
user, err := models.Users.Query(
|
||||
models.SelectWhere.Users.ID.EQ(userid),
|
||||
).One(ctx, h.db)
|
||||
user, err := h.db.GetUser(ctx, userid)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, connect.NewError(connect.CodeNotFound, err)
|
||||
@ -64,9 +60,7 @@ func (h *Handler) UpdatePassword(ctx context.Context, req *connect.Request[userv
|
||||
}
|
||||
|
||||
// Get user
|
||||
user, err := models.Users.Query(
|
||||
models.SelectWhere.Users.ID.EQ(userid),
|
||||
).One(ctx, h.db)
|
||||
user, err := h.db.GetUser(ctx, userid)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, connect.NewError(connect.CodeNotFound, err)
|
||||
@ -90,8 +84,9 @@ func (h *Handler) UpdatePassword(ctx context.Context, req *connect.Request[userv
|
||||
}
|
||||
|
||||
// Update password
|
||||
err = user.Update(ctx, h.db, &models.UserSetter{
|
||||
Password: omit.From(string(hash)),
|
||||
err = h.db.UpdateUser(ctx, sqlc.UpdateUserParams{
|
||||
Password: util.ToPointer(string(hash)),
|
||||
ID: userid,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, connect.NewError(connect.CodeInternal, err)
|
||||
@ -108,9 +103,7 @@ func (h *Handler) GetAPIKey(ctx context.Context, req *connect.Request[userv1.Get
|
||||
}
|
||||
|
||||
// Get user
|
||||
user, err := models.Users.Query(
|
||||
models.SelectWhere.Users.ID.EQ(userid),
|
||||
).One(ctx, h.db)
|
||||
user, err := h.db.GetUser(ctx, userid)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, connect.NewError(connect.CodeNotFound, err)
|
||||
@ -159,19 +152,17 @@ func (h *Handler) UpdateProfilePicture(ctx context.Context, req *connect.Request
|
||||
}
|
||||
|
||||
// Save bytes into file
|
||||
file, err := models.Files.Insert(&models.FileSetter{
|
||||
Name: omit.From(req.Msg.FileName),
|
||||
Data: omit.From(req.Msg.Data),
|
||||
UserID: omit.From(userid),
|
||||
}).One(ctx, h.db)
|
||||
fileID, err := h.db.InsertFile(ctx, sqlc.InsertFileParams{
|
||||
Name: req.Msg.FileName,
|
||||
Data: req.Msg.Data,
|
||||
UserID: userid,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, connect.NewError(connect.CodeInternal, err)
|
||||
}
|
||||
|
||||
// Get user
|
||||
user, err := models.Users.Query(
|
||||
models.SelectWhere.Users.ID.EQ(userid),
|
||||
).One(ctx, h.db)
|
||||
user, err := h.db.GetUser(ctx, userid)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return nil, connect.NewError(connect.CodeNotFound, err)
|
||||
@ -180,25 +171,24 @@ func (h *Handler) UpdateProfilePicture(ctx context.Context, req *connect.Request
|
||||
return nil, connect.NewError(connect.CodeInternal, err)
|
||||
}
|
||||
|
||||
// Get old profile picture ID
|
||||
var ppid *int64
|
||||
if user.ProfilePictureID.Ptr() != nil {
|
||||
ppid = user.ProfilePictureID.Ptr()
|
||||
}
|
||||
|
||||
// Update user profile picture
|
||||
err = user.Update(ctx, h.db, &models.UserSetter{
|
||||
ProfilePictureID: omitnull.From(file.ID),
|
||||
err = h.db.UpdateUser(ctx, sqlc.UpdateUserParams{
|
||||
// set
|
||||
ProfilePictureID: &fileID,
|
||||
|
||||
// where
|
||||
ID: userid,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, connect.NewError(connect.CodeInternal, err)
|
||||
}
|
||||
|
||||
// Delete old profile picture if exists
|
||||
if ppid != nil {
|
||||
_, err = models.Files.Delete(
|
||||
models.DeleteWhere.Files.ID.EQ(*ppid),
|
||||
).Exec(ctx, h.db)
|
||||
if user.ProfilePictureID != nil {
|
||||
err = h.db.DeleteFile(ctx, sqlc.DeleteFileParams{
|
||||
ID: *user.ProfilePictureID,
|
||||
UserID: userid,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, connect.NewError(connect.CodeInternal, err)
|
||||
}
|
||||
@ -272,7 +262,7 @@ func (h *Handler) UpdateProfilePicture(ctx context.Context, req *connect.Request
|
||||
// return nil
|
||||
// }
|
||||
|
||||
func NewHandler(db *bob.DB, key string) (string, http.Handler) {
|
||||
func NewHandler(db *sqlc.Queries, key string) (string, http.Handler) {
|
||||
interceptors := connect.WithInterceptors(interceptors.NewAuthInterceptor(key))
|
||||
|
||||
return userv1connect.NewUserServiceHandler(
|
||||
|
Reference in New Issue
Block a user