[Api.HttpPost] //(MatchAllBodyParameters = false)] public async static Task <HttpResponseMessage> CreateAsync( [Property(Name = AccountPropertyName)] Guid accountId, [Property(Name = AuthorizationPropertyName)] IRef <Authorization> authorizationRef, [Resource] AccountMapping accountMapping, Api.Azure.AzureApplication application, Api.SessionToken security, CreatedResponse onCreated, ForbiddenResponse onForbidden, UnauthorizedResponse onUnauthorized, ReferencedDocumentDoesNotExistsResponse <Authorization> onAuthenticationDoesNotExist, GeneralConflictResponse onFailure) { if (!await application.CanAdministerCredentialAsync(accountId, security)) { return(onUnauthorized()); } return(await await authorizationRef.StorageGetAsync( async authorization => { accountMapping.Method = authorization.Method; // method is used in the .mappingId var authorizationLookup = new AuthorizationLookup { accountMappingRef = accountMapping.mappingId, authorizationLookupRef = authorizationRef, }; return await await authorizationLookup.StorageCreateAsync( async(idDiscard) => { accountMapping.accountMappingLookup = await await authorization.ParseCredentailParameters( application, (accountKey, loginProvider) => { var lookup = new AccountMappingLookup() { accountkey = accountKey, accountMappingId = accountMapping.mappingId, Method = authorization.Method, }; return lookup.StorageCreateAsync( (discard) => new RefOptional <AccountMappingLookup>( lookup.accountMappingLookupId), () => new RefOptional <AccountMappingLookup>()); }, (why) => { var amLookupMaybe = new RefOptional <AccountMappingLookup>(); return amLookupMaybe.AsTask(); }); return await accountMapping.StorageCreateAsync( createdId => { return onCreated(); }, () => onForbidden().AddReason("Account is already mapped to that authentication.")); }, () => onFailure("Authorization is already mapped to another account.").AsTask()); }, () => onAuthenticationDoesNotExist().AsTask())); }
internal static async Task <TResult> CreateByMethodAndKeyAsync <TResult>(Authorization authorization, string externalAccountKey, Guid internalAccountId, Func <TResult> onCreated, Func <string, TResult> onFailure) { var accountMapping = new AccountMapping() { accountId = internalAccountId, }; accountMapping.Method = authorization.Method; // method is used in the .mappingId var authorizationLookup = new AuthorizationLookup { accountMappingRef = accountMapping.mappingId, authorizationLookupRef = authorization.authorizationRef, }; bool created = await authorizationLookup.StorageCreateAsync( (idDiscard) => { return(true); }, () => { // I guess this is cool... return(false); }); var lookup = new AccountMappingLookup() { accountkey = externalAccountKey, accountMappingId = accountMapping.mappingId, Method = authorization.Method, }; accountMapping.accountMappingLookup = await lookup.StorageCreateAsync( (discard) => new RefOptional <AccountMappingLookup>( lookup.accountMappingLookupId), () => new RefOptional <AccountMappingLookup>()); return(await accountMapping.StorageCreateAsync( createdId => { return onCreated(); }, () => onFailure("Account is already mapped to that authentication."))); }
internal static async Task <TResult> CreateByMethodAndKeyAsync <TResult>(Authorization authorization, string externalAccountKey, Guid internalAccountId, Func <TResult> onCreated, Func <TResult> onAlreadyMapped, bool shouldRemap = false) { var accountMapping = new AccountMapping() { accountId = internalAccountId, }; accountMapping.Method = authorization.Method; // method is used in the .mappingId accountMapping.authorization = authorization.authorizationRef; var authorizationLookup = new AuthorizationLookup { accountMappingRef = accountMapping.mappingId, authorizationLookupRef = authorization.authorizationRef, }; bool created = await authorizationLookup.StorageCreateAsync( (idDiscard) => { return(true); }, () => { // I guess this is cool... return(false); }); var lookup = new AccountMappingLookup() { accountkey = externalAccountKey, accountMappingId = accountMapping.mappingId, Method = authorization.Method, }; accountMapping.accountMappingLookup = await await lookup.StorageCreateAsync( (discard) => lookup.accountMappingLookupId.Optional().AsTask(), async() => { if (!shouldRemap) { return(RefOptional <AccountMappingLookup> .Empty()); } return(await lookup.accountMappingLookupId.StorageCreateOrUpdateAsync( async(created, lookupToUpdate, saveAsync) => { lookupToUpdate.accountMappingId = accountMapping.mappingId; await saveAsync(lookupToUpdate); return lookupToUpdate.accountMappingLookupId.Optional(); })); }); return(await await accountMapping.StorageCreateAsync( createdId => { return onCreated().AsTask(); }, async() => { if (!shouldRemap) { return onAlreadyMapped(); } return await accountMapping.mappingId.StorageCreateOrUpdateAsync( async(created, mapping, saveAsync) => { mapping.accountMappingLookup = accountMapping.accountMappingLookup; mapping.authorization = accountMapping.authorization; await saveAsync(mapping); return onCreated(); }); })); }