Beispiel #1
0
        private static string GetAssemblyDirectory(
            ILoggerProvider loggerProvider)
        {
            string toReturn = null;

            // For some strange reason, when deployed on Azure, the below
            // will be a path to a directory, not the location of the file.
            // Locally, this translates to a file.
            // Thus the craziness below.
            string assemblyLocation =
                typeof(FunctionLogicHarness).Assembly.Location;

            FileAttributes attr = File.GetAttributes(assemblyLocation);

            DirectoryInfo executionDirectory = null;

            if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
            {
                executionDirectory = new DirectoryInfo(assemblyLocation);
            }
            else
            {
                FileInfo fileInfo = new FileInfo(assemblyLocation);

                executionDirectory = fileInfo.Directory;
            }

            toReturn = executionDirectory.FullName;

            loggerProvider.Debug($"Execution location: {toReturn}.");

            return(toReturn);
        }
Beispiel #2
0
        private static HttpStatusCode PerformCreatePerson(
            ILoggerProvider loggerProvider,
            IPersonManager personManager,
            Models.CreatePersonBody.Person createPerson)
        {
            HttpStatusCode toReturn;

            loggerProvider.Debug(
                $"Invoking " +
                $"{nameof(IPersonManager)}.{nameof(IPersonManager.Create)}...");

            try
            {
                // Map to the global person class.
                Person person = new Person()
                {
                    Consent = new Consent()
                    {
                        GdprConsentDeclared = createPerson.Consent.GdprConsentDeclared.Value,
                        GdprConsentGiven    = createPerson.Consent.GdprConsentGiven,
                    },
                    ContactDetail = new ContactDetail()
                    {
                        Captured     = createPerson.ContactDetail.Captured,
                        EmailAddress = createPerson.ContactDetail.EmailAddress,
                        EmailVerificationCompletion = createPerson.ContactDetail.EmailVerificationCompletion,
                    },
                    Cookie = new MarketingDataCapture.Models.Cookie()
                    {
                        Captured         = createPerson.Cookie.Captured.Value,
                        CookieIdentifier = createPerson.Cookie.CookieIdentifier,
                    },
                    Route = new Route()
                    {
                        Captured        = createPerson.Route.Captured.Value,
                        RouteIdentifier = createPerson.Route.RouteIdentifier,
                    },
                    Enrolled  = createPerson.Enrolled,
                    FirstName = createPerson.FirstName,
                    LastName  = createPerson.LastName,
                };

                personManager.Create(person);

                loggerProvider.Info(
                    $"{nameof(IPersonManager)}.{nameof(IPersonManager.Create)} " +
                    $"invoked with success.");

                // Return Created.
                toReturn = HttpStatusCode.Created;
            }
            catch (PersonRecordExistsAlreadyException)
            {
                // Return conflicted.
                toReturn = HttpStatusCode.Conflict;
            }

            return(toReturn);
        }
Beispiel #3
0
        private static TRequestBody ParseRequestBody <TRequestBody>(
            HttpRequest httpRequest,
            ILoggerProvider loggerProvider)
            where TRequestBody : Models.ModelsBase
        {
            TRequestBody toReturn = null;

            Type requestBodyType = typeof(TRequestBody);

            loggerProvider.Debug("Reading the request body...");

            string requestBody = null;

            using (StreamReader streamReader = new StreamReader(httpRequest.Body))
            {
                requestBody = streamReader.ReadToEnd();
            }

            loggerProvider.Debug(
                $"Request body: \"{requestBody}\". Parsing body into " +
                $"{requestBodyType.Name} instance...");

            try
            {
                toReturn = JsonConvert.DeserializeObject <TRequestBody>(
                    requestBody);

                loggerProvider.Info($"Parsed: {toReturn}.");
            }
            catch (JsonReaderException jsonReaderException)
            {
                loggerProvider.Warning(
                    $"A {nameof(JsonReaderException)} was thrown when " +
                    $"deserialising the request body \"{requestBody}\". The " +
                    $"exception message: \"{jsonReaderException.Message}\".");
            }

            return(toReturn);
        }
Beispiel #4
0
        /// <summary>
        /// Validates an instance of type <typeparamref name="TRequestBody" />,
        /// according to it's attributes.
        /// </summary>
        /// <typeparam name="TRequestBody">
        /// A type deriving from <see cref="Models.ModelsBase" />.
        /// </typeparam>
        /// <param name="loggerProvider">
        /// An instance of <see cref="ILoggerProvider" />.
        /// </param>
        /// <param name="requestBody">
        /// An instance of type <typeparamref name="TRequestBody" />.
        /// </param>
        /// <returns>
        /// True if the instance passed validation, otherwise false.
        /// </returns>
        public static bool ValidateModel <TRequestBody>(
            ILoggerProvider loggerProvider,
            TRequestBody requestBody)
            where TRequestBody : Models.ModelsBase
        {
            bool toReturn = false;

            ValidationContext validationContext = new ValidationContext(
                requestBody,
                null,
                null);

            List <ValidationResult> validationResults =
                new List <ValidationResult>();

            loggerProvider.Debug($"Performing validation of {requestBody}...");

            toReturn = Validator.TryValidateObject(
                requestBody,
                validationContext,
                validationResults,
                true);

            if (toReturn)
            {
                loggerProvider.Info($"{requestBody} passed validation!");
            }
            else
            {
                string[] validationList = validationResults
                                          .Select(ValidationResultToString)
                                          .ToArray();

                string validationListConcat =
                    string.Join(", ", validationList);

                string validationFailuresDesc =
                    $"{validationResults.Count} validation error(s) were " +
                    $"highlighted. These are: {validationListConcat}";

                loggerProvider.Warning(
                    $"{requestBody} failed validation. " +
                    $"{validationFailuresDesc}.");
            }

            return(toReturn);
        }
Beispiel #5
0
        private static IPersonManager GetIPersonManagerInstance(
            ILoggerProvider loggerProvider)
        {
            IPersonManager toReturn = null;

            string path = GetAssemblyDirectory(loggerProvider);

            loggerProvider.Debug(
                $"Pulling back an instance of {nameof(IPersonManager)}...");

            Registry  registry  = new Registry(path);
            Container container = new Container(registry);

            IPersonManagerFactory personManagerFactory =
                container.GetInstance <IPersonManagerFactory>();

            toReturn = personManagerFactory.Create(loggerProvider);

            loggerProvider.Info(
                $"Instance of {nameof(IPersonManager)} pulled back.");

            return(toReturn);
        }
Beispiel #6
0
        private static HttpStatusCode PerformUpdatePerson(
            ILoggerProvider loggerProvider,
            IPersonManager personManager,
            Models.UpdatePersonBody.Person updatePerson)
        {
            HttpStatusCode toReturn;

            loggerProvider.Debug(
                $"Invoking " +
                $"{nameof(IPersonManager)}.{nameof(IPersonManager.Update)}...");

            try
            {
                // Map to the global person class.
                Person person = new Person()
                {
                    // ContactDetail is not optional...
                    ContactDetail = new ContactDetail()
                    {
                        // And neither is email address, as this is how we
                        // look up the person.
                        EmailAddress = updatePerson.ContactDetail.EmailAddress,

                        // This will only be updated, depending on
                        // UpdatePersonBody.ContactDetail.EmailVerificationCompletion.
                        EmailVerificationCompletion = updatePerson.ContactDetail.EmailVerificationCompletion,
                    },
                    FirstName = updatePerson.FirstName,
                    LastName  = updatePerson.LastName,
                };

                // Consent is optional. However, if declared, then...
                if (updatePerson.Consent != null)
                {
                    person.Consent = new Consent()
                    {
                        // It needs the GdprConsentDeclared property specified.
                        GdprConsentDeclared = updatePerson.Consent.GdprConsentDeclared.Value,

                        // Whereas valid states for this property is either
                        // true, false or null.
                        GdprConsentGiven = updatePerson.Consent.GdprConsentGiven,
                    };
                }

                // Cookie is also optional. However if declared, then...
                if (updatePerson.Cookie != null)
                {
                    person.Cookie = new MarketingDataCapture.Models.Cookie()
                    {
                        // Needs the captured date.
                        Captured = updatePerson.Cookie.Captured.Value,

                        // And the cookie identifier (doesn't make sense for
                        // it to be null).
                        CookieIdentifier = updatePerson.Cookie.CookieIdentifier,
                    };
                }

                // Route is also optional. However if declared...
                if (updatePerson.Route != null)
                {
                    person.Route = new Route()
                    {
                        // Then the captured date needs to be specified...
                        Captured = updatePerson.Route.Captured.Value,

                        // As does the RouteIdentifier.
                        RouteIdentifier = updatePerson.Route.RouteIdentifier,
                    };
                }

                personManager.Update(
                    person,
                    updatePerson.ContactDetail.UpdateEmailVerificationCompletion);

                loggerProvider.Info(
                    $"{nameof(IPersonManager)}.{nameof(IPersonManager.Update)} " +
                    $"invoked with success.");

                // Return Created.
                toReturn = HttpStatusCode.NoContent;
            }
            catch (PersonRecordDoesNotExistException)
            {
                // Return conflicted.
                toReturn = HttpStatusCode.NotFound;
            }

            return(toReturn);
        }