Advertisement
Guest User

Untitled

a guest
Oct 30th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. package routes
  2.  
  3. import (
  4. "fmt"
  5. "net/http"
  6. "strings"
  7.  
  8. "github.com/lilahamstern/api/auth"
  9.  
  10. "github.com/lilahamstern/api/mail"
  11.  
  12. "github.com/jinzhu/gorm"
  13.  
  14. "github.com/gin-gonic/gin"
  15. "github.com/lilahamstern/api/database"
  16. "github.com/lilahamstern/api/models"
  17. )
  18.  
  19. var db *gorm.DB
  20.  
  21. func init() {
  22. db = database.GetCon()
  23. }
  24.  
  25. // CreateUser will create an user
  26. func CreateUser(c *gin.Context) {
  27.  
  28. postData := [5]string{c.PostForm("firstname"), c.PostForm("lastname"), c.PostForm("email"), c.PostForm("password"), c.PostForm("birthday")}
  29.  
  30. var builder strings.Builder
  31. builder.WriteString("Missing:")
  32.  
  33. for key, value := range postData {
  34. if len(value) == 0 {
  35. if key == 0 {
  36. fmt.Fprint(&builder, " Firstname,")
  37. } else if key == 1 {
  38. fmt.Fprint(&builder, " Lastname,")
  39. } else if key == 2 {
  40. fmt.Fprint(&builder, " Email,")
  41. } else if key == 3 {
  42. fmt.Fprint(&builder, " Password,")
  43. } else if key == 4 {
  44. fmt.Fprint(&builder, " Birthday,")
  45. }
  46. }
  47. }
  48. if len(builder.String()) <= 8 {
  49. if mail.ValidateEmail(postData[2]) {
  50. var userEmail []models.User
  51. db.Where("email = ?", postData[2]).First(&userEmail)
  52.  
  53. if len(userEmail) != 0 {
  54. c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "Email allready exists!"})
  55. return
  56. }
  57.  
  58. if len(postData[3]) < 7 {
  59. c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "Password to short!"})
  60. return
  61. }
  62. pwdHash, err := auth.HashPassword(postData[3])
  63. if err != nil {
  64. fmt.Println(err)
  65. return
  66. }
  67. user := models.User{FirstName: strings.Title(postData[0]), LastName: strings.Title(postData[1]), Email: postData[2], Password: pwdHash, Birthday: postData[4]}
  68.  
  69. db.Save(&user)
  70.  
  71. subject := "Activation of account"
  72. reciver := postData[2]
  73.  
  74. r := mail.NewRequest([]string{reciver}, subject)
  75. r.SendVerifyMail("html/mailtemplate.html", map[string]string{"username": postData[0] + " " + postData[1]})
  76.  
  77. c.JSON(http.StatusCreated, gin.H{"status": http.StatusCreated, "message": "User created successfully!", "userId": user.ID})
  78. } else {
  79. c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "Invalid mail"})
  80. }
  81. } else {
  82. c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": builder.String()})
  83. }
  84.  
  85. }
  86.  
  87. // GetAllUsers will return all users to the api requester
  88. func GetAllUsers(c *gin.Context) {
  89. var users []models.User
  90. var _users []models.JSONUser
  91.  
  92. db.Find(&users)
  93.  
  94. if len(users) <= 0 {
  95. c.JSON(http.StatusNotFound, gin.H{"status": http.StatusNotFound, "message": "No users found!"})
  96. return
  97. }
  98.  
  99. for _, item := range users {
  100. activated := false
  101. if item.Activated == 1 {
  102. activated = true
  103. } else {
  104. activated = false
  105. }
  106. url := fmt.Sprint("http://localhost:5000/api/v1/users/", item.ID)
  107. _users = append(_users, models.JSONUser{ID: item.ID, FirstName: item.FirstName, LastName: item.LastName, Email: item.Email, Birthday: item.Birthday, Activated: activated, URL: models.RequestURL{RequestType: "GET", URL: url}})
  108. }
  109. c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": _users})
  110. }
  111.  
  112. // GetSingelUser will retrive singel user for the requester
  113. func GetSingelUser(c *gin.Context) {
  114. var user models.User
  115. userID := c.Param("id")
  116. db.First(&user, userID)
  117. if user.ID == 0 {
  118. c.JSON(http.StatusNotFound, gin.H{"status": http.StatusNotFound, "message": "No user found!"})
  119. return
  120. }
  121. activated := false
  122. if user.Activated == 1 {
  123. activated = true
  124. } else {
  125. activated = false
  126. }
  127. _user := models.JSONUser{ID: user.ID, FirstName: user.FirstName, LastName: user.LastName, Email: user.Email, Birthday: user.Birthday, Activated: activated, URL: models.RequestURL{RequestType: "GET", URL: "http://localhost:5000/api/v1/users"}}
  128. c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": _user})
  129. }
  130.  
  131. // UpdateUser will update a user on request
  132. func UpdateUser(c *gin.Context) {
  133. var user models.User
  134. userID := c.Param("id")
  135. db.First(&user, userID)
  136. if user.ID == 0 {
  137. c.JSON(http.StatusNotFound, gin.H{"status": http.StatusNotFound, "message": "No user found!"})
  138. return
  139. }
  140.  
  141. typeUpdate := c.PostForm("type")
  142. data := c.PostForm("data")
  143.  
  144. db.Model(user).UpdateColumn(typeUpdate, data)
  145. c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "message": "User updated"})
  146. }
  147.  
  148. // DeleteUser will delete the user on request
  149. func DeleteUser(c *gin.Context) {
  150. var user models.User
  151. userID := c.Param("id")
  152. db.First(&user, userID)
  153. if user.ID == 0 {
  154. c.JSON(http.StatusNotFound, gin.H{"status": http.StatusNotFound, "message": "No user found!"})
  155. return
  156. }
  157. db.Delete(&user)
  158. message := fmt.Sprint("User ", userID, " deleted successfully!")
  159. c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "message": message})
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement