public async Task <int> UpdateSiteAsync(int siteId, SiteUpdateModel updatedSite) { var currentSite = await GetSiteAsync(siteId); _context.Entry(currentSite).CurrentValues.SetValues(updatedSite); if (currentSite.SubmittedDate == null) { UpdateAddress(currentSite, updatedSite); UpdateVendors(currentSite, updatedSite); } UpdateContacts(currentSite, updatedSite); UpdateBusinessHours(currentSite, updatedSite); UpdateRemoteUsers(currentSite, updatedSite); await _businessEventService.CreateSiteEventAsync(currentSite.Id, currentSite.Provisioner.Id, "Site Updated"); try { return(await _context.SaveChangesAsync()); } catch (DbUpdateConcurrencyException) { return(0); } }
private void UpdateAddress(Site current, SiteUpdateModel updated) { if (updated?.PhysicalAddress != null) { if (current.PhysicalAddress == null) { current.PhysicalAddress = updated.PhysicalAddress; } else { _context.Entry(current.PhysicalAddress).CurrentValues.SetValues(updated.PhysicalAddress); } } }
public async Task <ISiteSettings> UpdateSite(Guid id, SiteUpdateModel patch, CancellationToken cancellationToken = default(CancellationToken)) { var site = await _siteQueries.Fetch(id, cancellationToken).ConfigureAwait(false); if (!string.IsNullOrEmpty(patch.SiteName)) { site.SiteName = patch.SiteName; } if (patch.AllowPersistentLogin.HasValue) { site.AllowPersistentLogin = patch.AllowPersistentLogin.Value; } await _siteCommands.Update(site).ConfigureAwait(false); return(site); }
public async Task <IActionResult> UpdateSite(int siteId, SiteUpdateModel updatedSite) { var site = await _siteService.GetSiteNoTrackingAsync(siteId); if (site == null) { return(NotFound(ApiResponse.Message($"Site not found with id {siteId}"))); } if (!site.Provisioner.PermissionsRecord().EditableBy(User)) { return(Forbid()); } await _siteService.UpdateSiteAsync(siteId, updatedSite); return(NoContent()); }
private void UpdateContacts(Site current, SiteUpdateModel updated) { string[] contactTypes = new string[] { nameof(current.AdministratorPharmaNet), nameof(current.PrivacyOfficer), nameof(current.TechnicalSupport) }; foreach (var contactType in contactTypes) { var contactIdName = $"{contactType}Id"; Contact currentContact = _context.Entry(current).Reference(contactType).CurrentValue as Contact; Contact updatedContact = typeof(SiteUpdateModel).GetProperty(contactType).GetValue(updated) as Contact; if (updatedContact != null) { if (updatedContact.Id != 0) { _context.Entry(current).Property(contactIdName).CurrentValue = updatedContact.Id; } else { if (currentContact == null) { _context.Entry(current).Reference(contactType).CurrentValue = updatedContact; currentContact = _context.Entry(current).Reference(contactType).CurrentValue as Contact; } else { this._context.Entry(currentContact).CurrentValues.SetValues(updatedContact); } if (updated.PhysicalAddress != null && current.PhysicalAddress != null) { this._context.Entry(current.PhysicalAddress).CurrentValues.SetValues(updated.PhysicalAddress); } else { current.PhysicalAddress = updated.PhysicalAddress; } } } } }
private void UpdateBusinessHours(Site current, SiteUpdateModel updated) { if (updated?.BusinessHours != null) { if (current.BusinessHours != null) { foreach (var businessHour in current.BusinessHours) { _context.Remove(businessHour); } } foreach (var businessHour in updated.BusinessHours) { businessHour.SiteId = current.Id; _context.Entry(businessHour).State = EntityState.Added; } } }
private void UpdateRemoteUsers(Site current, SiteUpdateModel updated) { // Wholesale replace the remote users foreach (var remoteUser in current.RemoteUsers) { foreach (var certification in remoteUser.RemoteUserCertifications) { _context.Remove(certification); } _context.RemoteUsers.Remove(remoteUser); } if (updated?.RemoteUsers != null && updated?.RemoteUsers.Count() != 0) { foreach (var remoteUser in updated.RemoteUsers) { remoteUser.SiteId = current.Id; var remoteUserCertifications = new List <RemoteUserCertification>(); foreach (var certification in remoteUser.RemoteUserCertifications) { var newCertification = new RemoteUserCertification { RemoteUser = remoteUser, CollegeCode = certification.CollegeCode, LicenseNumber = certification.LicenseNumber, LicenseCode = certification.LicenseCode }; _context.Entry(newCertification).State = EntityState.Added; remoteUserCertifications.Add(newCertification); } remoteUser.RemoteUserCertifications = remoteUserCertifications; _context.Entry(remoteUser).State = EntityState.Added; } } }
private void UpdateVendors(Site current, SiteUpdateModel updated) { if (updated?.SiteVendors != null) { if (current.SiteVendors != null) { foreach (var vendor in current.SiteVendors) { _context.Remove(vendor); } } foreach (var vendor in updated.SiteVendors) { var siteVendor = new SiteVendor { SiteId = current.Id, VendorCode = vendor.VendorCode }; _context.Entry(siteVendor).State = EntityState.Added; } } }
public async Task <OperationResult> Update(SiteUpdateModel updateModel) { try { var core = await _coreHelper.GetCore(); await using var dbContext = core.DbContextHelper.GetDbContext(); var site = await dbContext.Sites .Include(x => x.SiteTags) .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .Where(x => x.Id == updateModel.Id) .FirstOrDefaultAsync(); if (site == null) { return(new OperationResult( false, _localizationService.GetStringWithFormat("SiteParamNotFound", updateModel.Id))); } var language = await dbContext.Languages.SingleAsync(x => x.Id == (site.LanguageId == 0 ? 1 : site.LanguageId)); await core.Advanced_SiteItemUpdate((int)site.MicrotingUid, updateModel.SiteName, language.LanguageCode); // Tags var siteTagIds = site.SiteTags .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .Where(x => x.TagId != null) .Where(x => x.WorkflowState != Constants.WorkflowStates.Removed) .Select(x => (int)x.TagId) .ToList(); var forRemove = siteTagIds .Where(x => !updateModel.Tags.Contains(x)) .ToList(); foreach (var tagIdForRemove in forRemove) { var siteTag = await dbContext.SiteTags .Where(x => x.TagId == tagIdForRemove) .Where(x => x.SiteId == site.Id) .FirstOrDefaultAsync(); if (siteTag != null) { await siteTag.Delete(dbContext); } } var forCreate = updateModel.Tags .Where(x => !siteTagIds.Contains(x)) .ToList(); foreach (var tagIdForCreate in forCreate) { var siteTag = new SiteTag { TagId = tagIdForCreate, SiteId = site.Id, }; await siteTag.Create(dbContext); } return(new OperationResult(true)); } catch (Exception e) { _logger.LogError(e, e.Message); return(new OperationResult(false, _localizationService.GetStringWithFormat("SiteParamCouldNotBeUpdated", updateModel.Id))); } }
public async Task <OperationResult> Update([FromBody] SiteUpdateModel updateModel) { return(await _sitesService.Update(updateModel)); }