-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
40 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package handlers | ||
|
||
import ( | ||
"bkawk/go-echo/api/models" | ||
"context" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/labstack/echo/v4" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
) | ||
|
||
func CheckUsernameGet(c echo.Context) error { | ||
|
||
// Get the username from the query parameter | ||
username := c.QueryParam("username") | ||
|
||
// Get database connection from context | ||
db := c.Get("db").(*mongo.Database) | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) | ||
defer cancel() | ||
|
||
// Check if username exists in the database | ||
var existingUser models.User | ||
collection := db.Collection("users") | ||
err := collection.FindOne(ctx, bson.M{ | ||
"username": username, | ||
}).Decode(&existingUser) | ||
if err == nil { | ||
return c.JSON(http.StatusOK, echo.Map{"message": "Username not available"}) | ||
} | ||
|
||
return c.JSON(http.StatusOK, echo.Map{"message": "Username available"}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters