Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package api
- import (
- "context"
- proto "gitlab.com/just-book/just-book-backend/proto/defs"
- "strings"
- "net/http"
- "gitlab.com/just-book/just-book-backend/utils"
- "gitlab.com/just-book/just-book-backend/data"
- "fmt"
- "gopkg.in/asaskevich/govalidator.v4"
- "gitlab.com/just-book/just-book-backend/service"
- "gitlab.com/just-book/just-book-backend/log"
- )
- /**
- * := Coded with love by Sakib Sami on 25/2/18.
- * := root@sakib.ninja
- * := www.sakib.ninja
- * := Coffee : Dream : Code
- */
- func validateUserCreate(req *proto.ReqSignUp) []*proto.ErrorDetail {
- req.Name = strings.TrimSpace(req.GetName())
- req.Password = strings.TrimSpace(req.GetPassword())
- req.Phone = strings.TrimSpace(req.GetPhone())
- req.Username = strings.TrimSpace(req.GetUsername())
- req.Email = strings.TrimSpace(req.GetEmail())
- req.Department = strings.TrimSpace(req.GetDepartment())
- req.University = strings.TrimSpace(req.GetUniversity())
- var errors []*proto.ErrorDetail
- if len(req.GetName()) < utils.ConstantNameMinLen || len(req.GetName()) > utils.ConstantNameMaxLen {
- errors = append(errors, &proto.ErrorDetail{
- Field: "Name",
- Messages: []string{fmt.Sprintf("length must be between %d to %d", utils.ConstantNameMinLen, utils.ConstantNameMaxLen)},
- })
- }
- if len(req.GetPassword()) < utils.ConstantPasswordMinLen || len(req.GetPassword()) > utils.ConstantPasswordMaxLen {
- errors = append(errors, &proto.ErrorDetail{
- Field: "Password",
- Messages: []string{fmt.Sprintf("length must be between %d to %d", utils.ConstantPasswordMinLen, utils.ConstantPasswordMaxLen)},
- })
- }
- if len(req.GetPhone()) < utils.ConstantPhoneMinLen || len(req.GetPhone()) > utils.ConstantPhoneMaxLen {
- errors = append(errors, &proto.ErrorDetail{
- Field: "Phone",
- Messages: []string{fmt.Sprintf("length must be between %d to %d", utils.ConstantPhoneMinLen, utils.ConstantPhoneMaxLen)},
- })
- }
- if len(req.GetUsername()) < utils.ConstantUsernameMinLen || len(req.GetUsername()) > utils.ConstantUsernameMaxLen {
- errors = append(errors, &proto.ErrorDetail{
- Field: "Username",
- Messages: []string{fmt.Sprintf("length must be between %d to %d", utils.ConstantUsernameMinLen, utils.ConstantUsernameMaxLen)},
- })
- }
- if !govalidator.IsEmail(req.GetEmail()) {
- errors = append(errors, &proto.ErrorDetail{
- Field: "Email",
- Messages: []string{"must be valid"},
- })
- }
- if req.GetDepartment() == "" {
- errors = append(errors, &proto.ErrorDetail{
- Field: "Department",
- Messages: []string{"is required"},
- })
- }
- if req.GetUniversity() == "" {
- errors = append(errors, &proto.ErrorDetail{
- Field: "University",
- Messages: []string{"is required"},
- })
- }
- if len(errors) > 0 {
- return errors
- }
- return nil
- }
- func UserCreate(ctx context.Context, req *proto.ReqSignUp) (*proto.ResSignUp, error) {
- if err := validateUserCreate(req); err != nil {
- return &proto.ResSignUp{
- Error: &proto.Error{
- ID: utils.NewUUID(),
- Status: http.StatusUnprocessableEntity,
- Title: "Invalid data",
- ErrorDetails: err,
- },
- }, nil
- }
- u := data.User{
- ID: utils.NewUUID(),
- Name: req.GetName(),
- Phone: req.Phone,
- Email: req.Email,
- Username: req.Username,
- Department: req.Department,
- University: req.University,
- IsPhoneVerified: false,
- IsEmailVerified: false,
- IsPasswordResetRequested: false,
- Password: utils.NewPassword(req.Password),
- EmailVerificationToken: utils.NewUUID(),
- }
- if u.IsUsernameExists() {
- return &proto.ResSignUp{
- Error: &proto.Error{
- ID: utils.NewUUID(),
- Status: http.StatusConflict,
- Title: "Username already exists",
- },
- }, nil
- }
- if u.IsPhoneExists() {
- return &proto.ResSignUp{
- Error: &proto.Error{
- ID: utils.NewUUID(),
- Status: http.StatusConflict,
- Title: "Phone already exists",
- },
- }, nil
- }
- if u.IsEmailExists() {
- return &proto.ResSignUp{
- Error: &proto.Error{
- ID: utils.NewUUID(),
- Status: http.StatusConflict,
- Title: "Email already exists",
- },
- }, nil
- }
- if err := u.Save(); err != nil {
- return &proto.ResSignUp{
- Error: &proto.Error{
- ID: utils.NewUUID(),
- Status: http.StatusInternalServerError,
- Title: err.Error(),
- },
- }, nil
- }
- errE := service.SendVerificationEmail(u)
- if errE != nil {
- log.LoggerError("Couldn't send verification email", errE)
- }
- return &proto.ResSignUp{
- User: &proto.User{
- UserID: u.ID,
- Name: u.Name,
- Phone: u.Phone,
- Email: u.Email,
- Username: u.Username,
- Department: u.Department,
- University: u.University,
- IsPhoneVerified: u.IsPhoneVerified,
- IsEmailVerified: u.IsEmailVerified,
- UpdatedAt: u.UpdatedAt.String(),
- CreatedAt: u.CreatedAt.String(),
- },
- }, nil
- }
- func validateUserLogin(req *proto.ReqLogin) []*proto.ErrorDetail {
- req.Username = strings.TrimSpace(req.GetUsername())
- req.Password = strings.TrimSpace(req.GetPassword())
- var errors []*proto.ErrorDetail
- if len(req.GetUsername()) < utils.ConstantNameMinLen || len(req.GetUsername()) > utils.ConstantNameMaxLen {
- errors = append(errors, &proto.ErrorDetail{
- Field: "Name",
- Messages: []string{fmt.Sprintf("length must be between %d to %d", utils.ConstantNameMinLen, utils.ConstantNameMaxLen)},
- })
- }
- if len(req.GetPassword()) < utils.ConstantPasswordMinLen || len(req.GetPassword()) > utils.ConstantPasswordMaxLen {
- errors = append(errors, &proto.ErrorDetail{
- Field: "Password",
- Messages: []string{fmt.Sprintf("length must be between %d to %d", utils.ConstantPasswordMinLen, utils.ConstantPasswordMaxLen)},
- })
- }
- if len(errors) > 0 {
- return errors
- }
- return nil
- }
- func UserLogin(ctx context.Context, req *proto.ReqLogin) (*proto.ResLogin, error) {
- if err := validateUserLogin(req); err != nil {
- return &proto.ResLogin{
- Error: &proto.Error{
- ID: utils.NewUUID(),
- Status: http.StatusUnprocessableEntity,
- Title: "Invalid data",
- ErrorDetails: err,
- },
- }, nil
- }
- u := data.User{
- ID: utils.NewUUID(),
- Username: req.Username,
- Password: utils.NewPassword(req.Password),
- EmailVerificationToken: utils.NewUUID(),
- }
- if err := u.FindByUsername(req.Username); err != nil{
- return &proto.ResLogin{
- Error: &proto.Error{
- ID: utils.NewUUID(),
- Status: http.StatusConflict,
- Title: "Username doesn't exists",
- },
- }, nil
- }
- if !utils.IsPasswordMatched(req.Password, u.Password) {
- return &proto.ResLogin{
- Error: &proto.Error{
- ID: utils.NewUUID(),
- Status: http.StatusConflict,
- Title: "Wrong password",
- },
- }, nil
- }
- return &proto.ResLogin{
- Session: &proto.Session{
- AccessToken: u.AccessToken,
- RefreshToken: u.RefreshToken,
- CreatedAt: u.CreatedAt.String(),
- ExpireAt: u.ExpireAt.String(),
- },
- }, nil
- }
- func UserUpdate() {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement