Beispiel #1
0
        public async Task <ActionResult> CreateOrUpdateAsync(string offerName, string meterName, [FromBody] CustomMeter customMeter)
        {
            AADAuthHelper.VerifyUserAccess(this.HttpContext, _logger, true);

            if (customMeter == null)
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(nameof(customMeter)), UserErrorCode.PayloadNotProvided);
            }

            if (!offerName.Equals(customMeter.OfferName))
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(nameof(offerName)), UserErrorCode.NameMismatch);
            }

            if (!meterName.Equals(customMeter.MeterName))
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(nameof(meterName)), UserErrorCode.NameMismatch);
            }

            if (await _customMeterService.ExistsAsync(offerName, meterName))
            {
                await _customMeterService.UpdateAsync(offerName, meterName, customMeter);

                return(Ok(customMeter));
            }
            else
            {
                await _customMeterService.CreateAsync(offerName, meterName, customMeter);

                return(CreatedAtRoute(nameof(GetAsync) + nameof(CustomMeter), new { offerName, meterName }, customMeter));
            }
        }
        public async Task <ActionResult> CreateOrUpdateAsync(string meterName, [FromBody] CustomMeter customMeter)
        {
            if (customMeter == null)
            {
                throw new ArgumentNullException(nameof(customMeter));
            }

            if (!meterName.Equals(customMeter.MeterName))
            {
                throw new ArgumentException("The meter name in url doesn't match meter name in request body.");
            }

            if (await _customMeterService.ExistsAsync(meterName))
            {
                await _customMeterService.UpdateAsync(meterName, customMeter);

                return(Ok(customMeter));
            }
            else
            {
                await _customMeterService.CreateAsync(customMeter);

                return(CreatedAtAction(nameof(GetAsync), new { meterName = customMeter.MeterName }, customMeter));
            }
        }