# Post（帖子管理）

## **GET /api/Post/Posts**

* **描述**: 获取帖子列表。
* **参数**:
  * `page` (integer, optional, default=1): 页码
  * `pageSize` (integer, optional, default=10): 每页大小
* **响应**: 200 OK

请求示例

```
//api/Post/Posts?page=1&pageSize=10
```

示例代码

```javascript
const myHeaders = new Headers();
myHeaders.append("Authorization", "<API Key>");

const requestOptions = {
  method: "GET",
  headers: myHeaders,
  redirect: "follow"
};

fetch("//api/Post/Posts?page=1&pageSize=10", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.error(error));
```

返回示例

```json
{
  "success": true,
  "posts": [
    {
      "id": 4333,
      "title": "第一个帖子",
      "nickname": "admin imarket",
      "avatar": "/images/defaultAvatar.png",
      "content": "这是第一篇帖子",
      "favoriteNums": 0,
      "likeNums": 0,
      "createdAt": "2025-01-22T14:09:56"
    }
  ]
}
```

***

## **GET /api/Post/{id}**

* **描述**: 获取指定帖子详情。
* **参数**:
  * `id` (string, required): 帖子ID
* **响应**: 200 OK

请求示例

```
//api/Post/:id
```

示例代码

```javascript
const myHeaders = new Headers();
myHeaders.append("Authorization", "<API Key>");

const requestOptions = {
  method: "GET",
  headers: myHeaders,
  redirect: "follow"
};

fetch("//api/Post/:id", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.error(error));
```

返回示例

```json
{
  "success": true,
  "post": {
    "id": 1,
    "title": "第一个帖子",
    "content": "这是第一篇帖子",
    "images": [
      {
        "id": 555,
        "url": "/images/17a2de2230f94dbd9ac19bd556763e22.png",
        "postId": 423,
        "createdAt": "2025-01-22T14:09:56"
      }
    ],
    "status": 0,
    "categoryID": 1,
    "likeNums": 0,
    "favoriteNms": 0,
    "isLiked": false,
    "isFavorite": false,
    "createdAt": "2025-01-22T14:09:56",
    "username": "admin",
    "nickname": "admin imarket",
    "avatar": "/images/defaultAvatar.png"
  }
}
```

***

## **GET /api/Post/Categories**

* **描述**: 获取分类列表。
* **响应**: 200 OK

请求示例

```
//api/Post/Categories
```

示例代码

```javascript
const myHeaders = new Headers();
myHeaders.append("Authorization", "<API Key>");

const requestOptions = {
  method: "GET",
  headers: myHeaders,
  redirect: "follow"
};

fetch("//api/Post/Categories", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.error(error));
```

返回示例

```json
{
  "success": true,
  "categories": [
    {
      "id": 1,
      "name": "广场",
      "description": "这里是广场，随便说说"
    }
  ]
}
```

***

## **GET** api/Post/Categories/:id

* **描述**: 根据分类 ID 获取分类信息。
* **路径参数**:
  * `id` (string, required): 分类的唯一标识符。
* **响应**:
  * **200 OK**: 返回分类信息。
  * **404 Not Found**: 分类不存在。
* **请求示例**:

  javascript复制

  ```javascript
  const myHeaders = new Headers();
  myHeaders.append("Authorization", "<API Key>");

  const requestOptions = {
    method: "GET",
    headers: myHeaders,
    redirect: "follow"
  };

  fetch("/api/Post/Categories/12345", requestOptions)
    .then((response) => response.json())
    .then((result) => console.log(result))
    .catch((error) => console.error(error));
  ```
* **返回示例**:
  * 成功:

    json复制

    ```json
    {
      "success": true,
      "category": {
        "id": 1,
        "name": "技术",
        "description": "与技术相关的帖子",
        "createdAt": "2023-10-01T12:00:00Z"
      }
    }
    ```
  * 分类不存在:

    json复制

    ```json
    {
      "success": false,
      "message": "Category not found."
    }
    ```

#### **接口逻辑说明**

1. **获取分类信息**:
   * 根据路径参数 `id` 调用 `postCategoriesService.GetCategoryByIdAsync(id)` 获取分类信息。
   * 如果分类不存在，返回 `404 Not Found`。
2. **返回结果**:
   * 如果分类存在，返回 `200 OK` 和分类信息。

***

## **GET /api/Post/CategorisedPosts**

* **描述**: 获取指定分类的帖子。
* **参数**:
  * `categoryId` (string, required): 分类ID
  * `page` (integer, optional, default=1): 页码
  * `pageSize` (integer, optional, default=10): 每页大小
* **响应**: 200 OK

请求示例

```
//api/Post/CategorisedPosts?categoryId=<ulong>&page=1&pageSize=10
```

示例代码

```javascript
const myHeaders = new Headers();
myHeaders.append("Authorization", "<API Key>");

const requestOptions = {
  method: "GET",
  headers: myHeaders,
  redirect: "follow"
};

fetch("//api/Post/CategorisedPosts?categoryId=<ulong>&page=1&pageSize=10", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.error(error));
```

返回示例

```json
{
  "success": true,
  "posts": [
    {
      "id": 1,
      "title": "第一个帖子",
      "nickname": "admin imarket",
      "avatar": "/images/defaultAvatar.png",
      "content": "这是第一篇帖子",
      "favoriteNums": 0,
      "likeNums": 0,
      "createdAt": "2025-01-22T14:09:56"
    }
  ]
}
```

***

## **POST /api/Post/Create**

* **描述**: 创建帖子。
* **请求体**:
  * `application/json`: `CreatePostRequest` 对象
  * `text/json`: `CreatePostRequest` 对象
  * `application/*+json`: `CreatePostRequest` 对象
* **响应**: 200 OK

请求示例

```json
{
  "categoryId": "<ulong>",
  "content": "<string>",
  "title": "<string>",
  "images": [
    "<string>",
    "<string>"
  ]
}
```

示例代码

{% hint style="info" %}
images参数为调用图片上传接口获得的url组成的字符串数组（可为空）
{% endhint %}

**详情查看图片上传api页面**

{% content-ref url="image-tu-pian-guan-li" %}
[image-tu-pian-guan-li](https://jeanhuas-organization.gitbook.io/imarket/api/image-tu-pian-guan-li)
{% endcontent-ref %}

```javascript
const myHeaders = new Headers();
myHeaders.append("Content-Type", "application/json");
myHeaders.append("Authorization", "<API Key>");

const raw = JSON.stringify({
  "categoryId": "<ulong>",
  "content": "<string>",
  "title": "<string>",
  "images": [
    "<string>",
    "<string>"
  ]
});

const requestOptions = {
  method: "POST",
  headers: myHeaders,
  body: raw,
  redirect: "follow"
};

fetch("//api/Post/Create", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.error(error));
```

返回示例

```json
{
  "success": true
}
```

***

## **GET /api/Post/Delete**

* **描述**: 删除帖子。
* **参数**:
  * `postId` (string, required): 帖子ID
* **响应**: 200 OK

请求示例

```
//api/Post/Delete?postId=<ulong>
```

示例代码

```javascript
const myHeaders = new Headers();
myHeaders.append("Authorization", "<API Key>");

const requestOptions = {
  method: "GET",
  headers: myHeaders,
  redirect: "follow"
};

fetch("//api/Post/Delete?postId=<ulong>", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.error(error));
```

返回示例

```json
{
  "success": true
}
```

***

## **GET /api/Post/Finish**

* **描述**: 标记帖子为完成。
* **参数**:
  * `postId` (string, required): 帖子ID
* **响应**: 200 OK

请求示例

```
//api/Post/Finish?postId=<ulong>
```

示例代码

```javascript
const myHeaders = new Headers();
myHeaders.append("Authorization", "<API Key>");

const requestOptions = {
  method: "GET",
  headers: myHeaders,
  redirect: "follow"
};

fetch("//api/Post/Finish?postId=<ulong>", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.error(error));
```

返回示例

```json
{
  "success": true
}
```

***

## **GET /api/Post/Favorite**

* **描述**: 收藏帖子。
* **参数**:
  * `postId` (string, required): 帖子ID
* **响应**: 200 OK

请求示例

```
//api/Post/Favorite?postId=<ulong>
```

示例代码

```javascript
const myHeaders = new Headers();
myHeaders.append("Authorization", "<API Key>");

const requestOptions = {
  method: "GET",
  headers: myHeaders,
  redirect: "follow"
};

fetch("//api/Post/Favorite?postId=<ulong>", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.error(error));
```

返回示例

```json
{
  "success": true
}
```

***

## **GET /api/Post/Unfavorite**

* **描述**: 取消收藏帖子。
* **参数**:
  * `postId` (string, required): 帖子ID
* **响应**: 200 OK

请求示例

```
//api/Post/Unfavorite?postId=<ulong>
```

示例代码

```javascript
const myHeaders = new Headers();
myHeaders.append("Authorization", "<API Key>");

const requestOptions = {
  method: "GET",
  headers: myHeaders,
  redirect: "follow"
};

fetch("//api/Post/Unfavorite?postId=<ulong>", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.error(error));
```

返回示例

```json
{
  "success": true
}
```

***

## **GET /api/Post/Like**

* **描述**: 点赞帖子。
* **参数**:
  * `postId` (string, required): 帖子ID
* **响应**: 200 OK

请求示例

```
//api/Post/Like?postId=<ulong>
```

示例代码

```javascript
const myHeaders = new Headers();
myHeaders.append("Authorization", "<API Key>");

const requestOptions = {
  method: "GET",
  headers: myHeaders,
  redirect: "follow"
};

fetch("//api/Post/Like?postId=<ulong>", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.error(error));
```

返回示例

```json
{
  "success": true
}
```

***

## **GET /api/Post/Unlike**

* **描述**: 取消点赞帖子。
* **参数**:
  * `postId` (string, required): 帖子ID
* **响应**: 200 OK

请求示例

```
//api/Post/Unlike?postId=<ulong>
```

示例代码

```javascript
const myHeaders = new Headers();
myHeaders.append("Authorization", "<API Key>");

const requestOptions = {
  method: "GET",
  headers: myHeaders,
  redirect: "follow"
};

fetch("//api/Post/Unlike?postId=<ulong>", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.error(error));
```

返回示例

```json
{
  "success": true
}
```

***

## **GET /api/Post/GetFavorites**

* **描述**: 获取收藏的帖子列表。
* **参数**:
  * `page` (integer, optional, default=1): 页码
  * `pageSize` (integer, optional, default=10): 每页大小
* **响应**: 200 OK

请求示例

```
//api/Post/GetFavorites?page=1&pageSize=10
```

示例代码

```javascript
const myHeaders = new Headers();
myHeaders.append("Authorization", "<API Key>");

const requestOptions = {
  method: "GET",
  headers: myHeaders,
  redirect: "follow"
};

fetch("//api/Post/GetFavorites?page=1&pageSize=10", requestOptions)
  .then((response) => response.text())
  .then((result) => console.log(result))
  .catch((error) => console.error(error));
```

返回示例

```json
{
  "success": true,
  "favorite": [
    {
      "postId": "2f729287-77ea-47df-97c9-341256a72252",
      "createdAt": "2025-01-22T16:46:59"
    }
  ]
}
```
