Exemplo n.º 1
0
        public bool CanAddPlaylist(int userId)
        {
            try
            {
                
                using (var smartTimer = new SmartTimer((x, u) => GatewayLoggerInfo("Exit CanAddPlaylist", userId, x.Elapsed)))
                {
                    GatewayLoggerInfo("CanAddPlaylist", userId);
                    using (var connection = _provider.Create())
                    {
                        var parameters = new DynamicParameters();
                        parameters.Add("@userId", userId);
                        parameters.Add("@canAdd", dbType: DbType.Boolean, direction: ParameterDirection.Output);

                        connection.Execute("user.CanAddPlaylist", parameters, commandType: CommandType.StoredProcedure);

                        return parameters.Get<bool>("@canAdd");
                    }
                }
            }
            catch (System.Exception ex)
            {
                logger.Error(ex);
                throw;
            }
        }
Exemplo n.º 2
0
 public void AddOrUpdateUser(UserModel user)
 {
     try
     {
         using (var smartTimer = new SmartTimer((x,u) => GatewayLoggerInfo("Exit AddOrUpdateUser", user.Id, x.Elapsed)))
         {
             GatewayLoggerInfo("AddOrUpdateUser", user.Id);
             using (var connection = _provider.Create())
             {
                 connection.Execute("user.AddOrUpdate", new
                 {
                     Id = user.Id,
                     Firstname = user.Firstname,
                     Lastname = user.Lastname,
                     Email = user.Email,
                     Gender = user.Gender,
                     Name = user.Name,
                     Country = user.Country,
                     Lang = user.Lang,
                     Birthday = user.Birthday,
                 }, commandType: CommandType.StoredProcedure);
             }
         }
     }
     catch (System.Exception ex)
     {
         logger.Error(ex);
         throw;
     }
 }
Exemplo n.º 3
0
        public IEnumerable<string> GetRandomTracks(int userId)
        {

            try
            {
                using (var smartTimer = new SmartTimer((x, u) => GatewayLoggerInfo("Exit GetRandomTracks", userId, x.Elapsed)))
                {
                    GatewayLoggerInfo("GetRandomTracks", userId);
                    
                    using (var connection = _provider.Create())
                    {
                        var tracks = connection
                                .Query<string>("track.GetRandom", new { total = 10 },
                                commandType: CommandType.StoredProcedure);

                        return tracks ?? Enumerable.Empty<string>();
                    }
                }
            }
            catch (System.Exception ex)
            {
                logger.Error(ex);
                throw;
            }
        }
Exemplo n.º 4
0
        public HomeModule(IRadioGateway radioGateway, IMagicPlaylistGateway magicPlaylistGateway, HttpDeezer httpDeezer)
        {
            Get["/"] = _ =>
            {
                using (var smartTimer = new SmartTimer((x, u) => ModuleLoggerInfo("Exit Home", x.Elapsed)))
                {
                    ModuleLoggerInfo("Exit Home");
                    return View["index.html"];
                }    
            };

            Get["/channel"] = _ =>
            {
                using (var smartTimer = new SmartTimer((x, u) => ModuleLoggerInfo("Exit Channel", x.Elapsed)))
                {
                    ModuleLoggerInfo("Enter Channel");
                    return View["channel.html"];
                }
            };

            Post["/playlist"] = parameters =>
            {
                try
                {
                    using (var smartTimer = new SmartTimer((x, u) => ModuleLoggerInfo("Exit Playlist", u, x.Elapsed)))
                    {
                        ModuleLoggerInfo("Enter Playlist");

                        // Manage user
                        var user = this.Bind<UserModel>();

                        if (user == null || user.Id == 0)
                            throw new MagicPlaylistException("UserId is null or empty");

                        smartTimer.SetUserId(user.Id);

                        if (string.IsNullOrEmpty(user.AccessToken))
                            throw new MagicPlaylistException(user.Id, "AccessToken is null or empty");

                        if (!magicPlaylistGateway.CanAddPlaylist(user.Id))
                            throw new MagicPlaylistException(user.Id, "User can't add playlist");

                        magicPlaylistGateway.AddOrUpdateUser(user);

                        // Get tracks
                        var tracks = radioGateway.GetRandomTracks(user.Id);
                        if (tracks == null || !tracks.Any())
                            throw new MagicPlaylistException(user.Id, "DbTracks is null or empty");

                        // Add playlist
                        var deezerPlaylist = httpDeezer.AddPlaylist(user.Id, user.AccessToken, "MagicPlaylist");
                        if (deezerPlaylist == null)
                            throw new MagicPlaylistException(user.Id, "DeezerPlaylist is null");
                        else if (deezerPlaylist.HasError)
                            throw new MagicPlaylistException(user.Id, string.Format("DeezerPlaylist error:[{0}][{1}][{2}]",
                                deezerPlaylist.Error.Type, deezerPlaylist.Error.Message, deezerPlaylist.Error.Code));

                        // Add tracks
                        var deezerTracks = httpDeezer.AddTracks(user.Id, user.AccessToken, deezerPlaylist.Id, tracks);
                        if (deezerTracks == null)
                            throw new MagicPlaylistException(user.Id, "DeezerTracks is null");
                        else if (deezerTracks.HasError)
                            throw new MagicPlaylistException(user.Id, string.Format("DeezerTracks error:[{0}][{1}][{2}]",
                                deezerTracks.Error.Type, deezerTracks.Error.Message, deezerTracks.Error.Code));


                        return Success(deezerPlaylist.PlaylistUrl);
                    }
                }
                catch (Exception ex)
                {
                    logger.Error(ex);
                    var error = new ErrorModel(ex.GetType().Name, ex.Message, ex.StackTrace);
                    magicPlaylistGateway.AddError(error);
                    return Fail();
                }
            };
        }
Exemplo n.º 5
0
 public void AddError(ErrorModel error)
 {
     try
     {
         
         using (var smartTimer = new SmartTimer((x, u) => GatewayLoggerInfo(string.Format("Exit AddError", error.errorType), x.Elapsed)))
         {
             GatewayLoggerInfo(string.Format("[type:{0}]AddError", error.errorType));
             using (var connection = _provider.Create())
             {
                 connection.Execute("log.AddError", new
                 {
                     ErrorType = error.errorType,
                     Message = error.message,
                     StackTrace = error.stackTrace
                 }, commandType: CommandType.StoredProcedure);
             }
         }
     }
     catch (System.Exception ex)
     {
         logger.Error(ex);
         throw;
     }
 }