Advertisement
Guest User

Untitled

a guest
Mar 7th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. package api
  2.  
  3. import (
  4. "context"
  5. proto "gitlab.com/just-book/just-book-backend/proto/defs"
  6. "strings"
  7. "net/http"
  8. "gitlab.com/just-book/just-book-backend/utils"
  9. "gitlab.com/just-book/just-book-backend/data"
  10. "fmt"
  11. "gopkg.in/asaskevich/govalidator.v4"
  12. "gitlab.com/just-book/just-book-backend/service"
  13. "gitlab.com/just-book/just-book-backend/log"
  14. )
  15.  
  16. /**
  17. * := Coded with love by Sakib Sami on 25/2/18.
  18. * := root@sakib.ninja
  19. * := www.sakib.ninja
  20. * := Coffee : Dream : Code
  21. */
  22.  
  23. func validateUserCreate(req *proto.ReqSignUp) []*proto.ErrorDetail {
  24. req.Name = strings.TrimSpace(req.GetName())
  25. req.Password = strings.TrimSpace(req.GetPassword())
  26. req.Phone = strings.TrimSpace(req.GetPhone())
  27. req.Username = strings.TrimSpace(req.GetUsername())
  28. req.Email = strings.TrimSpace(req.GetEmail())
  29. req.Department = strings.TrimSpace(req.GetDepartment())
  30. req.University = strings.TrimSpace(req.GetUniversity())
  31.  
  32. var errors []*proto.ErrorDetail
  33. if len(req.GetName()) < utils.ConstantNameMinLen || len(req.GetName()) > utils.ConstantNameMaxLen {
  34. errors = append(errors, &proto.ErrorDetail{
  35. Field: "Name",
  36. Messages: []string{fmt.Sprintf("length must be between %d to %d", utils.ConstantNameMinLen, utils.ConstantNameMaxLen)},
  37. })
  38. }
  39. if len(req.GetPassword()) < utils.ConstantPasswordMinLen || len(req.GetPassword()) > utils.ConstantPasswordMaxLen {
  40. errors = append(errors, &proto.ErrorDetail{
  41. Field: "Password",
  42. Messages: []string{fmt.Sprintf("length must be between %d to %d", utils.ConstantPasswordMinLen, utils.ConstantPasswordMaxLen)},
  43. })
  44. }
  45. if len(req.GetPhone()) < utils.ConstantPhoneMinLen || len(req.GetPhone()) > utils.ConstantPhoneMaxLen {
  46. errors = append(errors, &proto.ErrorDetail{
  47. Field: "Phone",
  48. Messages: []string{fmt.Sprintf("length must be between %d to %d", utils.ConstantPhoneMinLen, utils.ConstantPhoneMaxLen)},
  49. })
  50. }
  51. if len(req.GetUsername()) < utils.ConstantUsernameMinLen || len(req.GetUsername()) > utils.ConstantUsernameMaxLen {
  52. errors = append(errors, &proto.ErrorDetail{
  53. Field: "Username",
  54. Messages: []string{fmt.Sprintf("length must be between %d to %d", utils.ConstantUsernameMinLen, utils.ConstantUsernameMaxLen)},
  55. })
  56. }
  57. if !govalidator.IsEmail(req.GetEmail()) {
  58. errors = append(errors, &proto.ErrorDetail{
  59. Field: "Email",
  60. Messages: []string{"must be valid"},
  61. })
  62. }
  63. if req.GetDepartment() == "" {
  64. errors = append(errors, &proto.ErrorDetail{
  65. Field: "Department",
  66. Messages: []string{"is required"},
  67. })
  68. }
  69. if req.GetUniversity() == "" {
  70. errors = append(errors, &proto.ErrorDetail{
  71. Field: "University",
  72. Messages: []string{"is required"},
  73. })
  74. }
  75.  
  76. if len(errors) > 0 {
  77. return errors
  78. }
  79. return nil
  80. }
  81.  
  82. func UserCreate(ctx context.Context, req *proto.ReqSignUp) (*proto.ResSignUp, error) {
  83. if err := validateUserCreate(req); err != nil {
  84. return &proto.ResSignUp{
  85. Error: &proto.Error{
  86. ID: utils.NewUUID(),
  87. Status: http.StatusUnprocessableEntity,
  88. Title: "Invalid data",
  89. ErrorDetails: err,
  90. },
  91. }, nil
  92. }
  93.  
  94. u := data.User{
  95. ID: utils.NewUUID(),
  96. Name: req.GetName(),
  97. Phone: req.Phone,
  98. Email: req.Email,
  99. Username: req.Username,
  100. Department: req.Department,
  101. University: req.University,
  102. IsPhoneVerified: false,
  103. IsEmailVerified: false,
  104. IsPasswordResetRequested: false,
  105. Password: utils.NewPassword(req.Password),
  106. EmailVerificationToken: utils.NewUUID(),
  107. }
  108.  
  109. if u.IsUsernameExists() {
  110. return &proto.ResSignUp{
  111. Error: &proto.Error{
  112. ID: utils.NewUUID(),
  113. Status: http.StatusConflict,
  114. Title: "Username already exists",
  115. },
  116. }, nil
  117. }
  118. if u.IsPhoneExists() {
  119. return &proto.ResSignUp{
  120. Error: &proto.Error{
  121. ID: utils.NewUUID(),
  122. Status: http.StatusConflict,
  123. Title: "Phone already exists",
  124. },
  125. }, nil
  126. }
  127. if u.IsEmailExists() {
  128. return &proto.ResSignUp{
  129. Error: &proto.Error{
  130. ID: utils.NewUUID(),
  131. Status: http.StatusConflict,
  132. Title: "Email already exists",
  133. },
  134. }, nil
  135. }
  136. if err := u.Save(); err != nil {
  137. return &proto.ResSignUp{
  138. Error: &proto.Error{
  139. ID: utils.NewUUID(),
  140. Status: http.StatusInternalServerError,
  141. Title: err.Error(),
  142. },
  143. }, nil
  144. }
  145.  
  146. errE := service.SendVerificationEmail(u)
  147. if errE != nil {
  148. log.LoggerError("Couldn't send verification email", errE)
  149. }
  150.  
  151. return &proto.ResSignUp{
  152. User: &proto.User{
  153. UserID: u.ID,
  154. Name: u.Name,
  155. Phone: u.Phone,
  156. Email: u.Email,
  157. Username: u.Username,
  158. Department: u.Department,
  159. University: u.University,
  160. IsPhoneVerified: u.IsPhoneVerified,
  161. IsEmailVerified: u.IsEmailVerified,
  162. UpdatedAt: u.UpdatedAt.String(),
  163. CreatedAt: u.CreatedAt.String(),
  164. },
  165. }, nil
  166. }
  167.  
  168. func validateUserLogin(req *proto.ReqLogin) []*proto.ErrorDetail {
  169. req.Username = strings.TrimSpace(req.GetUsername())
  170. req.Password = strings.TrimSpace(req.GetPassword())
  171.  
  172. var errors []*proto.ErrorDetail
  173. if len(req.GetUsername()) < utils.ConstantNameMinLen || len(req.GetUsername()) > utils.ConstantNameMaxLen {
  174. errors = append(errors, &proto.ErrorDetail{
  175. Field: "Name",
  176. Messages: []string{fmt.Sprintf("length must be between %d to %d", utils.ConstantNameMinLen, utils.ConstantNameMaxLen)},
  177. })
  178. }
  179. if len(req.GetPassword()) < utils.ConstantPasswordMinLen || len(req.GetPassword()) > utils.ConstantPasswordMaxLen {
  180. errors = append(errors, &proto.ErrorDetail{
  181. Field: "Password",
  182. Messages: []string{fmt.Sprintf("length must be between %d to %d", utils.ConstantPasswordMinLen, utils.ConstantPasswordMaxLen)},
  183. })
  184. }
  185. if len(errors) > 0 {
  186. return errors
  187. }
  188. return nil
  189. }
  190.  
  191. func UserLogin(ctx context.Context, req *proto.ReqLogin) (*proto.ResLogin, error) {
  192. if err := validateUserLogin(req); err != nil {
  193. return &proto.ResLogin{
  194. Error: &proto.Error{
  195. ID: utils.NewUUID(),
  196. Status: http.StatusUnprocessableEntity,
  197. Title: "Invalid data",
  198. ErrorDetails: err,
  199. },
  200. }, nil
  201. }
  202.  
  203. u := data.User{
  204. ID: utils.NewUUID(),
  205. Username: req.Username,
  206. Password: utils.NewPassword(req.Password),
  207. EmailVerificationToken: utils.NewUUID(),
  208. }
  209.  
  210. if err := u.FindByUsername(req.Username); err != nil{
  211. return &proto.ResLogin{
  212. Error: &proto.Error{
  213. ID: utils.NewUUID(),
  214. Status: http.StatusConflict,
  215. Title: "Username doesn't exists",
  216. },
  217. }, nil
  218. }
  219.  
  220. if !utils.IsPasswordMatched(req.Password, u.Password) {
  221. return &proto.ResLogin{
  222. Error: &proto.Error{
  223. ID: utils.NewUUID(),
  224. Status: http.StatusConflict,
  225. Title: "Wrong password",
  226. },
  227. }, nil
  228. }
  229.  
  230. return &proto.ResLogin{
  231. Session: &proto.Session{
  232. AccessToken: u.AccessToken,
  233. RefreshToken: u.RefreshToken,
  234. CreatedAt: u.CreatedAt.String(),
  235. ExpireAt: u.ExpireAt.String(),
  236. },
  237. }, nil
  238. }
  239.  
  240.  
  241.  
  242. func UserUpdate() {
  243.  
  244. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement