123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- package controllers
- import (
- "fmt"
- "github.com/gorilla/mux"
- "log"
- "net/http"
- "path/filepath"
- "github.com/satori/go.uuid"
- "rate-it-api/models"
- "rate-it-api/utils"
- )
- // UserCreate create a new user from form data
- func UserCreate(w http.ResponseWriter, r *http.Request) {
- r.ParseForm()
- var user = models.User{
- UUID: uuid.NewV4().String(),
- Email: r.FormValue("email"),
- Password: r.FormValue("password"),
- }
- err := user.Create()
- if err != nil {
- utils.SendJSONErrorResponse(w, user.Email+" already exists", http.StatusBadRequest)
- return
- }
- path, err := filepath.Abs("./templates/user_validation.html.tpl")
- if err != nil {
- utils.SendJSONErrorResponse(w, "Unable to send mail", http.StatusInternalServerError)
- return
- }
- type mailValuesStruct struct {
- UUID string
- Host string
- }
- config := utils.GetConfig()
- mailValues := mailValuesStruct{
- UUID: user.UUID,
- Host: config.Server,
- }
- msg, err := utils.HTML(path, mailValues)
- if err != nil {
- log.Fatal("Error template " + err.Error())
- utils.SendJSONErrorResponse(w, "Unable to send mail", http.StatusInternalServerError)
- return
- }
- // Define mail
- m := utils.Mail{
- ToAddr: user.Email,
- FromName: "no-reply",
- FromAddr: "no-reply@guern.eu",
- Subject: "Validation",
- Body: msg,
- }
- err = m.Send()
- if err != nil {
- log.Fatal("Error send validation mail" + err.Error())
- utils.SendJSONErrorResponse(w, "Unable to send mail", http.StatusInternalServerError)
- return
- }
- if err != nil {
- utils.SendJSONErrorResponse(w, err.Error(), http.StatusBadRequest)
- return
- }
- fmt.Fprintln(w, user.Email, user.Password)
- }
- // UserRenew validate e user email
- func UserRenew(w http.ResponseWriter, r *http.Request) {
- r.ParseForm()
- user, err := models.UserGetByUUID(r.Form.Get("email"))
- if err != nil {
- fmt.Println(err.Error())
- }
- fmt.Println(user.Email)
- }
- // UserValidate validate e user email
- func UserValidate(w http.ResponseWriter, r *http.Request) {
- vars := mux.Vars(r)
- user, err := models.UserGetByUUID(vars["uuid"])
- if err != nil {
- fmt.Println(err.Error())
- }
- models.UserValidate(user.UUID)
- }
|