Ejemplo n.º 1
0
        /// <summary>
        /// Creates an offerParameter within an offer.
        /// </summary>
        /// <param name="offerName">The name of the offer.</param>
        /// <param name="offerParameter">The offerParameter to create.</param>
        /// <returns>The created offerParameter.</returns>
        public async Task <OfferParameter> CreateAsync(string offerName, OfferParameter offerParameter)
        {
            if (offerParameter is null)
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(OfferParameter).Name),
                                                      UserErrorCode.PayloadNotProvided);
            }

            // Check that the offer does not already have an offerParameter with the same parameterName
            if (await ExistsAsync(offerName, offerParameter.ParameterName))
            {
                throw new LunaConflictUserException(LoggingUtils.ComposeAlreadyExistsErrorMessage(typeof(OfferParameter).Name,
                                                                                                  offerParameter.ParameterName,
                                                                                                  offerName: offerName));
            }

            if (ExpressionEvaluationUtils.ReservedParameterNames.Contains(offerParameter.ParameterName))
            {
                throw new LunaConflictUserException($"Parameter {offerParameter.ParameterName} is reserved. Please use a different name.");
            }
            _logger.LogInformation(LoggingUtils.ComposeCreateResourceMessage(typeof(OfferParameter).Name, offerParameter.ParameterName, offerName: offerName, payload: JsonSerializer.Serialize(offerParameter)));

            // Get the offer associated with the offerName provided
            var offer = await _offerService.GetAsync(offerName);

            // Set the FK to offer
            offerParameter.OfferId = offer.Id;

            // Add offerParameter to db
            _context.OfferParameters.Add(offerParameter);
            await _context._SaveChangesAsync();

            _logger.LogInformation(LoggingUtils.ComposeResourceCreatedMessage(typeof(OfferParameter).Name, offerParameter.ParameterName, offerName: offerName));

            return(offerParameter);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> CreateOrUpdateAsync(string offerName, string parameterName, [FromBody] OfferParameter offerParameter)
        {
            AADAuthHelper.VerifyUserAccess(this.HttpContext, _logger, true);
            if (offerParameter == null)
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(nameof(offerParameter)), UserErrorCode.PayloadNotProvided);
            }

            if (!parameterName.Equals(offerParameter.ParameterName))
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(typeof(OfferParameter).Name),
                                                      UserErrorCode.NameMismatch);
            }

            if (await _offerParameterService.ExistsAsync(offerName, parameterName))
            {
                _logger.LogInformation($"Update offer parameter {parameterName} in offer {offerName} with payload {JsonSerializer.Serialize(offerParameter)}.");
                await _offerParameterService.UpdateAsync(offerName, parameterName, offerParameter);

                return(Ok(offerParameter));
            }
            else
            {
                _logger.LogInformation($"Create offer parameter {parameterName} in offer {offerName} with payload {JsonSerializer.Serialize(offerParameter)}.");
                await _offerParameterService.CreateAsync(offerName, offerParameter);

                return(CreatedAtRoute(nameof(GetAsync) + nameof(OfferParameter), new { offerName = offerName, parameterName = offerParameter.ParameterName }, offerParameter));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Updates an offerParameter within an offer.
        /// </summary>
        /// <param name="offerName">The name of the offer.</param>
        /// <param name="parameterName">The name of the offerParameter to update.</param>
        /// <param name="offerParameter">The updated offerParameter.</param>
        /// <returns>The updated offerParameter.</returns>
        public async Task <OfferParameter> UpdateAsync(string offerName, string parameterName, OfferParameter offerParameter)
        {
            if (offerParameter is null)
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(OfferParameter).Name),
                                                      UserErrorCode.PayloadNotProvided);
            }

            // Check if (the parameterName has been updated) &&
            //          (an offerParameter with the same new parameterName does not already exist)
            if ((parameterName != offerParameter.ParameterName) && (await ExistsAsync(offerName, offerParameter.ParameterName)))
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(typeof(OfferParameter).Name),
                                                      UserErrorCode.NameMismatch);
            }

            _logger.LogInformation(LoggingUtils.ComposeUpdateResourceMessage(typeof(OfferParameter).Name, parameterName, offerName: offerName, payload: JsonSerializer.Serialize(offerParameter)));

            // Get the offerParameter that matches the parameterName provided
            var offerParameterDb = await GetAsync(offerName, parameterName);

            // Copy over the changes
            offerParameterDb.Copy(offerParameter);

            // Update offerParameterDb values and save changes in db
            _context.OfferParameters.Update(offerParameterDb);
            await _context._SaveChangesAsync();

            _logger.LogInformation(LoggingUtils.ComposeResourceUpdatedMessage(typeof(OfferParameter).Name, parameterName, offerName: offerName));

            return(offerParameterDb);
        }