Exemplo n.º 1
0
        private async Task <DataWithVersion <SettingsForClient> > GetForClientImpl()
        {
            M.Settings mSettings = await _db.Settings.FirstOrDefaultAsync();

            if (mSettings == null)
            {
                // This should never happen
                throw new BadRequestException("Settings have not been initialized");
            }

            // Prepare the settings for client
            var settings = _mapper.Map <SettingsForClient>(mSettings);

            settings.PrimaryLanguageName   = _culturesRepo.GetCulture(settings.PrimaryLanguageId)?.Name;
            settings.SecondaryLanguageName = _culturesRepo.GetCulture(settings.SecondaryLanguageId)?.Name;

            // Tag the settings for client with their current version
            var result = new DataWithVersion <SettingsForClient>
            {
                Version = mSettings.SettingsVersion.ToString(),
                Data    = settings
            };

            return(result);
        }
Exemplo n.º 2
0
        // Helper methods

        private async Task <GetByIdResponse <Settings> > GetImpl(GetByIdArguments args)
        {
            M.Settings mSettings = await _db.Settings.FirstOrDefaultAsync();

            if (mSettings == null)
            {
                // This should never happen
                throw new BadRequestException("Settings have not been initialized");
            }

            var settings = _mapper.Map <Settings>(mSettings);
            var result   = new GetByIdResponse <Settings>
            {
                CollectionName = "Settings",
                Entity         = settings,
            };

            if (!string.IsNullOrWhiteSpace(args.Expand))
            {
                Expand(args.Expand, settings, result);
            }

            return(result);
        }
Exemplo n.º 3
0
        public async Task <ActionResult <SaveSettingsResponse> > Save([FromBody] SettingsForSave settingsForSave, [FromQuery] SaveArguments args)
        {
            // Authorized access (Criteria are not supported here)
            var updatePermissions = await ControllerUtilities.GetPermissions(_db.AbstractPermissions, PermissionLevel.Update, "settings");

            if (!updatePermissions.Any())
            {
                return(StatusCode(403));
            }

            try
            {
                // Trim all string fields just in case
                settingsForSave.TrimStringProperties();

                // Validate
                ValidateAndPreprocessSettings(settingsForSave);

                if (!ModelState.IsValid)
                {
                    return(UnprocessableEntity(ModelState));
                }

                // Persist
                M.Settings mSettings = await _db.Settings.FirstOrDefaultAsync();

                if (mSettings == null)
                {
                    // This should never happen
                    return(BadRequest("Settings have not been initialized"));
                }

                _mapper.Map(settingsForSave, mSettings);

                mSettings.ModifiedAt      = DateTimeOffset.Now;
                mSettings.ModifiedById    = _tenantInfo.GetCurrentInfo().UserId.Value;
                mSettings.SettingsVersion = Guid.NewGuid(); // prompts clients to refresh

                await _db.SaveChangesAsync();

                // If requested, return the updated entity
                if (args.ReturnEntities ?? false)
                {
                    // If requested, return the same response you would get from a GET
                    var res = await GetImpl(new GetByIdArguments { Expand = args.Expand });

                    var result = new SaveSettingsResponse
                    {
                        CollectionName    = res.CollectionName,
                        Entity            = res.Entity,
                        RelatedEntities   = res.RelatedEntities,
                        SettingsForClient = await GetForClientImpl()
                    };

                    return(result);
                }
                else
                {
                    return(Ok());
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error: {ex.Message} {ex.StackTrace}");
                return(BadRequest(ex.Message));
            }
        }