Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package routes
- import (
- "fmt"
- "net/http"
- "strings"
- "github.com/lilahamstern/api/auth"
- "github.com/lilahamstern/api/mail"
- "github.com/jinzhu/gorm"
- "github.com/gin-gonic/gin"
- "github.com/lilahamstern/api/database"
- "github.com/lilahamstern/api/models"
- )
- var db *gorm.DB
- func init() {
- db = database.GetCon()
- }
- // CreateUser will create an user
- func CreateUser(c *gin.Context) {
- postData := [5]string{c.PostForm("firstname"), c.PostForm("lastname"), c.PostForm("email"), c.PostForm("password"), c.PostForm("birthday")}
- var builder strings.Builder
- builder.WriteString("Missing:")
- for key, value := range postData {
- if len(value) == 0 {
- if key == 0 {
- fmt.Fprint(&builder, " Firstname,")
- } else if key == 1 {
- fmt.Fprint(&builder, " Lastname,")
- } else if key == 2 {
- fmt.Fprint(&builder, " Email,")
- } else if key == 3 {
- fmt.Fprint(&builder, " Password,")
- } else if key == 4 {
- fmt.Fprint(&builder, " Birthday,")
- }
- }
- }
- if len(builder.String()) <= 8 {
- if mail.ValidateEmail(postData[2]) {
- var userEmail []models.User
- db.Where("email = ?", postData[2]).First(&userEmail)
- if len(userEmail) != 0 {
- c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "Email allready exists!"})
- return
- }
- if len(postData[3]) < 7 {
- c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "Password to short!"})
- return
- }
- pwdHash, err := auth.HashPassword(postData[3])
- if err != nil {
- fmt.Println(err)
- return
- }
- user := models.User{FirstName: strings.Title(postData[0]), LastName: strings.Title(postData[1]), Email: postData[2], Password: pwdHash, Birthday: postData[4]}
- db.Save(&user)
- subject := "Activation of account"
- reciver := postData[2]
- r := mail.NewRequest([]string{reciver}, subject)
- r.SendVerifyMail("html/mailtemplate.html", map[string]string{"username": postData[0] + " " + postData[1]})
- c.JSON(http.StatusCreated, gin.H{"status": http.StatusCreated, "message": "User created successfully!", "userId": user.ID})
- } else {
- c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": "Invalid mail"})
- }
- } else {
- c.JSON(http.StatusBadRequest, gin.H{"status": http.StatusBadRequest, "message": builder.String()})
- }
- }
- // GetAllUsers will return all users to the api requester
- func GetAllUsers(c *gin.Context) {
- var users []models.User
- var _users []models.JSONUser
- db.Find(&users)
- if len(users) <= 0 {
- c.JSON(http.StatusNotFound, gin.H{"status": http.StatusNotFound, "message": "No users found!"})
- return
- }
- for _, item := range users {
- activated := false
- if item.Activated == 1 {
- activated = true
- } else {
- activated = false
- }
- url := fmt.Sprint("http://localhost:5000/api/v1/users/", item.ID)
- _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}})
- }
- c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": _users})
- }
- // GetSingelUser will retrive singel user for the requester
- func GetSingelUser(c *gin.Context) {
- var user models.User
- userID := c.Param("id")
- db.First(&user, userID)
- if user.ID == 0 {
- c.JSON(http.StatusNotFound, gin.H{"status": http.StatusNotFound, "message": "No user found!"})
- return
- }
- activated := false
- if user.Activated == 1 {
- activated = true
- } else {
- activated = false
- }
- _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"}}
- c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "data": _user})
- }
- // UpdateUser will update a user on request
- func UpdateUser(c *gin.Context) {
- var user models.User
- userID := c.Param("id")
- db.First(&user, userID)
- if user.ID == 0 {
- c.JSON(http.StatusNotFound, gin.H{"status": http.StatusNotFound, "message": "No user found!"})
- return
- }
- typeUpdate := c.PostForm("type")
- data := c.PostForm("data")
- db.Model(user).UpdateColumn(typeUpdate, data)
- c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "message": "User updated"})
- }
- // DeleteUser will delete the user on request
- func DeleteUser(c *gin.Context) {
- var user models.User
- userID := c.Param("id")
- db.First(&user, userID)
- if user.ID == 0 {
- c.JSON(http.StatusNotFound, gin.H{"status": http.StatusNotFound, "message": "No user found!"})
- return
- }
- db.Delete(&user)
- message := fmt.Sprint("User ", userID, " deleted successfully!")
- c.JSON(http.StatusOK, gin.H{"status": http.StatusOK, "message": message})
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement