Skip to content

Commit

Permalink
insert user
Browse files Browse the repository at this point in the history
  • Loading branch information
sayem314 committed May 10, 2023
1 parent 45e0ea7 commit 085e450
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 7 deletions.
40 changes: 40 additions & 0 deletions internal/db/user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package db

import (
"context"
"time"

"github.com/lunarr-app/lunarr-go/internal/common"
"github.com/lunarr-app/lunarr-go/internal/models"
"go.mongodb.org/mongo-driver/bson"
)

// InsertUserMongo inserts a new user into the users.accounts collection
func InsertUser(user *models.UserMongo) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

_, err := UsersAccounts.InsertOne(ctx, user)
if err != nil {
common.Logger.Error().Err(err).Msg("Failed to insert user into MongoDB")
return err
}

return nil
}

// UpdateUserMongo updates an existing user in the users.accounts collection
func UpdateUser(username string, updates bson.M) error {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

filter := bson.M{"username": username}
update := bson.M{"$set": updates}
_, err := UsersAccounts.UpdateOne(ctx, filter, update)
if err != nil {
common.Logger.Error().Err(err).Msg("Failed to update user in MongoDB")
return err
}

return nil
}
20 changes: 13 additions & 7 deletions internal/handlers/signup.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"context"
"net/http"
"time"

"github.com/go-playground/validator/v10"
"github.com/kataras/iris/v12"
Expand Down Expand Up @@ -53,14 +54,19 @@ func SignupHandler(ctx iris.Context) {
}

// Create new user
newUser := &models.UserSignup{
Displayname: userReq.Displayname,
Username: userReq.Username,
Password: string(hashedPassword),
Sex: userReq.Sex,
newUser := &models.UserMongo{
Displayname: userReq.Displayname,
Username: userReq.Username,
Password: string(hashedPassword),
Sex: userReq.Sex,
Role: "subscriber",
APIKey: "",
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
LastSeenAt: time.Now(),
CurrentStatus: "active",
}
_, err = db.UsersAccounts.InsertOne(context.Background(), newUser)
if err != nil {
if err := db.InsertUser(newUser); err != nil {
ctx.StatusCode(http.StatusInternalServerError)
ctx.JSON(map[string]string{"status": http.StatusText(http.StatusInternalServerError), "message": "Failed to create user"})
return
Expand Down
15 changes: 15 additions & 0 deletions internal/models/users.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package models

import "time"

type UserLogin struct {
Username string `json:"username" validate:"required,min=2,max=16,alphanum"`
Password string `json:"password" validate:"required,min=6,max=32"`
Expand All @@ -11,3 +13,16 @@ type UserSignup struct {
Password string `json:"password" validate:"required,min=6,max=32" bson:"password"`
Sex string `json:"sex,omitempty" validate:"oneof=male female unknown" bson:"sex,omitempty,default=unknown"`
}

type UserMongo struct {
Displayname string `bson:"displayname" validate:"required,min=1,max=48"`
Username string `bson:"username" validate:"required,min=2,max=16,alphanum,usernamepattern"`
Password string `bson:"password" validate:"required,min=6,max=32"`
Sex string `bson:"sex" validate:"required,oneof=male female unknown"`
Role string `bson:"role" validate:"omitempty,oneof=admin superuser subscriber"`
APIKey string `bson:"api_key"`
CreatedAt time.Time `bson:"created_at"`
UpdatedAt time.Time `bson:"updated_at"`
LastSeenAt time.Time `bson:"last_seen_at"`
CurrentStatus string `bson:"current_status" validate:"omitempty,oneof=active restricted disabled banned"`
}

0 comments on commit 085e450

Please sign in to comment.