public BaseResponse insertUser(RegisterUserReqObj user)
        {
            string       token    = "";
            BaseResponse response = new BaseResponse();

            try
            {
                var db           = _db;
                var isRegistered = db.User.Where(c => c.Email.Equals(user.email));


                if (isRegistered.Count() == 0)
                {
                    Random rnd = new Random();
                    token = rnd.Next(100000, 999999).ToString();

                    var insertUserObj = new Users()
                    {
                        Email         = user.email,
                        Phone         = user.phone,
                        Password      = Utils.CreateMD5(user.password),
                        TokenActivate = token,
                        EmailVerified = 0,
                        UserStatus    = Constant.USER_ACTIVE,
                        CreatedAt     = DateTime.Today,
                        PushId        = "No Push",
                        Name          = user.name,
                        // the user.profileImage is a base64, we can't insert it into the db
                        // its a huge string..... its bad!
                        ProfileImage = Encoding.UTF8.GetBytes("")
                    };


                    db.User.Add(insertUserObj);
                    response.status = db.SaveChanges();


                    if (response.status == 1)
                    {
                        string subject = Application.Constant.INSERT_USER_EMAIL_SUBJECT;
                        string body    = Application.Constant.INSERT_USER_EMAIL_BODY + token;

                        if (Utils.sendEmail(subject, body, user.email))
                        {
                            response.status           = 1;
                            response.developerMessage = "Account Created, Check Your Email";
                        }
                        else
                        {
                            response.status           = 2;
                            response.developerMessage = "Couldn't Send Verification Email, Try Again Later!";
                        }
                    }
                    else
                    {
                        response.status           = -2;
                        response.developerMessage = "Couldn't Register, Try Again Later!";
                    }
                }
                else
                {
                    var registeredUser = isRegistered.SingleOrDefault();

                    if (registeredUser.UserStatus == Constant.USER_ACTIVE)
                    {
                        response.status           = -1;
                        response.developerMessage = "Email Already Exist";
                    }
                    else
                    {
                        Random rnd = new Random();
                        token = rnd.Next(100000, 999999).ToString();

                        registeredUser.Phone         = user.phone;
                        registeredUser.Password      = Utils.CreateMD5(user.password);
                        registeredUser.UserStatus    = Constant.USER_REACTIVE;
                        registeredUser.TokenActivate = token;
                        registeredUser.EmailVerified = 0;
                        db.SaveChanges();

                        string subject = Constant.REACTIVATION_EMAIL_SUBJECT;
                        string body    = Constant.REACTIVATION_EMAIL_BODY + token;

                        if (Utils.sendEmail(subject, body, user.email))
                        {
                            response.status           = 1;
                            response.developerMessage = "Account Reactivated, Check Your Email";
                        }
                        else
                        {
                            response.status           = 2;
                            response.developerMessage = "Couldn't Send Verification Email, Try Again Later!";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                response.developerMessage = "Something went wrong: " + ex.Message;
                response.status           = 3;
            }
            return(response);
        }
 public BaseResponse addUser([FromBody] RegisterUserReqObj user)
 {
     return(new BLL.BLL_Users(_db).addUser(user));
 }
Exemple #3
0
 public BaseResponse AddUser([FromBody] RegisterUserReqObj user)
 {
     return(new UserService(_db).insertUser(user));
 }
Exemple #4
0
        public BaseResponse insertUser(RegisterUserReqObj user)
        {
            string       token    = "";
            BaseResponse response = new BaseResponse();

            try
            {
                var db           = _db;
                var isRegistered = db.Users.Where(c => c.Email.Equals(user.email)).Include(m => m.ProfileImage).FirstOrDefault();


                if (isRegistered == null)
                {
                    Random rnd = new Random();
                    token = rnd.Next(100000, 999999).ToString();

                    var insertUserObj = new Users(_db);

                    insertUserObj.Phone        = user.phone;
                    insertUserObj.Name         = user.name;
                    insertUserObj.Email        = user.email;
                    insertUserObj.Password     = Utils.EncryptionUtils.CreateMD5(user.password);
                    insertUserObj.Address      = user.address;
                    insertUserObj.ProfileImage = new BinaryData(Encoding.UTF8.GetBytes(user.profileImage));



                    insertUserObj.PushId = "";

                    insertUserObj.TokenActivate = token;
                    insertUserObj.EmailVerified = 0;

                    insertUserObj.UserStatus = Constant.USER_ACTIVE;

                    // {
                    //     Email = user.email,
                    //     Phone = user.phone,
                    //     Password = Utils.EncryptionUtils.CreateMD5(user.password),
                    //     TokenActivate = token,
                    //     EmailVerified = 0,
                    //     UserStatus = Constant.USER_ACTIVE,
                    //     CreatedAt = DateTime.Today,
                    //     PushId = "No Push",
                    //     Name = user.name,
                    //     Address = user.address,
                    //     // the user.profileImage is a base64, we can't insert it into the db
                    //     // its a huge string..... its bad!
                    //     ProfileImage = Encoding.UTF8.GetBytes(user.profileImage)
                    // };



                    db.Users.Add(insertUserObj);
                    response.status = db.SaveChanges();

                    // db save changes will return 2 because there are two objects saved
                    // 1) The user object.
                    // 2) The image object.
                    if (response.status == 2)
                    {
                        string subject = Application.Constant.INSERT_USER_EMAIL_SUBJECT;
                        string body    = Application.Constant.INSERT_USER_EMAIL_BODY + token;

                        if (Utils.EmailUtils.sendEmail(subject, body, user.email))
                        {
                            response.status           = 1;
                            response.developerMessage = "Account Created, Check Your Email";
                        }
                        else
                        {
                            response.status           = 2;
                            response.developerMessage = "Couldn't Send Verification Email, Try Again Later!";
                        }
                    }
                    else
                    {
                        response.status           = -2;
                        response.developerMessage = "Couldn't Register, Try Again Later!";
                    }
                }
                else
                {
                    var registeredUser = isRegistered;

                    if (registeredUser.UserStatus == Constant.USER_ACTIVE)
                    {
                        response.status           = -1;
                        response.developerMessage = "Email Already Exist";
                    }
                    else
                    {
                        Random rnd = new Random();
                        token = rnd.Next(100000, 999999).ToString();

                        registeredUser.Phone         = user.phone;
                        registeredUser.Password      = Utils.EncryptionUtils.CreateMD5(user.password);
                        registeredUser.UserStatus    = Constant.USER_REACTIVE;
                        registeredUser.Address       = user.address;
                        registeredUser.TokenActivate = token;
                        registeredUser.EmailVerified = 0;

                        registeredUser.ProfileImage.Data = Encoding.UTF8.GetBytes(user.profileImage);

                        db.SaveChanges();

                        string subject = Constant.REACTIVATION_EMAIL_SUBJECT;
                        string body    = Constant.REACTIVATION_EMAIL_BODY + token;

                        if (Utils.EmailUtils.sendEmail(subject, body, user.email))
                        {
                            response.status           = 1;
                            response.developerMessage = "Account Reactivated, Check Your Email";
                        }
                        else
                        {
                            response.status           = 2;
                            response.developerMessage = "Couldn't Send Verification Email, Try Again Later!";
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                response.developerMessage = "Something went wrong: " + ex.Message;
                response.status           = 3;
            }
            return(response);
        }