示例#1
0
        public async Task <bool> Delete(int companyId, string username)
        {
            //-- Connects to the database
            using (var entity = new CompanyBrokerAccountEntities())
            {
                //-- fetches an account based on the informations
                var account = entity.CompanyAccounts.Where(a => a.CompanyId == companyId && a.Username == username).Single <CompanyAccount>();
                //-- checks the account
                if (account != null)
                {
                    //-- removes the account
                    entity.CompanyAccounts.Remove(account);
                    //-- informs of the state
                    entity.Entry(account).State = EntityState.Deleted;
                    //-- saves the state
                    await entity.SaveChangesAsync();

                    //-- return
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
示例#2
0
        public async Task <bool> CreateAccount(AccountRequest accountRequest)
        {
            bool resultProcess = false;
            //-- generating the salt
            var salt = GenerateSalt(32);

            if (accountRequest != null)
            {
                //-- Creates the new account
                var user = new CompanyAccount
                {
                    CompanyId    = accountRequest.CompanyId,
                    Username     = accountRequest.Username,
                    Email        = accountRequest.Email,
                    PasswordSalt = salt,
                    PasswordHash = GetHash(accountRequest.Password, salt),
                    Active       = accountRequest.Active
                };

                //-- Connects to the database using the entity
                using (var entitys = new CompanyBrokerAccountEntities())
                {
                    //-- adds a new user to the CompanyAccounts table
                    entitys.CompanyAccounts.Add(user);
                    //-- Saves the changes to the database
                    await entitys.SaveChangesAsync();

                    resultProcess = true;
                }
            }

            //-- Returns the user wished to be created
            return(resultProcess);
        }
示例#3
0
        public async Task <bool> PUT(AccountRequest AccountAPIModel)
        {
            //-- Getting access to the
            using (var entity = new CompanyBrokerAccountEntities())
            {
                //-- fetches the account
                var responsData = entity.CompanyAccounts.Where(a => a.CompanyId == AccountAPIModel.CompanyId && a.Username == AccountAPIModel.Username).Single <CompanyAccount>();
                //-- checks the account
                if (responsData != null)
                {
                    //-- sets the new informations
                    responsData.Email    = AccountAPIModel.Email;
                    responsData.Active   = AccountAPIModel.Active;
                    responsData.Username = AccountAPIModel.Username;

                    //-- checks if password has been changed
                    if (!string.IsNullOrEmpty(AccountAPIModel.Password))
                    {
                        //-- creates new salt
                        var newSalt = GenerateSalt(32);
                        //-- sets new password informations
                        responsData.PasswordHash = GetHash(AccountAPIModel.Password, newSalt);
                        responsData.PasswordSalt = newSalt;
                    }
                    //-- Sets the data entry and sate
                    entity.Entry(responsData).State = EntityState.Modified;
                    //-- saves the data
                    await entity.SaveChangesAsync();

                    //-- returns
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }