Пример #1
0
 public async Task<RepositoryResponse<bool>> Join()
 {
     using(SioChatServiceContext _context = new SioChatServiceContext())
     {
         var result = new RepositoryResponse<bool>() { IsSucceed = true };
         try
         {
             var user = new SioMessengerUser()
             {
                 Id = Id,
                 FacebookId = Id,
                 Avatar = Avatar,
                 CreatedDate = DateTime.UtcNow,
                 Name = Name,
                 Status = (int)SioChatEnums.OnlineStatus.Connected
             };
             if (_context.SioMessengerUser.Any(u=>u.Id == user.Id))
             {
                 _context.Entry(user).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
             }
             else
             {
                 _context.Entry(user).State = Microsoft.EntityFrameworkCore.EntityState.Added;
             }
             if (Device != null)
             {
                 //var cnn = _context.SioMessengerUserDevice.FirstOrDefault(c => c.UserId == Device.UserId && c.DeviceId == Device.DeviceId);
                 if (_context.SioMessengerUserDevice.Any(c => c.UserId == Device.UserId && c.DeviceId == Device.DeviceId))
                 {
                     Device.ConnectionId = Device.ConnectionId;
                     Device.Status = (int)SioChatEnums.DeviceStatus.Actived;
                     Device.StartDate = DateTime.UtcNow;
                     _context.Entry(Device).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
                 }
                 else
                 {
                     Device.Status = (int)SioChatEnums.DeviceStatus.Actived;
                     Device.StartDate = DateTime.UtcNow;
                     _context.Entry(Device).State = Microsoft.EntityFrameworkCore.EntityState.Added;
                 }
                 
             }
             result.IsSucceed = (await _context.SaveChangesAsync()) > 0;
         }
         catch (Exception ex)
         {
             result.IsSucceed = false;
             result.Exception = ex;
         }
         return result;
     }
     
 }
Пример #2
0
        public async Task <RepositoryResponse <bool> > InitCms(string siteName, InitCulture culture)
        {
            RepositoryResponse <bool> result         = new RepositoryResponse <bool>();
            SioCmsContext             context        = null;
            SioCmsAccountContext      accountContext = null;
            SioChatServiceContext     messengerContext;
            IDbContextTransaction     transaction    = null;
            IDbContextTransaction     accTransaction = null;
            bool isSucceed = true;

            try
            {
                if (!string.IsNullOrEmpty(SioService.GetConnectionString(SioConstants.CONST_CMS_CONNECTION)))
                {
                    context          = new SioCmsContext();
                    accountContext   = new SioCmsAccountContext();
                    messengerContext = new SioChatServiceContext();
                    //SioChatServiceContext._cnn = SioService.GetConnectionString(SioConstants.CONST_CMS_CONNECTION);
                    await context.Database.MigrateAsync();

                    await accountContext.Database.MigrateAsync();

                    await messengerContext.Database.MigrateAsync();

                    transaction = context.Database.BeginTransaction();

                    var countCulture = context.SioCulture.Count();

                    var isInit = countCulture > 0;

                    if (!isInit)
                    {
                        SioService.SetConfig <string>("SiteName", siteName);
                        isSucceed = InitCultures(culture, context, transaction);

                        isSucceed = isSucceed && InitPositions(context, transaction);

                        isSucceed = isSucceed && await InitConfigurationsAsync(siteName, culture, context, transaction);

                        isSucceed = isSucceed && await InitLanguagesAsync(culture, context, transaction);

                        isSucceed = isSucceed && InitThemes(context, transaction);
                    }
                    else
                    {
                        isSucceed = true;
                    }

                    if (isSucceed && context.SioPage.Count() == 0)
                    {
                        var cate = new SioPage()
                        {
                            Id              = 1,
                            Level           = 0,
                            Title           = "Home",
                            Specificulture  = culture.Specificulture,
                            Template        = "Pages/_Home.cshtml",
                            Type            = (int)SioPageType.Home,
                            CreatedBy       = "Admin",
                            CreatedDateTime = DateTime.UtcNow,
                            Status          = (int)PageStatus.Published
                        };


                        context.Entry(cate).State = EntityState.Added;
                        var alias = new SioUrlAlias()
                        {
                            Id              = 1,
                            SourceId        = "1",
                            Type            = (int)UrlAliasType.Page,
                            Specificulture  = culture.Specificulture,
                            CreatedDateTime = DateTime.UtcNow,
                            Alias           = cate.Title.ToLower()
                        };
                        context.Entry(alias).State = EntityState.Added;

                        var createVNHome = await context.SaveChangesAsync().ConfigureAwait(false);

                        isSucceed = createVNHome > 0;

                        var cate404 = new SioPage()
                        {
                            Id              = 2,
                            Title           = "404",
                            Level           = 0,
                            Specificulture  = culture.Specificulture,
                            Template        = "Pages/_404.cshtml",
                            Type            = (int)SioPageType.Article,
                            CreatedBy       = "Admin",
                            CreatedDateTime = DateTime.UtcNow,
                            Status          = (int)PageStatus.Published
                        };

                        var alias404 = new SioUrlAlias()
                        {
                            Id              = 2,
                            SourceId        = "2",
                            Type            = (int)UrlAliasType.Page,
                            Specificulture  = culture.Specificulture,
                            CreatedDateTime = DateTime.UtcNow,
                            Alias           = cate404.Title.ToLower()
                        };
                        context.Entry(cate404).State  = EntityState.Added;
                        context.Entry(alias404).State = EntityState.Added;

                        var create404 = await context.SaveChangesAsync().ConfigureAwait(false);

                        isSucceed = create404 > 0;
                    }

                    if (isSucceed)
                    {
                        transaction.Commit();
                    }
                    else
                    {
                        transaction.Rollback();
                    }
                }
                result.IsSucceed = isSucceed;
                return(result);
            }
            catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only
            {
                transaction?.Rollback();
                accTransaction?.Rollback();
                result.IsSucceed = false;
                result.Exception = ex;
                return(result);
            }
            finally
            {
                context?.Dispose();
                accountContext?.Dispose();
            }
        }
Пример #3
0
        public async Task <RepositoryResponse <bool> > InitCms(string siteName, InitCulture culture)
        {
            RepositoryResponse <bool> result         = new RepositoryResponse <bool>();
            SioCmsContext             context        = null;
            SioCmsAccountContext      accountContext = null;
            SioChatServiceContext     messengerContext;
            IDbContextTransaction     transaction    = null;
            IDbContextTransaction     accTransaction = null;
            bool isSucceed = true;

            try
            {
                if (!string.IsNullOrEmpty(SioService.GetConnectionString(SioConstants.CONST_CMS_CONNECTION)))
                {
                    context          = new SioCmsContext();
                    accountContext   = new SioCmsAccountContext();
                    messengerContext = new SioChatServiceContext();
                    //SioChatServiceContext._cnn = SioService.GetConnectionString(SioConstants.CONST_CMS_CONNECTION);
                    await context.Database.MigrateAsync();

                    await accountContext.Database.MigrateAsync();

                    await messengerContext.Database.MigrateAsync();

                    transaction = context.Database.BeginTransaction();

                    var countCulture = context.SioCulture.Count();

                    var isInit = countCulture > 0;

                    if (!isInit)
                    {
                        SioService.SetConfig <string>("SiteName", siteName);
                        isSucceed = InitCultures(culture, context, transaction);
                        if (isSucceed)
                        {
                            isSucceed = isSucceed && InitPositions(context, transaction);
                        }
                        else
                        {
                            result.Errors.Add("Cannot init Cultures");
                        }
                        if (isSucceed)
                        {
                            isSucceed = isSucceed && await InitConfigurationsAsync(siteName, culture, context, transaction);
                        }
                        else
                        {
                            result.Errors.Add("Cannot init Positions");
                        }
                        if (isSucceed)
                        {
                            isSucceed = isSucceed && await InitLanguagesAsync(culture, context, transaction);
                        }
                        else
                        {
                            result.Errors.Add("Cannot init Configurations");
                        }
                        if (isSucceed)
                        {
                            var initTheme = await InitThemesAsync(siteName, context, transaction);

                            isSucceed = isSucceed && initTheme.IsSucceed;
                            result.Errors.AddRange(initTheme.Errors);
                            result.Exception = initTheme.Exception;
                        }
                        else
                        {
                            result.Errors.Add("Cannot init Languages");
                        }
                    }
                    else
                    {
                        isSucceed = true;
                    }

                    if (isSucceed && context.SioPage.Count() == 0)
                    {
                        InitPages(culture.Specificulture, context, transaction);
                        isSucceed = (await context.SaveChangesAsync().ConfigureAwait(false)) > 0;
                    }
                    else
                    {
                        result.Errors.Add("Cannot init Themes");
                    }
                    if (isSucceed)
                    {
                        transaction.Commit();
                    }
                    else
                    {
                        transaction.Rollback();
                    }
                }
                result.IsSucceed = isSucceed;
                return(result);
            }
            catch (Exception ex) // TODO: Add more specific exeption types instead of Exception only
            {
                transaction?.Rollback();
                accTransaction?.Rollback();
                result.IsSucceed = false;
                result.Exception = ex;
                return(result);
            }
            finally
            {
                context?.Dispose();
                accountContext?.Dispose();
            }
        }