Skip to content

Commit

Permalink
Merge pull request #92 from atnomoverflow/feat/dto-marketing
Browse files Browse the repository at this point in the history
changing marketing api to read data from body and adding validation.
  • Loading branch information
Klaven authored Mar 19, 2024
2 parents 18575c6 + 11b8e35 commit 7d3945b
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions api/marketing/controllers/marketing.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"os"

"github.com/go-playground/validator/v10"
"github.com/gorilla/mux"
"github.com/sendgrid/sendgrid-go"
)
Expand All @@ -30,6 +31,11 @@ func (m *MarketingController) AddAllControllers(router *mux.Router) {
router.HandleFunc("/email", m.POSTEmailSubscriber).Methods(http.MethodPost)
}

// define custom type
type MarketingSubscribeDTO struct {
Email string `json:"email" validate:"required,email"`
}

// @BasePath /api/v1/

// CreateUser godoc
Expand All @@ -44,22 +50,32 @@ func (m *MarketingController) AddAllControllers(router *mux.Router) {
// @Router /marketing/email [post]
func (m *MarketingController) POSTEmailSubscriber(w http.ResponseWriter, r *http.Request) {

err := r.ParseForm()
var marketingSubscribeDTO MarketingSubscribeDTO
err := json.NewDecoder(r.Body).Decode(&marketingSubscribeDTO)

if err != nil {
http.Error(w, "Bad request", http.StatusBadRequest)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintln(w, "Decode error! please check your JSON formatting.")
return
}
validate := validator.New()

email := r.Form.Get("email")
fmt.Println("email: " + email)
err = m.Addrecipients(email)
err = validate.Struct(marketingSubscribeDTO)

if err != nil {
errors := err.(validator.ValidationErrors)
http.Error(w, fmt.Sprintf("Validation error: %s", errors), http.StatusBadRequest)
return
}
fmt.Printf("Email: %s\n", marketingSubscribeDTO.Email)
err = m.Addrecipients(marketingSubscribeDTO.Email)

if err != nil {
log.Println(err)
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
return
}

w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "New e-mail added to list successfully!")
}
Expand Down

0 comments on commit 7d3945b

Please sign in to comment.