public async Task <ContactDto> UpdateContactAsync(EditContactDto dto) { if (!await ValidateLocationInCharges(dto.LocationInCharges)) { throw new BusinessException("Giá trị không hợp lệ."); } var contactToUpdate = await _unitOfWork.GetRepository <ARContact>() .GetAllIncluding(x => x.ARContactForestCommuneGroups) .FirstOrDefaultAsync(x => x.Id == dto.Id); if (contactToUpdate == null) { throw new BusinessException("Không tìm thấy liên hệ."); } UpdateContactDto(contactToUpdate, dto); await _unitOfWork.GetRepository <ARContact>().UpdateAsync(contactToUpdate); foreach (var contactForestCommuneGroup in contactToUpdate.ARContactForestCommuneGroups) { if (dto.LocationInCharges.All(x => x.ForestCommuneID != contactForestCommuneGroup.FK_GECommuneID)) { await _unitOfWork.GetRepository <ARContactForestCommuneGroup>().DeleteAsync(contactForestCommuneGroup); } } foreach (var locationInCharge in dto.LocationInCharges) { if (contactToUpdate.ARContactForestCommuneGroups.All(x => x.FK_GECommuneID != locationInCharge.ForestCommuneID)) { await _unitOfWork.GetRepository <ARContactForestCommuneGroup>() .InsertAsync( new ARContactForestCommuneGroup { FK_ARContactID = dto.Id, FK_GECommuneID = locationInCharge.ForestCommuneID, FK_GEDistrictID = locationInCharge.ForestDistrictID, FK_GEStateProvinceID = locationInCharge.ForestStateProvinceID }); } } await _unitOfWork.CompleteAsync(); return(await GetContactAsync(contactToUpdate.Id)); }
private static void UpdateContactDto(ARContact entity, EditContactDto dto) { entity.ARContactContributor = dto.Contributor; entity.ARContactTitleContribute = dto.TitleContribute; entity.FK_ARContactTypeID = dto.ContactTypeID; entity.ARContactName = dto.ContactName; entity.ARContactAcronymName = dto.AcronymName; entity.ARContactUserContact = dto.UserContact; entity.ARContactPhone1 = dto.Phone1; entity.ARContactPhone2 = dto.Phone2; entity.ARContactEmail = dto.Email; entity.ARContactWebsite = dto.Website; entity.ARContactNote = dto.Note; entity.ARContactImage = dto.Images.JoinNotEmpty(";"); entity.FK_GEStateProvinceID = dto.StateProvinceID; entity.FK_GEDistrictID = dto.DistrictID; entity.FK_GECommuneID = dto.CommuneID; entity.ARUserHouseNumber = dto.HouseNumber; entity.ARUserAddress = dto.Address; }
public async Task <IActionResult> Create([FromBody] EditContactDto dto) { var result = await _contactService.UpdateContactAsync(dto); return(Success(result)); }