コード例 #1
0
 internal void LogAuditChanges(Project project)
 {
     // Record changes
     AuditProvider.LogChanges(
         ServiceContext.PortfolioContext,
         (ts, txt) => auditLogFactory(ts, txt),
         project.AuditLogs,
         DateTime.Now);
 }
        public async Task UpdateConfigAsync(string viewKey, PortfolioConfigUpdateRequest update)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(viewKey))
                {
                    viewKey = update.ViewKey;
                }

                var context = ServiceContext.PortfolioContext;

                // Map the updates to labels
                var labelUpdates = PortfolioMapper.ConfigMapper.Map <IEnumerable <PortfolioLabelConfig> >(update.Labels);

                // Get the config with labels
                var config = await context.PortfolioConfigurations
                             .IncludeFullConfiguration()
                             .Where(p => p.Portfolio.ViewKey == viewKey)
                             .SingleAsync();

                ServiceContext.AssertAdmin(config.Portfolio);

                // Update the labels
                foreach (var labelUpdate in labelUpdates)
                {
                    var label = config.Labels.Single(l => l.FieldName == labelUpdate.FieldName);
                    PortfolioMapper.UpdateMapper.Map(labelUpdate, label);
                }

                // Record changes
                AuditProvider.LogChanges(
                    context,
                    (ts, txt) => auditLogFactory(config, nameof(PortfolioLabelConfig), ts, txt),
                    context.PortfolioConfigAuditLogs,
                    DateTime.Now);

                // Map the collections here: don't do this in mapping because can't use async in resolvers
                await UpdateCollections(config);
            }
            catch (PortfolioConfigurationException pce)
            {
                var resp = new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    ReasonPhrase = pce.Message
                };
                throw new HttpResponseException(resp);
            }
            catch (DbEntityValidationException e)
            {
                var stringBuilder = new StringBuilder();
                foreach (var eve in e.EntityValidationErrors)
                {
                    var label = eve.Entry.Entity as PortfolioLabelConfig;
                    if (label != null)
                    {
                        stringBuilder.Append($"Problem with configuration for field {label.FieldTitle}: ");
                        stringBuilder.Append(string.Join("; ", eve.ValidationErrors.Select(ve => ve.ErrorMessage)));
                    }
                    else
                    {
                        stringBuilder.Append($"Contact administrator: unrecognised issue with configuration update.");
                    }
                }
                var resp = new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    ReasonPhrase = stringBuilder.ToString()
                };
                throw new HttpResponseException(resp);
            }
            catch (Exception e)
            {
                AppLog.Trace(e);
                var resp = new HttpResponseMessage(HttpStatusCode.BadRequest)
                {
                    ReasonPhrase = e.Message
                };
                throw new HttpResponseException(resp);
            }
        }