using FluentValidation.Results; using Raven.Client.Documents; using System; using System.Threading; using System.Threading.Tasks; using zero.Core.Entities; using zero.Core.Validation; using zero.Core.Extensions; namespace zero.Core.Identity { public class BackofficeUserStore where TUser : IUser, new() where TRole : IUserRole, new() { /// /// Gets the database context for this store. /// protected IDocumentStore Raven { get; set; } /// /// Configuration /// protected IZeroConfiguration Config { get; private set; } /// /// Currently logged-in user /// protected IUser Current { get; private set; } public BackofficeUserStore(IDocumentStore raven, IZeroConfiguration config, IUser currentUser) { Raven = raven; Config = config; } /// /// Adds a new backoffice user /// public async Task> Add(TUser model, CancellationToken token = default) { // set default language if (model.LanguageId.IsNullOrEmpty()) { model.LanguageId = Config.DefaultLanguage; } // validate model ValidationResult validation = await new BackofficeUserValidator(isCreate: true).ValidateAsync(model); if (!validation.IsValid) { return EntityChangeResult.Fail(validation); } return EntityChangeResult.Success(); } } }