Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
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);
        }
        public void ProcessAllActiveDataSources()
        {
            using (var db = new RssAggregatorModelContainer(true))
            {
                if (_systemTemplates == null)
                {
                    _systemTemplates = db.GetDBSet <Template>(el => el.Type == TemplateTypeEnum.System && el.Version == null).ToList();
                }

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

                foreach (var dataSource in db.GetDBSet <DataSources>(el => el.IsActive && el.IsNewsSource))
                {
                    try
                    {
                        var expectedFactory = ParcerProviderFactory.GetFactory(dataSource.Type);
                        var contetn         = expectedFactory.GetContent(new Uri(dataSource.Uri));
                        contetn.Wait();

                        foreach (var post in contetn.Result)
                        {
                            if (post.PostContent.Any())
                            {
                                try
                                {
                                    var postStringContetnt = new List <string>();

                                    var postId = db.GetPostTransliteratedName(db.GetTransliteration(post.PostId), user.Name);

                                    var newPost = new News()
                                    {
                                        PostId               = postId,
                                        AuthorName           = post.AuthorName,
                                        AuthorId             = post.AuthorId,
                                        AuthorLink           = string.Format("{0}/{1}", dataSource.BaseUri.TrimEnd(new[] { '/' }), post.AuthorLink.TrimStart(new[] { '/' })),
                                        PostLikes            = post.PostLikes,
                                        PostName             = post.PostName,
                                        PostLink             = null,//postId,
                                        PostTags             = post.PostTags.Aggregate(string.Empty, (agg, el) => agg + ", " + el).TrimStart(new[] { ',', ' ' }),
                                        External             = true,
                                        IsActive             = true,
                                        AdultContent         = false,
                                        DataSource           = dataSource,
                                        CreationDateTime     = DateTime.UtcNow,
                                        ModificationDateTime = DateTime.UtcNow,
                                        User     = user,
                                        Location = dataSource.Location
                                    };

                                    foreach (var postContent in post.PostContent.OrderBy(el => el.PostOrder))
                                    {
                                        switch (postContent.PostContentType)
                                        {
                                        case PostContentTypeEnum.Text:
                                            var textPostContainer = _systemTemplates.First(el => el.Name.ToLower() == TEXT_TEMPLATE_NAME.ToLower());
                                            foreach (var el in postContent.PostContent)
                                            {
                                                postStringContetnt.Add(textPostContainer.View.Replace(CONTENT_VALUE_PLACEHOLDER, el));
                                            }
                                            break;

                                        case PostContentTypeEnum.Img:
                                            if (postContent.PostContent.Any())
                                            {
                                                var imgPostContainer    = _systemTemplates.First(el => el.Name.ToLower() == IMG_TEMPLATE_NAME.ToLower());
                                                var imgGalleryContainer = _systemTemplates.First(el => el.Name.ToLower() == IMG_GALLERY_TEMPLATE_NAME.ToLower());

                                                var imgPostContainers = string.Empty;
                                                foreach (var el in postContent.PostContent)
                                                {
                                                    imgPostContainers += imgPostContainer.View.Replace(CONTENT_VALUE_PLACEHOLDER, el);
                                                }

                                                postStringContetnt.Add(imgGalleryContainer.View.Replace(CONTENT_VALUE_PLACEHOLDER, imgPostContainers));
                                            }
                                            break;

                                        case PostContentTypeEnum.Audio:
                                            var audioPostContainer = _systemTemplates.First(el => el.Name.ToLower() == AUDIO_TEMPLATE_NAME.ToLower());
                                            foreach (var el in ((BasePostContentModel <AudioPostContentContainerModel>)postContent).PostSpecificContent)
                                            {
                                                postStringContetnt.Add(audioPostContainer.View.Replace(CONTENT_VALUE_PLACEHOLDER, el.Link)
                                                                       .Replace(CONTENT_MEDIA_NAME_PLACEHOLDER, el.Name)
                                                                       .Replace(CONTENT_MEDIA_AUTHOR_PLACEHOLDER, el.Artist));
                                            }
                                            break;

                                        case PostContentTypeEnum.Video:
                                            if (postContent.PostContent.Any())
                                            {
                                                var videoPostContainer    = _systemTemplates.First(el => el.Name.ToLower() == VIDEO_TEMPLATE_NAME.ToLower());
                                                var videoGalleryContainer = _systemTemplates.First(el => el.Name.ToLower() == VIDEO_GALLERY_TEMPLATE_NAME.ToLower());

                                                var videoPostContainers = string.Empty;
                                                foreach (var el in ((BasePostContentModel <VideoPostContentContainerModel>)postContent).PostSpecificContent)
                                                {
                                                    videoPostContainers += videoPostContainer.View.Replace(CONTENT_VALUE_PLACEHOLDER, el.Data)
                                                                           .Replace(CONTENT_MEDIA_NAME_PLACEHOLDER, el.Name)
                                                                           .Replace(CONTENT_MEDIA_PREVIEW_PLACEHOLDER, el.ImagePreviewLink);
                                                }

                                                postStringContetnt.Add(videoGalleryContainer.View.Replace(CONTENT_VALUE_PLACEHOLDER, videoPostContainers));
                                            }
                                            break;
                                        }

                                        newPost.PostTags += string.Format(", {0}", db.GetTagName((TagTypeEnum)postContent.PostContentType, newPost.Location));
                                    }

                                    newPost.PostContent = postStringContetnt.Aggregate(string.Empty, (agg, el) => agg + '\n' + el);

                                    db.AddEntity(newPost);
                                }
                                catch (Exception ex)
                                {
                                    Logger.LogException(ex, LogTypeEnum.CORE, string.Format("DataSourceProcessing for {0} factory failed to process post: {1}", dataSource.Type, post.PostId));
                                }
                            }
                            else
                            {
                                Logger.LogException(string.Format("DataSourceProcessing for {0} factory has empty content for the post: {1}", dataSource.Type, post.PostId), LogTypeEnum.CORE);
                            }
                        }

                        if (dataSource.PostAmountPerIteration.HasValue)
                        {
                            if (contetn.Result.Count() != dataSource.PostAmountPerIteration.Value)
                            {
                                Logger.LogException(string.Format("DataSourceProcessing for {0} factory returned incorrect expected amount of post, expected {1}, got {2}", dataSource.Type, dataSource.PostAmountPerIteration.Value, contetn.Result.Count()), LogTypeEnum.CORE);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        Logger.LogException(ex, LogTypeEnum.CORE, string.Format("DataSourceProcessing for {0} factory failed", dataSource.Type));
                    }
                }
            }
        }