> For the complete documentation index, see [llms.txt](https://docs.zongsoft.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zongsoft.com/documentation/en/framework/data/writing.md).

# Write Operations

Illustrate real writing and its side effects with post voting, topic moderation, and browsing history.

Data writing changes records and may affect statistics, audit status, and file storage. Discussions Place these rules in a business service; callers should prefer using service actions rather than modifying tables directly from the controller.

## Create a New Model and Write a Vote

Source: [src/Services/PostService.cs](https://github.com/Zongsoft/Zongsoft.Discussions/blob/main/src/Services/PostService.cs#L50) (excerpt; see source for context).

{% code title="PostService.cs" %}

```csharp
public bool Upvote(ulong postId, byte value = 1)
{
	if(value == 0)
		value = 1;

	var userId = this.Principal.Identity.GetIdentifier<uint>();

	using(var transaction = new Transaction())
	{
		this.DataAccess.Delete<Post.PostVoting>(
			Condition.Equal(nameof(Post.PostVoting.PostId), postId) &
			Condition.Equal(nameof(Post.PostVoting.UserId), userId));

		this.DataAccess.Insert(Model.Build<Post.PostVoting>(voting =>
		{
			voting.PostId = postId;
			voting.UserId = userId;
			voting.Value = (sbyte)Math.Min(value, (sbyte)100);
			voting.Timestamp = DateTime.Now;
		}));

		//如果帖子投票统计信息更新成功
		if(this.SetPostVotes(postId))
		{
			//提交事务
			transaction.Commit();

			//返回成功
			return true;
		}
	}

	return false;
}
```

{% endcode %}

The actual process first deletes the current user's old votes for this post, then creates a new record through Model.Build, and finally recalculates the number of positive and negative votes. The transaction is committed only if the statistics update is successful. Value is limited to 1 to 100 and counts the number of records rather than the sum of weights.

## Deletion Also Requires Business Scope

The above Delete specifies both PostId and UserId: what is deleted is the current user's old vote for the current post. The absence of any one of these conditions will increase the scope of the effect. The general Delete API cannot replace the authorization judgment of the current identity and target resources.

## Update Topic Status

Source: [src/Services/ThreadService.cs](https://github.com/Zongsoft/Zongsoft.Discussions/blob/main/src/Services/ThreadService.cs#L103) (excerpt; see source for context).

{% code title="ThreadService.cs" %}

```csharp
public bool SetLocked(ulong threadId, bool value)
{
	return this.DataAccess.Update<Models.Thread>(new
	{
		IsLocked = value,
	}, Condition.Equal(nameof(Models.Thread.ThreadId), threadId) & GetIsModeratorCriteria()) > 0;
}
```

{% endcode %}

Locking is an explicit business action, and moderator conditions along with topic numbers control updates. The meaning of the fields, whether the reply entry checks for locking, and interface feedback need to be checked according to the complete call chain; just setting IsLocked does not mean that all custom write entries will automatically block replies.

## Add or Update Browsing History

Source: [src/Services/ThreadService.cs](https://github.com/Zongsoft/Zongsoft.Discussions/blob/main/src/Services/ThreadService.cs#L283) (excerpt; see source for context).

{% code title="ThreadService.cs" %}

```csharp
private void SetHistory(ulong threadId)
{
	//新增或更新当前用户对指定主题的浏览记录（自动递增浏览次数）
	this.DataAccess.Upsert<History>(new
	{
		UserId = this.Principal.Identity.GetIdentifier<uint>(),
		ThreadId = threadId,
		ViewedCount = Operand.Field(nameof(History.ViewedCount)) + 1,
		MostRecentViewedTime = DateTime.Now,
	});
}
```

{% endcode %}

Upsert performs a new or update around the view record key, while incrementing ViewedCount. Whether the corresponding write expression is supported depends on the database driver; do not generalize one driver's SQL form to all databases.

## Content File and Database Are Not in the Same Transaction

Long text may be saved to a file first, and then the path is written to the database. Database rollback does not automatically delete object storage files, so related services use failure compensation. Production integration also requires exception checking, cancellation, retries, and old file cleanup. See [file system](/documentation/en/framework/core/io.md) and [affairs](/documentation/en/framework/data/transactions.md) for details.

Discussions does not have a business process that directly calls IDataAccess.Import; bulk import capabilities still require independent verification of site fields, permissions, and duplicate data policies.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.zongsoft.com/documentation/en/framework/data/writing.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
