public ActionResult <ApiResponse <ForumPostModel> > CreateForumPost([FromBody] CreateForumPostModel forumPost) { try { var onlineUser = this._userService.GetOnlineUser(this.HttpContext); if (onlineUser == null) { return(Json(ApiResponse.WithError("Not Authority"))); } ForumPostModel result = null; var newPost = new ForumPost(); newPost.Post = forumPost.Post; newPost.UserId = forumPost.UserId; this._forumPostService.AddNewForumPost(newPost); result = this._forumPostService.GetById(newPost.Id); return(Json(ApiResponse <ForumPostModel> .WithSuccess(result))); } catch (Exception exp) { return(Json(ApiResponse <ForumPostModel> .WithError(exp.ToString()))); } }
public List <ForumPostModel> GetForumPosts() { List <ForumPostModel> posts = new List <ForumPostModel>(); try { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand("SELECT * FROM forum;", conn); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { ForumPostModel post = new ForumPostModel(); post.display_name = Convert.ToString(reader["display_name"]); post.forum_post = Convert.ToString(reader["forum_post"]); post.post_date = Convert.ToDateTime(reader["post_date"]); posts.Add(post); } } } catch (SqlException ex) { throw; } return(posts); }
public void AddNewPost(ForumPostModel post) { try { using (SqlConnection conn = new SqlConnection(connectionString)) { conn.Open(); SqlCommand cmd = new SqlCommand("INSERT INTO forum (forum_post, display_name, post_date) VALUES (@forum_post, @display_name, @post_date);", conn); cmd.Parameters.AddWithValue("@display_name", post.display_name); cmd.Parameters.AddWithValue("@forum_post", post.forum_post); cmd.Parameters.AddWithValue("@post_date", post.post_date); cmd.ExecuteNonQuery(); } } catch (SqlException ex) { throw; } }
public virtual async Task <ForumTopicPageModel> PrepareForumTopicPage(ForumTopic forumTopic, int pageNumber) { var posts = await _forumService.GetAllPosts(forumTopic.Id, "", string.Empty, pageNumber - 1, _forumSettings.PostsPageSize); //prepare model var model = new ForumTopicPageModel(); model.Id = forumTopic.Id; model.Subject = forumTopic.Subject; model.SeName = forumTopic.GetSeName(); var currentcustomer = _workContext.CurrentCustomer; model.IsCustomerAllowedToEditTopic = _forumService.IsCustomerAllowedToEditTopic(currentcustomer, forumTopic); model.IsCustomerAllowedToDeleteTopic = _forumService.IsCustomerAllowedToDeleteTopic(currentcustomer, forumTopic); model.IsCustomerAllowedToMoveTopic = _forumService.IsCustomerAllowedToMoveTopic(currentcustomer, forumTopic); model.IsCustomerAllowedToSubscribe = _forumService.IsCustomerAllowedToSubscribe(currentcustomer); if (model.IsCustomerAllowedToSubscribe) { model.WatchTopicText = _localizationService.GetResource("Forum.WatchTopic"); var forumTopicSubscription = (await _forumService.GetAllSubscriptions(currentcustomer.Id, "", forumTopic.Id, 0, 1)).FirstOrDefault(); if (forumTopicSubscription != null) { model.WatchTopicText = _localizationService.GetResource("Forum.UnwatchTopic"); } } model.PostsPageIndex = posts.PageIndex; model.PostsPageSize = posts.PageSize; model.PostsTotalRecords = posts.TotalCount; foreach (var post in posts) { var customer = await _serviceProvider.GetRequiredService <ICustomerService>().GetCustomerById(post.CustomerId); var forumPostModel = new ForumPostModel { Id = post.Id, ForumTopicId = post.TopicId, ForumTopicSeName = forumTopic.GetSeName(), FormattedText = post.FormatPostText(), IsCurrentCustomerAllowedToEditPost = _forumService.IsCustomerAllowedToEditPost(currentcustomer, post), IsCurrentCustomerAllowedToDeletePost = _forumService.IsCustomerAllowedToDeletePost(currentcustomer, post), CustomerId = post.CustomerId, AllowViewingProfiles = _customerSettings.AllowViewingProfiles, CustomerName = customer.FormatUserName(_customerSettings.CustomerNameFormat), IsCustomerForumModerator = customer.IsForumModerator(), IsCustomerGuest = customer.IsGuest(), ShowCustomersPostCount = _forumSettings.ShowCustomersPostCount, ForumPostCount = customer.GetAttributeFromEntity <int>(SystemCustomerAttributeNames.ForumPostCount), ShowCustomersJoinDate = _customerSettings.ShowCustomersJoinDate, CustomerJoinDate = customer.CreatedOnUtc, AllowPrivateMessages = _forumSettings.AllowPrivateMessages, SignaturesEnabled = _forumSettings.SignaturesEnabled, FormattedSignature = (customer.GetAttributeFromEntity <string>(SystemCustomerAttributeNames.Signature)).FormatForumSignatureText(), }; //created on string if (_forumSettings.RelativeDateTimeFormattingEnabled) { forumPostModel.PostCreatedOnStr = post.CreatedOnUtc.ToString("f"); } else { forumPostModel.PostCreatedOnStr = _dateTimeHelper.ConvertToUserTime(post.CreatedOnUtc, DateTimeKind.Utc).ToString("f"); } //avatar if (_customerSettings.AllowCustomersToUploadAvatars) { forumPostModel.CustomerAvatarUrl = await _pictureService.GetPictureUrl( customer.GetAttributeFromEntity <string>(SystemCustomerAttributeNames.AvatarPictureId), _mediaSettings.AvatarPictureSize, _customerSettings.DefaultAvatarEnabled, defaultPictureType : PictureType.Avatar); } //location forumPostModel.ShowCustomersLocation = _customerSettings.ShowCustomersLocation; if (_customerSettings.ShowCustomersLocation) { var countryId = customer.GetAttributeFromEntity <string>(SystemCustomerAttributeNames.CountryId); var country = await _countryService.GetCountryById(countryId); forumPostModel.CustomerLocation = country != null?country.GetLocalized(x => x.Name, _workContext.WorkingLanguage.Id) : string.Empty; } if (_forumSettings.AllowPostVoting) { forumPostModel.AllowPostVoting = true; forumPostModel.VoteCount = post.VoteCount; var postVote = await _forumService.GetPostVote(post.Id, _workContext.CurrentCustomer.Id); if (postVote != null) { forumPostModel.VoteIsUp = postVote.IsUp; } } // page number is needed for creating post link in _ForumPost partial view forumPostModel.CurrentTopicPage = pageNumber; model.ForumPostModels.Add(forumPostModel); } return(model); }
/// <summary> /// Prepare the forum topic page model /// </summary> /// <param name="forumTopic">Forum topic</param> /// <param name="page">Number of forum posts page</param> /// <returns>Forum topic page model</returns> public virtual ForumTopicPageModel PrepareForumTopicPageModel(ForumTopic forumTopic, int page) { if (forumTopic == null) { throw new ArgumentNullException(nameof(forumTopic)); } //load posts var posts = _forumService.GetAllPosts(forumTopic.Id, 0, string.Empty, page - 1, _forumSettings.PostsPageSize); //prepare model var model = new ForumTopicPageModel { Id = forumTopic.Id, Subject = forumTopic.Subject, SeName = _forumService.GetTopicSeName(forumTopic), IsCustomerAllowedToEditTopic = _forumService.IsCustomerAllowedToEditTopic(_workContext.CurrentCustomer, forumTopic), IsCustomerAllowedToDeleteTopic = _forumService.IsCustomerAllowedToDeleteTopic(_workContext.CurrentCustomer, forumTopic), IsCustomerAllowedToMoveTopic = _forumService.IsCustomerAllowedToMoveTopic(_workContext.CurrentCustomer, forumTopic), IsCustomerAllowedToSubscribe = _forumService.IsCustomerAllowedToSubscribe(_workContext.CurrentCustomer) }; if (model.IsCustomerAllowedToSubscribe) { model.WatchTopicText = _localizationService.GetResource("Forum.WatchTopic"); var forumTopicSubscription = _forumService.GetAllSubscriptions(_workContext.CurrentCustomer.Id, 0, forumTopic.Id, 0, 1).FirstOrDefault(); if (forumTopicSubscription != null) { model.WatchTopicText = _localizationService.GetResource("Forum.UnwatchTopic"); } } model.PostsPageIndex = posts.PageIndex; model.PostsPageSize = posts.PageSize; model.PostsTotalRecords = posts.TotalCount; foreach (var post in posts) { var forumPostModel = new ForumPostModel { Id = post.Id, ForumTopicId = post.TopicId, ForumTopicSeName = _forumService.GetTopicSeName(forumTopic), FormattedText = _forumService.FormatPostText(post), IsCurrentCustomerAllowedToEditPost = _forumService.IsCustomerAllowedToEditPost(_workContext.CurrentCustomer, post), IsCurrentCustomerAllowedToDeletePost = _forumService.IsCustomerAllowedToDeletePost(_workContext.CurrentCustomer, post), CustomerId = post.CustomerId, AllowViewingProfiles = _customerSettings.AllowViewingProfiles && !post.Customer.IsGuest(), CustomerName = _customerService.FormatUserName(post.Customer), IsCustomerForumModerator = post.Customer.IsForumModerator(), ShowCustomersPostCount = _forumSettings.ShowCustomersPostCount, ForumPostCount = _genericAttributeService.GetAttribute <int>(post.Customer, GSCustomerDefaults.ForumPostCountAttribute), ShowCustomersJoinDate = _customerSettings.ShowCustomersJoinDate && !post.Customer.IsGuest(), CustomerJoinDate = post.Customer.CreatedOnUtc, AllowPrivateMessages = _forumSettings.AllowPrivateMessages && !post.Customer.IsGuest(), SignaturesEnabled = _forumSettings.SignaturesEnabled, FormattedSignature = _forumService.FormatForumSignatureText(_genericAttributeService.GetAttribute <string>(post.Customer, GSCustomerDefaults.SignatureAttribute)), }; //created on string if (_forumSettings.RelativeDateTimeFormattingEnabled) { forumPostModel.PostCreatedOnStr = post.CreatedOnUtc.RelativeFormat(true, "f"); } else { forumPostModel.PostCreatedOnStr = _dateTimeHelper.ConvertToUserTime(post.CreatedOnUtc, DateTimeKind.Utc).ToString("f"); } //avatar if (_customerSettings.AllowCustomersToUploadAvatars) { forumPostModel.CustomerAvatarUrl = _pictureService.GetPictureUrl( _genericAttributeService.GetAttribute <int>(post.Customer, GSCustomerDefaults.AvatarPictureIdAttribute), _mediaSettings.AvatarPictureSize, _customerSettings.DefaultAvatarEnabled, defaultPictureType: PictureType.Avatar); } //location forumPostModel.ShowCustomersLocation = _customerSettings.ShowCustomersLocation && !post.Customer.IsGuest(); if (_customerSettings.ShowCustomersLocation) { var countryId = _genericAttributeService.GetAttribute <int>(post.Customer, GSCustomerDefaults.CountryIdAttribute); var country = _countryService.GetCountryById(countryId); forumPostModel.CustomerLocation = country != null?_localizationService.GetLocalized(country, x => x.Name) : string.Empty; } //votes if (_forumSettings.AllowPostVoting) { forumPostModel.AllowPostVoting = true; forumPostModel.VoteCount = post.VoteCount; var postVote = _forumService.GetPostVote(post.Id, _workContext.CurrentCustomer); if (postVote != null) { forumPostModel.VoteIsUp = postVote.IsUp; } } // page number is needed for creating post link in _ForumPost partial view forumPostModel.CurrentTopicPage = page; model.ForumPostModels.Add(forumPostModel); } return(model); }
/// <summary> /// Prepare the forum topic page model /// </summary> /// <param name="forumTopic">Forum topic</param> /// <param name="page">Number of forum posts page</param> /// <returns> /// A task that represents the asynchronous operation /// The task result contains the forum topic page model /// </returns> public virtual async Task <ForumTopicPageModel> PrepareForumTopicPageModelAsync(ForumTopic forumTopic, int page) { if (forumTopic == null) { throw new ArgumentNullException(nameof(forumTopic)); } //load posts var posts = await _forumService.GetAllPostsAsync(forumTopic.Id, 0, string.Empty, page - 1, _forumSettings.PostsPageSize); //prepare model var model = new ForumTopicPageModel { Id = forumTopic.Id, Subject = forumTopic.Subject, SeName = await _forumService.GetTopicSeNameAsync(forumTopic), IsCustomerAllowedToEditTopic = await _forumService.IsCustomerAllowedToEditTopicAsync(await _workContext.GetCurrentCustomerAsync(), forumTopic), IsCustomerAllowedToDeleteTopic = await _forumService.IsCustomerAllowedToDeleteTopicAsync(await _workContext.GetCurrentCustomerAsync(), forumTopic), IsCustomerAllowedToMoveTopic = await _forumService.IsCustomerAllowedToMoveTopicAsync(await _workContext.GetCurrentCustomerAsync(), forumTopic), IsCustomerAllowedToSubscribe = await _forumService.IsCustomerAllowedToSubscribeAsync(await _workContext.GetCurrentCustomerAsync()) }; if (model.IsCustomerAllowedToSubscribe) { model.WatchTopicText = await _localizationService.GetResourceAsync("Forum.WatchTopic"); var forumTopicSubscription = (await _forumService.GetAllSubscriptionsAsync((await _workContext.GetCurrentCustomerAsync()).Id, 0, forumTopic.Id, 0, 1)).FirstOrDefault(); if (forumTopicSubscription != null) { model.WatchTopicText = await _localizationService.GetResourceAsync("Forum.UnwatchTopic"); } } model.PostsPageIndex = posts.PageIndex; model.PostsPageSize = posts.PageSize; model.PostsTotalRecords = posts.TotalCount; foreach (var post in posts) { var customer = await _customerService.GetCustomerByIdAsync(post.CustomerId); var customerIsGuest = await _customerService.IsGuestAsync(customer); var customerIsModerator = customerIsGuest ? false : await _customerService.IsForumModeratorAsync(customer); var forumPostModel = new ForumPostModel { Id = post.Id, ForumTopicId = post.TopicId, ForumTopicSeName = await _forumService.GetTopicSeNameAsync(forumTopic), FormattedText = _forumService.FormatPostText(post), IsCurrentCustomerAllowedToEditPost = await _forumService.IsCustomerAllowedToEditPostAsync(await _workContext.GetCurrentCustomerAsync(), post), IsCurrentCustomerAllowedToDeletePost = await _forumService.IsCustomerAllowedToDeletePostAsync(await _workContext.GetCurrentCustomerAsync(), post), CustomerId = post.CustomerId, AllowViewingProfiles = _customerSettings.AllowViewingProfiles && !customerIsGuest, CustomerName = await _customerService.FormatUsernameAsync(customer), IsCustomerForumModerator = customerIsModerator, ShowCustomersPostCount = _forumSettings.ShowCustomersPostCount, ForumPostCount = await _genericAttributeService.GetAttributeAsync <Customer, int>(post.CustomerId, NopCustomerDefaults.ForumPostCountAttribute), ShowCustomersJoinDate = _customerSettings.ShowCustomersJoinDate && !customerIsGuest, CustomerJoinDate = customer?.CreatedOnUtc ?? DateTime.Now, AllowPrivateMessages = _forumSettings.AllowPrivateMessages && !customerIsGuest, SignaturesEnabled = _forumSettings.SignaturesEnabled, FormattedSignature = _forumService.FormatForumSignatureText(await _genericAttributeService.GetAttributeAsync <Customer, string>(post.CustomerId, NopCustomerDefaults.SignatureAttribute)), }; //created on string var languageCode = (await _workContext.GetWorkingLanguageAsync()).LanguageCulture; if (_forumSettings.RelativeDateTimeFormattingEnabled) { var postCreatedAgo = post.CreatedOnUtc.RelativeFormat(languageCode); forumPostModel.PostCreatedOnStr = string.Format(await _localizationService.GetResourceAsync("Common.RelativeDateTime.Past"), postCreatedAgo); } else { forumPostModel.PostCreatedOnStr = (await _dateTimeHelper.ConvertToUserTimeAsync(post.CreatedOnUtc, DateTimeKind.Utc)).ToString("f"); } //avatar if (_customerSettings.AllowCustomersToUploadAvatars) { forumPostModel.CustomerAvatarUrl = await _pictureService.GetPictureUrlAsync( await _genericAttributeService.GetAttributeAsync <Customer, int>(post.CustomerId, NopCustomerDefaults.AvatarPictureIdAttribute), _mediaSettings.AvatarPictureSize, _customerSettings.DefaultAvatarEnabled, defaultPictureType : PictureType.Avatar); } //location forumPostModel.ShowCustomersLocation = _customerSettings.ShowCustomersLocation && !customerIsGuest; if (_customerSettings.ShowCustomersLocation) { var countryId = await _genericAttributeService.GetAttributeAsync <Customer, int>(post.CustomerId, NopCustomerDefaults.CountryIdAttribute); var country = await _countryService.GetCountryByIdAsync(countryId); forumPostModel.CustomerLocation = country != null ? await _localizationService.GetLocalizedAsync(country, x => x.Name) : string.Empty; } //votes if (_forumSettings.AllowPostVoting) { forumPostModel.AllowPostVoting = true; forumPostModel.VoteCount = post.VoteCount; var postVote = await _forumService.GetPostVoteAsync(post.Id, await _workContext.GetCurrentCustomerAsync()); if (postVote != null) { forumPostModel.VoteIsUp = postVote.IsUp; } } // page number is needed for creating post link in _ForumPost partial view forumPostModel.CurrentTopicPage = page; model.ForumPostModels.Add(forumPostModel); } return(model); }
public ActionResult NewPost(ForumPostModel post, ForumDAL dal) { dal.AddNewPost(post); return(RedirectToAction("Forum")); }