Commit 5f6b3fdb authored by Rafael's avatar Rafael

added mutation for contact

parent 0d7ec305
Pipeline #22149625 passed with stage
in 2 minutes and 17 seconds
package resolvers
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
"gitlab.com/shereland/graphql/logging"
"io/ioutil"
"net/http"
"os"
"strings"
)
type Contact struct {
error *string
}
type bodyStruct struct {
TemplateName string `json:"templateName"`
TemplateOptions templateOptionsStruct `json:"templateOptions"`
EmailOptions emailOptionsStruct `json:"emailOptions"`
}
type templateOptionsStruct struct {
Name string `json:"name"`
Email string `json:"email"`
Phone string `json:"phone"`
Subject string `json:"subject"`
How_found_site string `json:"how_found_site"`
Message string `json:"message"`
}
type emailOptionsStruct struct {
To string `json:"to"`
}
type mailServerResponse struct {
Error *string `json:"error`
}
func prepareBody(name string,
email string,
phone string,
subject string,
how_found_site string,
message string) bodyStruct {
templateOptions := templateOptionsStruct{Name: name,
Email: email,
Phone: phone,
Subject: subject,
How_found_site: how_found_site,
Message: message}
emailOptions := emailOptionsStruct{To: os.Getenv("ADMIN_EMAIL")}
return bodyStruct{TemplateName: "contact",
TemplateOptions: templateOptions,
EmailOptions: emailOptions}
}
func sendContact(name string, email string, phone string, subject string, how_found_site string, message string) Contact {
methodLog := logging.Log.WithFields(log.Fields{
"method": "sendContact",
"name": name,
"email": email,
"phone": phone,
"subject": subject,
"how_found_site": how_found_site,
"message": message,
})
bodyStruct := prepareBody(name, email, phone, subject, how_found_site, message)
b, _ := json.Marshal(bodyStruct)
fmt.Println(string(b))
bodyReader := strings.NewReader(string(b))
resp, err := http.Post("http://mail:4444/email/send", "application/json", bodyReader)
if err != nil {
errorResponse := "Error while sending e-mail"
methodLog.Error(errorResponse)
return Contact{error: &errorResponse}
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
errorResponse := "Error while reading mail server data"
methodLog.Error(errorResponse)
return Contact{error: &errorResponse}
}
dat := mailServerResponse{}
if err = json.Unmarshal(body, &dat); err != nil {
errorResponse := "Error while decoding mail server response"
methodLog.Error(errorResponse)
return Contact{error: &errorResponse}
}
if dat.Error != nil {
errorResponse := "Error raised by the mail server: " + *dat.Error
methodLog.Error(errorResponse)
return Contact{error: &errorResponse}
}
return Contact{error: nil}
}
func (c Contact) Error() *string {
return c.error
}
......@@ -242,3 +242,21 @@ func (f friendshipRequestsResolver) Slug() string {
func (f friendshipRequestsResolver) Name() string {
return f.name
}
type ContactInput struct {
Name string
Email string
Phone *string
Subject string
How_found_site string
Message string
}
func (r *Resolver) Contact(args struct{ ContactInput ContactInput }) *Contact {
startingTime := time.Now()
contact := sendContact(args.ContactInput.Name, args.ContactInput.Email, *args.ContactInput.Phone, args.ContactInput.Subject, args.ContactInput.How_found_site, args.ContactInput.Message)
metrics.ResolverDurations.WithLabelValues("Contact").Observe(time.Since(startingTime).Seconds() * 1000)
return &contact
}
schema {
query: Query
mutation: Mutation
}
type Query {
......@@ -9,6 +10,9 @@ type Query {
posts(category: String, page: Int): Posts
popularPosts: [Post!]
}
type Mutation {
contact(contactInput: ContactInput!): Contact
}
# Album with photos for the blog
type Album {
......@@ -68,4 +72,17 @@ type Posts {
}
type Tweet {
content: String!
}
\ No newline at end of file
}
# Contact
input ContactInput {
name: String!
email: String!
phone: String
subject: String!
how_found_site: String!
message: String!
}
type Contact {
error: String
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment