/// <summary> /// Creates an aadSecretTmp object within an offer. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <param name="aadSecretTmp">The aadSecretTmp object to create.</param> /// <returns>The created aadSecretTmp object.</returns> public async Task <AadSecretTmp> CreateAsync(string offerName, AadSecretTmp aadSecretTmp) { if (aadSecretTmp is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(AadSecretTmp).Name), UserErrorCode.PayloadNotProvided); } // Check that the offer does not already have an aadSecretTmp with the same name if (await ExistsAsync(offerName, aadSecretTmp.Name)) { throw new LunaConflictUserException(LoggingUtils.ComposeAlreadyExistsErrorMessage(typeof(AadSecretTmp).Name, aadSecretTmp.Name, offerName: offerName)); } _logger.LogInformation(LoggingUtils.ComposeCreateResourceMessage(typeof(AadSecretTmp).Name, aadSecretTmp.Name, offerName: offerName, payload: JsonSerializer.Serialize(aadSecretTmp))); // Get the offer associated with the offerName provided var offer = await _offerService.GetAsync(offerName); // Set the FK to offer aadSecretTmp.OfferId = offer.Id; // Add aadSecretTmp to db _context.AadSecretTmps.Add(aadSecretTmp); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceCreatedMessage(typeof(AadSecretTmp).Name, aadSecretTmp.Name, offerName: offerName)); return(aadSecretTmp); }
/// <summary> /// Updates an aadSecretTmp object within an offer. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <param name="name">The name of the aadSecretTmp object to update.</param> /// <param name="aadSecretTmp">The updated aadSecretTmp object.</param> /// <returns>The updated aadSecretTmp object.</returns> public async Task <AadSecretTmp> UpdateAsync(string offerName, string name, AadSecretTmp aadSecretTmp) { if (aadSecretTmp is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(AadSecretTmp).Name), UserErrorCode.PayloadNotProvided); } // Check if (the name has been updated) && // (an aadSecretTmp with the same new name does not already exist) if ((name != aadSecretTmp.Name) && (await ExistsAsync(offerName, aadSecretTmp.Name))) { throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(typeof(AadSecretTmp).Name), UserErrorCode.NameMismatch); } _logger.LogInformation(LoggingUtils.ComposeUpdateResourceMessage(typeof(AadSecretTmp).Name, name, offerName: offerName, payload: JsonSerializer.Serialize(aadSecretTmp))); // Get the aadSecretTmp that matches the name provided var aadSecretTmpDb = await GetAsync(offerName, name); // Copy over the changes aadSecretTmpDb.Copy(aadSecretTmp); // Update aadSecretTmpDb values and save changes in db _context.AadSecretTmps.Update(aadSecretTmpDb); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceUpdatedMessage(typeof(AadSecretTmp).Name, name, offerName: offerName)); return(aadSecretTmpDb); }