Redmage/server/routes/subreddit_check.go

122 lines
3.2 KiB
Go
Raw Permalink Normal View History

2024-04-26 10:51:09 +07:00
package routes
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"github.com/tigorlazuardi/redmage/api"
"github.com/tigorlazuardi/redmage/api/reddit"
2024-04-26 10:51:09 +07:00
"github.com/tigorlazuardi/redmage/pkg/errs"
"github.com/tigorlazuardi/redmage/pkg/log"
2024-05-27 21:10:37 +07:00
"github.com/tigorlazuardi/redmage/views/subreddits/put"
2024-04-26 10:51:09 +07:00
)
func (routes *Routes) SubredditsCheckAPI(rw http.ResponseWriter, r *http.Request) {
ctx, span := tracer.Start(r.Context(), "*Routes.SubredditsCheck")
defer span.End()
var (
enc = json.NewEncoder(rw)
dec = json.NewDecoder(r.Body)
)
var body api.SubredditCheckParam
if err := dec.Decode(&body); err != nil {
rw.WriteHeader(http.StatusBadRequest)
_ = enc.Encode(map[string]string{"error": fmt.Sprintf("failed to decode json body: %s", err)})
return
}
if err := validateSubredditCheckParam(body); err != nil {
rw.WriteHeader(http.StatusBadRequest)
_ = enc.Encode(map[string]string{"error": err.Error()})
return
}
actual, err := routes.API.SubredditCheck(ctx, body)
if err != nil {
log.New(ctx).Err(err).Error("failed to check subreddit")
code, message := errs.HTTPMessage(err)
rw.WriteHeader(code)
_ = enc.Encode(map[string]string{"error": message})
return
}
_ = enc.Encode(map[string]string{"subreddit": actual})
}
func (routes *Routes) SubredditCheckHTMX(rw http.ResponseWriter, r *http.Request) {
2024-05-27 21:10:37 +07:00
var (
data put.NameInputData
subtype reddit.SubredditType
)
data.Value = r.FormValue("name")
_ = subtype.Parse(r.FormValue("type"))
2024-05-27 21:10:37 +07:00
if data.Value == "" {
if err := put.NameInput(data).Render(r.Context(), rw); err != nil {
log.New(r.Context()).Err(err).Error("failed to render subreddit input form")
}
return
}
ctx, span := tracer.Start(r.Context(), "*Routes.SubredditCheckHTMX")
defer span.End()
params := api.SubredditCheckParam{
2024-05-27 21:10:37 +07:00
Subreddit: data.Value,
SubredditType: subtype,
}
actual, err := routes.API.SubredditCheck(ctx, params)
if err != nil {
log.New(ctx).Err(err).Error("failed to check subreddit")
code, message := errs.HTTPMessage(err)
rw.WriteHeader(code)
data.Error = message
2024-05-27 21:10:37 +07:00
if err := put.NameInput(data).Render(r.Context(), rw); err != nil {
log.New(r.Context()).Err(err).Error("failed to render subreddit input form")
}
return
}
params.Subreddit = actual
data.Value = actual
exist, err := routes.API.SubredditRegistered(ctx, params)
if err != nil {
log.New(ctx).Err(err).Error("failed to check subreddit")
code, message := errs.HTTPMessage(err)
rw.WriteHeader(code)
data.Error = message
2024-05-27 21:10:37 +07:00
if err := put.NameInput(data).Render(r.Context(), rw); err != nil {
log.New(r.Context()).Err(err).Error("failed to render subreddit input form")
}
return
}
if exist {
rw.WriteHeader(http.StatusConflict)
data.Error = "subreddit already registered"
2024-05-27 21:10:37 +07:00
if err := put.NameInput(data).Render(r.Context(), rw); err != nil {
log.New(r.Context()).Err(err).Error("failed to render subreddit input form")
}
return
}
2024-05-27 21:10:37 +07:00
data.Valid = fmt.Sprintf("%s is valid", subtype)
2024-05-27 21:10:37 +07:00
if err := put.NameInput(data).Render(r.Context(), rw); err != nil {
log.New(r.Context()).Err(err).Error("failed to render subreddit input form")
}
}
2024-04-26 10:51:09 +07:00
func validateSubredditCheckParam(body api.SubredditCheckParam) error {
if body.Subreddit == "" {
return errors.New("subreddit name is required")
}
return nil
}