Exemplo n.º 1
0
        public async Task <AdminDTO> UpdateAdminAsync(UpdateAdminDTO updateAdminDTO)
        {
            if (string.IsNullOrWhiteSpace(updateAdminDTO.NewLogin) &&
                string.IsNullOrWhiteSpace(updateAdminDTO.NewPassword))
            {
                throw new BadArgumentException("The new login and password cannot be blank at the same time. There is nothing to update.");
            }

            AdminEntity admin = await _DBContext.Admins.FirstOrDefaultAsync(a => a.Id == updateAdminDTO.Id);

            if (admin == null)
            {
                throw new BadArgumentException($"Admin with id {updateAdminDTO.Id} not found.");
            }

            //Подумати!!!
            if (!string.IsNullOrWhiteSpace(updateAdminDTO.NewLogin))
            {
                admin.Login = updateAdminDTO.NewLogin;
            }
            if (!string.IsNullOrWhiteSpace(updateAdminDTO.NewPassword))
            {
                PasswordHashHelper.CreatePasswordHash(updateAdminDTO.NewPassword, out var hash, out var sail);

                admin.PasswordHash = hash;
                admin.PasswordSalt = sail;
            }

            _DBContext.Admins.Update(admin);
            await _DBContext.SaveChangesAsync();

            var updatedAdminDTO = _mapper.Map <AdminDTO>(admin);

            return(updatedAdminDTO);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> UpdateSource(int id, [FromBody] UpdateAdminDTO source)
        {
            try
            {
                if (source == null)
                {
                    return(BadRequest());
                }
                if (!ModelState.IsValid)
                {
                    return(BadRequest("Invalid model object"));
                }
                var sourceEntity = await _repositoryWrapper.AdminRepo.GetDataByIdAsync(id);

                if (sourceEntity == null)
                {
                    return(NotFound());
                }
                int genderId = sourceEntity.GenderId;
                _mapper.Map(source, sourceEntity);
                if (source.GenderId == 0)
                {
                    sourceEntity.GenderId = genderId;
                }
                _repositoryWrapper.AdminRepo.UpdateData(sourceEntity);
                await _repositoryWrapper.SaveAsync();

                return(Ok("Update successfully!"));
            }
            catch (Exception ex)
            {
                //_logger.LogError($"Something went wrong inside CreateSources action: {ex.Message}");
                if (ex.InnerException != null)
                {
                    return(BadRequest(ex.Message + "," + ex.InnerException.Message));
                }
                return(BadRequest(ex.Message));
            }
        }