Exemplo n.º 1
0
        public async Task <IActionResult> UpdateAccount(Guid id, [FromBody] AccountForUpdateDto account)
        {
            try
            {
                if (account == null)
                {
                    _logger.LogInfo("Account object sent from client is null");
                    return(BadRequest("Account object is null"));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid account object sent from client.");
                    return(BadRequest("Invalid model object"));
                }
                var accountEntity = await _repository.Account.GetAccountById(id);

                if (accountEntity == null)
                {
                    _logger.LogInfo($"Account with id: {id} hasn't been found in the db");
                }

                _mapper.Map(account, accountEntity);
                _repository.Account.UpdateAccount(accountEntity);
                await _repository.SaveAsync();

                return(NoContent());
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside UpdateAccount action: {ex.Message}");
                return(StatusCode(500, "Internal Server Error"));
            }
        }
Exemplo n.º 2
0
        public IActionResult UpdateAccount(Guid id, [FromBody] AccountForUpdateDto account)
        {
            try
            {
                if (account == null)
                {
                    _logger.LogError($"account object sent from client is null at: {DateTime.Now.ToString("dd/mm/YYYY HH:MM:SS")}");
                    return(BadRequest("account object is null"));
                }
                if (!ModelState.IsValid)
                {
                    _logger.LogError($"account object sent from client is not invalid at: {DateTime.Now.ToString("dd/mm/YYYY HH:MM:SS")}");
                    return(BadRequest("account model is invalid"));
                }
                var accountEntity = _repositoryWrapper.Account.GetAccountById(id);
                if (accountEntity == null)
                {
                    _logger.LogError($"account with id: {id} has'nt been in db.");
                    return(NotFound());
                }

                _mapper.Map(account, accountEntity);

                _repositoryWrapper.Account.UpdateAccount(accountEntity);
                _repositoryWrapper.Save();

                return(NoContent());
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside CreateOwner actio: {ex.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> UpdateAccountInfo([FromBody] AccountForUpdateDto updateDto)
        {
            var accountId = User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var result    = await _accountService.UpdateAccountInfo(updateDto, accountId);

            return(StatusCode((int)result.Code, result));
        }
        public IActionResult UpdateAccount(int id, [FromBody] AccountForUpdateDto account)
        {
            try
            {
                if (account == null)
                {
                    return(BadRequest("Account object is null"));
                }

                if (!ModelState.IsValid)
                {
                    return(BadRequest("Invalid model object"));
                }

                var accountEntity = _repository.Account.GetAccountById(id);
                if (accountEntity == null)
                {
                    return(NotFound());
                }

                _mapper.Map(account, accountEntity);

                _repository.Account.UpdateAccount(accountEntity);
                _repository.Save();

                return(NoContent());
            }
            catch (Exception ex)
            {
                return(StatusCode(500, "Internal server error: " + ex.Message));
            }
        }
Exemplo n.º 5
0
        public async Task <IActionResult> PutAccount(Guid id, AccountForUpdateDto accountForUpdate)
        {
            if (id != accountForUpdate.Id)
            {
                return(BadRequest());
            }

            var accountFromRepo = await _repo.GetAccount(id);

            _mapper.Map(accountForUpdate, accountFromRepo);

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AccountExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 6
0
        public IActionResult UpdateAccount(int id,
                                           [FromBody] AccountForUpdateDto account, bool includeForms = false)
        {
            if (account == null)
            {
                return(BadRequest());
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (!_accountRepository.AccountExists(id))
            {
                return(NotFound());
            }

            var accountEntity = _accountRepository.GetAccount(id, includeForms);

            if (accountEntity == null)
            {
                return(NotFound());
            }

            Mapper.Map(account, accountEntity);

            if (!_accountRepository.Save())
            {
                return(StatusCode(500, "A problem happened while handling your request."));
            }

            return(Ok(accountEntity));
        }
Exemplo n.º 7
0
        public async Task <ActionResult <AccountDto> > PutAccount(long id, AccountForUpdateDto accountForUpdate)
        {
            var userGuid = Guid.NewGuid();
            var account  = mapper.Map <Account>(accountForUpdate);

            if (id != account.Id || userGuid != account.UserGuid)
            {
                return(BadRequest());
            }

            _context.Entry(account).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AccountExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
Exemplo n.º 8
0
        public async Task <ApiResult <string> > UpdateAccountInfo(AccountForUpdateDto updateDto, string accountId)
        {
            var checkAccount = await _context.AppUsers.Where(x => x.Id.ToString() == accountId).SingleOrDefaultAsync();

            if (checkAccount == null)
            {
                return(new ApiResult <string>(HttpStatusCode.NotFound, $"Không tìm thấy tài khoản"));
            }
            var checkCustomer =
                await _context.Customers.Where(x => x.AppUserId == checkAccount.Id).SingleOrDefaultAsync();

            if (checkCustomer != null)
            {
                checkCustomer.Name        = updateDto.Name;
                checkCustomer.Gender      = updateDto.Gender;
                checkCustomer.Dob         = updateDto.Dob;
                checkCustomer.Address     = updateDto.Address;
                checkCustomer.PhoneNumber = updateDto.PhoneNumber;
                checkAccount.PhoneNumber  = updateDto.PhoneNumber;
                checkCustomer.Website     = updateDto.Website;
                checkCustomer.Fax         = updateDto.Fax;
                await _context.SaveChangesAsync();

                return(new ApiResult <string>(HttpStatusCode.OK)
                {
                    ResultObj = accountId,
                    Message = "Cập nhập thông tin tài khoản thành công"
                });
            }
            return(new ApiResult <string>(HttpStatusCode.BadRequest, "Người dùng liên kết với tài khoản không được phép chỉnh sửa thông tin, hãy liên hệ với quản trị viên để cập nhập thông tin cá nhân"));
        }
Exemplo n.º 9
0
        public async Task <IActionResult> UpdateAccount(AccountForUpdateDto account)
        {
            if (await _repo.UpdateAccount(account))
            {
                return(Ok());
            }

            return(Unauthorized());
        }
Exemplo n.º 10
0
        public async Task <IActionResult> UpdateAccount(long id, AccountForUpdateDto updateAccountDto)
        {
            var(newEmail, newContact) = (updateAccountDto.Email, updateAccountDto.ContactNumber);

            // can't update if param id is different from dto id
            if (id != updateAccountDto.Id)
            {
                return(BadRequest());
            }

            // find account with given id param
            var account = await Accounts.FindAsync(id);

            if (account == null)
            {
                return(NotFound());
            }

            // check if new email is "not null and different" from current email, check for duplicate
            if (!newEmail.IsNullOrEqual(account.Email))
            {
                var emailExists = await EmailExists(newEmail);

                if (emailExists)
                {
                    return(Conflict("Email already exists"));
                }
            }

            // check if new contact is "not null and different" from current contact, check for duplicate
            if (!newContact.IsNullOrEqual(account.ContactNumber))
            {
                var contactExists = await ContactExists(newContact);

                if (contactExists)
                {
                    return(Conflict("Contact Number already exists"));
                }
            }

            // perform update
            mapper.Map <AccountForUpdateDto, Account> (updateAccountDto, account);

            try
            {
                await context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException) when(IdDoesntExist(id))
            {
                return(NotFound());
            }

            return(NoContent());
        }
Exemplo n.º 11
0
        public async Task <IActionResult> UpdateAccount(int accountId, AccountForUpdateDto accountForUpdateDto)
        {
            var command = new UpdateAccountCommand(accountForUpdateDto, accountId);
            var result  = await Mediator.Send(command);

            if (result)
            {
                return(NoContent());
            }

            throw new Exception("Error updating the account.");
        }
Exemplo n.º 12
0
        public JsonResult UpdateAccount([FromBody] AccountForUpdateDto account)
        {
            string functionName = System.Reflection.MethodBase.GetCurrentMethod().Name;

            try
            {
                //Check id account exist in the database
                if (!_accountRepository.AccountExists(account.accountId))
                {
                    Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.accountNotFound));
                    return(Json(MessageResult.GetMessage(MessageType.ACCOUNT_NOT_FOUND)));
                }

                //Check value enter from the form
                if (account == null)
                {
                    Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.notInformationAccount));
                    return(Json(MessageResult.GetMessage(MessageType.NOT_INFORMATION_ACCOUNT)));
                }

                if (!ModelState.IsValid)
                {
                    Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.notFound));
                    return(Json(MessageResult.GetMessage(MessageType.NOT_FOUND)));
                }

                //This is get all information of account
                var accountEntity = _accountRepository.GetAccountById(account.accountId);

                if (accountEntity == null)
                {
                    Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.accountNotFound));
                    return(Json(MessageResult.GetMessage(MessageType.ACCOUNT_NOT_FOUND)));
                }

                //Map data enter from the form to account entity
                Mapper.Map(account, accountEntity);

                if (!_accountRepository.Save())
                {
                    Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.badRequest));
                    return(Json(MessageResult.GetMessage(MessageType.BAD_REQUEST)));
                }

                Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(Constants.accountUpdated));
                return(Json(MessageResult.GetMessage(MessageType.ACCOUNT_UPDATED)));
            }
            catch (Exception ex)
            {
                Log4Net.log.Error(className + "." + functionName + " - " + Log4Net.AddErrorLog(ex.Message));
                return(Json(MessageResult.ShowServerError(ex.Message)));
            }
        }
Exemplo n.º 13
0
        public async Task <IActionResult> UpdateUser(int id, AccountForUpdateDto accountForUpdateDto)
        {
            var accountFromRepo = await _repo.GetAccount(id);

            _mapper.Map(accountForUpdateDto, accountFromRepo);

            if (await _repo.SaveAll())
            {
                var accountToReturn = _mapper.Map <AccountToReturnDto>(accountFromRepo);
                return(Ok(accountToReturn));
            }


            throw new Exception($"Updating user {id} failed on save");
        }
Exemplo n.º 14
0
        public async Task <IActionResult> UpdateAccount(int userId, string Name, AccountForUpdateDto AccountForUpdateDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var AccountFromRepo = await _repo.GetAccountByName(userId, Name);

            _mapper.Map(AccountForUpdateDto, AccountFromRepo);

            if (await _repo.SaveAll())
            {
                return(CreatedAtRoute("GetAccount", new { Name = AccountFromRepo.Name, userId = userId }, AccountFromRepo));
            }

            throw new Exception($"Updating Account {Name} failed on save");
        }
Exemplo n.º 15
0
        /// <summary>
        /// Updates account
        /// </summary>
        /// <param name="account">Account for update</param>
        /// <returns></returns>
        public async Task <bool> UpdateAccount(AccountForUpdateDto account)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter {
                    ParameterName = "@accountId", DbType = DbType.Int32, Direction = ParameterDirection.Input, Value = account.Id
                },
                new SqlParameter {
                    ParameterName = "@name", DbType = DbType.String, Direction = ParameterDirection.Input, Value = account.Name
                },
                new SqlParameter {
                    ParameterName = "@balance", DbType = DbType.Decimal, Direction = ParameterDirection.Input, Value = account.Balance
                },
                new SqlParameter {
                    ParameterName = "@response", DbType = DbType.Boolean, Direction = ParameterDirection.Output
                }
            };

            await _context.Database.ExecuteSqlCommandAsync("EXECUTE UpdateAccount @accountId, @name, @balance, @response OUT", parameters);

            return((bool)parameters[parameters.Length - 1].Value);
        }
Exemplo n.º 16
0
        public IActionResult UpdateAccount(Guid id, [FromBody] AccountForUpdateDto account)
        {
            try
            {
                if (account == null)
                {
                    _logger.LogError("Owner object sent from client is null.");
                    return(BadRequest("Owner object is null"));
                }

                if (!ModelState.IsValid)
                {
                    _logger.LogError("Invalid owner object sent from client.");
                    return(BadRequest("Invalid model object"));
                }

                var accEntity = _repository.Account.GetAccountById(id);
                if (accEntity == null)
                {
                    _logger.LogError($"Owner with id: {id},n' a pas été trouvé dans la base de donnée.");
                    return(NotFound());
                }

                _mapper.Map(account, accEntity);

                _repository.Account.UpdateAccount(accEntity);
                _repository.Save();

                return(NoContent());
            }
            catch (Exception ex)
            {
                _logger.LogError($"Something went wrong inside UpdateOwner action: {ex.Message}");
                return(StatusCode(500, "Internal server error"));
            }
        }
 public UpdateAccountCommand(AccountForUpdateDto accountForUpdateDto, int accountId)
 {
     AccountId           = accountId;
     AccountForUpdateDto = accountForUpdateDto;
 }