package middleware import ( "net/http" "github.com/labstack/echo/v4" "github.com/onsonr/sonr/internal/context" hwayorm "github.com/onsonr/sonr/pkg/gateway/orm" ) 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) (*hwayorm.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(), hwayorm.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(), hwayorm.UpdateSessionWithProfileIDParams{ ProfileID: profile.ID, ID: sid, }) if err != nil { return nil, err } return &profile, nil } func UpdateProfile(c echo.Context) (*hwayorm.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(), hwayorm.UpdateProfileParams{ Address: address, Handle: handle, Name: name, }) if err != nil { return nil, err } return &profile, nil } func ReadProfile(c echo.Context) (*hwayorm.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 }