public void AbortTransactionWorksAsExpected() { // --- Arrange var data = new DataRecord { Name = "Data" }; // --- Act using (var dc = DataAccessFactory.CreateContext <ITestDataOperations>()) { dc.BeginTransaction(); dc.InsertData(data); dc.AbortTransaction(); } // --- Assert DataRecord back; using (var dc = DataAccessFactory.CreateReadOnlyContext <ITestDataOperations>()) { back = dc.GetData(data.Id); } back.ShouldBeNull(); }
public async Task BeginTransactionWorksAsExpected() { // --- Arrange var data = new DataRecord { Name = "Data" }; // --- Act using (var dc = DataAccessFactory.CreateContext <ITestDataOperations>()) { await dc.BeginTransactionAsync(); await dc.InsertDataAsync(data); await dc.CompleteAsync(); } // --- Assert DataRecord back; using (var dc = DataAccessFactory.CreateReadOnlyContext <ITestDataOperations>()) { back = await dc.GetDataAsync(data.Id); } back.ShouldNotBeNull(); }
/// <summary> /// Gets user information by the email address provided /// </summary> /// <param name="userEmail">Email of the user (used as unique ID)</param> /// <returns>User token</returns> public async Task <UserTokenDto> GetUserTokenByEmailAsync(string userEmail) { Verify.NotNullOrEmpty(userEmail, "userEmail"); Verify.RaiseWhenFailed(); using (var ctx = DataAccessFactory.CreateReadOnlyContext <ISubscriptionDataOperations>()) { var userRecord = await ctx.GetUserByEmailAsync(userEmail); if (userRecord == null) { throw new UnknownEmailException(userEmail); } var owner = await ctx.GetSubscriptionOwnerByIdAsync(userRecord.SubscriptionId ?? -1, userRecord.Id); // TODO: collect role information return(new UserTokenDto { UserId = userRecord.Id, UserName = userRecord.UserName, SubscriptionId = userRecord.SubscriptionId, IsSubscriptionOwner = owner != null, ServiceRoles = new List <ServiceRolesDto>() }); } }
/// <summary> /// Gets the items of the specified list. /// </summary> /// <param name="listId">List identifier</param> /// <param name="locale">Locale code of list items</param> /// <returns>Items belonging to the specified list</returns> public List <ListItemDefinitionDto> GetListItems(string listId, string locale = "def") { Verify.NotNullOrEmpty(listId, "listId"); Verify.NotNullOrEmpty(locale, "locale"); Verify.RaiseWhenFailed(); using (var ctx = DataAccessFactory.CreateReadOnlyContext <IConfigurationDataOperations>()) { // --- Van ilyen lista? var listInfo = ctx.GetListDefinitionById(listId); if (listInfo == null) { throw new ListDefinitionNotFoundException(listId); } return(ctx.GetListItems(listId) .Select(it => new ListItemDefinitionDto { Id = it.ItemId, IsSystemItem = it.IsSystemItem, DisplayName = GetListItemName(locale, listId, it.ItemId), Description = GetListItemDescription(locale, listId, it.ItemId) }) .ToList()); } }
/// <summary> /// Gets user information by its provider data /// </summary> /// <param name="provider">Provider ID</param> /// <param name="providerData">Provider data</param> /// <returns>User token</returns> public async Task <UserTokenDto> GetUserTokenByProviderDataAsync(string provider, string providerData) { Verify.NotNullOrEmpty(provider, "provider"); Verify.NotNullOrEmpty(providerData, "providerData"); Verify.RaiseWhenFailed(); var user = await GetUserByProviderDataAsync(provider, providerData); if (user == null) { throw new UnknownProviderDataException(provider, providerData); } using (var ctx = DataAccessFactory.CreateReadOnlyContext <ISubscriptionDataOperations>()) { var owner = await ctx.GetSubscriptionOwnerByIdAsync(user.SubscriptionId ?? -1, user.Id); // TODO: collect role information return(new UserTokenDto { UserId = user.Id, UserName = user.UserName, SubscriptionId = user.SubscriptionId, IsSubscriptionOwner = owner != null, ServiceRoles = new List <ServiceRolesDto>() }); } }
/// <summary> /// Gets the login account belonging to a user /// </summary> /// <param name="userId">User ID</param> /// <returns>Login accounts</returns> public async Task <List <UserAccountDto> > GetUserAccountsByUserIdAsync(Guid userId) { using (var ctx = DataAccessFactory.CreateReadOnlyContext <ISubscriptionDataOperations>()) { return((await ctx.GetUserAccountsByUserIdAsync(userId)) .Select(MapUserAccount).ToList()); } }
/// <summary> /// Gets the current configuration version /// </summary> /// <returns>Configuration version key</returns> public int?GetCurrentConfigurationVersion() { using (var ctx = DataAccessFactory.CreateReadOnlyContext <IConfigurationDataOperations>()) { var currentVersion = ctx.GetCurrentConfigurationVersion(); return(currentVersion == null ? (int?)null : currentVersion.CurrentVersion); } }
/// <summary> /// Gets all resource categories /// </summary> /// <returns>List of resource categories</returns> public List <string> GetLocalizedResourceCategories() { using (var ctx = DataAccessFactory.CreateReadOnlyContext <IConfigurationDataOperations>()) { return(ctx.FetchAllCategories() .Select(r => r.Category).ToList()); } }
/// <summary> /// Gets all dives of the current user /// </summary> /// <returns>All dives of the user</returns> public async Task <List <DiveLogEntryDto> > GetAllDivesOfUserAsync() { using (var ctx = DataAccessFactory.CreateReadOnlyContext <IDiveLogDataAccessOperations>()) { return((await ctx.GetAllDiveLogForUserIdAsync(ServicedUserId)) .Select(MapToDiveLogEntryDto) .ToList()); } }
/// <summary> /// Checks the uniqueness of the locale name /// </summary> /// <param name="name">Name of the locale</param> /// <returns>True, if the name is unique; otherwise, false</returns> public bool IsLocaleDisplayNameUnique(string name) { Verify.NotNullOrEmpty(name, "name"); Verify.RaiseWhenFailed(); using (var ctx = DataAccessFactory.CreateReadOnlyContext <IConfigurationDataOperations>()) { return(ctx.GetByName(name) == null); } }
/// <summary> /// Reads the coinfiguration values from the database /// </summary> /// <returns> /// The full configuration value set /// </returns> private static Dictionary <string, List <Tuple <string, string> > > PopulateValues() { using (var ctx = DataAccessFactory.CreateReadOnlyContext <IConfigurationDataOperations>()) { var configRecords = ctx.GetAllConfigurationValues(); return(configRecords .GroupBy(r => r.Category) .ToDictionary(g => g.Key, g => g.Select(r => new Tuple <string, string>(r.ConfigKey, r.Value)).ToList())); } }
/// <summary> /// Gets the user with the email /// </summary> /// <param name="email">User email</param> /// <returns>The user information, if found; otherwise, null</returns> public async Task <UserInfoDto> GetUserByEmailAsync(string email) { using (var ctx = DataAccessFactory.CreateReadOnlyContext <ISecurityDataOperations>()) { var user = await ctx.GetUserByEmailAsync(email); return(user == null ? await Task.FromResult <UserInfoDto>(null) : MapUser(user)); } }
/// <summary> /// Gets the user with the specified ID /// </summary> /// <param name="userId">User ID</param> /// <returns>The user information, if found; otherwise, null</returns> public async Task <UserDto> GetUserByIdAsync(Guid userId) { using (var ctx = DataAccessFactory.CreateReadOnlyContext <ISubscriptionDataOperations>()) { var user = await ctx.GetUserByIdAsync(userId); return(user == null ? await Task.FromResult <UserDto>(null) : MapUser(user)); } }
/// <summary> /// Gets the users invited to join to the subscription of the current user. /// </summary> /// <returns>List of user invitations</returns> public async Task <List <UserInvitationCoreDto> > GetInvitedUsersAsync() { if (!Principal.SubscriptionId.HasValue) { return(new List <UserInvitationCoreDto>()); } using (var ctx = DataAccessFactory.CreateReadOnlyContext <ISubscriptionDataOperations>()) { return((await ctx.GetUserInvitationBySubscriptionAsync(Principal.SubscriptionId)) .Select(MapUserInvitation).ToList()); } }
/// <summary> /// Gets all locales /// </summary> /// <returns>List of locales</returns> public IList <LocaleDto> GetAllLocales() { using (var ctx = DataAccessFactory.CreateReadOnlyContext <IConfigurationDataOperations>()) { return(ctx.FetchAllLocales() .Select(r => new LocaleDto { Code = r.Code, DisplayName = r.DisplayName }).ToList()); } }
public void CreateReadOnlyContextWorksAsExpected() { // --- Arrange var dc = new SqlDatabase(DB_CONN); dc.Execute("delete from [dbo].[Sample]"); // --- Act using (var ctx = DataAccessFactory.CreateReadOnlyContext<ITestDataOperations>()) { ctx.InsertSample(new SampleRecord { Id = 0, Name = "Haho" }); } }
/// <summary> /// Gets a specific dive record /// </summary> /// <param name="diveId">Dive log entry ID</param> /// <returns>Dive log entry, if exists; otherwise, null</returns> public async Task <DiveLogEntryDto> GetDiveByIdAsync(int diveId) { using (var ctx = DataAccessFactory.CreateReadOnlyContext <IDiveLogDataAccessOperations>()) { var dive = await ctx.GetDiveLogByIdAsync(diveId); if (dive == null) { throw new DiveNotFoundException(diveId); } return(MapToDiveLogEntryDto(dive)); } }
/// <summary> /// Gets the login account that belong to the specified user /// </summary> /// <param name="userId">User ID</param> /// <returns>List of users</returns> public async Task <List <UserAccountDto> > GetUserLogins(string userId) { using (var ctx = DataAccessFactory.CreateReadOnlyContext <ISecurityDataOperations>()) { return((await ctx.GetUserAccounts(userId)) .Select(u => new UserAccountDto { UserId = userId, Provider = u.Provider, ProviderData = u.ProviderData }).ToList()); } }
/// <summary> /// Gets the user with the email /// </summary> /// <param name="email">User email</param> /// <returns>The user information, if found; otherwise, null</returns> public async Task <UserDto> GetUserByEmailAsync(string email) { Verify.NotNullOrEmpty(email, "email"); Verify.RaiseWhenFailed(); using (var ctx = DataAccessFactory.CreateReadOnlyContext <ISubscriptionDataOperations>()) { var user = await ctx.GetUserByEmailAsync(email); return(user == null ? await Task.FromResult <UserDto>(null) : MapUser(user)); } }
/// <summary> /// Gets the user with the specified name /// </summary> /// <param name="subscriptionId">Subscription ID</param> /// <param name="userName">User name</param> /// <returns>The user information, if found; otherwise, null</returns> public async Task <UserDto> GetUserByNameAsync(int?subscriptionId, string userName) { Verify.NotNullOrEmpty(userName, "userName"); Verify.RaiseWhenFailed(); using (var ctx = DataAccessFactory.CreateReadOnlyContext <ISubscriptionDataOperations>()) { var user = await ctx.GetUserByUserNameAsync(subscriptionId, userName); return(user == null ? await Task.FromResult <UserDto>(null) : MapUser(user)); } }
/// <summary> /// Sets up the task by loading email template definitions /// </summary> /// <param name="context">Execution context</param> public override void Setup(ITaskExecutionContext context) { lock (s_Locker) { if (TemplateDefinitions == null) { using (var ctx = DataAccessFactory.CreateReadOnlyContext <IEmailDataOperations>()) { TemplateDefinitions = ctx.GetAllTemplateAsync().Result; } } } base.Setup(context); }
/// <summary> /// Gets the user information by its provider data /// </summary> /// <param name="provider">Provider ID</param> /// <param name="providerData">Provider data</param> /// <returns>The user information, if found; otherwise, null</returns> public async Task <UserDto> GetUserByProviderDataAsync(string provider, string providerData) { using (var ctx = DataAccessFactory.CreateReadOnlyContext <ISubscriptionDataOperations>()) { var account = await ctx.GetUserAccountByProviderAsync(provider, providerData); if (account == null) { return(null); } var user = await ctx.GetUserByIdAsync(account.UserId); return(user == null ? null : MapUser(user)); } }
/// <summary> /// Gets the resources in the specified category /// </summary> /// <param name="locale">Code of locale</param> /// <param name="category">Resource category</param> /// <returns> /// Resources in the specified category /// </returns> public List <LocalizedResourceDto> GetLocalizedResourcesByCategory(string locale, string category) { Verify.NotNull(locale, "locale"); Verify.NotNull(category, "category"); Verify.RaiseWhenFailed(); using (var ctx = DataAccessFactory.CreateReadOnlyContext <IConfigurationDataOperations>()) { var records = ctx.GetLocalizedResourcesByCategory(locale, category); return(records.Select(r => new LocalizedResourceDto { Locale = r.Locale, Category = r.Category, ResourceKey = r.ResourceKey, Value = r.Value }).ToList()); } }
/// <summary> /// Gets user information by the user ID provided /// </summary> /// <param name="userId">User ID within the system</param> /// <returns>User token</returns> public async Task <UserTokenDto> GetUserTokenByIdAsync(Guid userId) { using (var ctx = DataAccessFactory.CreateReadOnlyContext <ISubscriptionDataOperations>()) { var userRecord = await ctx.GetUserByIdAsync(userId); if (userRecord == null) { throw new UnknownUserIdException(userId.ToString("N")); } var owner = await ctx.GetSubscriptionOwnerByIdAsync(userRecord.SubscriptionId ?? -1, userRecord.Id); // TODO: collect role information return(new UserTokenDto { UserId = userRecord.Id, UserName = userRecord.UserName, SubscriptionId = userRecord.SubscriptionId, IsSubscriptionOwner = owner != null, ServiceRoles = new List <ServiceRolesDto>() }); } }
/// <summary> /// Runs the specific task. /// </summary> public override void Run() { // --- Get the emails waiting to be sent List <EmailToSendRecord> emailsToSend; using (var ctx = DataAccessFactory.CreateReadOnlyContext <IEmailDataOperations>()) { var emailCount = SmtpConfig.EmailCount; if (emailCount <= 1) { emailCount = 1; } emailsToSend = ctx.GetEmailsToSendAsync(DateTimeOffset.UtcNow).Result.Take(emailCount).ToList(); } // --- Process each email to send foreach (var emailToSend in emailsToSend) { // --- Assemble email body var templateId = emailToSend.TemplateType; var template = TemplateDefinitions.FirstOrDefault(t => t.Id == templateId); if (template == null) { // --- Do not send emails without a template emailToSend.RetryCount = SmtpConfig.MaxRetry; ProcessEmail(emailToSend, null, null, null, string.Format("Email template '{0}' not found.", templateId)); continue; } // --- Set the email body var subject = template.Subject; var message = template.Body; try { var parameters = GetEmailParametersDictionary(emailToSend.Parameters); foreach (var key in parameters.Keys) { var placeholder = "{{" + key + "}}"; message = message.Replace(placeholder, parameters[key]); subject = subject.Replace(placeholder, parameters[key]); } } catch (Exception ex) { // --- Email body assembly failed emailToSend.RetryCount = SmtpConfig.MaxRetry; ProcessEmail(emailToSend, null, null, null, string.Format("Email message construction failed. {0}", ex)); continue; } // --- Send out the email try { AppointmentDefinitionDto appointmentInfo = null; if (emailToSend.Appointment != null) { appointmentInfo = JsonConvert.DeserializeObject <AppointmentDefinitionDto>(emailToSend.Appointment); } var senderService = ServiceManager.GetService <IEmailSender>(); senderService.SendEmail( emailToSend.FromAddr, emailToSend.FromName, new[] { emailToSend.ToAddr }, subject, message, appointmentInfo); ProcessEmail(emailToSend, subject, message, emailToSend.Appointment, null); } catch (Exception ex) { ProcessEmail(emailToSend, null, null, null, ex.ToString()); } } try { Thread.Sleep(SmtpConfig.SendInterval); } catch (SystemException) { // --- This exception is caught intentionally } }