Пример #1
0
        internal object UpdateUserData(UserDataVM userDataVM)
        {
            var UserDataUpdate = db.Users.Find(AccessToken.GetUserId());

            //Check Is Update Language
            if (userDataVM.IsUpdateLanguage)
            {
                if (userDataVM.LangId <= 0)
                {
                    return(new ResponseVM(RequestTypeEnumVM.Error, $"{Token.Language} : {Token.FiledIsRequired}"));
                }

                if (!db.LanguageTypes.Any(c => c.Id == userDataVM.LangId))
                {
                    return(new ResponseVM(RequestTypeEnumVM.Error, $"{Token.Language} : {Token.NotFound}"));
                }

                UserDataUpdate.
                FKLanguage_Id = userDataVM.LangId;
            }

            //Check Is Update Email
            else if (userDataVM.IsUpdateEmail)
            {
                if (string.IsNullOrEmpty(userDataVM.Email))
                {
                    return(new ResponseVM(RequestTypeEnumVM.Error, $"{Token.Email} : {Token.FiledIsRequired}"));
                }

                if (!CheckedS.EmailIsCorrect(userDataVM.Email))
                {
                    return(new ResponseVM(RequestTypeEnumVM.Error, $"{Token.Email} : {Token.InvalidData}"));
                }

                if (db.Users.Where(c => c.Id != UserDataUpdate.Id).Any(c => c.Email == userDataVM.Email))
                {
                    return(new ResponseVM(RequestTypeEnumVM.Error, $"{Token.Email} : {Token.BeforeUsed}"));
                }

                UserDataUpdate.Email = userDataVM.Email;
                return(null);
            }

            //Check Is Update Phone
            else if (userDataVM.IsUpdatePhone)
            {
                if (string.IsNullOrEmpty(userDataVM.Phone))
                {
                    return new { RequestType = RequestTypeEnumVM.Error, Message = Token.Phone + " >> " + Token.FiledIsRequired }
                }
                ;

                if (!CheckedS.NumberValid(userDataVM.Phone))
                {
                    return new { RequestType = RequestTypeEnumVM.Error, Message = Token.Phone + " >> " + Token.InvalidNumber }
                }
                ;



                if (userDataVM.Phone.Length > 15)
                {
                    return new { RequestType = RequestTypeEnumVM.Error, Message = Token.Phone + " >> " + Token.MaxLength + "= 15" }
                }
                ;


                UserDataUpdate.PhoneNumber = userDataVM.Phone;
            }
            //Check Is Update Password
            else if (userDataVM.IsUpdatePassword)
            {
                if (string.IsNullOrEmpty(userDataVM.Password))
                {
                    return new { RequestType = RequestTypeEnumVM.Error, Message = Token.Password + " >> " + Token.FiledIsRequired }
                }
                ;
                UserDataUpdate.Password = userDataVM.Password;
            }
            //Check Is Update UserName
            else if (userDataVM.IsUpdateUserName)
            {
                if (string.IsNullOrEmpty(userDataVM.UserName))
                {
                    return new { RequestType = RequestTypeEnumVM.Error, Message = Token.Password + " >> " + Token.FiledIsRequired }
                }
                ;

                if (db.Users.Where(c => c.Id != UserDataUpdate.Id).Any(c => c.UserName == userDataVM.UserName))
                {
                    return new { RequestType = RequestTypeEnumVM.Error, Message = Token.UserName + " >> " + Token.ErrorDuplicate }
                }
                ;

                UserDataUpdate.UserName = userDataVM.UserName;
            }
            //Check Is Update Full Name
            else if (userDataVM.IsUpdateFullName)
            {
                if (string.IsNullOrEmpty(userDataVM.FullName))
                {
                    return new { RequestType = RequestTypeEnumVM.Error, Message = Token.FullName + " >> " + Token.FiledIsRequired }
                }
                ;

                UserDataUpdate.FullName = userDataVM.FullName;
            }

            //Check Is Update Country
            else if (userDataVM.IsUpdateCountry)
            {
                if (userDataVM.CountryId <= 0)
                {
                    return new { RequestType = RequestTypeEnumVM.Error, Message = Token.Country + " >> " + Token.FiledIsRequired }
                }
                ;

                UserDataUpdate.Address.FKCountry_Id = userDataVM.CountryId;
            }
            //Check Is Update City
            else if (userDataVM.IsUpdateCity)
            {
                if (string.IsNullOrEmpty(userDataVM.City))
                {
                    return new { RequestType = RequestTypeEnumVM.Error, Message = Token.City + " >> " + Token.FiledIsRequired }
                }
                ;

                UserDataUpdate.Address.City = userDataVM.City;
            }
            //Check Is Update Province
            else if (userDataVM.IsUpdateProvince)
            {
                if (userDataVM.ProvinceId <= 0)
                {
                    return new { RequestType = RequestTypeEnumVM.Error, Message = Token.Province + " >> " + Token.FiledIsRequired }
                }
                ;

                UserDataUpdate.Address.FkProvince_Id = userDataVM.ProvinceId;
            }
            //Check Is Update Profile Image
            else if (userDataVM.IsUpdateImageProfile)
            {
                if (string.IsNullOrEmpty(userDataVM.FileBase64))
                {
                    return new { RequestType = RequestTypeEnumVM.Error, Message = Token.ImageProfile + " >> " + Token.FiledIsRequired }
                }
                ;

                //Uplaod Image
                FileSaveVM FileSave = FileService.SaveFileBase64(new FileSaveVM()
                {
                    FileBase64     = userDataVM.FileBase64,
                    ServerPathSave = "/Files/Images/Users/Profile/"
                });


                if (!FileSave.IsSave)
                {
                    return new { RequestType = RequestTypeEnumVM.Error, Message = Token.CanNotSaveFile }
                }
                ;

                UserDataUpdate.AllFile.FileUrl             = FileSave.SavedPath;
                UserDataUpdate.AllFile.FKAppInformation_Id = AppInformationEnumVM.ApiLink
                ;
            }

            return(null);
        }
Пример #2
0
        /// <summary>
        /// Create User Calling From Api
        /// </summary>
        /// <param name="model"></param>
        /// <param name="pageAllow"></param>
        /// <returns></returns>
        internal object User(UserVM user)
        {
            if (user == null)
            {
                return new { RequestType = RequestTypeEnumVM.Error, Message = Token.DataNotFound }
            }
            ;

            //Check Required
            if (string.IsNullOrEmpty(user.Email))
            {
                return(new ResponseVM(RequestTypeEnumVM.Error, $"{Token.Email} : {Token.FiledIsRequired}"));
            }
            if (string.IsNullOrEmpty(user.UserName))
            {
                return(new ResponseVM(RequestTypeEnumVM.Error, $"{Token.UserName} : {Token.FiledIsRequired}"));
            }
            if (string.IsNullOrEmpty(user.Password))
            {
                return(new ResponseVM(RequestTypeEnumVM.Error, $"{Token.Password} : {Token.FiledIsRequired}"));
            }
            if (string.IsNullOrEmpty(user.PhoneNumber))
            {
                return(new ResponseVM(RequestTypeEnumVM.Error, $"{Token.Phone} : {Token.FiledIsRequired}"));
            }

            if (user.IsCompany && user.CompanyInfomration == null)
            {
                return(new ResponseVM(RequestTypeEnumVM.Error, Token.CompanyInformationIsRequired));
            }
            if (user.IsCompany && string.IsNullOrEmpty(user.CompanyInfomration.Description))
            {
                return(new ResponseVM(RequestTypeEnumVM.Error, $"{Token.CompanyDescription} : {Token.FiledIsRequired}"));
            }
            if (user.IsCompany && user.CompanyInfomration.ActivityId == 0)
            {
                return(new ResponseVM(RequestTypeEnumVM.Error, $"{Token.CompanyActivity} : {Token.FiledIsRequired}"));
            }
            if (user.IsCompany && !db.Activities.Any(c => c.Id == user.CompanyInfomration.ActivityId))
            {
                return(new ResponseVM(RequestTypeEnumVM.Error, $"{Token.CompanyActivity} : {Token.NotFound}"));
            }
            if (user.IsCompany && user.CompanyInfomration.Officess.Any(c => string.IsNullOrEmpty(c.Address)))
            {
                return(new ResponseVM(RequestTypeEnumVM.Error, LanguageService.IsEn?"There are some office addresses that don't have data": "هناك بعض عنوانين للمكاتب ليست بها بيانات"));
            }
            if (user.IsCompany && user.CompanyInfomration.Officess.Any(c => c.CountryId == 0))
            {
                return(new ResponseVM(RequestTypeEnumVM.Error, LanguageService.IsEn ? "There are some office countries that don't have data" : "هناك بعض الدول للمكاتب ليست بها بيانات"));
            }

            //Check   unic
            if (db.Users.Any(x => x.Email == user.Email))
            {
                return new { RequestType = RequestTypeEnumVM.Error, Message = Token.Email + "  : " + Token.BeforeUsed }
            }
            ;
            if (db.Users.Any(x => x.UserName == user.UserName))
            {
                return new { RequestType = RequestTypeEnumVM.Error, Message = Token.UserName + "  : " + Token.BeforeUsed }
            }
            ;
            if (db.Users.Any(x => x.PhoneNumber == user.PhoneNumber))
            {
                return new { RequestType = RequestTypeEnumVM.Error, Message = Token.Phone + "  : " + Token.BeforeUsed }
            }
            ;


            //Check  length
            if (user.Password.Length < 6)
            {
                return new { RequestType = RequestTypeEnumVM.Error, Message = Token.MinimumLength + "  : " + Token.For + "  : " + Token.Password + " 6" }
            }
            ;
            //Check UserName
            if (user.UserName.Length > 30)
            {
                return new { RequestType = RequestTypeEnumVM.Error, Message = Token.UserName + "  : " + Token.MaxLength + " 30" }
            }
            ;

            //Check Data Is Valid
            if (!CheckedS.EmailIsCorrect(user.Email))
            {
                return new { RequestType = RequestTypeEnumVM.Error, Message = Token.InvalidEmail }
            }
            ;

            List <UserRole> UserRoles = new List <UserRole>()
            {
                new UserRole()
                {
                    FkRole_Id = user.IsCompany?RoleEnumVM.Company:RoleEnumVM.Client,
                }
            };
            FileSaveVM           FileSave       = new FileSaveVM();
            List <CompanyOffice> CompanyOffices = new List <CompanyOffice>();

            if (!string.IsNullOrEmpty(user.Image))
            {
                FileSave = FileService.SaveFileBase64(new FileSaveVM
                {
                    FileBase64     = user.Image,
                    ServerPathSave = "/Files/Images/Users/",
                    Extension      = Path.GetExtension(user.ImageName)
                });

                if (!FileSave.IsSave)
                {
                    return(new ResponseVM(RequestTypeEnumVM.Error, Token.CanNotSaveFile));
                }
            }

            User User = new User()
            {
                Email            = user.Email,
                Password         = user.Password,
                UserName         = user.UserName,
                IsBlocked        = false,
                UserRoles        = UserRoles,
                CreateDate       = DateTime.Now,
                FullName         = "",
                FKLanguage_Id    = (int)LanguageEnum.Arabic,
                PhoneNumber      = user.PhoneNumber,
                PhoneActiveCode  = 4444,
                IsPhoneActivated = false,
                Address          = new Address(),
                AllFile          = new AllFile()
                {
                    FkFileType_Id       = FileTypeEnumVM.Image,
                    FKAppInformation_Id = AppInformationEnumVM.ApiFiles,
                    FileUrl             = FileSave.SavedPath == null?"":FileSave.SavedPath,
                },
            };

            if (user.IsCompany)
            {
                user.CompanyInfomration.Officess.ForEach(c =>
                {
                    CompanyOffices.Add(new CompanyOffice
                    {
                        Address = new Address
                        {
                            Street       = c.Address,
                            FKCountry_Id = c.CountryId
                        },
                        Location = new Location
                        {
                            Latitude  = "", //c.Latitude,
                            Longitude = "", // c.Longitude
                        }
                    });
                });
                User.Company = new Company
                {
                    Description    = user.CompanyInfomration.Description,
                    FKActivity_Id  = user.CompanyInfomration.ActivityId,
                    CompanyOffices = CompanyOffices
                };
            }

            db.Users.Add(User);
            return(null);
        }