public IActionResult UpdateCustomer(Guid id, [FromBody] CustomerUpdateDto updateDto) { if (updateDto == null) { return(BadRequest()); } var existingCustomer = _customerRepository.GetSingle(id); if (existingCustomer == null) { return(NotFound()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Mapper.Map(updateDto, existingCustomer); _customerRepository.Update(existingCustomer); bool result = _customerRepository.Save(); if (!result) { throw new Exception($"(PUT):something went wrong when updating the customer with id: {id}"); } return(Ok(Mapper.Map <CustomerDto>(existingCustomer))); }
public async Task <IActionResult> UpdateAsync(int?id, [FromBody] CustomerUpdateDto updateDto) { if (id == null || id.Value != updateDto.Id) { return(BadRequest(new { Error = "Invalid customer id." })); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var customer = await _unitOfWork.CustomerRepository.GetByIdAsync(id.Value); if (customer == null) { return(BadRequest(new { Error = $"Customer with id {id.Value} cannot be found." })); } var currentUser = await _userManager.GetUserAsync(HttpContext.User); customer.Name = updateDto.Name; customer.UpdateUserId = currentUser?.Id; customer.UpdateDate = DateTime.Now; await _unitOfWork.CompleteAsync(); var dto = _mapper.Map <CustomerDto>(customer); return(Ok(dto)); }
public async Task <IActionResult> UpdateCustomer([FromBody] CustomerUpdateDto customerDto) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var id = User.Claims.FirstOrDefault(c => c.Type == Helpers.Constants.Strings.JwtClaimIdentifiers.CustomerId)?.Value; var customer = _usersRepository.GetCustomerById(Guid.Parse(id)); if (customer == null) { return(NotFound()); } Mapper.Map(customerDto, customer.Identity); Mapper.Map(customerDto, customer); if (customerDto.CurrentPassword != null && customerDto.NewPassword != null) { var result = await _userManager.ChangePasswordAsync(customer.Identity, customerDto.CurrentPassword, customerDto.NewPassword); if (!result.Succeeded) { return(BadRequest()); } } await _usersRepository.UpdateCustomer(customer); return(NoContent()); }
public async Task <IActionResult> UpdateCustomerAsync(Guid id, [FromBody] CustomerUpdateDto customerUpdateDto) { if (customerUpdateDto == null) { return(BadRequest()); } var existingCustomer = await _customerRepository.GetSingleAsync(id); if (existingCustomer == null) { return(NotFound()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Mapper.Map(customerUpdateDto, existingCustomer); _customerRepository.Update(existingCustomer); bool result = await _customerRepository.SaveAsync(); if (!result) { return(new StatusCodeResult(500)); } return(Ok(Mapper.Map <CustomerDto>(existingCustomer))); }
public IActionResult UpdateCustomer(int id, [FromBody] CustomerUpdateDto customerToUpdate) { if (customerToUpdate == null) { return(BadRequest()); } Customer oldCustomer = _customerRepository.Get(id); if (oldCustomer == null) { return(NotFound()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Mapper.Map(customerToUpdate, oldCustomer); _customerRepository.Update(oldCustomer); bool result = _customerRepository.Save(); if (!result) { return(new StatusCodeResult(500)); } return(CreatedAtRoute("GetCustomerByID", new { id = oldCustomer.ID }, Mapper.Map <CustomerDto>(oldCustomer))); }
public IActionResult UpdateCustomer(Guid id, [FromBody] CustomerUpdateDto customerDtoToSave) { if (customerDtoToSave == null) { return(BadRequest("customerDtoToSave is null")); } var existingCustomer = _customerRepository.GetSingle(id); if (existingCustomer == null) { //return NotFound(); throw new Exception($" ----> UpdateCustomer() NotFound {id}"); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } AutoMapper.Mapper.Map(customerDtoToSave, existingCustomer); _customerRepository.Update(existingCustomer); bool result = _customerRepository.Save(); if (!result) { //return new StatusCodeResult(500); throw new Exception($" ----> UpdateCustomer() Error {id}"); } return(Ok(AutoMapper.Mapper.Map <CustomerUpdateDto>(existingCustomer))); }
public IActionResult Update(int id, [FromBody] CustomerUpdateDto updateDto) { if (updateDto == null) { return(BadRequest()); } var existingCustomer = _customerRepository.GetSingle(id); if (existingCustomer == null) { return(NotFound()); } Mapper.Map(updateDto, existingCustomer); _customerRepository.Update(id, existingCustomer); if (!_customerRepository.Save()) { throw new Exception("Updating an item failed on save."); } return(Ok(ExpandSingleItem(existingCustomer))); }
public IActionResult UpdateCustomer(Guid id, [FromBody] CustomerUpdateDto updatedto) { if (updatedto == null) { return(BadRequest("Empty Customer Object")); } var existingCustomer = _customerRepository.GetSingle(id); if (existingCustomer == null) { return(NotFound()); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Mapper.Map(updatedto, existingCustomer); _customerRepository.Update(existingCustomer); bool result = _customerRepository.Save(); if (!result) { //return new StatusCodeResult(500); throw new Exception("Error Updating Customer"); } return(Ok(Mapper.Map <CustomerDto>(existingCustomer))); }
public ActionResult <CustomerDto> UpdateCustomer(int id, [FromBody] CustomerUpdateDto updateDto) { if (updateDto == null) { return(BadRequest()); } var existingCustomer = _repository.GetSingle(id); if (existingCustomer == null) { return(NotFound()); } Mapper.Map(updateDto, existingCustomer); _repository.Update(id, existingCustomer); if (!_repository.Save()) { throw new Exception("Updating a Customer failed on save."); } return(Ok(Mapper.Map <CustomerDto>(existingCustomer))); }
public void Update(Customer oryginal, CustomerUpdateDto update) { if (oryginal != null && update != null) { _mapper.Map(update, oryginal); } }
public async Task <IActionResult> UpdateCustomerAsync(int id, CustomerUpdateDto customerUpdateDto) { if (id != customerUpdateDto.Id) { return(BadRequest()); } var customerFromRepo = await _repository.GetByIdAsync(id); if (customerFromRepo == null) { return(NotFound()); } _mapper.Map(customerUpdateDto, customerFromRepo); //_repository.UpdateCustomer(customerFromRepo.Value); // this is achieved by the previous code try { await _repository.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if ((await _repository.GetByIdAsync(id)) == null) { return(NotFound()); } } return(NoContent()); }
public override async Task <CustomerDto> Update(CustomerUpdateDto input) { //var output = await base.Update(input); //return output; CheckUpdatePermission(); var entity = await GetEntityByIdAsync(input.Id); MapToEntity(input, entity); #region Customer Category Setting // Assemble category. entity.CustomerCategorySettings = new List <CustomerCategorySetting>(); // 1. Get old category. List <CustomerCategorySetting> settings = customerCategorySettingRepository.GetAll() .Where(t => t.CustomerId == input.Id) .ToList(); // Get the category from database that had already existed. foreach (var item in settings) { if (input.CategorySettings.Contains(item.CategoryCode)) { // this category still alive. and ignore it for add. input.CategorySettings.Remove(item.CategoryCode); } else { // remove from local list object. // xx settings.Remove(item); // remove from database. customerCategorySettingRepository.Delete(item.Id); } } // Get the surplus category from input, that need to be added in database. foreach (var code in input.CategorySettings) { CustomerCategory category = customerCategoryRepository.FirstOrDefault(t => t.Code == code); customerCategorySettingRepository.Insert(new CustomerCategorySetting { CustomerCategoryId = category.Id, CustomerId = entity.Id, TenantId = this.AbpSession.TenantId, CreatorUserId = this.AbpSession.UserId, IsActive = true, Status = 0 }); } #endregion await CurrentUnitOfWork.SaveChangesAsync(); return(MapToEntityDto(entity)); }
public async Task <bool> CreateAsync(CustomerUpdateDto model, CancellationToken cancellationToken = default) { Customer customer = new Customer(); return(await _customerRepository .CreateAsync(model.MapTo(customer), cancellationToken) .ConfigureAwait(false)); }
public IResult Update(CustomerUpdateDto customerUpdateDto) { //mapping Customer customer = _mapper.Map <Customer>(customerUpdateDto); _customerDal.Update(customer); return(new SuccessResult(Messages.CustomerUpdated)); }
public async Task <CustomerDto> UpdateCustomer(CustomerUpdateDto customerDto) { var customer = this.mapper.Map <Domain.Customer>(customerDto); var result = await this.repository.UpdateAsync(customer).ConfigureAwait(false); return(this.mapper.Map <CustomerDto>(result)); }
public async Task <IActionResult> Edit(int id) { Customer updateCustomer = await _customerManager.FindById(id); CustomerUpdateDto customerUpdateDto = _mapper.Map <CustomerUpdateDto>(updateCustomer); return(View(customerUpdateDto)); }
public IActionResult EditCustomer(CustomerUpdateDto customer) { var result = _customerService.EditCustomer(customer); if (result.SuccessStatus) { return(Ok(result)); } return(BadRequest(result)); }
public IActionResult Update(CustomerUpdateDto customerUpdateDto) { var result = _customerService.Update(customerUpdateDto); if (result.Success) { return(Ok(result)); } return(BadRequest(result)); }
public void Update(int id, CustomerUpdateDto model) { var entry = _context.Customers.Single(x => x.CustomerId == id); entry.CustomerName = model.CustomerName; entry.CustomerAge = model.CustomerAge; entry.Customer_CategoryId = model.Customer_CategoryId; _context.SaveChanges(); }
public async Task <ActionResult> Put(Guid id, [FromBody] CustomerUpdateDto customerDto) { if (id != customerDto.Id) { return(BadRequest()); } await customerService.Put(customerDto); return(Ok()); }
public async Task <IActionResult> UpdateWithUserAsync(CustomerUpdateDto customerUpdateDto) { var result = await _customerService.UpdateWithUserAsync(customerUpdateDto); if (result.Success) { return(Ok(result)); } return(BadRequest(result)); }
public void UpdateCustomer(int id, CustomerUpdateDto customer) { var cus = GetCustomer(id); cus.FirstName = customer.FirstName; cus.LastName = customer.LastName; cus.Email = customer.Email; cus.Phone = customer.Phone; context.SaveChanges(); }
public static Customer MapTo(this CustomerUpdateDto @this, Customer that) { if (@this == null || that == null) { return(null); } that.PersonalNumber = @this.PersonalNumber; that.Address = @this.Address; that.Email = @this.EmailAddress; that.PhoneNumber = @this.PhoneNumber; return(that); }
public async Task <ActionResult <CustomerDto> > Customer([FromBody] CustomerUpdateDto customerModel) { try { var result = await _customerService.UpdateCustomer(customerModel).ConfigureAwait(false); return(result); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public ActionResult Update(int id, CustomerUpdateDto customerUpdateDto) { var customerFromDb = _customerService.Get(id); if (customerFromDb == null) { return(NotFound()); } _mapper.Map(customerUpdateDto, customerFromDb); _customerService.Update(customerFromDb); _customerService.SaveChanges(); return(NoContent()); }
public ActionResult UpdateCustomer(int id, CustomerUpdateDto customerUpdateDto) { var customerModelFromRepo = _customer.GetCustomerById(id); if (customerModelFromRepo == null) { return(NotFound()); } _mapper.Map(customerUpdateDto, customerModelFromRepo); _customer.UpdateCustomer(customerModelFromRepo); _customer.SaveChanges(); return(NoContent()); }
public IResult EditCustomer(CustomerUpdateDto customer) { var customerInfo = new Customer() { CustomerId = customer.CustomerId, CompanyName = customer.CompanyName, FindexScore = customer.FindexScore, UserId = customer.UserId }; _customerDal.Update(customerInfo); return(new SuccessResult(Messages.Updated)); }
public async Task <IResult> UpdateWithUserAsync(CustomerUpdateDto customerUpdateDto) { var userResult = await _userService.GetByIdAsync(customerUpdateDto.Id); if (!userResult.Success) { return(new ErrorResult(userResult.Message)); } if (!HashingHelper.VerifyPasswordHash(customerUpdateDto.ActivePassword, userResult.Data.PasswordHash, userResult.Data.PasswordSalt)) { return(new ErrorResult(Messages.PasswordError)); } var customerResult = await _customerDal.GetAsync(p => p.UserId == customerUpdateDto.Id); if (customerResult == null) { return(new ErrorResult(Messages.CustomerNotFound)); } customerResult.CompanyName = customerUpdateDto.CompanyName; userResult.Data.FirstName = customerUpdateDto.FirstName; userResult.Data.LastName = customerUpdateDto.LastName; userResult.Data.Email = customerUpdateDto.Email; if (customerUpdateDto.NewPassword.Length > 5) { HashingHelper.CreatePasswordHash(customerUpdateDto.NewPassword, out byte[] passwordHash, out byte[] passwordSalt); userResult.Data.PasswordHash = passwordHash; userResult.Data.PasswordSalt = passwordSalt; } var customerUpdateResult = await _customerDal.UpdateAsync(customerResult); if (!customerUpdateResult) { return(new ErrorResult(Messages.CustomerNotUpdated)); } var userUpdateResult = await _userService.UpdateAsync(userResult.Data); if (!userUpdateResult.Success) { return(new ErrorResult(Messages.UserNotUpdated)); } return(new SuccessResult(Messages.CustomerUpdated)); }
public async Task <IActionResult> UpdateCustomer(int id, CustomerUpdateDto update) { var userOryginal = await _unitOfWork.Customers.Get(id); if (userOryginal == null) { return(NotFound()); } _unitOfWork.Customers.Update(userOryginal, update); if (await _unitOfWork.Save()) { return(NoContent()); } throw new Exception($"Updating user {id} failed"); }
public async Task Put(CustomerUpdateDto customerDto) { //return Task.Run(()=> { //}); var customer = customerRepository .Set() .FirstOrDefault(x => x.Id == customerDto.Id); mapper.Map(customerDto, customer); customerRepository.Update(customer); await unitOfWork.SaveChangesAsync(); }