sonr/pkg/gateway/middleware/profiles.go
Prad Nukala 6072f6ecfa
feature/implement wss routes (#1196)
* feat(database): create schema for hway and motr

* fix(gateway): correct naming inconsistencies in handlers

* build: update schema file to be compatible with postgresql syntax

* fix: update schema to be compatible with PostgreSQL syntax

* chore: update query_hway.sql to follow sqlc syntax

* ```text
refactor: update query_hway.sql for PostgreSQL and sqlc
```

* feat: add vaults table to store encrypted data

* refactor: Update vaults table schema for sqlc compatibility

* chore(deps): Upgrade dependencies and add pgx/v5

* refactor(Makefile): move sqlc generate to internal/models

* docs(foundations): remove outdated pages

* chore(build): add Taskfile for build tasks

* refactor(embed): move embed files to internal package

* docs: add documentation for Cosmos SDK ORM
2024-12-18 20:53:45 +00:00

126 lines
3.4 KiB
Go

package middleware
import (
"net/http"
"github.com/labstack/echo/v4"
"github.com/onsonr/sonr/internal/context"
repository "github.com/onsonr/sonr/internal/models/drivers/hwayorm"
)
func CheckHandleUnique(c echo.Context, handle string) bool {
ctx, ok := c.(*GatewayContext)
if !ok {
return false
}
ok, err := ctx.dbq.CheckHandleExists(bgCtx(), handle)
if err != nil {
return false
}
if ok {
return false
}
context.WriteCookie(c, context.UserHandle, handle)
return true
}
func CreateProfile(c echo.Context) (*repository.Profile, error) {
ctx, ok := c.(*GatewayContext)
if !ok {
return nil, echo.NewHTTPError(http.StatusInternalServerError, "Profile Context not found")
}
address := c.FormValue("address")
handle := c.FormValue("handle")
origin := c.FormValue("origin")
name := c.FormValue("name")
profile, err := ctx.dbq.InsertProfile(bgCtx(), repository.InsertProfileParams{
Address: address,
Handle: handle,
Origin: origin,
Name: name,
})
if err != nil {
return nil, err
}
// Update session with profile id
sid := GetSessionID(c)
_, err = ctx.dbq.UpdateSessionWithProfileID(bgCtx(), repository.UpdateSessionWithProfileIDParams{
ProfileID: profile.ID,
ID: sid,
})
if err != nil {
return nil, err
}
return &profile, nil
}
func UpdateProfile(c echo.Context) (*repository.Profile, error) {
ctx, ok := c.(*GatewayContext)
if !ok {
return nil, echo.NewHTTPError(http.StatusInternalServerError, "Profile Context not found")
}
address := c.FormValue("address")
handle := c.FormValue("handle")
name := c.FormValue("name")
profile, err := ctx.dbq.UpdateProfile(bgCtx(), repository.UpdateProfileParams{
Address: address,
Handle: handle,
Name: name,
})
if err != nil {
return nil, err
}
return &profile, nil
}
func ReadProfile(c echo.Context) (*repository.Profile, error) {
ctx, ok := c.(*GatewayContext)
if !ok {
return nil, echo.NewHTTPError(http.StatusInternalServerError, "Profile Context not found")
}
handle := c.Param("handle")
profile, err := ctx.dbq.GetProfileByHandle(bgCtx(), handle)
if err != nil {
return nil, err
}
return &profile, nil
}
func DeleteProfile(c echo.Context) error {
ctx, ok := c.(*GatewayContext)
if !ok {
return echo.NewHTTPError(http.StatusInternalServerError, "Profile Context not found")
}
address := c.Param("address")
err := ctx.dbq.SoftDeleteProfile(bgCtx(), address)
if err != nil {
return err
}
return nil
}
// ╭───────────────────────────────────────────────────────────╮
// │ Create Profile (/register/profile) │
// ╰───────────────────────────────────────────────────────────╯
// DefaultCreateProfileParams returns a default CreateProfileParams
func DefaultCreateProfileParams() CreateProfileParams {
return CreateProfileParams{
TurnstileSiteKey: "",
FirstNumber: 0,
LastNumber: 0,
}
}
// CreateProfileParams represents the parameters for creating a profile
type CreateProfileParams struct {
TurnstileSiteKey string
FirstNumber int
LastNumber int
}
// Sum returns the sum of the first and last number
func (d CreateProfileParams) Sum() int {
return d.FirstNumber + d.LastNumber
}