2024-04-26 10:51:09 +07:00
|
|
|
package routes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/tigorlazuardi/redmage/api"
|
2024-05-02 23:16:28 +07:00
|
|
|
"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})
|
|
|
|
}
|
|
|
|
|
2024-05-02 23:16:28 +07:00
|
|
|
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")
|
2024-05-03 10:27:03 +07:00
|
|
|
_ = 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 {
|
2024-05-02 23:16:28 +07:00
|
|
|
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,
|
2024-05-03 10:27:03 +07:00
|
|
|
SubredditType: subtype,
|
2024-05-02 23:16:28 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2024-05-02 23:16:28 +07:00
|
|
|
log.New(r.Context()).Err(err).Error("failed to render subreddit input form")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2024-05-03 10:27:03 +07:00
|
|
|
params.Subreddit = actual
|
2024-05-02 23:16:28 +07:00
|
|
|
data.Value = actual
|
2024-05-03 10:27:03 +07:00
|
|
|
|
|
|
|
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 {
|
2024-05-03 10:27:03 +07:00
|
|
|
log.New(r.Context()).Err(err).Error("failed to render subreddit input form")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if exist {
|
2024-05-03 11:59:44 +07:00
|
|
|
rw.WriteHeader(http.StatusConflict)
|
2024-05-03 10:27:03 +07:00
|
|
|
data.Error = "subreddit already registered"
|
2024-05-27 21:10:37 +07:00
|
|
|
if err := put.NameInput(data).Render(r.Context(), rw); err != nil {
|
2024-05-03 10:27:03 +07:00
|
|
|
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-02 23:16:28 +07:00
|
|
|
|
2024-05-27 21:10:37 +07:00
|
|
|
if err := put.NameInput(data).Render(r.Context(), rw); err != nil {
|
2024-05-02 23:16:28 +07:00
|
|
|
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
|
|
|
|
}
|