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); }
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); }