private void CreateEditions()
        {
            var defaultEdition = _context.Editions.IgnoreQueryFilters().FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName);

            if (defaultEdition == null)
            {
                defaultEdition = new Edition {
                    Name = EditionManager.DefaultEditionName, DisplayName = EditionManager.DefaultEditionName
                };
                _context.Editions.Add(defaultEdition);
                _context.SaveChanges();

                /* Add desired features to the standard edition, if wanted... */
            }
        }
        public override async Task <ItemDto> Create(CreateItemDto input)
        {
            try
            {
                CheckCreatePermission();

                var item = ObjectMapper.Map <Item>(input);

                var itemPrices = input.ItemPrices;

                TimePlayZeroDbContextFactory fac     = new TimePlayZeroDbContextFactory();
                TimePlayZeroDbContext        context = fac.CreateDbContext(null);

                context.Items.Add(item);
                context.SaveChanges();
                if (string.IsNullOrEmpty(input.UnlimitedItemCode) == false)
                {
                    ItemPrice itemPrice = new ItemPrice();
                    itemPrice.Code         = input.UnlimitedItemCode;
                    itemPrice.IsUnlimited  = true;
                    itemPrice.CreationTime = DateTime.Now;
                    itemPrice.ItemId       = item.Id;
                    context.ItemPrices.Add(itemPrice);
                    context.SaveChanges();
                }

                foreach (var _itemPrice in itemPrices)
                {
                    var mappedPrice = ObjectMapper.Map <ItemPrice>(_itemPrice);

                    mappedPrice.ItemId = item.Id;
                    context.Add(mappedPrice);
                }

                context.SaveChanges();

                await CurrentUnitOfWork.SaveChangesAsync();

                return(MapToEntityDto(item));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void Create()
        {
            new DefaultEditionCreator(_context).Create();
            new DefaultLanguagesCreator(_context).Create();
            new HostRoleAndUserCreator(_context).Create();
            new DefaultSettingsCreator(_context).Create();

            _context.SaveChanges();
        }
        public override Task <ItemDto> Update(ItemEditDto input)
        {
            var itemPrices = input.ItemPrices;

            TimePlayZeroDbContextFactory fac     = new TimePlayZeroDbContextFactory();
            TimePlayZeroDbContext        context = fac.CreateDbContext(null);

            if (string.IsNullOrEmpty(input.UnlimitedItemCode) == false)
            {
                var unlimitedItemPrice = context.ItemPrices.Where(w => w.ItemId == input.Id && w.IsUnlimited).FirstOrDefault();

                if (unlimitedItemPrice == null)
                {
                    unlimitedItemPrice             = new ItemPrice();
                    unlimitedItemPrice.ItemId      = input.Id;
                    unlimitedItemPrice.Code        = input.UnlimitedItemCode;
                    unlimitedItemPrice.IsUnlimited = true;
                    context.ItemPrices.Add(unlimitedItemPrice);
                }
                else if (unlimitedItemPrice.Code != input.UnlimitedItemCode)
                {
                    unlimitedItemPrice.Code = input.UnlimitedItemCode;
                }
            }
            else
            {
                var unlimitedItemPrice = context.ItemPrices.Where(w => w.ItemId == input.Id && w.IsUnlimited).FirstOrDefault();

                if (unlimitedItemPrice != null)
                {
                    unlimitedItemPrice.IsDeleted = true;
                }
            }

            foreach (var _itemPrice in itemPrices)
            {
                var dbItem = context.ItemPrices.Where(w => w.Id == _itemPrice.Id).FirstOrDefault();

                if (dbItem != null)
                {
                    ObjectMapper.Map(_itemPrice, dbItem);
                    context.Entry(dbItem).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                }
                else
                {
                    var mappedPrice = ObjectMapper.Map <ItemPrice>(_itemPrice);
                    context.ItemPrices.Add(mappedPrice);
                }
            }

            context.SaveChanges();

            var res = base.Update(input);

            return(res);
        }
Пример #5
0
        private void AddLanguageIfNotExists(ApplicationLanguage language)
        {
            if (_context.Languages.IgnoreQueryFilters().Any(l => l.TenantId == language.TenantId && l.Name == language.Name))
            {
                return;
            }

            _context.Languages.Add(language);
            _context.SaveChanges();
        }
        private void AddSettingIfNotExists(string name, string value, int?tenantId = null)
        {
            if (_context.Settings.IgnoreQueryFilters().Any(s => s.Name == name && s.TenantId == tenantId && s.UserId == null))
            {
                return;
            }

            _context.Settings.Add(new Setting(tenantId, null, name, value));
            _context.SaveChanges();
        }
Пример #7
0
        public void FinishItem(int transactionId)
        {
            TimePlayZeroDbContextFactory fac     = new TimePlayZeroDbContextFactory();
            TimePlayZeroDbContext        context = fac.CreateDbContext(null);

            var tran = context.Transactions.Where(w => w.Id == transactionId).FirstOrDefault();

            if (tran != null)
            {
                tran.State = 1;
                context.SaveChanges();
            }
        }
Пример #8
0
        public override async Task <ItemGroupDto> Create(CreateItemGroupDto input)
        {
            CheckCreatePermission();

            var ItemGroup = ObjectMapper.Map <ItemGroup>(input);

            TimePlayZeroDbContextFactory fac     = new TimePlayZeroDbContextFactory();
            TimePlayZeroDbContext        context = fac.CreateDbContext(null);

            context.ItemGroups.Add(ItemGroup);
            context.SaveChanges();

            await CurrentUnitOfWork.SaveChangesAsync();

            return(MapToEntityDto(ItemGroup));
        }
        private void CreateDefaultTenant()
        {
            // Default tenant

            var defaultTenant = _context.Tenants.IgnoreQueryFilters().FirstOrDefault(t => t.TenancyName == AbpTenantBase.DefaultTenantName);

            if (defaultTenant == null)
            {
                defaultTenant = new Tenant(AbpTenantBase.DefaultTenantName, AbpTenantBase.DefaultTenantName);

                var defaultEdition = _context.Editions.IgnoreQueryFilters().FirstOrDefault(e => e.Name == EditionManager.DefaultEditionName);
                if (defaultEdition != null)
                {
                    defaultTenant.EditionId = defaultEdition.Id;
                }

                _context.Tenants.Add(defaultTenant);
                _context.SaveChanges();
            }
        }
        private void CreateRolesAndUsers()
        {
            // Admin role

            var adminRole = _context.Roles.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == _tenantId && r.Name == StaticRoleNames.Tenants.Admin);

            if (adminRole == null)
            {
                adminRole = _context.Roles.Add(new Role(_tenantId, StaticRoleNames.Tenants.Admin, StaticRoleNames.Tenants.Admin)
                {
                    IsStatic = true
                }).Entity;
                _context.SaveChanges();
            }

            // Grant all permissions to admin role

            var grantedPermissions = _context.Permissions.IgnoreQueryFilters()
                                     .OfType <RolePermissionSetting>()
                                     .Where(p => p.TenantId == _tenantId && p.RoleId == adminRole.Id)
                                     .Select(p => p.Name)
                                     .ToList();

            var permissions = PermissionFinder
                              .GetAllPermissions(new TimePlayZeroAuthorizationProvider())
                              .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Tenant) &&
                                     !grantedPermissions.Contains(p.Name))
                              .ToList();

            if (permissions.Any())
            {
                _context.Permissions.AddRange(
                    permissions.Select(permission => new RolePermissionSetting
                {
                    TenantId  = _tenantId,
                    Name      = permission.Name,
                    IsGranted = true,
                    RoleId    = adminRole.Id
                })
                    );
                _context.SaveChanges();
            }

            // Admin user

            var adminUser = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == _tenantId && u.UserName == AbpUserBase.AdminUserName);

            if (adminUser == null)
            {
                adminUser                  = User.CreateTenantAdminUser(_tenantId, "*****@*****.**");
                adminUser.Password         = new PasswordHasher <User>(new OptionsWrapper <PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(adminUser, "123qwe");
                adminUser.IsEmailConfirmed = true;
                adminUser.IsActive         = true;

                _context.Users.Add(adminUser);
                _context.SaveChanges();

                // Assign Admin role to admin user
                _context.UserRoles.Add(new UserRole(_tenantId, adminUser.Id, adminRole.Id));
                _context.SaveChanges();
            }
        }
Пример #11
0
        private void CreateHostRoleAndUsers()
        {
            // Admin role for host

            var adminRoleForHost = _context.Roles.IgnoreQueryFilters().FirstOrDefault(r => r.TenantId == null && r.Name == StaticRoleNames.Host.Admin);

            if (adminRoleForHost == null)
            {
                adminRoleForHost = _context.Roles.Add(new Role(null, StaticRoleNames.Host.Admin, StaticRoleNames.Host.Admin)
                {
                    IsStatic = true, IsDefault = true
                }).Entity;
                _context.SaveChanges();
            }

            // Grant all permissions to admin role for host

            var grantedPermissions = _context.Permissions.IgnoreQueryFilters()
                                     .OfType <RolePermissionSetting>()
                                     .Where(p => p.TenantId == null && p.RoleId == adminRoleForHost.Id)
                                     .Select(p => p.Name)
                                     .ToList();

            var permissions = PermissionFinder
                              .GetAllPermissions(new TimePlayZeroAuthorizationProvider())
                              .Where(p => p.MultiTenancySides.HasFlag(MultiTenancySides.Host) &&
                                     !grantedPermissions.Contains(p.Name))
                              .ToList();

            if (permissions.Any())
            {
                _context.Permissions.AddRange(
                    permissions.Select(permission => new RolePermissionSetting
                {
                    TenantId  = null,
                    Name      = permission.Name,
                    IsGranted = true,
                    RoleId    = adminRoleForHost.Id
                })
                    );
                _context.SaveChanges();
            }

            // Admin user for host

            var adminUserForHost = _context.Users.IgnoreQueryFilters().FirstOrDefault(u => u.TenantId == null && u.UserName == AbpUserBase.AdminUserName);

            if (adminUserForHost == null)
            {
                var user = new User
                {
                    TenantId         = null,
                    UserName         = AbpUserBase.AdminUserName,
                    Name             = "admin",
                    Surname          = "admin",
                    EmailAddress     = "*****@*****.**",
                    IsEmailConfirmed = true,
                    IsActive         = true
                };

                user.Password = new PasswordHasher <User>(new OptionsWrapper <PasswordHasherOptions>(new PasswordHasherOptions())).HashPassword(user, "123qwe");
                user.SetNormalizedNames();

                adminUserForHost = _context.Users.Add(user).Entity;
                _context.SaveChanges();

                // Assign Admin role to admin user
                _context.UserRoles.Add(new UserRole(null, adminUserForHost.Id, adminRoleForHost.Id));
                _context.SaveChanges();

                _context.SaveChanges();
            }
        }