Beispiel #1
0
        public async Task <IActionResult> Post(Feature feature)
        {
            if (feature == null)
            {
                return(BadRequest(CreateProblemDetailsResponse("Feature is required")));
            }

            if (string.IsNullOrWhiteSpace(feature.Name))
            {
                return(BadRequest(CreateProblemDetailsResponse("Invalid feature name")));
            }

            Feature existingFeatureWithName = await _featureService
                                              .GetByName(feature.Name)
                                              .ConfigureAwait(false);

            if (existingFeatureWithName != null)
            {
                return(BadRequest(CreateProblemDetailsResponse("Feature with the same name already exists")));
            }

            feature.IsEnabled = true;
            feature.CreatedOn = DateTimeOffset.Now;
            Feature newFeature = await _featureService
                                 .AddAsync(feature)
                                 .ConfigureAwait(false);

            if (newFeature == null)
            {
                return(StatusCode(StatusCodes.Status500InternalServerError));
            }

            if (feature.BrowserRestrictions != null && !feature.BrowserRestrictions.Any())
            {
                return(Ok(newFeature));
            }

            IList <SupportedBrowser> supportedBrowsers = await _supportedBrowserService.GetAllAsync()
                                                         .ConfigureAwait(false);

            foreach (SupportedBrowser supportedBrowser in supportedBrowsers)
            {
                newFeature.BrowserRestrictions.ToList()
                .Add(
                    await _browserRestrictionService.AddAsync(
                        new BrowserRestriction
                {
                    // CreatedBy = ,
                    CreatedOn          = DateTimeOffset.Now,
                    FeatureId          = newFeature.Id,
                    IsActive           = false,
                    SupportedBrowserId = supportedBrowser.SupportedBrowserId
                }).ConfigureAwait(false));
            }

            return(Ok(newFeature));
        }