AddPost

此博客介绍了AddPost方法,该方法用于添加新帖子,会检查是否允许重复帖子,若不允许且用户尝试添加重复帖子会抛出异常。方法接收一个包含帖子信息的对象,通过SQL命令将帖子信息插入数据库,最后返回新插入帖子的信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ContractedBlock.gifExpandedBlockStart.gifAddPost
ExpandedBlockStart.gifContractedBlock.gif        /**//// <summary>
InBlock.gif        
/// Adds a new Post.  This method checks the allowDuplicatePosts settings to determine whether
InBlock.gif        
/// or not to allow for duplicate posts.  If allowDuplicatePosts is set to false and the user
InBlock.gif        
/// attempts to enter a duplicate post, a PostDuplicateException exception is thrown.
InBlock.gif        
/// </summary>
InBlock.gif        
/// <param name="PostToAdd">A Post object containing the information needed to add a new
InBlock.gif        
/// post.  The essential fields of the Post class that must be set are: the Subject, the
InBlock.gif        
/// Body, the Username, and a ForumID or a ParentID (depending on whether the post to add is
InBlock.gif        
/// a new post or a reply to an existing post, respectively).</param>
InBlock.gif        
/// <returns>A Post object with information on the newly inserted post.  This returned Post
InBlock.gif        
/// object includes the ID of the newly added Post (PostID) as well as if the Post is
ExpandedBlockEnd.gif        
/// Approved or not.</returns>

None.gif        public override ForumPost AddPost (ForumPost post, int userID, bool autoApprove) 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            
int postID = -1;
InBlock.gif
InBlock.gif            
// Create Instance of Connection and Command Object
InBlock.gif            
//
InBlock.gif
            using( SqlConnection myConnection = GetSqlConnection() ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                SqlCommand myCommand 
= new SqlCommand(databaseOwner + ".cs_Post_CreateUpdate", myConnection);
InBlock.gif                myCommand.CommandType 
= CommandType.StoredProcedure;
InBlock.gif
InBlock.gif                
// Add parameters
InBlock.gif                
//
InBlock.gif
                myCommand.Parameters.Add(this.SettingsIDParameter());
InBlock.gif                myCommand.Parameters.Add(
"@SectionID", SqlDbType.Int).Value = post.SectionID;
InBlock.gif        
//3    //论坛板块
InBlock.gif
                myCommand.Parameters.Add("@ParentID", SqlDbType.Int).Value = post.ParentID;
InBlock.gif        
//0    //
InBlock.gif
                myCommand.Parameters.Add("@AllowDuplicatePosts", SqlDbType.Bit).Value = sqlHelper.GetSiteSettings().EnableDuplicatePosts;
InBlock.gif        
//false
InBlock.gif
                myCommand.Parameters.Add("@DuplicateIntervalInMinutes", SqlDbType.Int).Value = sqlHelper.GetSiteSettings().DuplicatePostIntervalInMinutes;
InBlock.gif        
//15
InBlock.gif
                myCommand.Parameters.Add("@Subject", SqlDbType.NVarChar, 256).Value = post.Subject;
InBlock.gif        
//论坛跟踪,发表新贴
InBlock.gif
                myCommand.Parameters.Add("@IsLocked", SqlDbType.Bit).Value = post.IsLocked;
InBlock.gif        
//false
InBlock.gif
                myCommand.Parameters.Add("@IsTracked", SqlDbType.Bit).Value = post.IsTracked;
InBlock.gif        
//false
InBlock.gif
                myCommand.Parameters.Add("@PostType", SqlDbType.Int).Value = post.PostType;
InBlock.gif        
//HTML
InBlock.gif
                myCommand.Parameters.Add("@EmoticonID", SqlDbType.Int).Value = post.EmoticonID;
InBlock.gif        
//0
InBlock.gif
                myCommand.Parameters.Add("@UserID", SqlDbType.Int).Value = userID;
InBlock.gif        
//2105
InBlock.gif
                myCommand.Parameters.Add("@Body", SqlDbType.NText).Value = post.Body;
InBlock.gif        
//test
InBlock.gif
                myCommand.Parameters.Add("@FormattedBody", SqlDbType.NText).Value = post.FormattedBody;
InBlock.gif        
//test
InBlock.gif
                myCommand.Parameters.Add("@UserHostAddress", SqlDbType.NVarChar, 32).Value = post.UserHostAddress;
InBlock.gif        
//127.0.0.1
InBlock.gif
                myCommand.Parameters.Add("@PostID", SqlDbType.Int).Direction = ParameterDirection.Output;
InBlock.gif        
//    //output参数
InBlock.gif
                if( userID == 0 ) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    myCommand.Parameters.Add(
"@PostAuthor", SqlDbType.NVarChar, 128).Value = post.Username;
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
if (post is Thread) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    myCommand.Parameters.Add(
"@IsSticky", SqlDbType.Bit).Value = ((Thread) post).IsSticky;
InBlock.gif            
//false
InBlock.gif
                    myCommand.Parameters.Add("@StickyDate", SqlDbType.DateTime).Value = ((Thread) post).StickyDate;
InBlock.gif            
//10/18/1980
ExpandedSubBlockEnd.gif
                }

InBlock.gif
InBlock.gif                SerializerData data 
= post.GetSerializerData();
InBlock.gif
InBlock.gif                myCommand.Parameters.Add(
"@PropertyNames", SqlDbType.NText).Value = data.Keys;
InBlock.gif        
//null
InBlock.gif
                myCommand.Parameters.Add("@PropertyValues", SqlDbType.NText).Value = data.Values;
InBlock.gif        
//null
InBlock.gif
InBlock.gif                
// If autoApprove is true, mark it as approved
InBlock.gif
                if(autoApprove)
InBlock.gif                    myCommand.Parameters.Add(
"@IsApproved", SqlDbType.Bit).Value = true;
InBlock.gif
InBlock.gif                myCommand.Parameters.Add(
"@PostConfiguration", SqlDbType.Int).Value = post.PostConfiguration;
InBlock.gif        
//0
InBlock.gif

InBlock.gif                myConnection.Open();
InBlock.gif                myCommand.ExecuteNonQuery();
InBlock.gif                myConnection.Close();
InBlock.gif
InBlock.gif                
// LN 5/27/04: try/catch added to get rid of exceptions
InBlock.gif
                try 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    postID 
= (int) myCommand.Parameters["@PostID"].Value;
ExpandedSubBlockEnd.gif                }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif                
catch dot.gif{}
InBlock.gif
InBlock.gif                
if (postID == -1
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
throw new CSException(CSExceptionType.PostDuplicate);
ExpandedSubBlockEnd.gif                }

InBlock.gif
InBlock.gif                
InBlock.gif            
InBlock.gif                
// Return the newly inserted Post
InBlock.gif                
//
InBlock.gif
                return GetPost(postID, userID, false);
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/konimeter/archive/2005/10/18/257105.html

import matrix4 from '@ohos.matrix4' import { promptAction, router } from '@kit.ArkUI'; import { NavBar } from '../component/NavBar'; import { getAlbum } from '../utils/SelectImage'; import { ForumPost, forumPostDao } from '../dataBase/ForumPostDao'; @Entry @Component struct AddPost { @State imageUrl: string = ''; @State context: string = '添加正文' @State tittle: string = '添加标题' private async addPost() { const userId = AppStorage.get("userId") as number; const now = new Date(); const formattedDate = `${now.getFullYear()}-${(now.getMonth() + 1).toString().padStart(2, '0')}-${now.getDate() .toString() .padStart(2, '0')} ${now.getHours().toString().padStart(2, '0')}:${now.getMinutes() .toString() .padStart(2, '0')}:${now.getSeconds().toString().padStart(2, '0')}`; const data: ForumPost = new ForumPost(1, this.tittle, this.context, this.imageUrl, userId, formattedDate); forumPostDao.insert(data).then(() => { router.back(); }); } build() { Column({ space: 10 }) { NavBar({ title: '发布帖子' }) Image(this.imageUrl === '' ? $r('app.media.addImage') : this.imageUrl) .size({ width: '95%', height: 300 }).onClick(async () => { const imageUrlData = await getAlbum(1); this.imageUrl = imageUrlData.imageUrl[0]; }) TextInput({ placeholder: this.tittle }).fontSize(16).borderRadius(5).onChange((value: string) => { this.tittle = value; }) TextArea({ text: this.context }) .placeholderFont({ size: 16, weight: 400 }) .width('100%') .height(200) .maxLength(200) .borderRadius(5) .fontSize(14) .showCounter(true, { thresholdPercentage: 50, highlightBorder: true }) .onChange((value: string) => { this.context = value }) Blank().layoutWeight(1) Button('发布笔记') .onClick(async (event: ClickEvent) => { this.addPost(); }) .backgroundColor('#ffff0000') .width('100%') .height(40) .alignSelf(ItemAlign.Center) }.width('100%').height('100%').alignItems(HorizontalAlign.Start).padding({ left: 10, right: 10 }) } }生成一个注释文档
06-08
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/extras/spring-security"> <head> <meta charset="UTF-8"> <title>管理列表</title> <link rel="stylesheet" type="text/css" href="/css/style1.css"> <link rel="stylesheet" type="text/css" href="/css/recipe.css"> <link rel="stylesheet" th:href="@{/css/bootstrap.css}"> <link rel="stylesheet" th:href="@{/css/AdminLTE.css}"> <script th:src="@{/js/jquery.min.js}"></script> <script th:src="@{/js/jquery.validate.min.js}"></script> <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/js/bootstrap.min.js"></script> </head> <body> <!--添加菜谱模态框--> <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> × </button> <h4 class="modal-title" id="myModalLabel">新增</h4> </div> <form id="form_data" th:action="@{/post/addPost}" method="post"> <div> 菜谱名称:<input type="text" id="post_id" name="name"/> 菜谱内容:<input type="text" id="post_content" name="content"/> 菜谱图片路径:<input type="text" id="post_img" name="img"> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">关闭 </button> <button type="submit" class="btn btn-primary" id="add"> 提交更改 </button> </div> </form> </div> </div> </div> <!--修改菜谱模态框--> <div class="modal fade" id="myUpdateModal" tabindex="-1" role="dialog" aria-labelledby="myUpdateModalLabel" aria-hidden="true"> <div class="modal-dialog"> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal" aria-hidden="true"> × </button> <h4 class="modal-title" id="myUpdateModalLabel">修改</h4> </div> <form id="form_update_data" th:action="@{/post/update}" method="post"> <div> 菜谱名称:<input type="text" id="post_update_id" name="name"/> 菜谱内容:<input type="text" id="post_update_content" name="content"/> 菜谱图片路径:<input type="text" id="post_update_img" name="img"> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">关闭 </button> <button type="submit" class="btn btn-primary" id="update"> 提交更改 </button> </div> </form> </div> </div> </div> <div class="recipe-container"> <div> <button type="button" class="btn btn-default" data-toggle="modal" data-target="#myModal">新增</button> </div> <div class="recipe-grid"> <div th:each="post : ${posts}" class="recipe-card"> <div class="post-title" th:text="${post.title}">标题</div> <div class="post-content" th:text="${post.content}">内容</div> <img th:src="@{'/image/' + ${post.imageUrls}}" alt="食谱图片"> <div class="recipe-steps" th:if="${post.recipeSteps}"> <h4>烹饪步骤:</h4> <ol> <li th:each="step : ${#strings.arraySplit(post.recipeSteps, ',')}" th:text="${step}">步骤</li> </ol> </div> <div> <button type="button" class="btn btn-default" data-toggle="modal" data-target="#myUpdateModal">修改</button> </div> </div> </div> </div> </body> </html>查错
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值