user.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package controllers
  2. import (
  3. "fmt"
  4. "github.com/gorilla/mux"
  5. "log"
  6. "net/http"
  7. "path/filepath"
  8. "github.com/satori/go.uuid"
  9. "rate-it-api/models"
  10. "rate-it-api/utils"
  11. )
  12. // UserCreate create a new user from form data
  13. func UserCreate(w http.ResponseWriter, r *http.Request) {
  14. r.ParseForm()
  15. var user = models.User{
  16. UUID: uuid.NewV4().String(),
  17. Email: r.FormValue("email"),
  18. Password: r.FormValue("password"),
  19. }
  20. err := user.Create()
  21. if err != nil {
  22. utils.SendJSONErrorResponse(w, user.Email+" already exists", http.StatusBadRequest)
  23. return
  24. }
  25. path, err := filepath.Abs("./templates/user_validation.html.tpl")
  26. if err != nil {
  27. utils.SendJSONErrorResponse(w, "Unable to send mail", http.StatusInternalServerError)
  28. return
  29. }
  30. type mailValuesStruct struct {
  31. UUID string
  32. Host string
  33. }
  34. config := utils.GetConfig()
  35. mailValues := mailValuesStruct{
  36. UUID: user.UUID,
  37. Host: config.Server,
  38. }
  39. msg, err := utils.HTML(path, mailValues)
  40. if err != nil {
  41. log.Fatal("Error template " + err.Error())
  42. utils.SendJSONErrorResponse(w, "Unable to send mail", http.StatusInternalServerError)
  43. return
  44. }
  45. // Define mail
  46. m := utils.Mail{
  47. ToAddr: user.Email,
  48. FromName: "no-reply",
  49. FromAddr: "no-reply@guern.eu",
  50. Subject: "Validation",
  51. Body: msg,
  52. }
  53. err = m.Send()
  54. if err != nil {
  55. log.Fatal("Error send validation mail" + err.Error())
  56. utils.SendJSONErrorResponse(w, "Unable to send mail", http.StatusInternalServerError)
  57. return
  58. }
  59. if err != nil {
  60. utils.SendJSONErrorResponse(w, err.Error(), http.StatusBadRequest)
  61. return
  62. }
  63. fmt.Fprintln(w, user.Email, user.Password)
  64. }
  65. // UserRenew validate e user email
  66. func UserRenew(w http.ResponseWriter, r *http.Request) {
  67. r.ParseForm()
  68. user, err := models.UserGetByUUID(r.Form.Get("email"))
  69. if err != nil {
  70. fmt.Println(err.Error())
  71. }
  72. fmt.Println(user.Email)
  73. }
  74. // UserValidate validate e user email
  75. func UserValidate(w http.ResponseWriter, r *http.Request) {
  76. vars := mux.Vars(r)
  77. user, err := models.UserGetByUUID(vars["uuid"])
  78. if err != nil {
  79. fmt.Println(err.Error())
  80. }
  81. models.UserValidate(user.UUID)
  82. }