🔧 fix(handlers_web.go): remove unused import and variable 'strconv'

 feat(handlers_web.go): add support for handling form data in 'handleRequestServicePostForm' function
🔧 fix(handlers_web.go): fix parsing and setting of form data in 'handleRequestServicePostForm' function
This commit is contained in:
pluja 2023-10-31 17:58:53 +01:00
parent 910a779f66
commit 56ce1519b2

View File

@ -4,7 +4,6 @@ import (
"context"
"fmt"
"math/rand"
"strconv"
"strings"
"github.com/gofiber/fiber/v2"
@ -135,6 +134,8 @@ func (s *Server) handleRequestServiceForm(c *fiber.Ctx) error {
if err != nil {
return err
}
reverse := true
return c.Render("request_service", fiber.Map{
"Title": "Request a service",
"Current": "request",
@ -143,10 +144,30 @@ func (s *Server) handleRequestServiceForm(c *fiber.Ctx) error {
"Difficulty": difficulty,
"Id": id,
},
"Error": c.Query("error", ""),
"Attributes": database.ServiceAttributes.GetSortedAttributes(reverse),
"Error": c.Query("error", ""),
}, "base")
}
type RequestFormData struct {
Type string `form:"type"`
Category string `form:"category"`
Name string `form:"name"`
Description string `form:"description"`
Urls string `form:"urls"`
LogoUrl string `form:"logo_url"`
TosUrls string `form:"tos_urls"`
OnionUrls string `form:"onion_urls"`
Tags string `form:"tags"`
Xmr bool `form:"xmr"`
Btc bool `form:"btc"`
Ln bool `form:"ln"`
Fiat bool `form:"fiat"`
Cash bool `form:"cash"`
KYCLevel int `form:"kyc_level"`
Attributes []string `form:"attributes"`
}
func (s *Server) handleRequestServicePostForm(c *fiber.Ctx) error {
nonce, id := c.FormValue("pow-nonce"), c.FormValue("pow-id")
log.Printf("Nonce: %v, ID: %v", nonce, id)
@ -154,29 +175,19 @@ func (s *Server) handleRequestServicePostForm(c *fiber.Ctx) error {
return c.Redirect("/request/service?error=invalid-captcha")
}
// KYC Level
log.Printf("KYC Level: %v", c.FormValue("kyc_level"))
klInt, err := strconv.Atoi(c.FormValue("kyc_level"))
log.Printf("KYC Level Int: %v", klInt)
if err != nil || klInt < 0 || klInt >= 4 {
log.Error().Err(err).Msgf("Invalid KYC Level value: %v", c.FormValue("kyc_level"))
data := new(RequestFormData)
if err := c.BodyParser(data); err != nil {
log.Error().Err(err).Msg("Could not parse form data")
return c.Redirect("/request/service?error=invalid-form")
}
if data.KYCLevel < 0 || data.KYCLevel >= 4 {
log.Error().Msgf("Invalid KYC Level value: %v", c.FormValue("kyc_level"))
return c.Redirect("/request/service?error=invalid-kyc-level")
}
var serviceType service.Type
switch c.FormValue("type") {
case "exchange":
serviceType = service.TypeExchange
case "service":
serviceType = service.TypeService
case "wallet":
serviceType = service.TypeWallet
default:
return c.Redirect("/request/service?error=invalid-service-type")
}
// Parse tags
tags := strings.ReplaceAll(c.FormValue("tags"), " ", ",")
tags := strings.ReplaceAll(data.Tags, " ", ",")
// Remove trailing commas
tags = strings.TrimSuffix(tags, ",")
// Remove duplicate commas
@ -186,22 +197,27 @@ func (s *Server) handleRequestServicePostForm(c *fiber.Ctx) error {
// Convert to lowercase
tags = strings.ToLower(tags)
_, err = database.Client.Service.Create().
SetName(strings.ToLower(c.FormValue("name"))).
SetDescription(c.FormValue("description")).
SetType(serviceType).
SetLogoURL(utils.UrlParser(c.FormValue("logo_url"))).
SetUrls(utils.UrlListParser(c.FormValue("urls"))).
SetTosUrls(utils.UrlListParser(c.FormValue("tos_urls"))).
SetOnionUrls(utils.UrlListParser(c.FormValue("tor_urls"))).
log.Printf("%+v", data)
_, err := database.Client.Service.Create().
SetName(strings.ToLower(data.Name)).
SetDescription(data.Description).
SetType(service.Type(data.Type)).
SetLogoURL(utils.UrlParser(data.LogoUrl)).
SetUrls(utils.UrlListParser(data.Urls)).
SetTosUrls(utils.UrlListParser(data.TosUrls)).
SetOnionUrls(utils.UrlListParser(data.OnionUrls)).
SetTags(tags).
SetXmr(c.FormValue("xmr") == "on").
SetBtc(c.FormValue("btc") == "on").
SetLightning(c.FormValue("ln") == "on").
SetFiat(c.FormValue("fiat") == "on").
SetCash(c.FormValue("cash") == "on").
SetKycLevel(klInt).
SetPending(true).
SetXmr(data.Xmr).
SetBtc(data.Btc).
SetLightning(data.Ln).
SetFiat(data.Fiat).
SetCash(data.Cash).
SetKycLevel(data.KYCLevel).
SetCategory(data.Category).
SetAttributes(strings.Join(data.Attributes, ",")).
SetPending(false).
SetListed(true).
Save(c.Context())
if err != nil {
log.Error().Err(err).Msg("Could not save service to database")