async Task Register()
        {
            try
            {
                if (Password != PasswordConfirm)
                {
                    await Application.Current.MainPage.DisplayAlert("Error", "Password don't match", "Try again");

                    return;
                }

                if (string.IsNullOrEmpty(FirstName) || string.IsNullOrEmpty(LastName) || string.IsNullOrEmpty(Phone) || string.IsNullOrEmpty(Email) ||
                    string.IsNullOrEmpty(Username) || string.IsNullOrEmpty(Password) || string.IsNullOrEmpty(PasswordConfirm))
                {
                    await Application.Current.MainPage.DisplayAlert("Error", "All fields are required", "Try again");

                    return;
                }

                var userList = await _serviceCustomer.Get <List <Data.Model.Customer> >(null);

                foreach (var item in userList)
                {
                    if (item.Username == Username)
                    {
                        await Application.Current.MainPage.DisplayAlert("Error", "Username already exist !", "Try again");

                        return;
                    }
                }

                var request = new CustomerUpsert
                {
                    FirstName       = FirstName,
                    LastName        = LastName,
                    Phone           = Phone,
                    Email           = Email,
                    CityId          = CityId,
                    Username        = Username,
                    Password        = Password,
                    PasswordConfirm = PasswordConfirm,
                    CustomerTypeId  = 2
                };

                await _serviceCustomer.Insert <Data.Model.Customer>(request);

                await Application.Current.MainPage.DisplayAlert("Registred succesfully.", "Now just log in.", "OK");

                await Application.Current.MainPage.Navigation.PushModalAsync(new LoginPage());
            }
            catch (Exception err)
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Wrong input", "Try again");
            }
        }
        public Data.Customer Update(int id, CustomerUpsert customer)
        {
            var entity = _context.Customer.Find(id);

            _context.Customer.Attach(entity);
            _context.Customer.Update(entity);

            _mapper.Map(customer, entity);
            _context.SaveChanges();

            return(_mapper.Map <Data.Customer>(entity));
        }
        public Model.Users Update(int id, CustomerUpsert customer)
        {
            Database.Users user = _context.Users.FirstOrDefault(e => e.UserId == id);
            if (user == null)
            {
                throw new Exception("User don't exist");
            }
            _context.Users.Attach(user);
            _context.Users.Update(user);
            _mapper.Map(customer, user);

            _context.SaveChanges();
            return(_mapper.Map <Model.Users>(user));
        }
        public Data.Customer Insert(CustomerUpsert customer)
        {
            var entity = _mapper.Map <Customer>(customer);

            if (customer.Password != customer.PasswordConfirm)
            {
                throw new Exception("Password and password confirm don't match !");
            }

            entity.PasswordSalt = HashGenerator.GenerateSalt();
            entity.PasswordHash = HashGenerator.GenerateHash(entity.PasswordSalt, customer.Password);

            _context.Add(entity);
            _context.SaveChanges();
            return(_mapper.Map <Data.Customer>(entity));
        }
        public Data.Model.Customer Insert(CustomerUpsert request)
        {
            var entity = _mapper.Map <Customer>(request);

            if (request.Password != request.PasswordConfirm)
            {
                throw new Exception("Password and password confirm not matched !");
            }
            entity.PasswordSalt = HashGenerator.GenerateSalt();
            entity.PasswordHash = HashGenerator.GenerateHash(entity.PasswordSalt, request.Password);

            _context.Add(entity);
            _context.SaveChanges();

            return(_mapper.Map <Data.Model.Customer>(entity));
        }
Example #6
0
 public Model.Users Update(int id, CustomerUpsert request)
 {
     return(_service.Update(id, request));
 }
        public async Task SaveEditChanges()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(FirstName) || string.IsNullOrWhiteSpace(LastName) || string.IsNullOrWhiteSpace(Email) ||
                    string.IsNullOrWhiteSpace(PhoneNumber) || string.IsNullOrWhiteSpace(Username))
                {
                    await Application.Current.MainPage.DisplayAlert("Error", "All fields are requreid", "Try again");

                    return;
                }

                if (Username.Length < 4)
                {
                    await Application.Current.MainPage.DisplayAlert("Error", "Username must have minimum 4 characters", "Try again");

                    return;
                }

                var userList = await _serviceCustomer.Get <List <Data.Model.Customer> >(null);

                foreach (var item in userList)
                {
                    if (item.Username.StartsWith(Username) && APIService.Username != item.Username)
                    {
                        await Application.Current.MainPage.DisplayAlert("Error", "Username already exist", "Try again");

                        return;
                    }
                }

                var customerID = await _serviceCustomer.GetById <Data.Model.Customer>(APIService.CustomerId);

                customer = customerID;

                // Password

                var request = new CustomerUpsert
                {
                    FirstName       = FirstName,
                    LastName        = LastName,
                    Phone           = PhoneNumber,
                    CityId          = customer.CityId,
                    Email           = Email,
                    Username        = Username,
                    CustomerTypeId  = 2,
                    Password        = APIService.Password,
                    PasswordConfirm = APIService.Password
                };

                APIService.Username = Username;

                var userUpdate = await _serviceCustomer.Update <Data.Model.Customer>(APIService.CustomerId, request);

                await Application.Current.MainPage.DisplayAlert("Succesfull", "Succesfully changed, please log in with new credentials.", "OK");

                await Application.Current.MainPage.Navigation.PushModalAsync(new LoginPage());
            }
            catch (Exception ex)
            {
                await Application.Current.MainPage.DisplayAlert("Error", "Something went wrong.", "Try again");
            }
        }
 public ActionResult <Data.Model.Customer> Update(int id, [FromBody] CustomerUpsert request)
 {
     return(_service.Update(id, request));
 }
 public ActionResult <Data.Model.Customer> Insert(CustomerUpsert request)
 {
     return(_service.Insert(request));
 }
 public ActionResult <Data.Customer> Update(int id, CustomerUpsert request)
 {
     return(_service.Update(id, request));
 }