예제 #1
0
        public GenericResult <bool> LogOut(HttpResponseMessage responce, HttpRequestMessage request)
        {
            var result = new GenericResult <bool>();

            try
            {
                var cookie = new CookieHeaderValue(DEFAULT_COOKIE_NAME, string.Empty)
                {
                    Expires = DateTimeOffset.UtcNow.AddDays(-1),
                    Domain  = request.RequestUri.Host,
                    Path    = "/"
                };

                responce.Headers.AddCookies(new[] { cookie });

                result.SetDataResult(true);
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }
예제 #2
0
        public GenericResult <TemplateModel> GetUserTemplate(string templateOrTemplateType, int userId)
        {
            var result = new GenericResult <TemplateModel>();

            try
            {
                using (var db = new RssAggregatorModelContainer())
                {
                    Expression <Func <Template, bool> > predicate;

                    TemplateTypeEnum expectedTemplate;
                    if (Enum.TryParse(templateOrTemplateType, out expectedTemplate))
                    {
                        predicate = el => el.Type == expectedTemplate;
                    }
                    else
                    {
                        predicate = el => el.Name.ToLower() == templateOrTemplateType.ToLower();
                    }

                    var selectedEntity = db.GetEntity <Template>(predicate);

                    result.SetDataResult(selectedEntity == null ? null : selectedEntity.GetModel());
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }
예제 #3
0
        public GenericResult <IEnumerable <NewsModel> > GetNews(int pageSize, int pageNumber, bool hideAdult)
        {
            var result = new GenericResult <IEnumerable <NewsModel> >();

            try
            {
                if (pageSize > 0 && pageNumber > 0)
                {
                    using (var db = new RssAggregatorModelContainer())
                    {
                        result.SetDataResult(db.GetDBSet <News>(el => el.IsActive)
                                             .OrderBy(el => el.Id)
                                             .Skip(pageSize * (pageNumber - 1))
                                             .Take(pageSize)
                                             .ToList()
                                             .Select(el => el.GetModel())
                                             .ToList());
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }
예제 #4
0
        public GenericResult <bool> AddNotPlayebleSong(OnlineRadioNotPlayebleSongModel inputParams)
        {
            var result = new GenericResult <bool>();

            try
            {
                using (var db = new RssAggregatorModelContainer(true))
                {
                    db.AddEntity(new SongsBlackList
                    {
                        SongURL = inputParams.SongURL,
                        City    = inputParams.City,
                        Country = inputParams.Country
                    });

                    result.SetDataResult(true);
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }
예제 #5
0
        public GenericResult <IEnumerable <KeyValuePair <string, int> > > GetAllNewsTags()
        {
            var result = new GenericResult <IEnumerable <KeyValuePair <string, int> > >();

            try
            {
                using (var db = new RssAggregatorModelContainer())
                {
                    result.SetDataResult(db.GetDBSet <News>(el => el.IsActive)
                                         .Select(el => el.PostTags)
                                         .ToList()
                                         .AsParallel()
                                         .SelectMany(el => el.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                                         .GroupBy(el => el)
                                         .ToDictionary(el => el.Key, el => el.Count()));
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }
예제 #6
0
        private GenericResult <bool> LogException(string errorMessage, LogTypeEnum type, string source = null)
        {
            var result = new GenericResult <bool>();

            try
            {
                Logger.LogException(errorMessage, type, source: source);
                result.SetDataResult(true);
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }
예제 #7
0
        private GenericResult <bool> LogException(Exception occuredException, LogTypeEnum type)
        {
            var result = new GenericResult <bool>();

            try
            {
                Logger.LogException(occuredException, type);
                result.SetDataResult(true);
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }
예제 #8
0
        public GenericResult <bool> EmailExists(string email)
        {
            var result = new GenericResult <bool>();

            try
            {
                using (var db = new RssAggregatorModelContainer())
                {
                    result.SetDataResult(db.GetEntity <User>(el => el.Email.ToLower() == email.ToLower()) != null);
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }
예제 #9
0
        public GenericResult <UserModel> GetUserData(HttpRequestMessage request)
        {
            var result = new GenericResult <UserModel>();

            try
            {
                using (var db = new RssAggregatorModelContainer())
                {
                    var requestCookie = request.Headers.GetCookies(DEFAULT_COOKIE_NAME).FirstOrDefault();
                    if (requestCookie != null)
                    {
                        var userKey = requestCookie[DEFAULT_COOKIE_NAME].Value;
                        if (!string.IsNullOrEmpty(userKey))
                        {
                            var user = db.GetEntity <User>(el => el.IsActive && el.UserKey.ToLower() == userKey.ToLower());
                            if (user != null)
                            {
                                result.SetDataResult(user.GetModel());
                            }
                            else
                            {
                                result.SetDataResult((UserModel)null);
                            }
                        }
                        else
                        {
                            result.SetDataResult((UserModel)null);
                        }
                    }
                    else
                    {
                        result.SetDataResult((UserModel)null);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }
예제 #10
0
        public GenericResult <string> Translate(string text)
        {
            var result = new GenericResult <string>();

            try
            {
                using (var db = new RssAggregatorModelContainer())
                {
                    result.SetDataResult(db.GetTransliteration(text));
                }

                return(result);
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }
예제 #11
0
        public GenericResult <IEnumerable <TemplateModel> > GetAllUserTemplates()
        {
            var result = new GenericResult <IEnumerable <TemplateModel> >();

            try
            {
                using (var db = new RssAggregatorModelContainer())
                {
                    result.SetDataResult(db.GetDBSet <Template>(el => el.Type == TemplateTypeEnum.Header || el.Type == TemplateTypeEnum.Post)
                                         .ToList()
                                         .Select(el => el.GetModel()));
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }
예제 #12
0
        public GenericResult <string> Login(UserModel userModel, HttpResponseMessage responce, HttpRequestMessage request)
        {
            var result = new GenericResult <string>();

            try
            {
                using (var db = new RssAggregatorModelContainer())
                {
                    var user = db.GetEntity <User>(el => el.IsActive && el.Name.ToLower() == userModel.Name.ToLower() && el.Password.ToLower() == userModel.Password.ToLower());
                    if (user != null)
                    {
                        result.SetDataResult(user.Name);

                        var cookie = new CookieHeaderValue(DEFAULT_COOKIE_NAME, user.UserKey)
                        {
                            Domain = request.RequestUri.Host,
                            Path   = "/"
                        };

                        if (userModel.CreateCookie.HasValue && userModel.CreateCookie.Value)
                        {
                            cookie.Expires = DateTimeOffset.UtcNow.AddDays(COOKIE_EXPIRE_DAYS);
                        }

                        responce.Headers.AddCookies(new[] { cookie });
                    }
                    else
                    {
                        result.SetWarningResultCode(SettingService.GetSetting("LOGIN_IncorrectUserNameOrPassword"));
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }
예제 #13
0
        public GenericResult <NewsModel> GetNewsItem(string newsItemId)
        {
            var result = new GenericResult <NewsModel>();

            try
            {
                if (!string.IsNullOrEmpty(newsItemId))
                {
                    using (var db = new RssAggregatorModelContainer())
                    {
                        result.SetDataResult(db.GetEntity <News>(el => el.IsActive && el.PostId == newsItemId).GetModel());
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }
예제 #14
0
        public GenericResult <IEnumerable <NavigationModel> > GetNavigationData()
        {
            var result = new GenericResult <IEnumerable <NavigationModel> >();

            try
            {
                using (var db = new RssAggregatorModelContainer())
                {
                    result.SetDataResult(db.GetDBSet <Navigation>(el => el.IsActive)
                                         .OrderBy(el => el.OrderNo)
                                         .ToList()
                                         .Select(el => el.GetModel())
                                         .ToList());
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }
예제 #15
0
        public async Task <GenericResult <IEnumerable <string> > > TypeAheadSearch(string question)
        {
            var result = new GenericResult <IEnumerable <string> >();

            try
            {
                using (var webClient = new HttpClient())
                {
                    var serverResponceTask = webClient.GetAsync(string.Format("{0}/?terms.prefix={1}", _onlineRadioTypeAheadPostfix.TrimEnd(new[] { '/' }), question));

                    var serverResult           = await(await serverResponceTask).Content.ReadAsStringAsync();
                    var serializedServerResult = JsonConvert.DeserializeObject <OnlineRadioTypeAhedJsonModel>(serverResult);

                    result.SetDataResult(serializedServerResult == null ? Enumerable.Empty <string>() : serializedServerResult.terms.GetAllKeys());
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }
예제 #16
0
        public async Task <GenericResult <IEnumerable <OnlineRadioServiceSongModel> > > Search(OnlineRadioServiceSearchModel searchModel)
        {
            var result = new GenericResult <IEnumerable <OnlineRadioServiceSongModel> >();

            try
            {
                var serverFactory = ParcerProviderFactory.GetFactory(DataSourceEnum.OnlineRadio);
                serverFactory.AddSearchCriteria(searchModel.Question);
                serverFactory.SetPageNumber(searchModel.PageNumber);

                var serverFactoryResult = await serverFactory.GetContent(new Uri(_onlineRadioSearchURl));

                if (serverFactoryResult != null && serverFactoryResult.Any())
                {
                    var allSong = new ConcurrentBag <OnlineRadioServiceSongModel>();
                    IEnumerable <SongsBlackList> songsBlackList;

                    using (var db = new RssAggregatorModelContainer())
                    {
                        songsBlackList = db.GetDBSet <SongsBlackList>(el => string.Compare(el.City, searchModel.City, true) == 0 && string.Compare(el.Country, searchModel.Country, true) == 0).ToList();
                    }

                    foreach (var postItem in serverFactoryResult)
                    {
                        foreach (var content in postItem.PostContent)
                        {
                            if (content.PostContentType == CORE.Models.Enums.PostContentTypeEnum.Audio)
                            {
                                Parallel.ForEach(((BasePostContentModel <AudioPostContentContainerModel>)content).PostSpecificContent, audioContent =>
                                {
                                    if (!string.IsNullOrEmpty(audioContent.Link))
                                    {
                                        using (var webClient = new WebClient())
                                        {
                                            var serverResult = webClient.DownloadString(string.Format("{0}/{1}", _onlineRadionBaseURL.TrimEnd(new[] { '/' }), audioContent.Link.TrimStart(new[] { '/' })));

                                            var serializedServerResult = JsonConvert.DeserializeObject <OnlineRadioServiceSongUrlJsonModel>(serverResult);
                                            if (serializedServerResult != null && !string.IsNullOrEmpty(serializedServerResult.url) && !songsBlackList.Any(el => el.SongURL == serializedServerResult.url))
                                            {
                                                allSong.Add(new OnlineRadioServiceSongModel
                                                {
                                                    Artist = audioContent.Artist,
                                                    Name   = audioContent.Name,
                                                    Link   = serializedServerResult.url
                                                });
                                            }
                                        }
                                    }
                                });
                            }
                        }
                    }

                    result.SetDataResult(allSong);
                }
                else
                {
                    result.SetDataResult(Enumerable.Empty <OnlineRadioServiceSongModel>());
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }
예제 #17
0
        public GenericResult <bool> AddEditNewsItem(NewsItemModel inputParams)
        {
            var result = new GenericResult <bool>();

            try
            {
                using (var db = new RssAggregatorModelContainer(true))
                {
                    var user     = db.GetEntity <User>(el => el.Id == inputParams.UserId && el.IsActive);
                    var userName = user.Name.ToLower();

                    var translatePost = TranslateService.Translate(inputParams.PostName);
                    var postId        = db.GetPostTransliteratedName(translatePost.InfoResult.ResultCode == Models.Enums.ResultCodeEnum.Success
                                                                    ? translatePost.DataResult
                                                                    : Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}{1}", userName, DateTime.UtcNow.Ticks))),
                                                                     userName);

                    var systemDataSource = db.GetEntity <DataSources>(el => el.Type == DataSourceEnum.System);


                    if (inputParams.IsNewOne && !string.IsNullOrEmpty(inputParams.PostId))
                    {
                        var newsEntity = db.GetEntity <News>(el => el.IsActive && el.User.Id == user.Id && el.PostId == inputParams.PostId);
                        if (newsEntity != null)
                        {
                            newsEntity.PostContent          = inputParams.PostContent;
                            newsEntity.PostTags             = inputParams.PostTags;
                            newsEntity.AdultContent         = inputParams.AdultContent;
                            newsEntity.ModificationDateTime = DateTime.UtcNow;

                            result.SetDataResult(true);
                        }
                        else
                        {
                            Logger.LogException(string.Format("News item not found, expected user Id:{0}, expected news item id:{1}", user.Id, inputParams.PostId), LogTypeEnum.BAL);

                            result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
                        }
                    }
                    else
                    {
                        db.AddEntity(new News
                        {
                            PostId               = postId,
                            AuthorId             = userName,
                            AuthorName           = userName,
                            AuthorLink           = string.Format("{0}/{1}", systemDataSource.Uri.TrimEnd(new[] { '/' }), userName),
                            PostName             = inputParams.PostName,
                            PostLink             = postId,
                            PostContent          = inputParams.PostContent,
                            PostTags             = inputParams.PostTags,
                            IsActive             = true,
                            AdultContent         = inputParams.AdultContent,
                            CreationDateTime     = DateTime.UtcNow,
                            ModificationDateTime = DateTime.UtcNow,

                            DataSource = systemDataSource,
                            User       = user,
                            Location   = inputParams.Locaion.HasValue ? (LocationEnum)inputParams.Locaion.Value : user.Location
                        });
                    }

                    result.SetDataResult(true);
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }
예제 #18
0
        public GenericResult <bool> LogUserActivity(LogUserActivityModel userActivityModel, HttpResponseMessage responce, HttpRequestMessage request)
        {
            var result = new GenericResult <bool>();

            try
            {
                var userData = UserService.GetUserData(request);
                if (userData.InfoResult.ResultCode == Models.Enums.ResultCodeEnum.Success)
                {
                    using (var db = new RssAggregatorModelContainer(true))
                    {
                        var userKey   = string.Empty;
                        var isNewUser = true;

                        User user = null;

                        var requestCookie = request.Headers.GetCookies(DEFAULT_COOKIE_NAME).FirstOrDefault();
                        if (requestCookie != null)
                        {
                            userKey   = requestCookie[DEFAULT_COOKIE_NAME].Value;
                            isNewUser = false;
                        }
                        else
                        {
                            userKey = Guid.NewGuid().ToString();

                            var cookie = new CookieHeaderValue(DEFAULT_COOKIE_NAME, userKey)
                            {
                                Expires = DateTimeOffset.UtcNow.AddDays(COOKIE_EXPIRE_DAYS),
                                Domain  = request.RequestUri.Host,
                                Path    = "/"
                            };

                            responce.Headers.AddCookies(new[] { cookie });
                        }

                        if (userData.DataResult != null && !string.IsNullOrEmpty(userData.DataResult.UserKey))
                        {
                            user      = db.GetEntity <User>(el => el.IsActive && el.UserKey.ToLower() == userData.DataResult.UserKey.ToLower());
                            isNewUser = user == null;
                        }

                        db.AddEntity(new UserActivityLog
                        {
                            Activity       = (ActivityEnum)userActivityModel.Activity,
                            Browser        = userActivityModel.Browser,
                            BrowserVersion = userActivityModel.BrowserVersion,
                            City           = userActivityModel.City,
                            Country        = userActivityModel.Country,
                            DateTime       = DateTime.UtcNow,
                            Organization   = userActivityModel.Organization,
                            Region         = userActivityModel.Region,
                            User           = user,
                            IsNew          = isNewUser,
                            UserKey        = userKey
                        });

                        result.SetDataResult(true);
                    }
                }
                else
                {
                    result.SetErrorResultCode(userData);
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }
예제 #19
0
        public GenericResult <string> CreateUpdate(UserModel userModel, HttpResponseMessage responce, HttpRequestMessage request)
        {
            var result = new GenericResult <string>();

            try
            {
                Func <string, string> getHashSum = (source) =>
                {
                    using (var md5Hash = MD5.Create())
                    {
                        return(md5Hash.ComputeHash(Encoding.UTF8.GetBytes(source)).Aggregate(string.Empty, (agg, el) => agg + el.ToString("x2")));
                    }
                };

                using (var db = new RssAggregatorModelContainer(true))
                {
                    var user = new User();

                    if (userModel.Exists.HasValue && userModel.Exists.Value)
                    {
                        var requestCookie = request.Headers.GetCookies(DEFAULT_COOKIE_NAME).FirstOrDefault();
                        if (requestCookie != null)
                        {
                            var userKey = requestCookie[DEFAULT_COOKIE_NAME].Value;
                            if (!string.IsNullOrEmpty(userKey))
                            {
                                user = db.GetEntity <User>(el => el.IsActive && el.UserKey.ToLower() == userKey.ToLower() && el.Name.ToLower() == userModel.Name.ToLower());
                                if (user != null)
                                {
                                    user.Email    = userModel.Email.ToLower();
                                    user.Password = userModel.Password.ToLower();
                                    user.UserKey  = getHashSum(string.Format("{0}{1}", userModel.Name.ToLower(), userModel.Password.ToLower()));

                                    result.SetDataResult(userModel.Name);
                                }
                                else
                                {
                                    Logger.LogException(string.Format("Can`t find user by UserKey, Name: {0}, UserKey: {1}", userModel.Name, userKey), LogTypeEnum.BAL);
                                    result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
                                }
                            }
                            else
                            {
                                Logger.LogException(string.Format("Can`t find UserKey in cookies, Name: {0}", userModel.Name), LogTypeEnum.BAL);
                                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
                            }
                        }
                        else
                        {
                            Logger.LogException(string.Format("Can`t find UserKey in cookies, Name: {0}", userModel.Name), LogTypeEnum.BAL);
                            result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
                        }
                    }
                    else
                    {
                        var userNameExists = db.GetEntity <User>(el => el.IsActive && el.Name.ToLower() == userModel.Name.ToLower());
                        if (userNameExists != null)
                        {
                            result.SetErrorResultCode(SettingService.GetSetting("LOGIN_UserNameAlreadyExists"));
                        }
                        else
                        {
                            user.Name     = userModel.Name;
                            user.Email    = userModel.Email;
                            user.Password = userModel.Password.ToLower();
                            user.IsActive = true;
                            user.UserKey  = getHashSum(string.Format("{0}{1}", userModel.Name.ToLower(), userModel.Password.ToLower()));
                            user.Type     = UserTypeEnum.User;

                            db.AddEntity(user);
                        }

                        result.SetDataResult(userModel.Name);
                    }

                    if (!string.IsNullOrEmpty(result.DataResult))
                    {
                        var cookie = new CookieHeaderValue(DEFAULT_COOKIE_NAME, user.UserKey)
                        {
                            Domain = request.RequestUri.Host,
                            Path   = "/"
                        };

                        if (userModel.CreateCookie.HasValue && userModel.CreateCookie.Value)
                        {
                            cookie.Expires = DateTimeOffset.UtcNow.AddDays(COOKIE_EXPIRE_DAYS);
                        }

                        responce.Headers.AddCookies(new[] { cookie });
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.LogException(ex, LogTypeEnum.BAL);
                result.SetErrorResultCode(SettingService.GetUserFriendlyExceptionMessage());
            }

            return(result);
        }