public bool Send(string destination, string subject, string body) { //todo: send an email _iLog.Info("Email Sent to " + destination); return(true); }
/// <summary> /// Performs 6 common actions for all function entry points: /// 1) Initialises and creates an instance of /// <see cref="IPersonManager" /> via the container, providing an /// entry point to the logic library. /// 2) Deserialises the request body into a /// <typeparamref name="TRequestBody" /> instance. /// 3) Validates the root of the deserialised /// <typeparamref name="TRequestBody" /> instance. /// 4) Validates the rest of the <typeparamref name="TRequestBody" /> /// instance via <paramref name="validateModelLogic" />. /// 5) Wraps <paramref name="serviceInvocationLogic" /> in a try catch, /// providing global error handling across all functions. In every /// instance, if an unhandled/unexpected exception is thrown, then /// a 500 will be returned, and the exception logged via the /// provided <paramref name="loggerProvider" />. /// 6) Provides the <see cref="IActionResult" />. /// </summary> /// <typeparam name="TRequestBody"> /// A type deriving from <see cref="Models.ModelsBase" />. /// </typeparam> /// <param name="httpRequest"> /// An instance of <see cref="HttpRequest" />. /// </param> /// <param name="loggerProvider"> /// An instance of type <see cref="ILoggerProvider" />. /// </param> /// <param name="validateModelLogic"> /// Additional validation logic beyond the immediate route of the /// provided instance of <typeparamref name="TRequestBody" />. /// </param> /// <param name="serviceInvocationLogic"> /// The service invocation logic, accepting an instance of type /// <see cref="IPersonManager" /> and the deserialised /// <typeparamref name="TRequestBody" /> instance. /// Returns a <see cref="HttpStatusCode" /> depending on expected /// outcomes. /// </param> /// <returns> /// An instance of <see cref="IActionResult" />. /// </returns> public static IActionResult Execute <TRequestBody>( HttpRequest httpRequest, ILoggerProvider loggerProvider, Func <ILoggerProvider, TRequestBody, bool> validateModelLogic, Func <ILoggerProvider, IPersonManager, TRequestBody, HttpStatusCode> serviceInvocationLogic) where TRequestBody : Models.ModelsBase { IActionResult toReturn = null; HttpStatusCode httpStatusCode; try { httpStatusCode = InvokePersonManager( httpRequest, loggerProvider, validateModelLogic, serviceInvocationLogic); } catch (Exception exception) { loggerProvider.Error( "An unhandled exception was thrown.", exception); httpStatusCode = HttpStatusCode.InternalServerError; } toReturn = new StatusCodeResult((int)httpStatusCode); loggerProvider.Info($"Returning {httpStatusCode}."); return(toReturn); }
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); }
public ActionResult Index() { //string name = _iTest.SayHello("Sourav"); //return View(model: name); Course c = new Course(); c = _iCourse.Get(1); _iLog.Info("Log from Index"); _iNotification.Send("*****@*****.**", "Test", "Body message"); return(View(c)); }
/// <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); }
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); }
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); }
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); }