using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc.ViewFeatures; using Newtonsoft.Json; using Nop.Core; using Nop.Services.Logging; namespace Nop.Services.Messages; /// /// Notification service /// public partial class NotificationService : INotificationService { #region Fields protected readonly IHttpContextAccessor _httpContextAccessor; protected readonly ILogger _logger; protected readonly ITempDataDictionaryFactory _tempDataDictionaryFactory; protected readonly IWorkContext _workContext; #endregion #region Ctor public NotificationService(IHttpContextAccessor httpContextAccessor, ILogger logger, ITempDataDictionaryFactory tempDataDictionaryFactory, IWorkContext workContext) { _httpContextAccessor = httpContextAccessor; _logger = logger; _tempDataDictionaryFactory = tempDataDictionaryFactory; _workContext = workContext; } #endregion #region Utilities /// /// Save message into TempData /// /// Notification type /// Message /// A value indicating whether the message should not be encoded protected virtual void PrepareTempData(NotifyType type, string message, bool encode = true) { var context = _httpContextAccessor.HttpContext; var tempData = _tempDataDictionaryFactory.GetTempData(context); //Messages have stored in a serialized list var messages = tempData.TryGetValue(NopMessageDefaults.NotificationListKey, out var value) ? JsonConvert.DeserializeObject>(value.ToString()) : new List(); messages.Add(new NotifyData { Message = message, Type = type, Encode = encode }); tempData[NopMessageDefaults.NotificationListKey] = JsonConvert.SerializeObject(messages); } /// /// Log exception /// /// Exception /// A task that represents the asynchronous operation protected virtual async Task LogExceptionAsync(Exception exception) { if (exception == null) return; var customer = await _workContext.GetCurrentCustomerAsync(); await _logger.ErrorAsync(exception.Message, exception, customer); } #endregion #region Methods /// /// Display notification /// /// Notification type /// Message /// A value indicating whether the message should not be encoded public virtual void Notification(NotifyType type, string message, bool encode = true) { PrepareTempData(type, message, encode); } /// /// Display success notification /// /// Message /// A value indicating whether the message should not be encoded public virtual void SuccessNotification(string message, bool encode = true) { PrepareTempData(NotifyType.Success, message, encode); } /// /// Display warning notification /// /// Message /// A value indicating whether the message should not be encoded public virtual void WarningNotification(string message, bool encode = true) { PrepareTempData(NotifyType.Warning, message, encode); } /// /// Display error notification /// /// Message /// A value indicating whether the message should not be encoded public virtual void ErrorNotification(string message, bool encode = true) { PrepareTempData(NotifyType.Error, message, encode); } /// /// Display error notification /// /// Exception /// A value indicating whether exception should be logged /// A task that represents the asynchronous operation public virtual async Task ErrorNotificationAsync(Exception exception, bool logException = true) { if (exception == null) return; if (logException) await LogExceptionAsync(exception); ErrorNotification(exception.Message); } #endregion }