public void HandleCreate(CreateUserCommand command, IAppUnitOfWorkFactory uowFactory)
        {
            using (var uow = uowFactory.Create())
            {
                var email = command.Email.ToLowerInvariant().Trim();

                if (uow.Users.Any(x => x.Email == email))
                {
                    throw new DomainException("The user with the same email already exists");
                }

                var user = new User()
                {
                    Email          = email,
                    PasswordHashed = Tools.Common.Utils.SecurityUtils.Hash(command.Password,
                                                                           Tools.Common.Utils.SecurityUtils.HashMethod.Sha256),
                    FirstName = command.FirstName.Trim(),
                    LastName  = command.LastName.Trim(),
                    Phone     = command.Phone,
                    Role      = UserRole.Regular,
                };
                uow.UserRepository.Add(user);
                uow.SaveChanges();
                command.UserId = user.Id;
            }
        }
Esempio n. 2
0
 public ApiTestController(
     IMessagePipelineService pipelineService,
     ILoggerFactory loggerFactory,
     IAppUnitOfWorkFactory appUnitOfWorkFactory) : base(pipelineService, loggerFactory)
 {
     this.appUnitOfWorkFactory = appUnitOfWorkFactory;
 }
        public void HandleUpdate(UpdateUserCommand command, IAppUnitOfWorkFactory uowFactory)
        {
            using (var uow = uowFactory.Create())
            {
                var email = command.Email.ToLowerInvariant().Trim();

                if (uow.Users.Any(x => x.Email == email && x.Id != command.UserId))
                {
                    throw new DomainException("The user with the same email already exists");
                }

                var dbUser = uow.UserRepository.Get(command.UserId);
                dbUser.Email     = email;
                dbUser.FirstName = command.FirstName;
                dbUser.LastName  = command.LastName;
                dbUser.Phone     = command.Phone;
                dbUser.IsActive  = command.IsActive;
                dbUser.UpdatedAt = DateTime.Now;

                if (string.IsNullOrEmpty(command.Password) == false)
                {
                    dbUser.PasswordHashed = Tools.Common.Utils.SecurityUtils.Hash(command.Password,
                                                                                  Tools.Common.Utils.SecurityUtils.HashMethod.Sha256);
                }
                uow.SaveChanges();
            }
        }
 public void HandleCreate(CreateProductCommand command, IAppUnitOfWorkFactory uowFactory)
 {
     using (IAppUnitOfWork uow = uowFactory.Create())
     {
         Company company = uow.CompanyRepository.Get(command.CompanyId);
         if (company == null)
         {
             throw new DomainException("The assigned company not found");
         }
         User creator = uow.Users.FirstOrDefault(u => u.Id == command.CreatedByUserId);
         if (creator == null)
         {
             throw new DomainException("Can not find creator");
         }
         var product = new Product
         {
             Company    = company,
             Name       = command.Name,
             Comment    = command.Comment,
             Quantity   = command.Quantity,
             Sku        = command.Sku,
             Properties = new List <ProductProperty>(command.Properties),
             IsActive   = command.IsActive,
             CreatedAt  = DateTime.Now,
             CreatedBy  = creator
         };
         uow.ProductRepository.Add(product);
         uow.SaveChanges();
         command.ProductId = product.Id;
     }
 }
Esempio n. 5
0
        /// <summary>
        /// Handles task create.
        /// </summary>
        /// <param name="command">Command.</param>
        /// <param name="uowFactory">Application unit of work factory.</param>
        public void HandleCreateTask(CreateTaskCommand command, IAppUnitOfWorkFactory uowFactory)
        {
            using (var uow = uowFactory.Create())
            {
                var project = uow.ProjectRepository.Get(command.ProjectId);
                if (project != null && project.User.Id != command.UserId)
                {
                    throw new DomainException("User does not own the project.");
                }

                var user = uow.UserRepository.Get(command.UserId);

                var task = new Entities.Task()
                {
                    DueDate = DateTime.MaxValue,
                    IsDone  = command.IsDone,
                    Text    = command.Text.Trim(),
                    Project = project,
                    User    = user,
                };
                uow.TaskRepository.Add(task);
                uow.SaveChanges();
                command.TaskId = task.Id;
            }
        }
 /// <summary>
 /// Handle delete command.
 /// </summary>
 public void HandleDelete(DeleteCompanyCommand command, IAppUnitOfWorkFactory uowFactory)
 {
     using (IAppUnitOfWork uow = uowFactory.Create())
     {
         Company company = uow.CompanyRepository.Get(command.CompanyId);
         uow.CompanyRepository.Remove(company);
         uow.SaveChanges();
     }
 }
 public UserHandlers(
     ILoggerFactory loggerFactory,
     IAppUnitOfWorkFactory uowFactory,
     AppUserManager userManager)
 {
     this.logger      = loggerFactory.CreateLogger <UserHandlers>();
     this.uowFactory  = uowFactory;
     this.userManager = userManager;
 }
Esempio n. 8
0
 public UpdateDailyReportCommand(
     CrmService crmService,
     ConversationService conversationService,
     IAppUnitOfWorkFactory uowFactory,
     TelemetryClient telemetry)
 {
     this.crmService          = crmService;
     this.conversationService = conversationService;
     this.uowFactory          = uowFactory;
     this.telemetry           = telemetry;
 }
 /// <summary>
 /// Handle update command.
 /// </summary>
 public void HandleUpdate(UpdateCompanyCommand command, IAppUnitOfWorkFactory uowFactory)
 {
     using (IAppUnitOfWork uow = uowFactory.Create())
     {
         if (uow.Companies.Any(c => c.Name == command.Name.Trim() && c.Id != command.CompanyId))
         {
             throw new DomainException("The company with the same name already exists.");
         }
         Company company = uow.CompanyRepository.Get(command.CompanyId);
         company.Name = command.Name;
         uow.SaveChanges();
     }
 }
Esempio n. 10
0
        /// <summary>
        /// Handles task check/uncheck (done/not done).
        /// </summary>
        /// <param name="command">Command.</param>
        /// <param name="uowFactory">Application unit of work factory.</param>
        public void HandleCheckTask(CheckTaskCommand command, IAppUnitOfWorkFactory uowFactory)
        {
            using (var uow = uowFactory.Create())
            {
                var dbtask = uow.TaskRepository.Get(command.TaskId);
                if (dbtask.User.Id != command.UserId)
                {
                    throw new DomainException("You cannot check/uncheck task for another user.");
                }

                dbtask.IsDone = command.IsDone;
                uow.SaveChanges();
            }
        }
Esempio n. 11
0
        /// <summary>
        /// Handles task remove.
        /// </summary>
        /// <param name="command">Command.</param>
        /// <param name="uowFactory">Application unit of work.</param>
        public void HandleRemoveTask(RemoveTaskCommand command, IAppUnitOfWorkFactory uowFactory)
        {
            using (var uow = uowFactory.Create())
            {
                var dbtask = uow.TaskRepository.Get(command.TaskId);
                if (dbtask.User.Id != command.UserId)
                {
                    throw new DomainException("You cannot remove task for another user.");
                }

                uow.TaskRepository.Remove(dbtask);
                uow.SaveChanges();
            }
        }
 public void HandleDelete(DeleteProductCommand command, IAppUnitOfWorkFactory uowFactory)
 {
     using (IAppUnitOfWork uow = uowFactory.Create())
     {
         Product product = uow.ProductRepository.Get(command.ProductId, Product.IncludeAll);
         if (product == null)
         {
             throw new NotFoundException("Deleted product not found");
         }
         // Delete properties before
         uow.ProductPropertyRepository.RemoveRange(product.Properties);
         uow.ProductRepository.Remove(product);
         uow.SaveChanges();
     }
 }
Esempio n. 13
0
 /// <summary>
 /// Create project handler.
 /// </summary>
 /// <param name="command">Command.</param>
 /// <param name="uowFactory">Application unit of work factory.</param>
 public void HandleCreateProject(CreateProjectCommand command, IAppUnitOfWorkFactory uowFactory)
 {
     using (var uow = uowFactory.Create())
     {
         var project = new Entities.Project()
         {
             Name  = command.Name,
             Color = command.Color,
             User  = uow.UserRepository.Get(command.CreatedByUserId),
         };
         uow.ProjectRepository.Add(project);
         uow.SaveChanges();
         command.ProjectId = project.Id;
     }
 }
Esempio n. 14
0
        /// <summary>
        /// Remove project handler.
        /// </summary>
        /// <param name="command">Command.</param>
        /// <param name="uowFactory">Application unit of work factory.</param>
        public void HandleRemoveProject(RemoveProjectCommand command, IAppUnitOfWorkFactory uowFactory)
        {
            using (var uow = uowFactory.Create())
            {
                var project = uow.ProjectRepository.Get(command.ProjectId);

                if (project.User.Id != command.UpdatedByUserId)
                {
                    throw new ForbiddenException("Only user who created project can remove it.");
                }

                uow.TaskRepository.RemoveRange(uow.TaskRepository.Where(t => t.Project.Id == project.Id));
                uow.ProjectRepository.Remove(project);
                uow.SaveChanges();
            }
        }
Esempio n. 15
0
        /// <summary>
        /// Update project handler.
        /// </summary>
        /// <param name="command">Command.</param>
        /// <param name="uowFactory">Application unit of work factory.</param>
        public void HandleUpdateProject(UpdateProjectCommand command, IAppUnitOfWorkFactory uowFactory)
        {
            using (var uow = uowFactory.Create())
            {
                var project = uow.ProjectRepository.Get(command.ProjectId);

                if (project.User.Id != command.UpdatedByUserId)
                {
                    throw new ForbiddenException("Only user who created project can update it.");
                }

                project.Name      = command.Name;
                project.Color     = command.Color;
                project.UpdatedAt = DateTime.Now;
                uow.SaveChanges();
            }
        }
 public void HandleUpdate(UpdateProductCommand command, IAppUnitOfWorkFactory uowFactory)
 {
     using (IAppUnitOfWork uow = uowFactory.Create())
     {
         Product product = uow.ProductRepository.Get(command.ProductId, Product.IncludeAll);
         Company company = uow.CompanyRepository.Get(command.CompanyId);
         if (company == null)
         {
             throw new DomainException("The assigned company not found");
         }
         User updater = uow.Users.FirstOrDefault(u => u.Id == command.UpdatedByUserId);
         if (updater == null)
         {
             throw new DomainException("Can not find updater");
         }
         // Delete properties
         foreach (ProductProperty removedProperty in product.Properties.Where(oldP => !command.Properties.Any(newP => newP.Id == oldP.Id)).ToList())
         {
             product.Properties.Remove(removedProperty);
             uow.ProductPropertyRepository.Remove(removedProperty);
         }
         // Update existing properties
         foreach (ProductProperty existProperty in product.Properties)
         {
             ProductProperty updatedProperty = command.Properties.SingleOrDefault(pp => pp.Id == existProperty.Id);
             existProperty.Name  = updatedProperty.Name;
             existProperty.Value = updatedProperty.Value;
         }
         // Add new properties
         foreach (ProductProperty property in command.Properties.Where(pp => pp.Id == 0))
         {
             product.Properties.Add(property);
         }
         product.Company   = company;
         product.Name      = command.Name;
         product.Comment   = command.Comment;
         product.Quantity  = command.Quantity;
         product.Sku       = command.Sku;
         product.IsActive  = command.IsActive;
         product.UpdatedAt = DateTime.Now;
         uow.SaveChanges();
     }
 }
Esempio n. 17
0
        /// <summary>
        /// Handles task update.
        /// </summary>
        /// <param name="command">Command.</param>
        /// <param name="uowFactory">Application unit of work factory.</param>
        public void HandleUpdateTask(UpdateTaskCommand command, IAppUnitOfWorkFactory uowFactory)
        {
            using (var uow = uowFactory.Create())
            {
                var project = uow.ProjectRepository.Get(command.ProjectId);
                if (project != null && project.User.Id != command.UserId)
                {
                    throw new DomainException("User does not own the project.");
                }

                var dbtask = uow.TaskRepository.Get(command.Id);
                if (dbtask.User.Id != command.UserId)
                {
                    throw new DomainException("You cannot update task for another user.");
                }

                dbtask.DueDate = DateTime.MaxValue;
                dbtask.IsDone  = command.IsDone;
                dbtask.Text    = command.Text.Trim();
                dbtask.Project = project;
                uow.SaveChanges();
            }
        }
 /// <summary>
 /// Handle create command.
 /// </summary>
 public void HandleCreate(CreateCompanyCommand command, IAppUnitOfWorkFactory uowFactory)
 {
     using (IAppUnitOfWork uow = uowFactory.Create())
     {
         if (uow.Companies.Any(c => c.Name == command.Name.Trim()))
         {
             throw new DomainException("The company with the same name already exists.");
         }
         User creator = uow.Users.FirstOrDefault(u => u.Id == command.CreatedByUserId);
         if (creator == null)
         {
             throw new DomainException("Cannot find creator.");
         }
         var company = new Company
         {
             Name      = command.Name,
             CreatedBy = creator
         };
         uow.CompanyRepository.Add(company);
         uow.SaveChanges();
         command.CompanyId = company.Id;
     }
 }
Esempio n. 19
0
        public void HandleLogin(LoginUserCommand command, IAppUnitOfWorkFactory uowFactory)
        {
            using (var uow = uowFactory.Create())
            {
                var email = command.Email.ToLowerInvariant().Trim();
                var user  = uow.Users.FirstOrDefault(u => u.Email == email && u.IsActive);
                if (user == null)
                {
                    command.IsSuccess = false;
                    return;
                }

                var isPasswordCorrect = Tools.Common.Utils.SecurityUtils.CheckHash(command.Password,
                                                                                   user.PasswordHashed);
                if (!isPasswordCorrect)
                {
                    command.IsSuccess = false;
                    return;
                }

                command.User      = user;
                command.IsSuccess = true;
            }
        }
Esempio n. 20
0
 public GetDayJobsCommand(CrmService crmService, IAppUnitOfWorkFactory uowFactory)
 {
     this.crmService = crmService;
     this.uowFactory = uowFactory;
 }
 public TelegramChatService(IAppUnitOfWorkFactory unitOfWork)
 {
     uow = unitOfWork;
 }
Esempio n. 22
0
 public NotifySuccessfulConnectionCommand(IAppUnitOfWorkFactory uowFactory)
 {
     this.uowFactory = uowFactory;
 }
Esempio n. 23
0
 public UserService(IAppUnitOfWorkFactory unitOfWorkFactory)
 {
     uow = unitOfWorkFactory;
 }
Esempio n. 24
0
 public NotificationSubscriptionService(IAppUnitOfWorkFactory unitOfWork)
 {
     uow = unitOfWork;
 }
Esempio n. 25
0
 public TestSource(IAppUnitOfWorkFactory appUnitOfWorkFactory)
 {
     AppUnitOfWorkFactory = appUnitOfWorkFactory;
 }
Esempio n. 26
0
 public CheckDailyReportExistsCommand(CrmService crmService, IAppUnitOfWorkFactory uowFactory)
 {
     this.crmService = crmService;
     this.uowFactory = uowFactory;
 }
Esempio n. 27
0
 public TestSourceBasedTests(IAppUnitOfWorkFactory appUnitOfWorkFactory) : base(appUnitOfWorkFactory)
 {
 }
Esempio n. 28
0
 public SubscribeDailyReportNotificationsCommand(NotificationSubscriptionService subscriptionService, IAppUnitOfWorkFactory unitOfWork)
 {
     this.subscriptionService = subscriptionService;
     uow = unitOfWork;
 }
Esempio n. 29
0
        /// <summary>
        /// Parse date from a command. If command text is empty, retrieves current date for associated user.
        /// </summary>
        /// <param name="commandContext">Command context.</param>
        /// <param name="uowFactory">Factory for creating database connections.</param>
        /// <returns>Parsed date.</returns>
        public static async Task <DateTime> ParseDateFromMessage(CommandContext commandContext, IAppUnitOfWorkFactory uowFactory)
        {
            var      message = (commandContext.Message ?? "").Trim();
            DateTime date;

            if (DateTime.TryParse(message, out date))
            {
                return(date);
            }

            int  dateOffset;
            bool correctDateOffset = int.TryParse(message, out dateOffset);

            if (!correctDateOffset)
            {
                dateOffset = 0;
            }
            if (correctDateOffset || string.IsNullOrEmpty(message))
            {
                // Get "today" for current user
                User user;
                using (var database = uowFactory.Create())
                {
                    user = await database.Users.FirstOrDefaultAsync(u => u.Chat.ChatId == commandContext.ChatId);
                }
                date = TimeZoneInfo.ConvertTimeFromUtc(DateTime.UtcNow, TimeZoneInfo.FindSystemTimeZoneById(user.TimeZoneCode));
                date = date.AddDays(dateOffset);
                return(date);
            }

            throw new FormatException("Could not parse the date, please use MM/dd or MM/dd/yyyy format.");
        }