Exemple #1
0
        public async Task <IActionResult> ViewMyChannels(ViewMyChannelsAddressModel model)
        {
            var app = await ApiService.ValidateAccessTokenAsync(model.AccessToken);

            var appLocal = await _dbContext.Apps.SingleOrDefaultAsync(t => t.Id == app.AppId);

            if (appLocal == null)
            {
                appLocal = new StargateApp
                {
                    Id       = app.AppId,
                    Channels = new List <Channel>()
                };
                _dbContext.Apps.Add(appLocal);
                await _dbContext.SaveChangesAsync();
            }
            var channels = await _dbContext
                           .Channels
                           .Where(t => t.AppId == app.AppId)
                           .ToListAsync();

            var viewModel = new ViewMyChannelsViewModel
            {
                AppId   = appLocal.Id,
                Channel = channels,
                code    = ErrorType.Success,
                message = "Successfully get your channels!"
            };

            return(Json(viewModel));
        }
Exemple #2
0
        public async Task <IActionResult> ViewMyChannels(ViewMyChannelsAddressModel model)
        {
            var appid    = _tokenManager.ValidateAccessToken(model.AccessToken);
            var appLocal = await _dbContext.Apps.SingleOrDefaultAsync(t => t.Id == appid);

            if (appLocal == null)
            {
                appLocal = new StargateApp
                {
                    Id       = appid,
                    Channels = new List <Channel>()
                };
                _dbContext.Apps.Add(appLocal);
                await _dbContext.SaveChangesAsync();
            }
            var channels = await _dbContext
                           .Channels
                           .Where(t => t.AppId == appid)
                           .ToListAsync();

            var viewModel = new ViewMyChannelsViewModel
            {
                AppId    = appLocal.Id,
                Channels = channels
                           .Select(t => new ChannelDetail(t,
                                                          _connectedCountService.GetConnectedCount(t.Id),
                                                          _lastAccessService.GetLastAccessTime(t.Id)))
                           .ToList(),
                Code    = ErrorType.Success,
                Message = "Successfully get your channels!"
            };

            return(Json(viewModel));
        }
        public async Task <IActionResult> ViewMyChannels(ViewMyChannelsAddressModel model)
        {
            var appid    = _tokenManager.ValidateAccessToken(model.AccessToken);
            var appLocal = await _dbContext.Apps.SingleOrDefaultAsync(t => t.Id == appid);

            if (appLocal == null)
            {
                appLocal = new StargateApp
                {
                    Id = appid
                };
                await _dbContext.Apps.AddAsync(appLocal);

                await _dbContext.SaveChangesAsync();
            }
            var channels = _stargateMemory
                           .GetChannelsUnderApp(appid);
            var viewModel = new ViewMyChannelsViewModel
            {
                AppId    = appid,
                Channels = channels,
                Code     = ErrorType.Success,
                Message  = "Successfully get your channels!"
            };

            return(Ok(viewModel));
        }
        public async Task <IActionResult> CreateChannel(CreateChannelAddressModel model)
        {
            //Update app info
            var appid    = _tokenManager.ValidateAccessToken(model.AccessToken);
            var appLocal = await _dbContext.Apps.SingleOrDefaultAsync(t => t.Id == appid);

            if (appLocal == null)
            {
                appLocal = new StargateApp
                {
                    Id = appid
                };
                await _dbContext.Apps.AddAsync(appLocal);
            }
            var channelId = _counter.GetUniqueNo();
            var key       = Guid.NewGuid().ToString("N");

            _stargateMemory.CreateChannel(channelId, appid, model.Description, key);
            //return model
            var viewModel = new CreateChannelViewModel
            {
                ChannelId  = channelId,
                ConnectKey = key,
                Code       = ErrorType.Success,
                Message    = "Successfully created your channel!"
            };

            return(Ok(viewModel));
        }
        public async Task <IActionResult> PushMessage(PushMessageAddressModel model)
        {
            //Ensure app is created
            var app = await ApiService.ValidateAccessTokenAsync(model.AccessToken);

            var appLocal = await _dbContext.Apps.SingleOrDefaultAsync(t => t.Id == app.AppId);

            if (appLocal == null)
            {
                appLocal = new StargateApp
                {
                    Id       = app.AppId,
                    Channels = new List <Channel>()
                };
                _dbContext.Apps.Add(appLocal);
                await _dbContext.SaveChangesAsync();
            }
            //Ensure channel
            var channel = await _dbContext.Channels.SingleOrDefaultAsync(t => t.Id == model.ChannelId);

            if (channel == null)
            {
                return(Json(new AiurProtocal
                {
                    code = ErrorType.NotFound,
                    message = "We can not find your channel!"
                }));
            }
            if (channel.AppId != app.AppId)
            {
                return(Json(new AiurProtocal
                {
                    code = ErrorType.Unauthorized,
                    message = "The channel you wanna create message is not your app's channel!"
                }));
            }
            //Create Message
            var message = new Message
            {
                Id        = Startup.MessageIdCounter.GetUniqueNo,
                ChannelId = channel.Id,
                Content   = model.MessageContent
            };

            StargateMemory.Messages.Add(message);
            return(Json(new AiurProtocal
            {
                code = ErrorType.Success,
                message = $"You have successfully created a message at channel:{channel.Id}!"
            }));
        }
Exemple #6
0
        public async Task <IActionResult> CreateChannel([FromForm] CreateChannelAddressModel model)
        {
            //Update app info
            var app = await ApiService.ValidateAccessTokenAsync(model.AccessToken);

            var appLocal = await _dbContext.Apps.Include(t => t.Channels).SingleOrDefaultAsync(t => t.Id == app.AppId);

            if (appLocal == null)
            {
                appLocal = new StargateApp
                {
                    Id       = app.AppId,
                    Channels = new List <Channel>()
                };
                _dbContext.Apps.Add(appLocal);
            }
            //Create and save to database
            var newChannel = new Channel
            {
                Description = model.Description,
                ConnectKey  = StringOperation.RandomString(20)
            };

            appLocal.Channels.Add(newChannel);
            await _dbContext.SaveChangesAsync();

            //return model
            var viewModel = new CreateChannelViewModel
            {
                ChannelId  = newChannel.Id,
                ConnectKey = newChannel.ConnectKey,
                code       = ErrorType.Success,
                message    = "Successfully created your channel!"
            };

            return(Json(viewModel));
        }
Exemple #7
0
        public async Task <IActionResult> CreateChannel(CreateChannelAddressModel model)
        {
            //Update app info
            var appid = await _tokenManager.ValidateAccessToken(model.AccessToken);

            var appLocal = await _dbContext.Apps.Include(t => t.Channels).SingleOrDefaultAsync(t => t.Id == appid);

            if (appLocal == null)
            {
                appLocal = new StargateApp
                {
                    Id       = appid,
                    Channels = new List <Channel>()
                };
                _dbContext.Apps.Add(appLocal);
            }
            //Create and save to database
            var newChannel = new Channel
            {
                Description = model.Description,
                ConnectKey  = Guid.NewGuid().ToString("N")
            };

            appLocal.Channels.Add(newChannel);
            await _dbContext.SaveChangesAsync();

            //return model
            var viewModel = new CreateChannelViewModel
            {
                ChannelId  = newChannel.Id,
                ConnectKey = newChannel.ConnectKey,
                Code       = ErrorType.Success,
                Message    = "Successfully created your channel!"
            };

            return(Json(viewModel));
        }