🎉 init: 小龙的工作空间
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -0,0 +1,7 @@
|
||||
node_modules/
|
||||
dist/
|
||||
data/
|
||||
.env
|
||||
*.db
|
||||
*.db-journal
|
||||
*.db-wal
|
||||
@@ -0,0 +1,118 @@
|
||||
# SocialApp — Backend API
|
||||
|
||||
> 一款以内容分享和用户互动为核心的社交应用。
|
||||
|
||||
## Tech Stack
|
||||
|
||||
- **Runtime**: Node.js
|
||||
- **Framework**: Fastify 5
|
||||
- **Language**: TypeScript
|
||||
- **Database**: SQLite (better-sqlite3)
|
||||
- **Auth**: JWT + bcrypt
|
||||
|
||||
## Getting Started
|
||||
|
||||
```bash
|
||||
# Install dependencies
|
||||
npm install
|
||||
|
||||
# Development (hot reload)
|
||||
npm run dev
|
||||
|
||||
# Build
|
||||
npm run build
|
||||
|
||||
# Production start
|
||||
npm run start
|
||||
|
||||
# Run tests
|
||||
npm test
|
||||
```
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
src/
|
||||
├── index.ts # Server entry point
|
||||
├── db/
|
||||
│ ├── schema.ts # SQLite schema
|
||||
│ └── client.ts # Database client
|
||||
├── routes/
|
||||
│ ├── auth.ts # Auth routes (register/login/me)
|
||||
│ └── *.ts # CRUD routes
|
||||
├── services/
|
||||
│ └── *.ts # Business logic
|
||||
├── middleware/
|
||||
│ └── auth.ts # JWT middleware
|
||||
├── types/
|
||||
│ └── index.ts # TypeScript types
|
||||
└── __tests__/
|
||||
└── *.test.ts # Tests
|
||||
```
|
||||
|
||||
## API Endpoints
|
||||
|
||||
### Auth
|
||||
- `POST /api/auth/register` — Register
|
||||
- `POST /api/auth/login` — Login
|
||||
- `GET /api/auth/me` — Current user (auth required)
|
||||
|
||||
### Resources
|
||||
#### contracts
|
||||
- `GET /api/contracts` — List all
|
||||
- `GET /api/contracts/:id` — Get by ID
|
||||
- `POST /api/contracts` — Create
|
||||
- `PUT /api/contracts/:id` — Update
|
||||
- `DELETE /api/contracts/:id` — Delete
|
||||
|
||||
#### approvals
|
||||
- `GET /api/approvals` — List all
|
||||
- `GET /api/approvals/:id` — Get by ID
|
||||
- `POST /api/approvals` — Create
|
||||
- `PUT /api/approvals/:id` — Update
|
||||
- `DELETE /api/approvals/:id` — Delete
|
||||
|
||||
#### customers
|
||||
- `GET /api/customers` — List all
|
||||
- `GET /api/customers/:id` — Get by ID
|
||||
- `POST /api/customers` — Create
|
||||
- `PUT /api/customers/:id` — Update
|
||||
- `DELETE /api/customers/:id` — Delete
|
||||
|
||||
#### reminders
|
||||
- `GET /api/reminders` — List all
|
||||
- `GET /api/reminders/:id` — Get by ID
|
||||
- `POST /api/reminders` — Create
|
||||
- `PUT /api/reminders/:id` — Update
|
||||
- `DELETE /api/reminders/:id` — Delete
|
||||
|
||||
#### articles
|
||||
- `GET /api/articles` — List all
|
||||
- `GET /api/articles/:id` — Get by ID
|
||||
- `POST /api/articles` — Create
|
||||
- `PUT /api/articles/:id` — Update
|
||||
- `DELETE /api/articles/:id` — Delete
|
||||
|
||||
#### tags
|
||||
- `GET /api/tags` — List all
|
||||
- `GET /api/tags/:id` — Get by ID
|
||||
- `POST /api/tags` — Create
|
||||
- `PUT /api/tags/:id` — Update
|
||||
- `DELETE /api/tags/:id` — Delete
|
||||
|
||||
#### comments
|
||||
- `GET /api/comments` — List all
|
||||
- `GET /api/comments/:id` — Get by ID
|
||||
- `POST /api/comments` — Create
|
||||
- `PUT /api/comments/:id` — Update
|
||||
- `DELETE /api/comments/:id` — Delete
|
||||
|
||||
#### media
|
||||
- `GET /api/media` — List all
|
||||
- `GET /api/media/:id` — Get by ID
|
||||
- `POST /api/media` — Create
|
||||
- `PUT /api/media/:id` — Update
|
||||
- `DELETE /api/media/:id` — Delete
|
||||
|
||||
### System
|
||||
- `GET /api/health` — Health check
|
||||
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "socialapp",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "tsx watch src/index.ts",
|
||||
"build": "tsc",
|
||||
"start": "node dist/index.js",
|
||||
"test": "node --import tsx --test src/__tests__/*.test.ts",
|
||||
"test:watch": "node --import tsx --test --watch src/__tests__/*.test.ts"
|
||||
},
|
||||
"dependencies": {
|
||||
"fastify": "^5.0.0",
|
||||
"@fastify/cors": "^10.0.0",
|
||||
"@fastify/jwt": "^9.0.0",
|
||||
"sql.js": "^1.12.0",
|
||||
"bcrypt": "^5.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.0.0",
|
||||
"@types/bcrypt": "^5.0.0",
|
||||
"typescript": "^5.6.0",
|
||||
"tsx": "^4.0.0",
|
||||
"pino-pretty": "^11.0.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
// Approval CRUD test
|
||||
import { describe, it, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { buildApp } from "../index.js";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
let app: FastifyInstance;
|
||||
let token: string;
|
||||
let createdId: string;
|
||||
let payload: Record<string, unknown>;
|
||||
|
||||
before(async () => {
|
||||
process.env.JWT_SECRET = "test-secret";
|
||||
process.env.DATABASE_URL = ":memory:";
|
||||
app = await buildApp();
|
||||
await app.ready();
|
||||
|
||||
// Register and login to get a token
|
||||
const regRes = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/auth/register",
|
||||
payload: { username: `test_${Date.now()}`, password: "password123" },
|
||||
});
|
||||
token = regRes.json().token;
|
||||
|
||||
// Resolve user FK references with the registered user's real ID
|
||||
payload = {
|
||||
"userId": regRes.json().user.id,
|
||||
"entityType": "sample-entitytype",
|
||||
"entityId": "sample-entityid",
|
||||
"applicantId": regRes.json().user.id,
|
||||
"status": "sample-status",
|
||||
"formData": "sample-formdata"
|
||||
};
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe("GET /api/approvals", () => {
|
||||
it("returns empty list", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: "/api/approvals",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.ok(Array.isArray(body.data));
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/approvals", () => {
|
||||
it("creates a approval", async () => {
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/approvals",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload,
|
||||
});
|
||||
assert.equal(res.statusCode, 201);
|
||||
const body = res.json();
|
||||
assert.ok(body.data.id);
|
||||
createdId = body.data.id;
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/approvals/:id", () => {
|
||||
it("returns the created approval", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/approvals/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.equal(body.data.id, createdId);
|
||||
});
|
||||
|
||||
it("returns 404 for nonexistent id", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/approvals/nonexistent`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PUT /api/approvals/:id", () => {
|
||||
it("updates the approval", async () => {
|
||||
const res = await app.inject({
|
||||
method: "PUT",
|
||||
url: `/api/approvals/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload,
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/approvals/:id", () => {
|
||||
it("deletes the approval", async () => {
|
||||
const res = await app.inject({
|
||||
method: "DELETE",
|
||||
url: `/api/approvals/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 204);
|
||||
});
|
||||
|
||||
it("returns 404 after delete", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/approvals/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,123 @@
|
||||
// Articles CRUD test
|
||||
import { describe, it, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { buildApp } from "../index.js";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
let app: FastifyInstance;
|
||||
let token: string;
|
||||
let createdId: string;
|
||||
let payload: Record<string, unknown>;
|
||||
|
||||
before(async () => {
|
||||
process.env.JWT_SECRET = "test-secret";
|
||||
process.env.DATABASE_URL = ":memory:";
|
||||
app = await buildApp();
|
||||
await app.ready();
|
||||
|
||||
// Register and login to get a token
|
||||
const regRes = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/auth/register",
|
||||
payload: { username: `test_${Date.now()}`, password: "password123" },
|
||||
});
|
||||
token = regRes.json().token;
|
||||
|
||||
// Resolve user FK references with the registered user's real ID
|
||||
payload = {
|
||||
"userId": regRes.json().user.id,
|
||||
"title": "sample-title",
|
||||
"content": "sample-content",
|
||||
"excerpt": "sample-excerpt",
|
||||
"coverUrl": "sample-coverurl",
|
||||
"authorId": "sample-authorid",
|
||||
"category": "sample-category",
|
||||
"publishedAt": "sample-publishedat"
|
||||
};
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe("GET /api/articles", () => {
|
||||
it("returns empty list", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: "/api/articles",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.ok(Array.isArray(body.data));
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/articles", () => {
|
||||
it("creates a article", async () => {
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/articles",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload,
|
||||
});
|
||||
assert.equal(res.statusCode, 201);
|
||||
const body = res.json();
|
||||
assert.ok(body.data.id);
|
||||
createdId = body.data.id;
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/articles/:id", () => {
|
||||
it("returns the created article", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/articles/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.equal(body.data.id, createdId);
|
||||
});
|
||||
|
||||
it("returns 404 for nonexistent id", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/articles/nonexistent`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PUT /api/articles/:id", () => {
|
||||
it("updates the article", async () => {
|
||||
const res = await app.inject({
|
||||
method: "PUT",
|
||||
url: `/api/articles/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload,
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/articles/:id", () => {
|
||||
it("deletes the article", async () => {
|
||||
const res = await app.inject({
|
||||
method: "DELETE",
|
||||
url: `/api/articles/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 204);
|
||||
});
|
||||
|
||||
it("returns 404 after delete", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/articles/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,96 @@
|
||||
// Auth routes test
|
||||
import { describe, it, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { buildApp } from "../index.js";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
let app: FastifyInstance;
|
||||
let token: string;
|
||||
|
||||
before(async () => {
|
||||
process.env.JWT_SECRET = "test-secret";
|
||||
process.env.DATABASE_URL = ":memory:";
|
||||
app = await buildApp();
|
||||
await app.ready();
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe("POST /api/auth/register", () => {
|
||||
it("registers a new user", async () => {
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/auth/register",
|
||||
payload: { username: "testuser", password: "password123" },
|
||||
});
|
||||
assert.equal(res.statusCode, 201);
|
||||
const body = res.json();
|
||||
assert.ok(body.token);
|
||||
assert.equal(body.user.username, "testuser");
|
||||
token = body.token;
|
||||
});
|
||||
|
||||
it("rejects duplicate username", async () => {
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/auth/register",
|
||||
payload: { username: "testuser", password: "password123" },
|
||||
});
|
||||
assert.equal(res.statusCode, 409);
|
||||
});
|
||||
|
||||
it("rejects short password", async () => {
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/auth/register",
|
||||
payload: { username: "user2", password: "123" },
|
||||
});
|
||||
assert.equal(res.statusCode, 400);
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/auth/login", () => {
|
||||
it("logs in with correct credentials", async () => {
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/auth/login",
|
||||
payload: { username: "testuser", password: "password123" },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.ok(body.token);
|
||||
token = body.token;
|
||||
});
|
||||
|
||||
it("rejects wrong password", async () => {
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/auth/login",
|
||||
payload: { username: "testuser", password: "wrongpassword" },
|
||||
});
|
||||
assert.equal(res.statusCode, 401);
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/auth/me", () => {
|
||||
it("returns current user with valid token", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: "/api/auth/me",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.equal(body.data.username, "testuser");
|
||||
});
|
||||
|
||||
it("rejects without token", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: "/api/auth/me",
|
||||
});
|
||||
assert.equal(res.statusCode, 401);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,120 @@
|
||||
// Comments CRUD test
|
||||
import { describe, it, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { buildApp } from "../index.js";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
let app: FastifyInstance;
|
||||
let token: string;
|
||||
let createdId: string;
|
||||
let payload: Record<string, unknown>;
|
||||
|
||||
before(async () => {
|
||||
process.env.JWT_SECRET = "test-secret";
|
||||
process.env.DATABASE_URL = ":memory:";
|
||||
app = await buildApp();
|
||||
await app.ready();
|
||||
|
||||
// Register and login to get a token
|
||||
const regRes = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/auth/register",
|
||||
payload: { username: `test_${Date.now()}`, password: "password123" },
|
||||
});
|
||||
token = regRes.json().token;
|
||||
|
||||
// Resolve user FK references with the registered user's real ID
|
||||
payload = {
|
||||
"userId": regRes.json().user.id,
|
||||
"entityType": "sample-entitytype",
|
||||
"entityId": "sample-entityid",
|
||||
"content": "sample-content",
|
||||
"parentId": "sample-parentid"
|
||||
};
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe("GET /api/comments", () => {
|
||||
it("returns empty list", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: "/api/comments",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.ok(Array.isArray(body.data));
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/comments", () => {
|
||||
it("creates a comment", async () => {
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/comments",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload,
|
||||
});
|
||||
assert.equal(res.statusCode, 201);
|
||||
const body = res.json();
|
||||
assert.ok(body.data.id);
|
||||
createdId = body.data.id;
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/comments/:id", () => {
|
||||
it("returns the created comment", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/comments/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.equal(body.data.id, createdId);
|
||||
});
|
||||
|
||||
it("returns 404 for nonexistent id", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/comments/nonexistent`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PUT /api/comments/:id", () => {
|
||||
it("updates the comment", async () => {
|
||||
const res = await app.inject({
|
||||
method: "PUT",
|
||||
url: `/api/comments/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload,
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/comments/:id", () => {
|
||||
it("deletes the comment", async () => {
|
||||
const res = await app.inject({
|
||||
method: "DELETE",
|
||||
url: `/api/comments/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 204);
|
||||
});
|
||||
|
||||
it("returns 404 after delete", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/comments/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,124 @@
|
||||
// Contract CRUD test
|
||||
import { describe, it, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { buildApp } from "../index.js";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
let app: FastifyInstance;
|
||||
let token: string;
|
||||
let createdId: string;
|
||||
let payload: Record<string, unknown>;
|
||||
|
||||
before(async () => {
|
||||
process.env.JWT_SECRET = "test-secret";
|
||||
process.env.DATABASE_URL = ":memory:";
|
||||
app = await buildApp();
|
||||
await app.ready();
|
||||
|
||||
// Register and login to get a token
|
||||
const regRes = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/auth/register",
|
||||
payload: { username: `test_${Date.now()}`, password: "password123" },
|
||||
});
|
||||
token = regRes.json().token;
|
||||
|
||||
// Resolve user FK references with the registered user's real ID
|
||||
payload = {
|
||||
"userId": regRes.json().user.id,
|
||||
"title": "sample-title",
|
||||
"partyA": "sample-partya",
|
||||
"partyB": "sample-partyb",
|
||||
"amount": 1,
|
||||
"signedAt": "sample-signedat",
|
||||
"expiresAt": "sample-expiresat",
|
||||
"status": "sample-status",
|
||||
"fileUrl": "sample-fileurl"
|
||||
};
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe("GET /api/contracts", () => {
|
||||
it("returns empty list", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: "/api/contracts",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.ok(Array.isArray(body.data));
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/contracts", () => {
|
||||
it("creates a contract", async () => {
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/contracts",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload,
|
||||
});
|
||||
assert.equal(res.statusCode, 201);
|
||||
const body = res.json();
|
||||
assert.ok(body.data.id);
|
||||
createdId = body.data.id;
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/contracts/:id", () => {
|
||||
it("returns the created contract", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/contracts/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.equal(body.data.id, createdId);
|
||||
});
|
||||
|
||||
it("returns 404 for nonexistent id", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/contracts/nonexistent`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PUT /api/contracts/:id", () => {
|
||||
it("updates the contract", async () => {
|
||||
const res = await app.inject({
|
||||
method: "PUT",
|
||||
url: `/api/contracts/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload,
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/contracts/:id", () => {
|
||||
it("deletes the contract", async () => {
|
||||
const res = await app.inject({
|
||||
method: "DELETE",
|
||||
url: `/api/contracts/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 204);
|
||||
});
|
||||
|
||||
it("returns 404 after delete", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/contracts/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,122 @@
|
||||
// Customer CRUD test
|
||||
import { describe, it, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { buildApp } from "../index.js";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
let app: FastifyInstance;
|
||||
let token: string;
|
||||
let createdId: string;
|
||||
let payload: Record<string, unknown>;
|
||||
|
||||
before(async () => {
|
||||
process.env.JWT_SECRET = "test-secret";
|
||||
process.env.DATABASE_URL = ":memory:";
|
||||
app = await buildApp();
|
||||
await app.ready();
|
||||
|
||||
// Register and login to get a token
|
||||
const regRes = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/auth/register",
|
||||
payload: { username: `test_${Date.now()}`, password: "password123" },
|
||||
});
|
||||
token = regRes.json().token;
|
||||
|
||||
// Resolve user FK references with the registered user's real ID
|
||||
payload = {
|
||||
"userId": regRes.json().user.id,
|
||||
"name": "sample-name",
|
||||
"email": "sample-email",
|
||||
"phone": "sample-phone",
|
||||
"company": "sample-company",
|
||||
"source": "sample-source",
|
||||
"tags": "sample-tags"
|
||||
};
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe("GET /api/customers", () => {
|
||||
it("returns empty list", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: "/api/customers",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.ok(Array.isArray(body.data));
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/customers", () => {
|
||||
it("creates a customer", async () => {
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/customers",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload,
|
||||
});
|
||||
assert.equal(res.statusCode, 201);
|
||||
const body = res.json();
|
||||
assert.ok(body.data.id);
|
||||
createdId = body.data.id;
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/customers/:id", () => {
|
||||
it("returns the created customer", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/customers/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.equal(body.data.id, createdId);
|
||||
});
|
||||
|
||||
it("returns 404 for nonexistent id", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/customers/nonexistent`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PUT /api/customers/:id", () => {
|
||||
it("updates the customer", async () => {
|
||||
const res = await app.inject({
|
||||
method: "PUT",
|
||||
url: `/api/customers/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload,
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/customers/:id", () => {
|
||||
it("deletes the customer", async () => {
|
||||
const res = await app.inject({
|
||||
method: "DELETE",
|
||||
url: `/api/customers/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 204);
|
||||
});
|
||||
|
||||
it("returns 404 after delete", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/customers/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,118 @@
|
||||
// Item CRUD test
|
||||
import { describe, it, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { buildApp } from "../index.js";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
let app: FastifyInstance;
|
||||
let token: string;
|
||||
let createdId: string;
|
||||
|
||||
before(async () => {
|
||||
process.env.JWT_SECRET = "test-secret";
|
||||
process.env.DATABASE_URL = ":memory:";
|
||||
app = await buildApp();
|
||||
await app.ready();
|
||||
|
||||
// Register and login to get a token
|
||||
const regRes = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/auth/register",
|
||||
payload: { username: `test_${Date.now()}`, password: "password123" },
|
||||
});
|
||||
token = regRes.json().token;
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe("GET /api/items", () => {
|
||||
it("returns empty list", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: "/api/items",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.ok(Array.isArray(body.data));
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/items", () => {
|
||||
it("creates a item", async () => {
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/items",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: {
|
||||
"userId": "00000000-0000-0000-0000-000000000001",
|
||||
"title": "sample-title",
|
||||
"data": "sample-data"
|
||||
},
|
||||
});
|
||||
assert.equal(res.statusCode, 201);
|
||||
const body = res.json();
|
||||
assert.ok(body.data.id);
|
||||
createdId = body.data.id;
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/items/:id", () => {
|
||||
it("returns the created item", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/items/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.equal(body.data.id, createdId);
|
||||
});
|
||||
|
||||
it("returns 404 for nonexistent id", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/items/nonexistent`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PUT /api/items/:id", () => {
|
||||
it("updates the item", async () => {
|
||||
const res = await app.inject({
|
||||
method: "PUT",
|
||||
url: `/api/items/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: {
|
||||
"userId": "00000000-0000-0000-0000-000000000001",
|
||||
"title": "sample-title",
|
||||
"data": "sample-data"
|
||||
},
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/items/:id", () => {
|
||||
it("deletes the item", async () => {
|
||||
const res = await app.inject({
|
||||
method: "DELETE",
|
||||
url: `/api/items/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 204);
|
||||
});
|
||||
|
||||
it("returns 404 after delete", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/items/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,120 @@
|
||||
// Media CRUD test
|
||||
import { describe, it, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { buildApp } from "../index.js";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
let app: FastifyInstance;
|
||||
let token: string;
|
||||
let createdId: string;
|
||||
let payload: Record<string, unknown>;
|
||||
|
||||
before(async () => {
|
||||
process.env.JWT_SECRET = "test-secret";
|
||||
process.env.DATABASE_URL = ":memory:";
|
||||
app = await buildApp();
|
||||
await app.ready();
|
||||
|
||||
// Register and login to get a token
|
||||
const regRes = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/auth/register",
|
||||
payload: { username: `test_${Date.now()}`, password: "password123" },
|
||||
});
|
||||
token = regRes.json().token;
|
||||
|
||||
// Resolve user FK references with the registered user's real ID
|
||||
payload = {
|
||||
"userId": regRes.json().user.id,
|
||||
"filename": "sample-filename",
|
||||
"url": "sample-url",
|
||||
"mimeType": "sample-mimetype",
|
||||
"sizeBytes": 1
|
||||
};
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe("GET /api/media", () => {
|
||||
it("returns empty list", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: "/api/media",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.ok(Array.isArray(body.data));
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/media", () => {
|
||||
it("creates a media", async () => {
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/media",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload,
|
||||
});
|
||||
assert.equal(res.statusCode, 201);
|
||||
const body = res.json();
|
||||
assert.ok(body.data.id);
|
||||
createdId = body.data.id;
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/media/:id", () => {
|
||||
it("returns the created media", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/media/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.equal(body.data.id, createdId);
|
||||
});
|
||||
|
||||
it("returns 404 for nonexistent id", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/media/nonexistent`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PUT /api/media/:id", () => {
|
||||
it("updates the media", async () => {
|
||||
const res = await app.inject({
|
||||
method: "PUT",
|
||||
url: `/api/media/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload,
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/media/:id", () => {
|
||||
it("deletes the media", async () => {
|
||||
const res = await app.inject({
|
||||
method: "DELETE",
|
||||
url: `/api/media/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 204);
|
||||
});
|
||||
|
||||
it("returns 404 after delete", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/media/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,121 @@
|
||||
// Reminder CRUD test
|
||||
import { describe, it, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { buildApp } from "../index.js";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
let app: FastifyInstance;
|
||||
let token: string;
|
||||
let createdId: string;
|
||||
let payload: Record<string, unknown>;
|
||||
|
||||
before(async () => {
|
||||
process.env.JWT_SECRET = "test-secret";
|
||||
process.env.DATABASE_URL = ":memory:";
|
||||
app = await buildApp();
|
||||
await app.ready();
|
||||
|
||||
// Register and login to get a token
|
||||
const regRes = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/auth/register",
|
||||
payload: { username: `test_${Date.now()}`, password: "password123" },
|
||||
});
|
||||
token = regRes.json().token;
|
||||
|
||||
// Resolve user FK references with the registered user's real ID
|
||||
payload = {
|
||||
"userId": regRes.json().user.id,
|
||||
"entityType": "sample-entitytype",
|
||||
"entityId": "sample-entityid",
|
||||
"remindAt": "sample-remindat",
|
||||
"message": "sample-message",
|
||||
"sent": true
|
||||
};
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe("GET /api/reminders", () => {
|
||||
it("returns empty list", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: "/api/reminders",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.ok(Array.isArray(body.data));
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/reminders", () => {
|
||||
it("creates a reminder", async () => {
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/reminders",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload,
|
||||
});
|
||||
assert.equal(res.statusCode, 201);
|
||||
const body = res.json();
|
||||
assert.ok(body.data.id);
|
||||
createdId = body.data.id;
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/reminders/:id", () => {
|
||||
it("returns the created reminder", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/reminders/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.equal(body.data.id, createdId);
|
||||
});
|
||||
|
||||
it("returns 404 for nonexistent id", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/reminders/nonexistent`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PUT /api/reminders/:id", () => {
|
||||
it("updates the reminder", async () => {
|
||||
const res = await app.inject({
|
||||
method: "PUT",
|
||||
url: `/api/reminders/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload,
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/reminders/:id", () => {
|
||||
it("deletes the reminder", async () => {
|
||||
const res = await app.inject({
|
||||
method: "DELETE",
|
||||
url: `/api/reminders/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 204);
|
||||
});
|
||||
|
||||
it("returns 404 after delete", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/reminders/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,118 @@
|
||||
// Tags CRUD test
|
||||
import { describe, it, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { buildApp } from "../index.js";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
let app: FastifyInstance;
|
||||
let token: string;
|
||||
let createdId: string;
|
||||
let payload: Record<string, unknown>;
|
||||
|
||||
before(async () => {
|
||||
process.env.JWT_SECRET = "test-secret";
|
||||
process.env.DATABASE_URL = ":memory:";
|
||||
app = await buildApp();
|
||||
await app.ready();
|
||||
|
||||
// Register and login to get a token
|
||||
const regRes = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/auth/register",
|
||||
payload: { username: `test_${Date.now()}`, password: "password123" },
|
||||
});
|
||||
token = regRes.json().token;
|
||||
|
||||
// Resolve user FK references with the registered user's real ID
|
||||
payload = {
|
||||
"userId": regRes.json().user.id,
|
||||
"name": "sample-name",
|
||||
"color": "sample-color"
|
||||
};
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe("GET /api/tags", () => {
|
||||
it("returns empty list", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: "/api/tags",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.ok(Array.isArray(body.data));
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/tags", () => {
|
||||
it("creates a tag", async () => {
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/tags",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload,
|
||||
});
|
||||
assert.equal(res.statusCode, 201);
|
||||
const body = res.json();
|
||||
assert.ok(body.data.id);
|
||||
createdId = body.data.id;
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/tags/:id", () => {
|
||||
it("returns the created tag", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/tags/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.equal(body.data.id, createdId);
|
||||
});
|
||||
|
||||
it("returns 404 for nonexistent id", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/tags/nonexistent`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PUT /api/tags/:id", () => {
|
||||
it("updates the tag", async () => {
|
||||
const res = await app.inject({
|
||||
method: "PUT",
|
||||
url: `/api/tags/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload,
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/tags/:id", () => {
|
||||
it("deletes the tag", async () => {
|
||||
const res = await app.inject({
|
||||
method: "DELETE",
|
||||
url: `/api/tags/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 204);
|
||||
});
|
||||
|
||||
it("returns 404 after delete", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/tags/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,118 @@
|
||||
// User CRUD test
|
||||
import { describe, it, before, after } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { buildApp } from "../index.js";
|
||||
import type { FastifyInstance } from "fastify";
|
||||
|
||||
let app: FastifyInstance;
|
||||
let token: string;
|
||||
let createdId: string;
|
||||
|
||||
before(async () => {
|
||||
process.env.JWT_SECRET = "test-secret";
|
||||
process.env.DATABASE_URL = ":memory:";
|
||||
app = await buildApp();
|
||||
await app.ready();
|
||||
|
||||
// Register and login to get a token
|
||||
const regRes = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/auth/register",
|
||||
payload: { username: `test_${Date.now()}`, password: "password123" },
|
||||
});
|
||||
token = regRes.json().token;
|
||||
});
|
||||
|
||||
after(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
describe("GET /api/users", () => {
|
||||
it("returns empty list", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: "/api/users",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.ok(Array.isArray(body.data));
|
||||
});
|
||||
});
|
||||
|
||||
describe("POST /api/users", () => {
|
||||
it("creates a user", async () => {
|
||||
const res = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/users",
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: {
|
||||
"phone": "sample-phone",
|
||||
"nickname": "sample-nickname",
|
||||
"avatarUrl": "sample-avatarurl"
|
||||
},
|
||||
});
|
||||
assert.equal(res.statusCode, 201);
|
||||
const body = res.json();
|
||||
assert.ok(body.data.id);
|
||||
createdId = body.data.id;
|
||||
});
|
||||
});
|
||||
|
||||
describe("GET /api/users/:id", () => {
|
||||
it("returns the created user", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/users/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
const body = res.json();
|
||||
assert.equal(body.data.id, createdId);
|
||||
});
|
||||
|
||||
it("returns 404 for nonexistent id", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/users/nonexistent`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
|
||||
describe("PUT /api/users/:id", () => {
|
||||
it("updates the user", async () => {
|
||||
const res = await app.inject({
|
||||
method: "PUT",
|
||||
url: `/api/users/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
payload: {
|
||||
"phone": "sample-phone",
|
||||
"nickname": "sample-nickname",
|
||||
"avatarUrl": "sample-avatarurl"
|
||||
},
|
||||
});
|
||||
assert.equal(res.statusCode, 200);
|
||||
});
|
||||
});
|
||||
|
||||
describe("DELETE /api/users/:id", () => {
|
||||
it("deletes the user", async () => {
|
||||
const res = await app.inject({
|
||||
method: "DELETE",
|
||||
url: `/api/users/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 204);
|
||||
});
|
||||
|
||||
it("returns 404 after delete", async () => {
|
||||
const res = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/users/${createdId}`,
|
||||
headers: { authorization: `Bearer ${token}` },
|
||||
});
|
||||
assert.equal(res.statusCode, 404);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,93 @@
|
||||
// SQLite database client (sql.js — pure WASM, no native deps)
|
||||
import initSqlJs, { type Database, type BindParams } from "sql.js";
|
||||
import { createTables } from "./schema.js";
|
||||
|
||||
let db: Database | null = null;
|
||||
let initPromise: Promise<Database> | null = null;
|
||||
|
||||
/** Initialize the database (call once at startup). */
|
||||
export async function initDb(dbPath?: string): Promise<Database> {
|
||||
if (db) return db;
|
||||
if (initPromise) return initPromise;
|
||||
|
||||
initPromise = (async () => {
|
||||
const SQL = await initSqlJs();
|
||||
const path = dbPath || process.env.DATABASE_URL || ":memory:";
|
||||
|
||||
// Try to load existing database from file
|
||||
let buffer: ArrayLike<number> | undefined;
|
||||
if (path !== ":memory:") {
|
||||
try {
|
||||
const fs = await import("node:fs/promises");
|
||||
const data = await fs.readFile(path);
|
||||
buffer = new Uint8Array(data);
|
||||
} catch {
|
||||
// File doesn't exist yet — start fresh
|
||||
}
|
||||
}
|
||||
|
||||
db = new SQL.Database(buffer);
|
||||
db.run("PRAGMA foreign_keys = ON");
|
||||
createTables(db);
|
||||
return db;
|
||||
})();
|
||||
|
||||
return initPromise;
|
||||
}
|
||||
|
||||
/** Get the initialized database (must call initDb first). */
|
||||
export function getDb(): Database {
|
||||
if (!db) throw new Error("Database not initialized. Call initDb() first.");
|
||||
return db;
|
||||
}
|
||||
|
||||
/** Save database to disk. */
|
||||
export async function saveDb(dbPath?: string): Promise<void> {
|
||||
if (!db) return;
|
||||
const path = dbPath || process.env.DATABASE_URL || "./data/app.db";
|
||||
if (path === ":memory:") return;
|
||||
const fs = await import("node:fs/promises");
|
||||
const { dirname } = await import("node:path");
|
||||
await fs.mkdir(dirname(path), { recursive: true });
|
||||
const data = db.export();
|
||||
await fs.writeFile(path, Buffer.from(data));
|
||||
}
|
||||
|
||||
export async function closeDb(): Promise<void> {
|
||||
if (db) {
|
||||
await saveDb();
|
||||
db.close();
|
||||
db = null;
|
||||
initPromise = null;
|
||||
}
|
||||
}
|
||||
|
||||
// Helper: run a query and return all rows as objects
|
||||
export function queryAll<T = Record<string, unknown>>(sql: string, params: BindParams = []): T[] {
|
||||
const d = getDb();
|
||||
const stmt = d.prepare(sql);
|
||||
if (params) stmt.bind(params);
|
||||
const results: T[] = [];
|
||||
while (stmt.step()) {
|
||||
const row = stmt.getAsObject();
|
||||
results.push(row as unknown as T);
|
||||
}
|
||||
stmt.free();
|
||||
return results;
|
||||
}
|
||||
|
||||
// Helper: run a query and return the first row
|
||||
export function queryOne<T = Record<string, unknown>>(sql: string, params: BindParams = []): T | undefined {
|
||||
const rows = queryAll<T>(sql, params);
|
||||
return rows[0];
|
||||
}
|
||||
|
||||
// Helper: run a mutation and return { changes, lastInsertRowid }
|
||||
export function execute(sql: string, params: BindParams = []): { changes: number; lastInsertRowid: number } {
|
||||
const d = getDb();
|
||||
d.run(sql, params);
|
||||
return {
|
||||
changes: d.getRowsModified(),
|
||||
lastInsertRowid: 0,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Auto-generated SQLite schema
|
||||
import type { Database } from "sql.js";
|
||||
|
||||
export function createTables(db: Database): void {
|
||||
const statements = [
|
||||
"CREATE TABLE IF NOT EXISTS users (\n id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n username TEXT NOT NULL UNIQUE,\n password_hash TEXT NOT NULL,\n nickname TEXT,\n role TEXT DEFAULT 'user',\n phone TEXT,\n avatar_url TEXT,\n created_at TEXT,\n updated_at TEXT\n);",
|
||||
"CREATE TABLE IF NOT EXISTS contracts (\n id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n user_id TEXT NOT NULL REFERENCES users(id),\n title TEXT NOT NULL,\n party_a TEXT,\n party_b TEXT,\n amount REAL,\n signed_at TEXT,\n expires_at TEXT,\n status TEXT DEFAULT 'draft',\n file_url TEXT,\n created_at TEXT,\n updated_at TEXT\n);",
|
||||
"CREATE TABLE IF NOT EXISTS approvals (\n id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n user_id TEXT NOT NULL REFERENCES users(id),\n entity_type TEXT NOT NULL,\n entity_id TEXT,\n applicant_id TEXT NOT NULL REFERENCES users(id),\n status TEXT DEFAULT 'pending',\n form_data TEXT,\n created_at TEXT,\n updated_at TEXT\n);",
|
||||
"CREATE TABLE IF NOT EXISTS customers (\n id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n user_id TEXT NOT NULL REFERENCES users(id),\n name TEXT NOT NULL,\n email TEXT,\n phone TEXT,\n company TEXT,\n source TEXT,\n tags TEXT DEFAULT '[]',\n created_at TEXT,\n updated_at TEXT\n);",
|
||||
"CREATE TABLE IF NOT EXISTS reminders (\n id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n user_id TEXT NOT NULL REFERENCES users(id),\n entity_type TEXT NOT NULL,\n entity_id TEXT,\n remind_at TEXT NOT NULL,\n message TEXT,\n sent INTEGER DEFAULT 'false',\n created_at TEXT,\n updated_at TEXT\n);",
|
||||
"CREATE TABLE IF NOT EXISTS articles (\n id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n user_id TEXT REFERENCES users(id),\n title TEXT NOT NULL,\n content TEXT,\n excerpt TEXT,\n cover_url TEXT,\n status TEXT,\n author_id TEXT REFERENCES authors(id),\n category TEXT,\n published_at TEXT,\n created_at TEXT,\n updated_at TEXT\n);",
|
||||
"CREATE TABLE IF NOT EXISTS tags (\n id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n user_id TEXT REFERENCES users(id),\n name TEXT NOT NULL UNIQUE,\n color TEXT,\n created_at TEXT,\n updated_at TEXT\n);",
|
||||
"CREATE TABLE IF NOT EXISTS comments (\n id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n user_id TEXT REFERENCES users(id),\n entity_type TEXT NOT NULL,\n entity_id TEXT NOT NULL,\n content TEXT NOT NULL,\n parent_id TEXT REFERENCES parents(id),\n created_at TEXT,\n updated_at TEXT\n);",
|
||||
"CREATE TABLE IF NOT EXISTS media (\n id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(16)))),\n user_id TEXT REFERENCES users(id),\n filename TEXT NOT NULL,\n url TEXT NOT NULL,\n mime_type TEXT,\n size_bytes INTEGER,\n created_at TEXT,\n updated_at TEXT\n);"
|
||||
];
|
||||
for (const sql of statements) {
|
||||
const trimmed = sql.trim();
|
||||
if (trimmed) db.run(trimmed);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// SocialApp — Fastify Backend Server
|
||||
import Fastify from "fastify";
|
||||
import cors from "@fastify/cors";
|
||||
import fjwt from "@fastify/jwt";
|
||||
import { initDb, closeDb } from "./db/client.js";
|
||||
import { authRoutes } from "./routes/auth.js";
|
||||
import { contractsRoutes } from "./routes/contracts.js";
|
||||
import { approvalsRoutes } from "./routes/approvals.js";
|
||||
import { customersRoutes } from "./routes/customers.js";
|
||||
import { remindersRoutes } from "./routes/reminders.js";
|
||||
import { articlesRoutes } from "./routes/articles.js";
|
||||
import { tagsRoutes } from "./routes/tags.js";
|
||||
import { commentsRoutes } from "./routes/comments.js";
|
||||
import { mediaRoutes } from "./routes/media.js";
|
||||
|
||||
const JWT_SECRET = process.env.JWT_SECRET || "change-me-in-production-5f616668";
|
||||
|
||||
export async function buildApp() {
|
||||
const app = Fastify({
|
||||
logger: {
|
||||
level: process.env.LOG_LEVEL || "info",
|
||||
transport: process.env.NODE_ENV !== "production"
|
||||
? { target: "pino-pretty", options: { colorize: true } }
|
||||
: undefined,
|
||||
},
|
||||
});
|
||||
|
||||
// Init database
|
||||
await initDb();
|
||||
|
||||
// Plugins
|
||||
await app.register(cors, {
|
||||
origin: process.env.CORS_ORIGIN || "*",
|
||||
methods: ["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
|
||||
});
|
||||
|
||||
await app.register(fjwt, { secret: JWT_SECRET });
|
||||
|
||||
// Routes
|
||||
await app.register(authRoutes, { prefix: "/api/auth" });
|
||||
await app.register(contractsRoutes, { prefix: "/api/contracts" });
|
||||
await app.register(approvalsRoutes, { prefix: "/api/approvals" });
|
||||
await app.register(customersRoutes, { prefix: "/api/customers" });
|
||||
await app.register(remindersRoutes, { prefix: "/api/reminders" });
|
||||
await app.register(articlesRoutes, { prefix: "/api/articles" });
|
||||
await app.register(tagsRoutes, { prefix: "/api/tags" });
|
||||
await app.register(commentsRoutes, { prefix: "/api/comments" });
|
||||
await app.register(mediaRoutes, { prefix: "/api/media" });
|
||||
|
||||
// Health check
|
||||
app.get("/api/health", async () => ({ status: "ok", timestamp: new Date().toISOString() }));
|
||||
|
||||
// Graceful shutdown
|
||||
app.addHook("onClose", async () => {
|
||||
closeDb();
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
// Start server if called directly (not when imported by tests)
|
||||
const port = parseInt(process.env.PORT || "3001", 10);
|
||||
const host = process.env.HOST || "0.0.0.0";
|
||||
|
||||
async function main() {
|
||||
const app = await buildApp();
|
||||
try {
|
||||
await app.listen({ port, host });
|
||||
} catch (err) {
|
||||
app.log.error(err);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Guard: only run when executed directly, not when imported
|
||||
const isMain = process.argv[1] && (import.meta.url === `file://${process.argv[1]}` || import.meta.url.endsWith(process.argv[1]) || process.argv[1].endsWith("/src/index.ts") || process.argv[1].endsWith("/src/index.js"));
|
||||
if (isMain) {
|
||||
main();
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
// JWT Authentication Middleware
|
||||
import type { FastifyRequest, FastifyReply } from "fastify";
|
||||
import type { JwtPayload } from "../types/index.js";
|
||||
|
||||
/**
|
||||
* Verify JWT token and attach user to request.
|
||||
*/
|
||||
export async function authenticate(request: FastifyRequest, reply: FastifyReply): Promise<void> {
|
||||
try {
|
||||
await request.jwtVerify();
|
||||
} catch (err) {
|
||||
reply.status(401).send({ error: "Unauthorized", message: "Invalid or expired token", statusCode: 401 });
|
||||
}
|
||||
}
|
||||
|
||||
/** Helper to get typed user from request (after authenticate). */
|
||||
export function getUser(request: FastifyRequest): JwtPayload {
|
||||
return request.user as unknown as JwtPayload;
|
||||
}
|
||||
|
||||
/**
|
||||
* Require admin role.
|
||||
* Must be used after authenticate.
|
||||
*/
|
||||
export async function requireAdmin(request: FastifyRequest, reply: FastifyReply): Promise<void> {
|
||||
const user = request.user as unknown as JwtPayload | undefined;
|
||||
if (!user || user.role !== "admin") {
|
||||
reply.status(403).send({ error: "Forbidden", message: "Admin access required", statusCode: 403 });
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Optional auth: attach user if token present, but don't fail if missing.
|
||||
*/
|
||||
export async function optionalAuth(request: FastifyRequest): Promise<void> {
|
||||
try {
|
||||
await request.jwtVerify();
|
||||
} catch {
|
||||
// No token or invalid — continue without user
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// Auto-generated Approval routes
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { authenticate } from "../middleware/auth.js";
|
||||
import { ApprovalService } from "../services/approval.js";
|
||||
import type { CreateApprovalInput, UpdateApprovalInput } from "../types/index.js";
|
||||
|
||||
const service = new ApprovalService();
|
||||
|
||||
export async function approvalsRoutes(app: FastifyInstance): Promise<void> {
|
||||
// All routes require authentication
|
||||
app.addHook("onRequest", authenticate);
|
||||
|
||||
// GET /api/approvals — list
|
||||
app.get("/", async (request, reply) => {
|
||||
const items = service.list();
|
||||
return { data: items };
|
||||
});
|
||||
|
||||
// GET /api/approvals/:id — get by id
|
||||
app.get<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const item = service.getById(request.params.id);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Approval not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// POST /api/approvals — create
|
||||
app.post<{ Body: CreateApprovalInput }>("/", async (request, reply) => {
|
||||
const item = service.create(request.body);
|
||||
return reply.status(201).send({ data: item });
|
||||
});
|
||||
|
||||
// PUT /api/approvals/:id — update
|
||||
app.put<{ Params: { id: string }; Body: UpdateApprovalInput }>("/:id", async (request, reply) => {
|
||||
const item = service.update(request.params.id, request.body);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Approval not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// DELETE /api/approvals/:id — delete
|
||||
app.delete<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const deleted = service.delete(request.params.id);
|
||||
if (!deleted) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Approval not found", statusCode: 404 });
|
||||
}
|
||||
return reply.status(204).send();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// Auto-generated Articles routes
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { authenticate } from "../middleware/auth.js";
|
||||
import { ArticlesService } from "../services/article.js";
|
||||
import type { CreateArticlesInput, UpdateArticlesInput } from "../types/index.js";
|
||||
|
||||
const service = new ArticlesService();
|
||||
|
||||
export async function articlesRoutes(app: FastifyInstance): Promise<void> {
|
||||
// All routes require authentication
|
||||
app.addHook("onRequest", authenticate);
|
||||
|
||||
// GET /api/articles — list
|
||||
app.get("/", async (request, reply) => {
|
||||
const items = service.list();
|
||||
return { data: items };
|
||||
});
|
||||
|
||||
// GET /api/articles/:id — get by id
|
||||
app.get<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const item = service.getById(request.params.id);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Articles not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// POST /api/articles — create
|
||||
app.post<{ Body: CreateArticlesInput }>("/", async (request, reply) => {
|
||||
const item = service.create(request.body);
|
||||
return reply.status(201).send({ data: item });
|
||||
});
|
||||
|
||||
// PUT /api/articles/:id — update
|
||||
app.put<{ Params: { id: string }; Body: UpdateArticlesInput }>("/:id", async (request, reply) => {
|
||||
const item = service.update(request.params.id, request.body);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Articles not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// DELETE /api/articles/:id — delete
|
||||
app.delete<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const deleted = service.delete(request.params.id);
|
||||
if (!deleted) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Articles not found", statusCode: 404 });
|
||||
}
|
||||
return reply.status(204).send();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
// Authentication routes
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import bcrypt from "bcrypt";
|
||||
import { queryOne, execute } from "../db/client.js";
|
||||
import { authenticate } from "../middleware/auth.js";
|
||||
import type { User, RegisterInput, LoginInput, AuthResponse } from "../types/index.js";
|
||||
|
||||
const SALT_ROUNDS = 10;
|
||||
|
||||
export async function authRoutes(app: FastifyInstance): Promise<void> {
|
||||
// POST /api/auth/register
|
||||
app.post<{ Body: RegisterInput }>("/register", async (request, reply) => {
|
||||
const { username, password, nickname } = request.body;
|
||||
|
||||
if (!username || !password) {
|
||||
return reply.status(400).send({
|
||||
error: "Bad Request",
|
||||
message: "Username and password are required",
|
||||
statusCode: 400,
|
||||
});
|
||||
}
|
||||
|
||||
if (password.length < 6) {
|
||||
return reply.status(400).send({
|
||||
error: "Bad Request",
|
||||
message: "Password must be at least 6 characters",
|
||||
statusCode: 400,
|
||||
});
|
||||
}
|
||||
|
||||
const existing = queryOne("SELECT id FROM users WHERE username = ?", [username]);
|
||||
if (existing) {
|
||||
return reply.status(409).send({
|
||||
error: "Conflict",
|
||||
message: "Username already exists",
|
||||
statusCode: 409,
|
||||
});
|
||||
}
|
||||
|
||||
const id = crypto.randomUUID();
|
||||
const passwordHash = await bcrypt.hash(password, SALT_ROUNDS);
|
||||
const now = new Date().toISOString();
|
||||
|
||||
execute(
|
||||
"INSERT INTO users (id, username, password_hash, nickname, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?)",
|
||||
[id, username, passwordHash, nickname || username, now, now]
|
||||
);
|
||||
|
||||
const user = queryOne<User>(
|
||||
"SELECT id, username, nickname, role, created_at, updated_at FROM users WHERE id = ?",
|
||||
[id]
|
||||
);
|
||||
if (!user) {
|
||||
return reply.status(500).send({ error: "Internal Error", message: "Failed to create user", statusCode: 500 });
|
||||
}
|
||||
|
||||
const token = app.jwt.sign({ userId: id, username, role: user.role });
|
||||
|
||||
return reply.status(201).send({ token, user } satisfies AuthResponse);
|
||||
});
|
||||
|
||||
// POST /api/auth/login
|
||||
app.post<{ Body: LoginInput }>("/login", async (request, reply) => {
|
||||
const { username, password } = request.body;
|
||||
|
||||
if (!username || !password) {
|
||||
return reply.status(400).send({
|
||||
error: "Bad Request",
|
||||
message: "Username and password are required",
|
||||
statusCode: 400,
|
||||
});
|
||||
}
|
||||
|
||||
const user = queryOne<User & { password_hash: string }>(
|
||||
"SELECT * FROM users WHERE username = ?",
|
||||
[username]
|
||||
);
|
||||
|
||||
if (!user) {
|
||||
return reply.status(401).send({
|
||||
error: "Unauthorized",
|
||||
message: "Invalid username or password",
|
||||
statusCode: 401,
|
||||
});
|
||||
}
|
||||
|
||||
const valid = await bcrypt.compare(password, user.password_hash);
|
||||
if (!valid) {
|
||||
return reply.status(401).send({
|
||||
error: "Unauthorized",
|
||||
message: "Invalid username or password",
|
||||
statusCode: 401,
|
||||
});
|
||||
}
|
||||
|
||||
const token = app.jwt.sign({ userId: user.id, username: user.username, role: user.role });
|
||||
|
||||
const { password_hash, ...safeUser } = user;
|
||||
|
||||
return { token, user: safeUser } satisfies AuthResponse;
|
||||
});
|
||||
|
||||
// GET /api/auth/me — current user info
|
||||
app.get("/me", { onRequest: [authenticate] }, async (request, reply) => {
|
||||
const jwtUser = request.user as unknown as { userId: string };
|
||||
const user = queryOne<User>(
|
||||
"SELECT id, username, nickname, role, created_at, updated_at FROM users WHERE id = ?",
|
||||
[jwtUser.userId]
|
||||
);
|
||||
|
||||
if (!user) {
|
||||
return reply.status(404).send({
|
||||
error: "Not Found",
|
||||
message: "User not found",
|
||||
statusCode: 404,
|
||||
});
|
||||
}
|
||||
|
||||
return { data: user };
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// Auto-generated Comments routes
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { authenticate } from "../middleware/auth.js";
|
||||
import { CommentsService } from "../services/comment.js";
|
||||
import type { CreateCommentsInput, UpdateCommentsInput } from "../types/index.js";
|
||||
|
||||
const service = new CommentsService();
|
||||
|
||||
export async function commentsRoutes(app: FastifyInstance): Promise<void> {
|
||||
// All routes require authentication
|
||||
app.addHook("onRequest", authenticate);
|
||||
|
||||
// GET /api/comments — list
|
||||
app.get("/", async (request, reply) => {
|
||||
const items = service.list();
|
||||
return { data: items };
|
||||
});
|
||||
|
||||
// GET /api/comments/:id — get by id
|
||||
app.get<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const item = service.getById(request.params.id);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Comments not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// POST /api/comments — create
|
||||
app.post<{ Body: CreateCommentsInput }>("/", async (request, reply) => {
|
||||
const item = service.create(request.body);
|
||||
return reply.status(201).send({ data: item });
|
||||
});
|
||||
|
||||
// PUT /api/comments/:id — update
|
||||
app.put<{ Params: { id: string }; Body: UpdateCommentsInput }>("/:id", async (request, reply) => {
|
||||
const item = service.update(request.params.id, request.body);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Comments not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// DELETE /api/comments/:id — delete
|
||||
app.delete<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const deleted = service.delete(request.params.id);
|
||||
if (!deleted) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Comments not found", statusCode: 404 });
|
||||
}
|
||||
return reply.status(204).send();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// Auto-generated Contract routes
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { authenticate } from "../middleware/auth.js";
|
||||
import { ContractService } from "../services/contract.js";
|
||||
import type { CreateContractInput, UpdateContractInput } from "../types/index.js";
|
||||
|
||||
const service = new ContractService();
|
||||
|
||||
export async function contractsRoutes(app: FastifyInstance): Promise<void> {
|
||||
// All routes require authentication
|
||||
app.addHook("onRequest", authenticate);
|
||||
|
||||
// GET /api/contracts — list
|
||||
app.get("/", async (request, reply) => {
|
||||
const items = service.list();
|
||||
return { data: items };
|
||||
});
|
||||
|
||||
// GET /api/contracts/:id — get by id
|
||||
app.get<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const item = service.getById(request.params.id);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Contract not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// POST /api/contracts — create
|
||||
app.post<{ Body: CreateContractInput }>("/", async (request, reply) => {
|
||||
const item = service.create(request.body);
|
||||
return reply.status(201).send({ data: item });
|
||||
});
|
||||
|
||||
// PUT /api/contracts/:id — update
|
||||
app.put<{ Params: { id: string }; Body: UpdateContractInput }>("/:id", async (request, reply) => {
|
||||
const item = service.update(request.params.id, request.body);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Contract not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// DELETE /api/contracts/:id — delete
|
||||
app.delete<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const deleted = service.delete(request.params.id);
|
||||
if (!deleted) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Contract not found", statusCode: 404 });
|
||||
}
|
||||
return reply.status(204).send();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// Auto-generated Customer routes
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { authenticate } from "../middleware/auth.js";
|
||||
import { CustomerService } from "../services/customer.js";
|
||||
import type { CreateCustomerInput, UpdateCustomerInput } from "../types/index.js";
|
||||
|
||||
const service = new CustomerService();
|
||||
|
||||
export async function customersRoutes(app: FastifyInstance): Promise<void> {
|
||||
// All routes require authentication
|
||||
app.addHook("onRequest", authenticate);
|
||||
|
||||
// GET /api/customers — list
|
||||
app.get("/", async (request, reply) => {
|
||||
const items = service.list();
|
||||
return { data: items };
|
||||
});
|
||||
|
||||
// GET /api/customers/:id — get by id
|
||||
app.get<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const item = service.getById(request.params.id);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Customer not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// POST /api/customers — create
|
||||
app.post<{ Body: CreateCustomerInput }>("/", async (request, reply) => {
|
||||
const item = service.create(request.body);
|
||||
return reply.status(201).send({ data: item });
|
||||
});
|
||||
|
||||
// PUT /api/customers/:id — update
|
||||
app.put<{ Params: { id: string }; Body: UpdateCustomerInput }>("/:id", async (request, reply) => {
|
||||
const item = service.update(request.params.id, request.body);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Customer not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// DELETE /api/customers/:id — delete
|
||||
app.delete<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const deleted = service.delete(request.params.id);
|
||||
if (!deleted) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Customer not found", statusCode: 404 });
|
||||
}
|
||||
return reply.status(204).send();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// Auto-generated Item routes
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { authenticate } from "../middleware/auth.js";
|
||||
import { ItemService } from "../services/item.js";
|
||||
import type { CreateItemInput, UpdateItemInput } from "../types/index.js";
|
||||
|
||||
const service = new ItemService();
|
||||
|
||||
export async function itemsRoutes(app: FastifyInstance): Promise<void> {
|
||||
// All routes require authentication
|
||||
app.addHook("onRequest", authenticate);
|
||||
|
||||
// GET /api/items — list
|
||||
app.get("/", async (request, reply) => {
|
||||
const items = service.list();
|
||||
return { data: items };
|
||||
});
|
||||
|
||||
// GET /api/items/:id — get by id
|
||||
app.get<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const item = service.getById(request.params.id);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Item not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// POST /api/items — create
|
||||
app.post<{ Body: CreateItemInput }>("/", async (request, reply) => {
|
||||
const item = service.create(request.body);
|
||||
return reply.status(201).send({ data: item });
|
||||
});
|
||||
|
||||
// PUT /api/items/:id — update
|
||||
app.put<{ Params: { id: string }; Body: UpdateItemInput }>("/:id", async (request, reply) => {
|
||||
const item = service.update(request.params.id, request.body);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Item not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// DELETE /api/items/:id — delete
|
||||
app.delete<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const deleted = service.delete(request.params.id);
|
||||
if (!deleted) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Item not found", statusCode: 404 });
|
||||
}
|
||||
return reply.status(204).send();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// Auto-generated Media routes
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { authenticate } from "../middleware/auth.js";
|
||||
import { MediaService } from "../services/media.js";
|
||||
import type { CreateMediaInput, UpdateMediaInput } from "../types/index.js";
|
||||
|
||||
const service = new MediaService();
|
||||
|
||||
export async function mediaRoutes(app: FastifyInstance): Promise<void> {
|
||||
// All routes require authentication
|
||||
app.addHook("onRequest", authenticate);
|
||||
|
||||
// GET /api/media — list
|
||||
app.get("/", async (request, reply) => {
|
||||
const items = service.list();
|
||||
return { data: items };
|
||||
});
|
||||
|
||||
// GET /api/media/:id — get by id
|
||||
app.get<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const item = service.getById(request.params.id);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Media not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// POST /api/media — create
|
||||
app.post<{ Body: CreateMediaInput }>("/", async (request, reply) => {
|
||||
const item = service.create(request.body);
|
||||
return reply.status(201).send({ data: item });
|
||||
});
|
||||
|
||||
// PUT /api/media/:id — update
|
||||
app.put<{ Params: { id: string }; Body: UpdateMediaInput }>("/:id", async (request, reply) => {
|
||||
const item = service.update(request.params.id, request.body);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Media not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// DELETE /api/media/:id — delete
|
||||
app.delete<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const deleted = service.delete(request.params.id);
|
||||
if (!deleted) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Media not found", statusCode: 404 });
|
||||
}
|
||||
return reply.status(204).send();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// Auto-generated Reminder routes
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { authenticate } from "../middleware/auth.js";
|
||||
import { ReminderService } from "../services/reminder.js";
|
||||
import type { CreateReminderInput, UpdateReminderInput } from "../types/index.js";
|
||||
|
||||
const service = new ReminderService();
|
||||
|
||||
export async function remindersRoutes(app: FastifyInstance): Promise<void> {
|
||||
// All routes require authentication
|
||||
app.addHook("onRequest", authenticate);
|
||||
|
||||
// GET /api/reminders — list
|
||||
app.get("/", async (request, reply) => {
|
||||
const items = service.list();
|
||||
return { data: items };
|
||||
});
|
||||
|
||||
// GET /api/reminders/:id — get by id
|
||||
app.get<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const item = service.getById(request.params.id);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Reminder not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// POST /api/reminders — create
|
||||
app.post<{ Body: CreateReminderInput }>("/", async (request, reply) => {
|
||||
const item = service.create(request.body);
|
||||
return reply.status(201).send({ data: item });
|
||||
});
|
||||
|
||||
// PUT /api/reminders/:id — update
|
||||
app.put<{ Params: { id: string }; Body: UpdateReminderInput }>("/:id", async (request, reply) => {
|
||||
const item = service.update(request.params.id, request.body);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Reminder not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// DELETE /api/reminders/:id — delete
|
||||
app.delete<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const deleted = service.delete(request.params.id);
|
||||
if (!deleted) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Reminder not found", statusCode: 404 });
|
||||
}
|
||||
return reply.status(204).send();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// Auto-generated Tags routes
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { authenticate } from "../middleware/auth.js";
|
||||
import { TagsService } from "../services/tag.js";
|
||||
import type { CreateTagsInput, UpdateTagsInput } from "../types/index.js";
|
||||
|
||||
const service = new TagsService();
|
||||
|
||||
export async function tagsRoutes(app: FastifyInstance): Promise<void> {
|
||||
// All routes require authentication
|
||||
app.addHook("onRequest", authenticate);
|
||||
|
||||
// GET /api/tags — list
|
||||
app.get("/", async (request, reply) => {
|
||||
const items = service.list();
|
||||
return { data: items };
|
||||
});
|
||||
|
||||
// GET /api/tags/:id — get by id
|
||||
app.get<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const item = service.getById(request.params.id);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Tags not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// POST /api/tags — create
|
||||
app.post<{ Body: CreateTagsInput }>("/", async (request, reply) => {
|
||||
const item = service.create(request.body);
|
||||
return reply.status(201).send({ data: item });
|
||||
});
|
||||
|
||||
// PUT /api/tags/:id — update
|
||||
app.put<{ Params: { id: string }; Body: UpdateTagsInput }>("/:id", async (request, reply) => {
|
||||
const item = service.update(request.params.id, request.body);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Tags not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// DELETE /api/tags/:id — delete
|
||||
app.delete<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const deleted = service.delete(request.params.id);
|
||||
if (!deleted) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "Tags not found", statusCode: 404 });
|
||||
}
|
||||
return reply.status(204).send();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
// Auto-generated User routes
|
||||
import type { FastifyInstance } from "fastify";
|
||||
import { authenticate } from "../middleware/auth.js";
|
||||
import { UserService } from "../services/user.js";
|
||||
import type { CreateUserInput, UpdateUserInput } from "../types/index.js";
|
||||
|
||||
const service = new UserService();
|
||||
|
||||
export async function usersRoutes(app: FastifyInstance): Promise<void> {
|
||||
// All routes require authentication
|
||||
app.addHook("onRequest", authenticate);
|
||||
|
||||
// GET /api/users — list
|
||||
app.get("/", async (request, reply) => {
|
||||
const items = service.list();
|
||||
return { data: items };
|
||||
});
|
||||
|
||||
// GET /api/users/:id — get by id
|
||||
app.get<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const item = service.getById(request.params.id);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "User not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// POST /api/users — create
|
||||
app.post<{ Body: CreateUserInput }>("/", async (request, reply) => {
|
||||
const item = service.create(request.body);
|
||||
return reply.status(201).send({ data: item });
|
||||
});
|
||||
|
||||
// PUT /api/users/:id — update
|
||||
app.put<{ Params: { id: string }; Body: UpdateUserInput }>("/:id", async (request, reply) => {
|
||||
const item = service.update(request.params.id, request.body);
|
||||
if (!item) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "User not found", statusCode: 404 });
|
||||
}
|
||||
return { data: item };
|
||||
});
|
||||
|
||||
// DELETE /api/users/:id — delete
|
||||
app.delete<{ Params: { id: string } }>("/:id", async (request, reply) => {
|
||||
const deleted = service.delete(request.params.id);
|
||||
if (!deleted) {
|
||||
return reply.status(404).send({ error: "Not Found", message: "User not found", statusCode: 404 });
|
||||
}
|
||||
return reply.status(204).send();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// Auto-generated Approval service
|
||||
import { queryAll, queryOne, execute } from "../db/client.js";
|
||||
import type { Approval, CreateApprovalInput, UpdateApprovalInput } from "../types/index.js";
|
||||
|
||||
export class ApprovalService {
|
||||
/** List all approvals */
|
||||
list(): Approval[] {
|
||||
return queryAll<Approval>("SELECT * FROM approvals ORDER BY created_at DESC");
|
||||
}
|
||||
|
||||
/** Get by ID */
|
||||
getById(id: string): Approval | undefined {
|
||||
return queryOne<Approval>("SELECT * FROM approvals WHERE id = ?", [id]);
|
||||
}
|
||||
|
||||
/** Create */
|
||||
create(input: CreateApprovalInput): Approval {
|
||||
const id = crypto.randomUUID();
|
||||
const now = new Date().toISOString();
|
||||
const cols = ["id", "user_id", "entity_type", "entity_id", "applicant_id", "status", "form_data", "created_at", "updated_at"];
|
||||
const values = [id, input.userId ?? null, input.entityType ?? null, input.entityId ?? null, input.applicantId ?? null, input.status ?? null, input.formData ?? null, now, now];
|
||||
|
||||
execute(`INSERT INTO approvals (${cols.join(", ")}) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Update */
|
||||
update(id: string, input: UpdateApprovalInput): Approval | undefined {
|
||||
const existing = this.getById(id);
|
||||
if (!existing) return undefined;
|
||||
|
||||
const sets: string[] = [];
|
||||
const values: unknown[] = [];
|
||||
if (input.userId !== undefined) { sets.push("user_id = ?"); values.push(input.userId); }
|
||||
if (input.entityType !== undefined) { sets.push("entity_type = ?"); values.push(input.entityType); }
|
||||
if (input.entityId !== undefined) { sets.push("entity_id = ?"); values.push(input.entityId); }
|
||||
if (input.applicantId !== undefined) { sets.push("applicant_id = ?"); values.push(input.applicantId); }
|
||||
if (input.status !== undefined) { sets.push("status = ?"); values.push(input.status); }
|
||||
if (input.formData !== undefined) { sets.push("form_data = ?"); values.push(input.formData); }
|
||||
|
||||
if (sets.length === 0) return existing;
|
||||
|
||||
sets.push("updated_at = ?");
|
||||
values.push(new Date().toISOString());
|
||||
|
||||
values.push(id);
|
||||
execute(`UPDATE approvals SET ${sets.join(", ")} WHERE id = ?`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Delete */
|
||||
delete(id: string): boolean {
|
||||
const result = execute("DELETE FROM approvals WHERE id = ?", [id]);
|
||||
return result.changes > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
// Auto-generated Articles service
|
||||
import { queryAll, queryOne, execute } from "../db/client.js";
|
||||
import type { Articles, CreateArticlesInput, UpdateArticlesInput } from "../types/index.js";
|
||||
|
||||
export class ArticlesService {
|
||||
/** List all articles */
|
||||
list(): Articles[] {
|
||||
return queryAll<Articles>("SELECT * FROM articles ORDER BY created_at DESC");
|
||||
}
|
||||
|
||||
/** Get by ID */
|
||||
getById(id: string): Articles | undefined {
|
||||
return queryOne<Articles>("SELECT * FROM articles WHERE id = ?", [id]);
|
||||
}
|
||||
|
||||
/** Create */
|
||||
create(input: CreateArticlesInput): Articles {
|
||||
const id = crypto.randomUUID();
|
||||
const now = new Date().toISOString();
|
||||
const cols = ["id", "user_id", "title", "content", "excerpt", "cover_url", "author_id", "category", "published_at", "created_at", "updated_at"];
|
||||
const values = [id, input.userId ?? null, input.title ?? null, input.content ?? null, input.excerpt ?? null, input.coverUrl ?? null, input.authorId ?? null, input.category ?? null, input.publishedAt ?? null, now, now];
|
||||
|
||||
execute(`INSERT INTO articles (${cols.join(", ")}) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Update */
|
||||
update(id: string, input: UpdateArticlesInput): Articles | undefined {
|
||||
const existing = this.getById(id);
|
||||
if (!existing) return undefined;
|
||||
|
||||
const sets: string[] = [];
|
||||
const values: unknown[] = [];
|
||||
if (input.userId !== undefined) { sets.push("user_id = ?"); values.push(input.userId); }
|
||||
if (input.title !== undefined) { sets.push("title = ?"); values.push(input.title); }
|
||||
if (input.content !== undefined) { sets.push("content = ?"); values.push(input.content); }
|
||||
if (input.excerpt !== undefined) { sets.push("excerpt = ?"); values.push(input.excerpt); }
|
||||
if (input.coverUrl !== undefined) { sets.push("cover_url = ?"); values.push(input.coverUrl); }
|
||||
if (input.authorId !== undefined) { sets.push("author_id = ?"); values.push(input.authorId); }
|
||||
if (input.category !== undefined) { sets.push("category = ?"); values.push(input.category); }
|
||||
if (input.publishedAt !== undefined) { sets.push("published_at = ?"); values.push(input.publishedAt); }
|
||||
|
||||
if (sets.length === 0) return existing;
|
||||
|
||||
sets.push("updated_at = ?");
|
||||
values.push(new Date().toISOString());
|
||||
|
||||
values.push(id);
|
||||
execute(`UPDATE articles SET ${sets.join(", ")} WHERE id = ?`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Delete */
|
||||
delete(id: string): boolean {
|
||||
const result = execute("DELETE FROM articles WHERE id = ?", [id]);
|
||||
return result.changes > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// Auto-generated Comments service
|
||||
import { queryAll, queryOne, execute } from "../db/client.js";
|
||||
import type { Comments, CreateCommentsInput, UpdateCommentsInput } from "../types/index.js";
|
||||
|
||||
export class CommentsService {
|
||||
/** List all comments */
|
||||
list(): Comments[] {
|
||||
return queryAll<Comments>("SELECT * FROM comments ORDER BY created_at DESC");
|
||||
}
|
||||
|
||||
/** Get by ID */
|
||||
getById(id: string): Comments | undefined {
|
||||
return queryOne<Comments>("SELECT * FROM comments WHERE id = ?", [id]);
|
||||
}
|
||||
|
||||
/** Create */
|
||||
create(input: CreateCommentsInput): Comments {
|
||||
const id = crypto.randomUUID();
|
||||
const now = new Date().toISOString();
|
||||
const cols = ["id", "user_id", "entity_type", "entity_id", "content", "parent_id", "created_at", "updated_at"];
|
||||
const values = [id, input.userId ?? null, input.entityType ?? null, input.entityId ?? null, input.content ?? null, input.parentId ?? null, now, now];
|
||||
|
||||
execute(`INSERT INTO comments (${cols.join(", ")}) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Update */
|
||||
update(id: string, input: UpdateCommentsInput): Comments | undefined {
|
||||
const existing = this.getById(id);
|
||||
if (!existing) return undefined;
|
||||
|
||||
const sets: string[] = [];
|
||||
const values: unknown[] = [];
|
||||
if (input.userId !== undefined) { sets.push("user_id = ?"); values.push(input.userId); }
|
||||
if (input.entityType !== undefined) { sets.push("entity_type = ?"); values.push(input.entityType); }
|
||||
if (input.entityId !== undefined) { sets.push("entity_id = ?"); values.push(input.entityId); }
|
||||
if (input.content !== undefined) { sets.push("content = ?"); values.push(input.content); }
|
||||
if (input.parentId !== undefined) { sets.push("parent_id = ?"); values.push(input.parentId); }
|
||||
|
||||
if (sets.length === 0) return existing;
|
||||
|
||||
sets.push("updated_at = ?");
|
||||
values.push(new Date().toISOString());
|
||||
|
||||
values.push(id);
|
||||
execute(`UPDATE comments SET ${sets.join(", ")} WHERE id = ?`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Delete */
|
||||
delete(id: string): boolean {
|
||||
const result = execute("DELETE FROM comments WHERE id = ?", [id]);
|
||||
return result.changes > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
// Auto-generated Contract service
|
||||
import { queryAll, queryOne, execute } from "../db/client.js";
|
||||
import type { Contract, CreateContractInput, UpdateContractInput } from "../types/index.js";
|
||||
|
||||
export class ContractService {
|
||||
/** List all contracts */
|
||||
list(): Contract[] {
|
||||
return queryAll<Contract>("SELECT * FROM contracts ORDER BY created_at DESC");
|
||||
}
|
||||
|
||||
/** Get by ID */
|
||||
getById(id: string): Contract | undefined {
|
||||
return queryOne<Contract>("SELECT * FROM contracts WHERE id = ?", [id]);
|
||||
}
|
||||
|
||||
/** Create */
|
||||
create(input: CreateContractInput): Contract {
|
||||
const id = crypto.randomUUID();
|
||||
const now = new Date().toISOString();
|
||||
const cols = ["id", "user_id", "title", "party_a", "party_b", "amount", "signed_at", "expires_at", "status", "file_url", "created_at", "updated_at"];
|
||||
const values = [id, input.userId ?? null, input.title ?? null, input.partyA ?? null, input.partyB ?? null, input.amount ?? null, input.signedAt ?? null, input.expiresAt ?? null, input.status ?? null, input.fileUrl ?? null, now, now];
|
||||
|
||||
execute(`INSERT INTO contracts (${cols.join(", ")}) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Update */
|
||||
update(id: string, input: UpdateContractInput): Contract | undefined {
|
||||
const existing = this.getById(id);
|
||||
if (!existing) return undefined;
|
||||
|
||||
const sets: string[] = [];
|
||||
const values: unknown[] = [];
|
||||
if (input.userId !== undefined) { sets.push("user_id = ?"); values.push(input.userId); }
|
||||
if (input.title !== undefined) { sets.push("title = ?"); values.push(input.title); }
|
||||
if (input.partyA !== undefined) { sets.push("party_a = ?"); values.push(input.partyA); }
|
||||
if (input.partyB !== undefined) { sets.push("party_b = ?"); values.push(input.partyB); }
|
||||
if (input.amount !== undefined) { sets.push("amount = ?"); values.push(input.amount); }
|
||||
if (input.signedAt !== undefined) { sets.push("signed_at = ?"); values.push(input.signedAt); }
|
||||
if (input.expiresAt !== undefined) { sets.push("expires_at = ?"); values.push(input.expiresAt); }
|
||||
if (input.status !== undefined) { sets.push("status = ?"); values.push(input.status); }
|
||||
if (input.fileUrl !== undefined) { sets.push("file_url = ?"); values.push(input.fileUrl); }
|
||||
|
||||
if (sets.length === 0) return existing;
|
||||
|
||||
sets.push("updated_at = ?");
|
||||
values.push(new Date().toISOString());
|
||||
|
||||
values.push(id);
|
||||
execute(`UPDATE contracts SET ${sets.join(", ")} WHERE id = ?`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Delete */
|
||||
delete(id: string): boolean {
|
||||
const result = execute("DELETE FROM contracts WHERE id = ?", [id]);
|
||||
return result.changes > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
// Auto-generated Customer service
|
||||
import { queryAll, queryOne, execute } from "../db/client.js";
|
||||
import type { Customer, CreateCustomerInput, UpdateCustomerInput } from "../types/index.js";
|
||||
|
||||
export class CustomerService {
|
||||
/** List all customers */
|
||||
list(): Customer[] {
|
||||
return queryAll<Customer>("SELECT * FROM customers ORDER BY created_at DESC");
|
||||
}
|
||||
|
||||
/** Get by ID */
|
||||
getById(id: string): Customer | undefined {
|
||||
return queryOne<Customer>("SELECT * FROM customers WHERE id = ?", [id]);
|
||||
}
|
||||
|
||||
/** Create */
|
||||
create(input: CreateCustomerInput): Customer {
|
||||
const id = crypto.randomUUID();
|
||||
const now = new Date().toISOString();
|
||||
const cols = ["id", "user_id", "name", "email", "phone", "company", "source", "tags", "created_at", "updated_at"];
|
||||
const values = [id, input.userId ?? null, input.name ?? null, input.email ?? null, input.phone ?? null, input.company ?? null, input.source ?? null, input.tags ?? null, now, now];
|
||||
|
||||
execute(`INSERT INTO customers (${cols.join(", ")}) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Update */
|
||||
update(id: string, input: UpdateCustomerInput): Customer | undefined {
|
||||
const existing = this.getById(id);
|
||||
if (!existing) return undefined;
|
||||
|
||||
const sets: string[] = [];
|
||||
const values: unknown[] = [];
|
||||
if (input.userId !== undefined) { sets.push("user_id = ?"); values.push(input.userId); }
|
||||
if (input.name !== undefined) { sets.push("name = ?"); values.push(input.name); }
|
||||
if (input.email !== undefined) { sets.push("email = ?"); values.push(input.email); }
|
||||
if (input.phone !== undefined) { sets.push("phone = ?"); values.push(input.phone); }
|
||||
if (input.company !== undefined) { sets.push("company = ?"); values.push(input.company); }
|
||||
if (input.source !== undefined) { sets.push("source = ?"); values.push(input.source); }
|
||||
if (input.tags !== undefined) { sets.push("tags = ?"); values.push(input.tags); }
|
||||
|
||||
if (sets.length === 0) return existing;
|
||||
|
||||
sets.push("updated_at = ?");
|
||||
values.push(new Date().toISOString());
|
||||
|
||||
values.push(id);
|
||||
execute(`UPDATE customers SET ${sets.join(", ")} WHERE id = ?`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Delete */
|
||||
delete(id: string): boolean {
|
||||
const result = execute("DELETE FROM customers WHERE id = ?", [id]);
|
||||
return result.changes > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// Auto-generated Item service
|
||||
import { queryAll, queryOne, execute } from "../db/client.js";
|
||||
import type { Item, CreateItemInput, UpdateItemInput } from "../types/index.js";
|
||||
|
||||
export class ItemService {
|
||||
/** List all items */
|
||||
list(): Item[] {
|
||||
return queryAll<Item>("SELECT * FROM items ORDER BY created_at DESC");
|
||||
}
|
||||
|
||||
/** Get by ID */
|
||||
getById(id: string): Item | undefined {
|
||||
return queryOne<Item>("SELECT * FROM items WHERE id = ?", [id]);
|
||||
}
|
||||
|
||||
/** Create */
|
||||
create(input: CreateItemInput): Item {
|
||||
const id = crypto.randomUUID();
|
||||
const now = new Date().toISOString();
|
||||
const hasCreatedAt = true;
|
||||
const hasUpdatedAt = false;
|
||||
const cols = ["id", "user_id", "title", "data", "created_at"];
|
||||
const placeholders = cols.map(() => "?").join(", ");
|
||||
const values = [id, input.userId ?? null, input.title ?? null, input.data ?? null, now];
|
||||
|
||||
execute(`INSERT INTO items (${cols.join(", ")}) VALUES (${placeholders})`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Update */
|
||||
update(id: string, input: UpdateItemInput): Item | undefined {
|
||||
const existing = this.getById(id);
|
||||
if (!existing) return undefined;
|
||||
|
||||
const sets: string[] = [];
|
||||
const values: unknown[] = [];
|
||||
if (input.userId !== undefined) { sets.push("user_id = ?"); values.push(input.userId); }
|
||||
if (input.title !== undefined) { sets.push("title = ?"); values.push(input.title); }
|
||||
if (input.data !== undefined) { sets.push("data = ?"); values.push(input.data); }
|
||||
|
||||
if (sets.length === 0) return existing;
|
||||
|
||||
|
||||
|
||||
values.push(id);
|
||||
execute(`UPDATE items SET ${sets.join(", ")} WHERE id = ?`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Delete */
|
||||
delete(id: string): boolean {
|
||||
const result = execute("DELETE FROM items WHERE id = ?", [id]);
|
||||
return result.changes > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
// Auto-generated Media service
|
||||
import { queryAll, queryOne, execute } from "../db/client.js";
|
||||
import type { Media, CreateMediaInput, UpdateMediaInput } from "../types/index.js";
|
||||
|
||||
export class MediaService {
|
||||
/** List all media */
|
||||
list(): Media[] {
|
||||
return queryAll<Media>("SELECT * FROM media ORDER BY created_at DESC");
|
||||
}
|
||||
|
||||
/** Get by ID */
|
||||
getById(id: string): Media | undefined {
|
||||
return queryOne<Media>("SELECT * FROM media WHERE id = ?", [id]);
|
||||
}
|
||||
|
||||
/** Create */
|
||||
create(input: CreateMediaInput): Media {
|
||||
const id = crypto.randomUUID();
|
||||
const now = new Date().toISOString();
|
||||
const cols = ["id", "user_id", "filename", "url", "mime_type", "size_bytes", "created_at", "updated_at"];
|
||||
const values = [id, input.userId ?? null, input.filename ?? null, input.url ?? null, input.mimeType ?? null, input.sizeBytes ?? null, now, now];
|
||||
|
||||
execute(`INSERT INTO media (${cols.join(", ")}) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Update */
|
||||
update(id: string, input: UpdateMediaInput): Media | undefined {
|
||||
const existing = this.getById(id);
|
||||
if (!existing) return undefined;
|
||||
|
||||
const sets: string[] = [];
|
||||
const values: unknown[] = [];
|
||||
if (input.userId !== undefined) { sets.push("user_id = ?"); values.push(input.userId); }
|
||||
if (input.filename !== undefined) { sets.push("filename = ?"); values.push(input.filename); }
|
||||
if (input.url !== undefined) { sets.push("url = ?"); values.push(input.url); }
|
||||
if (input.mimeType !== undefined) { sets.push("mime_type = ?"); values.push(input.mimeType); }
|
||||
if (input.sizeBytes !== undefined) { sets.push("size_bytes = ?"); values.push(input.sizeBytes); }
|
||||
|
||||
if (sets.length === 0) return existing;
|
||||
|
||||
sets.push("updated_at = ?");
|
||||
values.push(new Date().toISOString());
|
||||
|
||||
values.push(id);
|
||||
execute(`UPDATE media SET ${sets.join(", ")} WHERE id = ?`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Delete */
|
||||
delete(id: string): boolean {
|
||||
const result = execute("DELETE FROM media WHERE id = ?", [id]);
|
||||
return result.changes > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// Auto-generated Reminder service
|
||||
import { queryAll, queryOne, execute } from "../db/client.js";
|
||||
import type { Reminder, CreateReminderInput, UpdateReminderInput } from "../types/index.js";
|
||||
|
||||
export class ReminderService {
|
||||
/** List all reminders */
|
||||
list(): Reminder[] {
|
||||
return queryAll<Reminder>("SELECT * FROM reminders ORDER BY created_at DESC");
|
||||
}
|
||||
|
||||
/** Get by ID */
|
||||
getById(id: string): Reminder | undefined {
|
||||
return queryOne<Reminder>("SELECT * FROM reminders WHERE id = ?", [id]);
|
||||
}
|
||||
|
||||
/** Create */
|
||||
create(input: CreateReminderInput): Reminder {
|
||||
const id = crypto.randomUUID();
|
||||
const now = new Date().toISOString();
|
||||
const cols = ["id", "user_id", "entity_type", "entity_id", "remind_at", "message", "sent", "created_at", "updated_at"];
|
||||
const values = [id, input.userId ?? null, input.entityType ?? null, input.entityId ?? null, input.remindAt ?? null, input.message ?? null, input.sent ?? null, now, now];
|
||||
|
||||
execute(`INSERT INTO reminders (${cols.join(", ")}) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Update */
|
||||
update(id: string, input: UpdateReminderInput): Reminder | undefined {
|
||||
const existing = this.getById(id);
|
||||
if (!existing) return undefined;
|
||||
|
||||
const sets: string[] = [];
|
||||
const values: unknown[] = [];
|
||||
if (input.userId !== undefined) { sets.push("user_id = ?"); values.push(input.userId); }
|
||||
if (input.entityType !== undefined) { sets.push("entity_type = ?"); values.push(input.entityType); }
|
||||
if (input.entityId !== undefined) { sets.push("entity_id = ?"); values.push(input.entityId); }
|
||||
if (input.remindAt !== undefined) { sets.push("remind_at = ?"); values.push(input.remindAt); }
|
||||
if (input.message !== undefined) { sets.push("message = ?"); values.push(input.message); }
|
||||
if (input.sent !== undefined) { sets.push("sent = ?"); values.push(input.sent); }
|
||||
|
||||
if (sets.length === 0) return existing;
|
||||
|
||||
sets.push("updated_at = ?");
|
||||
values.push(new Date().toISOString());
|
||||
|
||||
values.push(id);
|
||||
execute(`UPDATE reminders SET ${sets.join(", ")} WHERE id = ?`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Delete */
|
||||
delete(id: string): boolean {
|
||||
const result = execute("DELETE FROM reminders WHERE id = ?", [id]);
|
||||
return result.changes > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
// Auto-generated Tags service
|
||||
import { queryAll, queryOne, execute } from "../db/client.js";
|
||||
import type { Tags, CreateTagsInput, UpdateTagsInput } from "../types/index.js";
|
||||
|
||||
export class TagsService {
|
||||
/** List all tags */
|
||||
list(): Tags[] {
|
||||
return queryAll<Tags>("SELECT * FROM tags ORDER BY created_at DESC");
|
||||
}
|
||||
|
||||
/** Get by ID */
|
||||
getById(id: string): Tags | undefined {
|
||||
return queryOne<Tags>("SELECT * FROM tags WHERE id = ?", [id]);
|
||||
}
|
||||
|
||||
/** Create */
|
||||
create(input: CreateTagsInput): Tags {
|
||||
const id = crypto.randomUUID();
|
||||
const now = new Date().toISOString();
|
||||
const cols = ["id", "user_id", "name", "color", "created_at", "updated_at"];
|
||||
const values = [id, input.userId ?? null, input.name ?? null, input.color ?? null, now, now];
|
||||
|
||||
execute(`INSERT INTO tags (${cols.join(", ")}) VALUES (?, ?, ?, ?, ?, ?)`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Update */
|
||||
update(id: string, input: UpdateTagsInput): Tags | undefined {
|
||||
const existing = this.getById(id);
|
||||
if (!existing) return undefined;
|
||||
|
||||
const sets: string[] = [];
|
||||
const values: unknown[] = [];
|
||||
if (input.userId !== undefined) { sets.push("user_id = ?"); values.push(input.userId); }
|
||||
if (input.name !== undefined) { sets.push("name = ?"); values.push(input.name); }
|
||||
if (input.color !== undefined) { sets.push("color = ?"); values.push(input.color); }
|
||||
|
||||
if (sets.length === 0) return existing;
|
||||
|
||||
sets.push("updated_at = ?");
|
||||
values.push(new Date().toISOString());
|
||||
|
||||
values.push(id);
|
||||
execute(`UPDATE tags SET ${sets.join(", ")} WHERE id = ?`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Delete */
|
||||
delete(id: string): boolean {
|
||||
const result = execute("DELETE FROM tags WHERE id = ?", [id]);
|
||||
return result.changes > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
// Auto-generated User service
|
||||
import { queryAll, queryOne, execute } from "../db/client.js";
|
||||
import type { User, CreateUserInput, UpdateUserInput } from "../types/index.js";
|
||||
|
||||
export class UserService {
|
||||
/** List all users */
|
||||
list(): User[] {
|
||||
return queryAll<User>("SELECT * FROM users ORDER BY created_at DESC");
|
||||
}
|
||||
|
||||
/** Get by ID */
|
||||
getById(id: string): User | undefined {
|
||||
return queryOne<User>("SELECT * FROM users WHERE id = ?", [id]);
|
||||
}
|
||||
|
||||
/** Create */
|
||||
create(input: CreateUserInput): User {
|
||||
const id = crypto.randomUUID();
|
||||
const now = new Date().toISOString();
|
||||
const hasCreatedAt = true;
|
||||
const hasUpdatedAt = true;
|
||||
const cols = ["id", "phone", "nickname", "avatar_url", "created_at", "updated_at"];
|
||||
const placeholders = cols.map(() => "?").join(", ");
|
||||
const values = [id, input.phone ?? null, input.nickname ?? null, input.avatarUrl ?? null, now, now];
|
||||
|
||||
execute(`INSERT INTO users (${cols.join(", ")}) VALUES (${placeholders})`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Update */
|
||||
update(id: string, input: UpdateUserInput): User | undefined {
|
||||
const existing = this.getById(id);
|
||||
if (!existing) return undefined;
|
||||
|
||||
const sets: string[] = [];
|
||||
const values: unknown[] = [];
|
||||
if (input.phone !== undefined) { sets.push("phone = ?"); values.push(input.phone); }
|
||||
if (input.nickname !== undefined) { sets.push("nickname = ?"); values.push(input.nickname); }
|
||||
if (input.avatarUrl !== undefined) { sets.push("avatar_url = ?"); values.push(input.avatarUrl); }
|
||||
|
||||
if (sets.length === 0) return existing;
|
||||
|
||||
sets.push("updated_at = ?");
|
||||
values.push(new Date().toISOString());
|
||||
|
||||
values.push(id);
|
||||
execute(`UPDATE users SET ${sets.join(", ")} WHERE id = ?`, values);
|
||||
return this.getById(id)!;
|
||||
}
|
||||
|
||||
/** Delete */
|
||||
delete(id: string): boolean {
|
||||
const result = execute("DELETE FROM users WHERE id = ?", [id]);
|
||||
return result.changes > 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
// Augment Fastify request with JWT user
|
||||
import type { JwtPayload } from "./index.js";
|
||||
|
||||
declare module "fastify" {
|
||||
interface FastifyRequest {
|
||||
user?: JwtPayload;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,320 @@
|
||||
// Auto-generated types (from Model Contract)
|
||||
|
||||
export interface User {
|
||||
id?: string;
|
||||
username: string;
|
||||
passwordHash: string;
|
||||
nickname?: string;
|
||||
role?: string;
|
||||
phone?: string;
|
||||
avatarUrl?: string;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export interface Contract {
|
||||
id?: string;
|
||||
userId: string;
|
||||
title: string;
|
||||
partyA?: string;
|
||||
partyB?: string;
|
||||
amount?: number;
|
||||
signedAt?: string;
|
||||
expiresAt?: string;
|
||||
status?: string;
|
||||
fileUrl?: string;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export interface Approval {
|
||||
id?: string;
|
||||
userId: string;
|
||||
entityType: string;
|
||||
entityId?: string;
|
||||
applicantId: string;
|
||||
status?: string;
|
||||
formData?: Record<string, unknown>;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export interface Customer {
|
||||
id?: string;
|
||||
userId: string;
|
||||
name: string;
|
||||
email?: string;
|
||||
phone?: string;
|
||||
company?: string;
|
||||
source?: string;
|
||||
tags?: Record<string, unknown>;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export interface Reminder {
|
||||
id?: string;
|
||||
userId: string;
|
||||
entityType: string;
|
||||
entityId?: string;
|
||||
remindAt: string;
|
||||
message?: string;
|
||||
sent?: boolean;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export interface Articles {
|
||||
id: string;
|
||||
userId?: string;
|
||||
title: string;
|
||||
content?: string;
|
||||
excerpt?: string;
|
||||
coverUrl?: string;
|
||||
status?: string;
|
||||
authorId?: string;
|
||||
category?: string;
|
||||
publishedAt?: string;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export interface Tags {
|
||||
id: string;
|
||||
userId?: string;
|
||||
name: string;
|
||||
color?: string;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export interface Comments {
|
||||
id: string;
|
||||
userId?: string;
|
||||
entityType: string;
|
||||
entityId: string;
|
||||
content: string;
|
||||
parentId?: string;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export interface Media {
|
||||
id: string;
|
||||
userId?: string;
|
||||
filename: string;
|
||||
url: string;
|
||||
mimeType?: string;
|
||||
sizeBytes?: number;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
}
|
||||
|
||||
export interface CreateUserInput {
|
||||
username: string;
|
||||
passwordHash: string;
|
||||
nickname?: string;
|
||||
role?: string;
|
||||
phone?: string;
|
||||
avatarUrl?: string;
|
||||
}
|
||||
|
||||
export interface CreateContractInput {
|
||||
userId: string;
|
||||
title: string;
|
||||
partyA?: string;
|
||||
partyB?: string;
|
||||
amount?: number;
|
||||
signedAt?: string;
|
||||
expiresAt?: string;
|
||||
status?: string;
|
||||
fileUrl?: string;
|
||||
}
|
||||
|
||||
export interface CreateApprovalInput {
|
||||
userId: string;
|
||||
entityType: string;
|
||||
entityId?: string;
|
||||
applicantId: string;
|
||||
status?: string;
|
||||
formData?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface CreateCustomerInput {
|
||||
userId: string;
|
||||
name: string;
|
||||
email?: string;
|
||||
phone?: string;
|
||||
company?: string;
|
||||
source?: string;
|
||||
tags?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface CreateReminderInput {
|
||||
userId: string;
|
||||
entityType: string;
|
||||
entityId?: string;
|
||||
remindAt: string;
|
||||
message?: string;
|
||||
sent?: boolean;
|
||||
}
|
||||
|
||||
export interface CreateArticlesInput {
|
||||
userId?: string;
|
||||
title: string;
|
||||
content?: string;
|
||||
excerpt?: string;
|
||||
coverUrl?: string;
|
||||
authorId?: string;
|
||||
category?: string;
|
||||
publishedAt?: string;
|
||||
}
|
||||
|
||||
export interface CreateTagsInput {
|
||||
userId?: string;
|
||||
name: string;
|
||||
color?: string;
|
||||
}
|
||||
|
||||
export interface CreateCommentsInput {
|
||||
userId?: string;
|
||||
entityType: string;
|
||||
entityId: string;
|
||||
content: string;
|
||||
parentId?: string;
|
||||
}
|
||||
|
||||
export interface CreateMediaInput {
|
||||
userId?: string;
|
||||
filename: string;
|
||||
url: string;
|
||||
mimeType?: string;
|
||||
sizeBytes?: number;
|
||||
}
|
||||
|
||||
export interface UpdateUserInput {
|
||||
username?: string;
|
||||
passwordHash?: string;
|
||||
nickname?: string;
|
||||
role?: string;
|
||||
phone?: string;
|
||||
avatarUrl?: string;
|
||||
}
|
||||
|
||||
export interface UpdateContractInput {
|
||||
userId?: string;
|
||||
title?: string;
|
||||
partyA?: string;
|
||||
partyB?: string;
|
||||
amount?: number;
|
||||
signedAt?: string;
|
||||
expiresAt?: string;
|
||||
status?: string;
|
||||
fileUrl?: string;
|
||||
}
|
||||
|
||||
export interface UpdateApprovalInput {
|
||||
userId?: string;
|
||||
entityType?: string;
|
||||
entityId?: string;
|
||||
applicantId?: string;
|
||||
status?: string;
|
||||
formData?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface UpdateCustomerInput {
|
||||
userId?: string;
|
||||
name?: string;
|
||||
email?: string;
|
||||
phone?: string;
|
||||
company?: string;
|
||||
source?: string;
|
||||
tags?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface UpdateReminderInput {
|
||||
userId?: string;
|
||||
entityType?: string;
|
||||
entityId?: string;
|
||||
remindAt?: string;
|
||||
message?: string;
|
||||
sent?: boolean;
|
||||
}
|
||||
|
||||
export interface UpdateArticlesInput {
|
||||
userId?: string;
|
||||
title?: string;
|
||||
content?: string;
|
||||
excerpt?: string;
|
||||
coverUrl?: string;
|
||||
authorId?: string;
|
||||
category?: string;
|
||||
publishedAt?: string;
|
||||
}
|
||||
|
||||
export interface UpdateTagsInput {
|
||||
userId?: string;
|
||||
name?: string;
|
||||
color?: string;
|
||||
}
|
||||
|
||||
export interface UpdateCommentsInput {
|
||||
userId?: string;
|
||||
entityType?: string;
|
||||
entityId?: string;
|
||||
content?: string;
|
||||
parentId?: string;
|
||||
}
|
||||
|
||||
export interface UpdateMediaInput {
|
||||
userId?: string;
|
||||
filename?: string;
|
||||
url?: string;
|
||||
mimeType?: string;
|
||||
sizeBytes?: number;
|
||||
}
|
||||
|
||||
// ─── Auth ───────────────────────────────────────────
|
||||
export interface LoginInput {
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
export interface RegisterInput {
|
||||
username: string;
|
||||
password: string;
|
||||
nickname?: string;
|
||||
}
|
||||
|
||||
export interface AuthResponse {
|
||||
token: string;
|
||||
user: User;
|
||||
}
|
||||
|
||||
// ─── API ────────────────────────────────────────────
|
||||
export interface ApiResponse<T> {
|
||||
data: T;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
export interface PaginatedResponse<T> {
|
||||
data: T[];
|
||||
total: number;
|
||||
page: number;
|
||||
pageSize: number;
|
||||
}
|
||||
|
||||
export interface ErrorResponse {
|
||||
error: string;
|
||||
message: string;
|
||||
statusCode: number;
|
||||
}
|
||||
|
||||
// ─── JWT ────────────────────────────────────────────
|
||||
export interface JwtPayload {
|
||||
userId: string;
|
||||
username: string;
|
||||
role: string;
|
||||
iat?: number;
|
||||
exp?: number;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
// Type declarations for sql.js (no native types package available)
|
||||
declare module "sql.js" {
|
||||
export interface Database {
|
||||
run(sql: string, params?: BindParams): void;
|
||||
exec(sql: string): void;
|
||||
prepare(sql: string): Statement;
|
||||
export(): Uint8Array;
|
||||
close(): void;
|
||||
getRowsModified(): number;
|
||||
}
|
||||
|
||||
export interface Statement {
|
||||
bind(params?: BindParams): boolean;
|
||||
step(): boolean;
|
||||
getAsObject<T = Record<string, unknown>>(): T;
|
||||
getColumnNames(): string[];
|
||||
free(): boolean;
|
||||
}
|
||||
|
||||
export type BindParams = unknown[] | Record<string, unknown>;
|
||||
|
||||
export interface SqlJsStatic {
|
||||
Database: new (data?: ArrayLike<number>) => Database;
|
||||
}
|
||||
|
||||
export default function initSqlJs(config?: Record<string, unknown>): Promise<SqlJsStatic>;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "bundler",
|
||||
"lib": [
|
||||
"ES2022"
|
||||
],
|
||||
"outDir": "./dist",
|
||||
"rootDir": "./src",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"resolveJsonModule": true,
|
||||
"declaration": true,
|
||||
"sourceMap": true
|
||||
},
|
||||
"include": [
|
||||
"src/**/*"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
"dist",
|
||||
"src/__tests__"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
# socialapp — Desktop
|
||||
|
||||
> Electron desktop application wrapping the socialapp fullstack web project.
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
Web Fullstack Project
|
||||
×
|
||||
Desktop Shell (Electron)
|
||||
=
|
||||
Desktop Application
|
||||
```
|
||||
|
||||
- **Main Process** — Window management, menu, tray, IPC, auto-update
|
||||
- **Preload** — Secure bridge between main and renderer
|
||||
- **Renderer** — Your existing web frontend (Next.js)
|
||||
|
||||
## Development
|
||||
|
||||
```bash
|
||||
# Install dependencies
|
||||
npm install
|
||||
|
||||
# Start dev mode (web + api + electron concurrently)
|
||||
npm run dev
|
||||
```
|
||||
|
||||
Dev mode:
|
||||
- Web frontend: `http://localhost:3000`
|
||||
- API backend: `http://localhost:3001`
|
||||
- Electron loads the web URL directly
|
||||
|
||||
## Build
|
||||
|
||||
```bash
|
||||
# Build for current platform
|
||||
npm run build
|
||||
|
||||
# Platform-specific builds
|
||||
npm run build:electron:mac
|
||||
npm run build:electron:win
|
||||
npm run build:electron:linux
|
||||
```
|
||||
|
||||
Output: `release/` directory with platform installers.
|
||||
|
||||
## Project Structure
|
||||
|
||||
```
|
||||
electron/
|
||||
├── main.ts — Main process (BrowserWindow, Menu, lifecycle)
|
||||
├── preload.ts — Secure IPC bridge (contextBridge)
|
||||
├── ipc.ts — IPC handlers (dialogs, store, notifications)
|
||||
├── tray.ts — System tray
|
||||
├── updater.ts — Auto-update (electron-updater)
|
||||
└── utils.ts — Shared utilities
|
||||
```
|
||||
|
||||
## IPC API (available in renderer)
|
||||
|
||||
```typescript
|
||||
window.electronAPI.getAppVersion()
|
||||
window.electronAPI.getPlatform()
|
||||
window.electronAPI.minimizeWindow()
|
||||
window.electronAPI.maximizeWindow()
|
||||
window.electronAPI.closeWindow()
|
||||
window.electronAPI.openFile(options?)
|
||||
window.electronAPI.saveFile(options?)
|
||||
window.electronAPI.showNotification(title, body)
|
||||
window.electronAPI.openExternal(url)
|
||||
window.electronAPI.storeGet(key)
|
||||
window.electronAPI.storeSet(key, value)
|
||||
window.electronAPI.checkForUpdates()
|
||||
window.electronAPI.downloadUpdate()
|
||||
window.electronAPI.quitAndInstall()
|
||||
```
|
||||
|
||||
## Auto Update
|
||||
|
||||
Configured via `electron-builder.yml`. Uses `electron-updater` with generic provider.
|
||||
Set your release URL in the `publish` section.
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 66 B |
@@ -0,0 +1,100 @@
|
||||
# electron-builder.yml — Auto-generated by SF-06
|
||||
|
||||
appId: com.socialapp.desktop
|
||||
productName: socialapp
|
||||
copyright: "Copyright © 2026 socialapp"
|
||||
|
||||
# ─── Directories ─────────────────────────────────
|
||||
directories:
|
||||
output: release
|
||||
buildResources: assets
|
||||
|
||||
# ─── Files ───────────────────────────────────────
|
||||
files:
|
||||
- dist/electron/**/*
|
||||
- "!node_modules/**/*"
|
||||
- "**/*.node"
|
||||
|
||||
# ─── Extra Resources ─────────────────────────────
|
||||
extraResources:
|
||||
- from: "../web/out"
|
||||
to: "web"
|
||||
filter:
|
||||
- "**/*"
|
||||
- from: "../api/dist"
|
||||
to: "api"
|
||||
filter:
|
||||
- "**/*"
|
||||
- from: "../api/data"
|
||||
to: "data"
|
||||
filter:
|
||||
- "**/*"
|
||||
|
||||
# ─── macOS ───────────────────────────────────────
|
||||
mac:
|
||||
category: public.app-category.productivity
|
||||
icon: assets/icon.png
|
||||
target:
|
||||
- target: dmg
|
||||
arch:
|
||||
- x64
|
||||
- arm64
|
||||
- target: zip
|
||||
arch:
|
||||
- x64
|
||||
- arm64
|
||||
darkModeSupport: true
|
||||
hardenedRuntime: true
|
||||
gatekeeperAssess: false
|
||||
entitlements: entitlements.mac.plist
|
||||
entitlementsInherit: entitlements.mac.plist
|
||||
|
||||
# ─── Windows ─────────────────────────────────────
|
||||
win:
|
||||
icon: assets/icon.png
|
||||
target:
|
||||
- target: nsis
|
||||
arch:
|
||||
- x64
|
||||
- target: portable
|
||||
arch:
|
||||
- x64
|
||||
publisherName: socialapp
|
||||
|
||||
nsis:
|
||||
oneClick: false
|
||||
perMachine: false
|
||||
allowToChangeInstallationDirectory: true
|
||||
installerIcon: assets/icon.ico
|
||||
uninstallerIcon: assets/icon.ico
|
||||
installerHeaderIcon: assets/icon.ico
|
||||
createDesktopShortcut: true
|
||||
createStartMenuShortcut: true
|
||||
shortcutName: socialapp
|
||||
|
||||
# ─── Linux ───────────────────────────────────────
|
||||
linux:
|
||||
icon: assets/icon.png
|
||||
category: Utility
|
||||
target:
|
||||
- target: AppImage
|
||||
arch:
|
||||
- x64
|
||||
- target: deb
|
||||
arch:
|
||||
- x64
|
||||
- target: rpm
|
||||
arch:
|
||||
- x64
|
||||
desktop:
|
||||
Name: socialapp
|
||||
Comment: socialapp Desktop Application
|
||||
Terminal: false
|
||||
Type: Application
|
||||
Categories: Utility;
|
||||
|
||||
# ─── Auto Update ─────────────────────────────────
|
||||
publish:
|
||||
provider: generic
|
||||
url: https://releases.example.com/socialapp/
|
||||
channel: latest
|
||||
@@ -0,0 +1,77 @@
|
||||
import { ipcMain, app, dialog, shell, BrowserWindow, Notification } from "electron";
|
||||
import Store from "electron-store";
|
||||
|
||||
const store = new Store() as any;
|
||||
|
||||
export function setupIPC(mainWindow: BrowserWindow): void {
|
||||
// ── App Info ──
|
||||
ipcMain.handle("app:version", () => app.getVersion());
|
||||
ipcMain.handle("app:platform", () => process.platform);
|
||||
ipcMain.handle("app:isDev", () => !app.isPackaged);
|
||||
|
||||
// ── Window Controls ──
|
||||
ipcMain.on("window:minimize", () => {
|
||||
mainWindow?.minimize();
|
||||
});
|
||||
|
||||
ipcMain.on("window:maximize", () => {
|
||||
if (mainWindow?.isMaximized()) {
|
||||
mainWindow.unmaximize();
|
||||
} else {
|
||||
mainWindow?.maximize();
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.on("window:close", () => {
|
||||
mainWindow?.close();
|
||||
});
|
||||
|
||||
ipcMain.handle("window:isMaximized", () => {
|
||||
return mainWindow?.isMaximized() ?? false;
|
||||
});
|
||||
|
||||
// ── File Dialogs ──
|
||||
ipcMain.handle("dialog:openFile", async (_event, options?) => {
|
||||
const result = await dialog.showOpenDialog(mainWindow, {
|
||||
properties: ["openFile"],
|
||||
...options,
|
||||
});
|
||||
return result.canceled ? undefined : result.filePaths;
|
||||
});
|
||||
|
||||
ipcMain.handle("dialog:saveFile", async (_event, options?) => {
|
||||
const result = await dialog.showSaveDialog(mainWindow, {
|
||||
...options,
|
||||
});
|
||||
return result.canceled ? undefined : result.filePath;
|
||||
});
|
||||
|
||||
// ── Notifications ──
|
||||
ipcMain.on("notification:show", (_event, { title, body }) => {
|
||||
if (Notification.isSupported()) {
|
||||
new Notification({ title, body }).show();
|
||||
}
|
||||
});
|
||||
|
||||
// ── Shell ──
|
||||
ipcMain.on("shell:openExternal", (_event, url: string) => {
|
||||
shell.openExternal(url);
|
||||
});
|
||||
|
||||
ipcMain.on("shell:openPath", (_event, path: string) => {
|
||||
shell.openPath(path);
|
||||
});
|
||||
|
||||
// ── Persistent Store ──
|
||||
ipcMain.handle("store:get", (_event, key: string) => {
|
||||
return store.get(key);
|
||||
});
|
||||
|
||||
ipcMain.handle("store:set", (_event, key: string, value: unknown) => {
|
||||
store.set(key, value);
|
||||
});
|
||||
|
||||
ipcMain.handle("store:delete", (_event, key: string) => {
|
||||
store.delete(key);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
import { app, BrowserWindow, Menu, nativeImage, ipcMain } from "electron";
|
||||
import { join } from "path";
|
||||
import { existsSync } from "fs";
|
||||
import { setupTray } from "./tray";
|
||||
import { setupIPC } from "./ipc";
|
||||
import { setupUpdater } from "./updater";
|
||||
import { checkSingleInstance, getWebUrl, getApiPath, isDev } from "./utils";
|
||||
|
||||
// ─── Constants ───────────────────────────────────
|
||||
const WEB_PORT = 3000;
|
||||
const API_PORT = 3001;
|
||||
const APP_NAME = "socialapp";
|
||||
|
||||
// ─── Single Instance Lock ────────────────────────
|
||||
if (!checkSingleInstance()) {
|
||||
app.quit();
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
// ─── State ───────────────────────────────────────
|
||||
let mainWindow: BrowserWindow | null = null;
|
||||
let apiProcess: ReturnType<typeof import("child_process").spawn> | null = null;
|
||||
|
||||
// ─── Create Window ───────────────────────────────
|
||||
function createWindow(): BrowserWindow {
|
||||
const preloadPath = join(__dirname, "preload.js");
|
||||
|
||||
const win = new BrowserWindow({
|
||||
title: APP_NAME,
|
||||
width: 1400,
|
||||
height: 900,
|
||||
minWidth: 800,
|
||||
minHeight: 600,
|
||||
show: false,
|
||||
icon: getIconPath(),
|
||||
webPreferences: {
|
||||
preload: preloadPath,
|
||||
contextIsolation: true,
|
||||
nodeIntegration: false,
|
||||
sandbox: false,
|
||||
},
|
||||
titleBarStyle: process.platform === "darwin" ? "hiddenInset" : "default",
|
||||
trafficLightPosition: { x: 16, y: 16 },
|
||||
});
|
||||
|
||||
// ── Load content ──
|
||||
const url = getWebUrl(isDev(), WEB_PORT);
|
||||
if (url.startsWith("http")) {
|
||||
win.loadURL(url);
|
||||
} else {
|
||||
win.loadFile(url);
|
||||
}
|
||||
|
||||
// ── Show when ready ──
|
||||
win.once("ready-to-show", () => {
|
||||
win.show();
|
||||
if (isDev()) {
|
||||
win.webContents.openDevTools({ mode: "detach" });
|
||||
}
|
||||
});
|
||||
|
||||
// ── External links → default browser ──
|
||||
win.webContents.setWindowOpenHandler(({ url }) => {
|
||||
if (url.startsWith("http")) {
|
||||
require("electron").shell.openExternal(url);
|
||||
}
|
||||
return { action: "deny" };
|
||||
});
|
||||
|
||||
// ── Prevent navigation away ──
|
||||
win.webContents.on("will-navigate", (event, url) => {
|
||||
const allowed = isDev()
|
||||
? url.startsWith("http://localhost:3000")
|
||||
: url.startsWith("file://");
|
||||
if (!allowed) event.preventDefault();
|
||||
});
|
||||
|
||||
return win;
|
||||
}
|
||||
|
||||
// ─── Icon Path ───────────────────────────────────
|
||||
function getIconPath(): string {
|
||||
const candidates = [
|
||||
join(__dirname, "..", "assets", "icon.png"),
|
||||
join(__dirname, "..", "assets", "icon.icns"),
|
||||
join(__dirname, "..", "assets", "icon.ico"),
|
||||
];
|
||||
for (const p of candidates) {
|
||||
if (existsSync(p)) return p;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
// ─── API Server (Production) ─────────────────────
|
||||
function startApiServer(): void {
|
||||
if (isDev()) return;
|
||||
|
||||
const apiPath = getApiPath();
|
||||
if (!apiPath || !existsSync(apiPath)) {
|
||||
console.warn("[main] API server not found at", apiPath);
|
||||
return;
|
||||
}
|
||||
|
||||
const { spawn } = require("child_process");
|
||||
apiProcess = spawn("node", [apiPath], {
|
||||
env: {
|
||||
...process.env,
|
||||
NODE_ENV: "production",
|
||||
PORT: String(API_PORT),
|
||||
HOST: "127.0.0.1",
|
||||
},
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
|
||||
apiProcess!.stdout?.on("data", (data: Buffer) => {
|
||||
console.log("[api]", data.toString().trim());
|
||||
});
|
||||
apiProcess!.stderr?.on("data", (data: Buffer) => {
|
||||
console.error("[api]", data.toString().trim());
|
||||
});
|
||||
apiProcess!.on("exit", (code: number) => {
|
||||
console.warn("[main] API server exited with code", code);
|
||||
apiProcess = null;
|
||||
});
|
||||
}
|
||||
|
||||
function stopApiServer(): void {
|
||||
if (apiProcess) {
|
||||
apiProcess.kill("SIGTERM");
|
||||
apiProcess = null;
|
||||
}
|
||||
}
|
||||
|
||||
// ─── Menu ────────────────────────────────────────
|
||||
function buildMenu(): void {
|
||||
const isMac = process.platform === "darwin";
|
||||
|
||||
const template: Electron.MenuItemConstructorOptions[] = [
|
||||
...(isMac
|
||||
? [{
|
||||
label: APP_NAME,
|
||||
submenu: [
|
||||
{ role: "about" as const },
|
||||
{ type: "separator" as const },
|
||||
{ role: "services" as const },
|
||||
{ type: "separator" as const },
|
||||
{ role: "hide" as const },
|
||||
{ role: "hideOthers" as const },
|
||||
{ role: "unhide" as const },
|
||||
{ type: "separator" as const },
|
||||
{ role: "quit" as const },
|
||||
],
|
||||
}]
|
||||
: []),
|
||||
{
|
||||
label: "Edit",
|
||||
submenu: [
|
||||
{ role: "undo" },
|
||||
{ role: "redo" },
|
||||
{ type: "separator" },
|
||||
{ role: "cut" },
|
||||
{ role: "copy" },
|
||||
{ role: "paste" },
|
||||
{ role: "selectAll" },
|
||||
],
|
||||
},
|
||||
{
|
||||
label: "View",
|
||||
submenu: [
|
||||
{ role: "reload" },
|
||||
{ role: "forceReload" },
|
||||
{ role: "toggleDevTools" },
|
||||
{ type: "separator" },
|
||||
{ role: "resetZoom" },
|
||||
{ role: "zoomIn" },
|
||||
{ role: "zoomOut" },
|
||||
{ type: "separator" },
|
||||
{ role: "togglefullscreen" },
|
||||
],
|
||||
},
|
||||
{
|
||||
label: "Window",
|
||||
submenu: [
|
||||
{ role: "minimize" },
|
||||
{ role: "zoom" },
|
||||
...(isMac
|
||||
? [
|
||||
{ type: "separator" as const },
|
||||
{ role: "front" as const },
|
||||
]
|
||||
: [{ role: "close" as const }]),
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
Menu.setApplicationMenu(Menu.buildFromTemplate(template));
|
||||
}
|
||||
|
||||
// ─── App Lifecycle ───────────────────────────────
|
||||
app.whenReady().then(() => {
|
||||
startApiServer();
|
||||
mainWindow = createWindow();
|
||||
buildMenu();
|
||||
if (mainWindow) {
|
||||
setupTray(mainWindow, APP_NAME);
|
||||
setupIPC(mainWindow);
|
||||
}
|
||||
setupUpdater(APP_NAME);
|
||||
|
||||
app.on("activate", () => {
|
||||
if (BrowserWindow.getAllWindows().length === 0) {
|
||||
mainWindow = createWindow();
|
||||
if (mainWindow) {
|
||||
setupTray(mainWindow, APP_NAME);
|
||||
setupIPC(mainWindow);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
app.on("window-all-closed", () => {
|
||||
stopApiServer();
|
||||
if (process.platform !== "darwin") {
|
||||
app.quit();
|
||||
}
|
||||
});
|
||||
|
||||
app.on("before-quit", () => {
|
||||
stopApiServer();
|
||||
});
|
||||
|
||||
app.on("gpu-process-crashed" as any, () => {
|
||||
console.warn("[main] GPU process crashed — continuing");
|
||||
});
|
||||
|
||||
process.on("exit", () => {
|
||||
stopApiServer();
|
||||
});
|
||||
@@ -0,0 +1,92 @@
|
||||
import { contextBridge, ipcRenderer } from "electron";
|
||||
|
||||
// ─── Expose safe API to renderer ─────────────────
|
||||
contextBridge.exposeInMainWorld("electronAPI", {
|
||||
// ── App Info ──
|
||||
getAppVersion: () => ipcRenderer.invoke("app:version"),
|
||||
getPlatform: () => ipcRenderer.invoke("app:platform"),
|
||||
getIsDev: () => ipcRenderer.invoke("app:isDev"),
|
||||
|
||||
// ── Window Controls ──
|
||||
minimizeWindow: () => ipcRenderer.send("window:minimize"),
|
||||
maximizeWindow: () => ipcRenderer.send("window:maximize"),
|
||||
closeWindow: () => ipcRenderer.send("window:close"),
|
||||
isMaximized: () => ipcRenderer.invoke("window:isMaximized"),
|
||||
|
||||
// ── File Dialogs ──
|
||||
openFile: (options?: Electron.OpenDialogOptions) =>
|
||||
ipcRenderer.invoke("dialog:openFile", options),
|
||||
saveFile: (options?: Electron.SaveDialogOptions) =>
|
||||
ipcRenderer.invoke("dialog:saveFile", options),
|
||||
|
||||
// ── Notifications ──
|
||||
showNotification: (title: string, body: string) =>
|
||||
ipcRenderer.send("notification:show", { title, body }),
|
||||
|
||||
// ── Shell ──
|
||||
openExternal: (url: string) => ipcRenderer.send("shell:openExternal", url),
|
||||
openPath: (path: string) => ipcRenderer.send("shell:openPath", path),
|
||||
|
||||
// ── Store ──
|
||||
storeGet: (key: string) => ipcRenderer.invoke("store:get", key),
|
||||
storeSet: (key: string, value: unknown) =>
|
||||
ipcRenderer.invoke("store:set", key, value),
|
||||
storeDelete: (key: string) => ipcRenderer.invoke("store:delete", key),
|
||||
|
||||
// ── Updates ──
|
||||
checkForUpdates: () => ipcRenderer.invoke("updater:check"),
|
||||
downloadUpdate: () => ipcRenderer.invoke("updater:download"),
|
||||
quitAndInstall: () => ipcRenderer.send("updater:quitAndInstall"),
|
||||
|
||||
// ── Update Events ──
|
||||
onUpdateAvailable: (callback: (info: unknown) => void) => {
|
||||
ipcRenderer.on("updater:available", (_event, info) => callback(info));
|
||||
},
|
||||
onUpdateDownloaded: (callback: (info: unknown) => void) => {
|
||||
ipcRenderer.on("updater:downloaded", (_event, info) => callback(info));
|
||||
},
|
||||
onUpdateProgress: (callback: (progress: unknown) => void) => {
|
||||
ipcRenderer.on("updater:progress", (_event, progress) => callback(progress));
|
||||
},
|
||||
onUpdateError: (callback: (error: string) => void) => {
|
||||
ipcRenderer.on("updater:error", (_event, error) => callback(error));
|
||||
},
|
||||
|
||||
// ── Remove listener ──
|
||||
removeAllListeners: (channel: string) => {
|
||||
ipcRenderer.removeAllListeners(channel);
|
||||
},
|
||||
});
|
||||
|
||||
// ─── Type declarations for renderer ──────────────
|
||||
export interface ElectronAPI {
|
||||
getAppVersion: () => Promise<string>;
|
||||
getPlatform: () => Promise<string>;
|
||||
getIsDev: () => Promise<boolean>;
|
||||
minimizeWindow: () => void;
|
||||
maximizeWindow: () => void;
|
||||
closeWindow: () => void;
|
||||
isMaximized: () => Promise<boolean>;
|
||||
openFile: (options?: Electron.OpenDialogOptions) => Promise<string[] | undefined>;
|
||||
saveFile: (options?: Electron.SaveDialogOptions) => Promise<string | undefined>;
|
||||
showNotification: (title: string, body: string) => void;
|
||||
openExternal: (url: string) => void;
|
||||
openPath: (path: string) => void;
|
||||
storeGet: (key: string) => Promise<unknown>;
|
||||
storeSet: (key: string, value: unknown) => Promise<void>;
|
||||
storeDelete: (key: string) => Promise<void>;
|
||||
checkForUpdates: () => Promise<void>;
|
||||
downloadUpdate: () => Promise<void>;
|
||||
quitAndInstall: () => void;
|
||||
onUpdateAvailable: (callback: (info: unknown) => void) => void;
|
||||
onUpdateDownloaded: (callback: (info: unknown) => void) => void;
|
||||
onUpdateProgress: (callback: (progress: unknown) => void) => void;
|
||||
onUpdateError: (callback: (error: string) => void) => void;
|
||||
removeAllListeners: (channel: string) => void;
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
electronAPI: ElectronAPI;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
import { Tray, Menu, nativeImage, BrowserWindow, app } from "electron";
|
||||
import { join } from "path";
|
||||
import { existsSync } from "fs";
|
||||
|
||||
let tray: Tray | null = null;
|
||||
|
||||
export function setupTray(mainWindow: BrowserWindow, appName: string): void {
|
||||
const iconPath = getTrayIcon();
|
||||
if (!iconPath) {
|
||||
console.warn("[tray] No icon found, skipping tray setup");
|
||||
return;
|
||||
}
|
||||
|
||||
const icon = nativeImage.createFromPath(iconPath);
|
||||
tray = new Tray(icon.resize({ width: 16, height: 16 }));
|
||||
tray.setToolTip(appName);
|
||||
|
||||
const contextMenu = Menu.buildFromTemplate([
|
||||
{
|
||||
label: "Show " + appName,
|
||||
click: () => {
|
||||
mainWindow.show();
|
||||
mainWindow.focus();
|
||||
},
|
||||
},
|
||||
{ type: "separator" },
|
||||
{
|
||||
label: "Quit",
|
||||
click: () => {
|
||||
app.quit();
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
tray.setContextMenu(contextMenu);
|
||||
|
||||
tray.on("click", () => {
|
||||
if (mainWindow.isVisible()) {
|
||||
mainWindow.focus();
|
||||
} else {
|
||||
mainWindow.show();
|
||||
}
|
||||
});
|
||||
|
||||
tray.on("double-click", () => {
|
||||
mainWindow.show();
|
||||
mainWindow.focus();
|
||||
});
|
||||
}
|
||||
|
||||
function getTrayIcon(): string | null {
|
||||
const candidates = [
|
||||
join(__dirname, "..", "assets", "tray-icon.png"),
|
||||
join(__dirname, "..", "assets", "icon.png"),
|
||||
join(__dirname, "..", "assets", "icon.ico"),
|
||||
];
|
||||
for (const p of candidates) {
|
||||
if (existsSync(p)) return p;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import { autoUpdater } from "electron-updater";
|
||||
import { ipcMain, BrowserWindow } from "electron";
|
||||
|
||||
let updateAvailable = false;
|
||||
let updateDownloaded = false;
|
||||
|
||||
export function setupUpdater(appName: string): void {
|
||||
autoUpdater.autoDownload = false;
|
||||
autoUpdater.autoInstallOnAppQuit = true;
|
||||
autoUpdater.logger = {
|
||||
info: (msg) => console.log("[updater]", msg),
|
||||
warn: (msg) => console.warn("[updater]", msg),
|
||||
error: (msg) => console.error("[updater]", msg),
|
||||
debug: (msg) => console.debug("[updater]", msg),
|
||||
};
|
||||
|
||||
autoUpdater.on("checking-for-update", () => {
|
||||
console.log("[updater] Checking for updates...");
|
||||
});
|
||||
|
||||
autoUpdater.on("update-available", (info) => {
|
||||
updateAvailable = true;
|
||||
console.log("[updater] Update available:", info.version);
|
||||
broadcast("updater:available", info);
|
||||
});
|
||||
|
||||
autoUpdater.on("update-not-available", (info) => {
|
||||
console.log("[updater] Up to date:", info.version);
|
||||
});
|
||||
|
||||
autoUpdater.on("download-progress", (progress) => {
|
||||
broadcast("updater:progress", {
|
||||
percent: progress.percent,
|
||||
bytesPerSecond: progress.bytesPerSecond,
|
||||
transferred: progress.transferred,
|
||||
total: progress.total,
|
||||
});
|
||||
});
|
||||
|
||||
autoUpdater.on("update-downloaded", (info) => {
|
||||
updateDownloaded = true;
|
||||
console.log("[updater] Update downloaded:", info.version);
|
||||
broadcast("updater:downloaded", info);
|
||||
});
|
||||
|
||||
autoUpdater.on("error", (err) => {
|
||||
console.error("[updater] Error:", err.message);
|
||||
broadcast("updater:error", err.message);
|
||||
});
|
||||
|
||||
ipcMain.handle("updater:check", async () => {
|
||||
try {
|
||||
const result = await autoUpdater.checkForUpdates();
|
||||
return result?.updateInfo ?? null;
|
||||
} catch (err) {
|
||||
console.error("[updater] Check failed:", err);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.handle("updater:download", async () => {
|
||||
if (!updateAvailable) return;
|
||||
try {
|
||||
await autoUpdater.downloadUpdate();
|
||||
} catch (err) {
|
||||
console.error("[updater] Download failed:", err);
|
||||
}
|
||||
});
|
||||
|
||||
ipcMain.on("updater:quitAndInstall", () => {
|
||||
if (updateDownloaded) {
|
||||
autoUpdater.quitAndInstall(false, true);
|
||||
}
|
||||
});
|
||||
|
||||
setTimeout(() => {
|
||||
autoUpdater.checkForUpdates().catch(() => {});
|
||||
}, 10_000);
|
||||
}
|
||||
|
||||
function broadcast(channel: string, data: unknown): void {
|
||||
for (const win of BrowserWindow.getAllWindows()) {
|
||||
if (!win.isDestroyed()) {
|
||||
win.webContents.send(channel, data);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import { app } from "electron";
|
||||
import { join } from "path";
|
||||
import { existsSync } from "fs";
|
||||
|
||||
// ─── Single Instance Lock ────────────────────────
|
||||
export function checkSingleInstance(): boolean {
|
||||
const gotLock = app.requestSingleInstanceLock();
|
||||
if (!gotLock) {
|
||||
app.quit();
|
||||
return false;
|
||||
}
|
||||
|
||||
app.on("second-instance", (_event, _argv, _cwd) => {
|
||||
const windows = require("electron").BrowserWindow.getAllWindows();
|
||||
if (windows.length > 0) {
|
||||
const win = windows[0];
|
||||
if (win.isMinimized()) win.restore();
|
||||
win.focus();
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// ─── Dev mode detection ──────────────────────────
|
||||
export function isDev(): boolean {
|
||||
return !app.isPackaged;
|
||||
}
|
||||
|
||||
// ─── Web URL ─────────────────────────────────────
|
||||
export function getWebUrl(dev: boolean, port: number): string {
|
||||
if (dev) {
|
||||
return "http://localhost:" + port;
|
||||
}
|
||||
|
||||
const appPath = app.isPackaged
|
||||
? join(process.resourcesPath, "web")
|
||||
: join(__dirname, "..", "..", "web", "out");
|
||||
|
||||
const staticIndex = join(appPath, "index.html");
|
||||
if (existsSync(staticIndex)) {
|
||||
return staticIndex;
|
||||
}
|
||||
|
||||
const standalone = join(appPath, ".next", "standalone", "server.js");
|
||||
if (existsSync(standalone)) {
|
||||
return "http://localhost:3000";
|
||||
}
|
||||
|
||||
const fallback = join(appPath, "index.html");
|
||||
if (existsSync(fallback)) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
console.warn("[utils] No web build found at", appPath);
|
||||
return "http://localhost:3000";
|
||||
}
|
||||
|
||||
// ─── API Path ────────────────────────────────────
|
||||
export function getApiPath(): string | null {
|
||||
if (app.isPackaged) {
|
||||
const packed = join(process.resourcesPath, "api", "dist", "index.js");
|
||||
if (existsSync(packed)) return packed;
|
||||
const packedSrc = join(process.resourcesPath, "api", "src", "index.js");
|
||||
if (existsSync(packedSrc)) return packedSrc;
|
||||
}
|
||||
|
||||
const devPath = join(__dirname, "..", "..", "api", "dist", "index.js");
|
||||
if (existsSync(devPath)) return devPath;
|
||||
|
||||
const devSrc = join(__dirname, "..", "..", "api", "src", "index.js");
|
||||
if (existsSync(devSrc)) return devSrc;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// ─── Get API port from env ───────────────────────
|
||||
export function getApiPort(): number {
|
||||
return 3001;
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
||||
<plist version="1.0">
|
||||
<dict>
|
||||
<key>com.apple.security.cs.allow-jit</key>
|
||||
<true/>
|
||||
<key>com.apple.security.cs.allow-unsigned-executable-memory</key>
|
||||
<true/>
|
||||
<key>com.apple.security.cs.allow-dyld-environment-variables</key>
|
||||
<true/>
|
||||
<key>com.apple.security.network.client</key>
|
||||
<true/>
|
||||
<key>com.apple.security.network.server</key>
|
||||
<true/>
|
||||
<key>com.apple.security.files.user-selected.read-write</key>
|
||||
<true/>
|
||||
</dict>
|
||||
</plist>
|
||||
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"name": "socialapp-desktop",
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"description": "socialapp — Desktop Application powered by Electron",
|
||||
"main": "dist/electron/main.js",
|
||||
"scripts": {
|
||||
"dev": "concurrently \"npm run dev:web\" \"npm run dev:api\" \"npm run dev:electron\"",
|
||||
"dev:electron": "tsc && electron .",
|
||||
"dev:web": "cd ../web && npm run dev",
|
||||
"dev:api": "cd ../api && npm run dev",
|
||||
"build": "npm run build:web && npm run build:api && npm run build:electron",
|
||||
"build:web": "cd ../web && npm run build",
|
||||
"build:api": "cd ../api && npm run build",
|
||||
"build:electron": "tsc && electron-builder --config electron-builder.yml",
|
||||
"build:electron:win": "tsc && electron-builder --win --config electron-builder.yml",
|
||||
"build:electron:mac": "tsc && electron-builder --mac --config electron-builder.yml",
|
||||
"build:electron:linux": "tsc && electron-builder --linux --config electron-builder.yml",
|
||||
"pack": "tsc && electron-builder --dir --config electron-builder.yml",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"lint": "eslint electron/"
|
||||
},
|
||||
"dependencies": {
|
||||
"electron-updater": "^6.3.9",
|
||||
"electron-store": "^10.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"electron": "^35.0.0",
|
||||
"electron-builder": "^25.1.8",
|
||||
"concurrently": "^9.1.0",
|
||||
"typescript": "^5.7.0",
|
||||
"@types/node": "^22.10.0"
|
||||
},
|
||||
"build": {
|
||||
"productName": "socialapp",
|
||||
"appId": "com.socialapp.desktop",
|
||||
"directories": {
|
||||
"output": "release"
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"module": "commonjs",
|
||||
"moduleResolution": "node",
|
||||
"lib": [
|
||||
"ES2022"
|
||||
],
|
||||
"outDir": "dist",
|
||||
"rootDir": "electron",
|
||||
"strict": true,
|
||||
"esModuleInterop": true,
|
||||
"skipLibCheck": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"resolveJsonModule": true,
|
||||
"declaration": false,
|
||||
"sourceMap": true,
|
||||
"noUnusedLocals": false,
|
||||
"noUnusedParameters": false,
|
||||
"types": [
|
||||
"node"
|
||||
]
|
||||
},
|
||||
"include": [
|
||||
"electron/**/*.ts"
|
||||
],
|
||||
"exclude": [
|
||||
"node_modules",
|
||||
"dist",
|
||||
"release"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { EmptyState } from "@/components/ui/EmptyState";
|
||||
import { useArticles } from "@/hooks/useArticles";
|
||||
|
||||
export default function PagePage() {
|
||||
const { data, loading, error, refetch } = useArticles();
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-xl font-bold text-gray-800">文章发布</h1>
|
||||
<p className="text-sm text-gray-500 mt-1">文章发布</p>
|
||||
</div>
|
||||
<Button onClick={() => setOpen(true)} variant="primary" size="sm">新增</Button>
|
||||
</div>
|
||||
|
||||
{loading ? (
|
||||
<div className="flex justify-center py-12">
|
||||
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-500" />
|
||||
</div>
|
||||
) : error ? (
|
||||
<Card className="text-center py-8">
|
||||
<p className="text-red-500 text-sm">{error}</p>
|
||||
<Button onClick={refetch} variant="secondary" size="sm" className="mt-2">重试</Button>
|
||||
</Card>
|
||||
) : data.length === 0 ? (
|
||||
<EmptyState
|
||||
icon="📭"
|
||||
title="暂无数据"
|
||||
description="还没有任何记录,点击上方按钮开始"
|
||||
action={<Button onClick={() => setOpen(true)} variant="primary" size="sm">创建第一条</Button>}
|
||||
/>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
{data.map((item: any) => (
|
||||
<Card key={item.id}>
|
||||
<h3 className="font-medium text-gray-800">{item.title || item.name || `Item ${item.id.slice(0, 8)}`}</h3>
|
||||
<p className="text-xs text-gray-400 mt-1">{item.createdAt || ""}</p>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { EmptyState } from "@/components/ui/EmptyState";
|
||||
import { useComments } from "@/hooks/useComments";
|
||||
|
||||
export default function PagePage() {
|
||||
const { data, loading, error, refetch } = useComments();
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-xl font-bold text-gray-800">评论管理</h1>
|
||||
<p className="text-sm text-gray-500 mt-1">评论管理</p>
|
||||
</div>
|
||||
<Button onClick={() => setOpen(true)} variant="primary" size="sm">新增</Button>
|
||||
</div>
|
||||
|
||||
{loading ? (
|
||||
<div className="flex justify-center py-12">
|
||||
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-500" />
|
||||
</div>
|
||||
) : error ? (
|
||||
<Card className="text-center py-8">
|
||||
<p className="text-red-500 text-sm">{error}</p>
|
||||
<Button onClick={refetch} variant="secondary" size="sm" className="mt-2">重试</Button>
|
||||
</Card>
|
||||
) : data.length === 0 ? (
|
||||
<EmptyState
|
||||
icon="📭"
|
||||
title="暂无数据"
|
||||
description="还没有任何记录,点击上方按钮开始"
|
||||
action={<Button onClick={() => setOpen(true)} variant="primary" size="sm">创建第一条</Button>}
|
||||
/>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
{data.map((item: any) => (
|
||||
<Card key={item.id}>
|
||||
<h3 className="font-medium text-gray-800">{item.title || item.name || `Item ${item.id.slice(0, 8)}`}</h3>
|
||||
<p className="text-xs text-gray-400 mt-1">{item.createdAt || ""}</p>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
|
||||
body {
|
||||
-webkit-font-smoothing: antialiased;
|
||||
-moz-osx-font-smoothing: grayscale;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import type { Metadata } from "next";
|
||||
import "./globals.css";
|
||||
import { Sidebar } from "@/components/layout/Sidebar";
|
||||
import { BottomNav } from "@/components/layout/BottomNav";
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: "SocialApp",
|
||||
description: "一款以内容分享和用户互动为核心的社交应用。",
|
||||
};
|
||||
|
||||
export default function RootLayout({ children }: { children: React.ReactNode }) {
|
||||
return (
|
||||
<html lang="zh-CN">
|
||||
<body className="bg-gray-50 min-h-screen">
|
||||
<div className="flex">
|
||||
{/* Desktop Sidebar */}
|
||||
<Sidebar items={[{"name":"首页","href":"/home","icon":"🏠"},{"name":"文章发布","href":"/articles","icon":"📋"},{"name":"分类标签","href":"/tags","icon":"📅"},{"name":"评论管理","href":"/comments","icon":"✏️"},{"name":"媒体库","href":"/media","icon":"📸"}] } projectName="SocialApp" />
|
||||
{/* Main Content */}
|
||||
<main className="flex-1 lg:pl-64 pb-20 lg:pb-0">
|
||||
<div className="max-w-2xl mx-auto px-4 py-6">
|
||||
{children}
|
||||
</div>
|
||||
</main>
|
||||
</div>
|
||||
{/* Mobile Bottom Nav */}
|
||||
<BottomNav items={[{"name":"首页","href":"/home","icon":"🏠"},{"name":"文章发布","href":"/articles","icon":"📋"},{"name":"分类标签","href":"/tags","icon":"📅"},{"name":"评论管理","href":"/comments","icon":"✏️"}] } />
|
||||
</body>
|
||||
</html>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { EmptyState } from "@/components/ui/EmptyState";
|
||||
import { useMedia } from "@/hooks/useMedia";
|
||||
|
||||
export default function PagePage() {
|
||||
const { data, loading, error, refetch } = useMedia();
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-xl font-bold text-gray-800">媒体库</h1>
|
||||
<p className="text-sm text-gray-500 mt-1">媒体库</p>
|
||||
</div>
|
||||
<Button onClick={() => setOpen(true)} variant="primary" size="sm">新增</Button>
|
||||
</div>
|
||||
|
||||
{loading ? (
|
||||
<div className="flex justify-center py-12">
|
||||
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-500" />
|
||||
</div>
|
||||
) : error ? (
|
||||
<Card className="text-center py-8">
|
||||
<p className="text-red-500 text-sm">{error}</p>
|
||||
<Button onClick={refetch} variant="secondary" size="sm" className="mt-2">重试</Button>
|
||||
</Card>
|
||||
) : data.length === 0 ? (
|
||||
<EmptyState
|
||||
icon="📭"
|
||||
title="暂无数据"
|
||||
description="还没有任何记录,点击上方按钮开始"
|
||||
action={<Button onClick={() => setOpen(true)} variant="primary" size="sm">创建第一条</Button>}
|
||||
/>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
{data.map((item: any) => (
|
||||
<Card key={item.id}>
|
||||
<h3 className="font-medium text-gray-800">{item.title || item.name || `Item ${item.id.slice(0, 8)}`}</h3>
|
||||
<p className="text-xs text-gray-400 mt-1">{item.createdAt || ""}</p>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { EmptyState } from "@/components/ui/EmptyState";
|
||||
|
||||
|
||||
export default function PagePage() {
|
||||
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-xl font-bold text-gray-800">消息</h1>
|
||||
<p className="text-sm text-gray-500 mt-1">私信列表、聊天</p>
|
||||
</div>
|
||||
<Button onClick={() => setOpen(true)} variant="primary" size="sm">新增</Button>
|
||||
</div>
|
||||
|
||||
{<Card>
|
||||
<p className="text-gray-500 text-sm">消息 页面内容</p>
|
||||
<p className="text-xs text-gray-400 mt-1">路由: /messages</p>
|
||||
</Card>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
import Link from "next/link";
|
||||
|
||||
export default function HomePage() {
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
{/* Hero Section */}
|
||||
<section className="bg-gradient-to-br from-blue-500 to-blue-700 rounded-2xl p-6 text-white">
|
||||
<h1 className="text-2xl font-bold">SocialApp</h1>
|
||||
<p className="mt-2 text-blue-100 text-sm">应用首页</p>
|
||||
</section>
|
||||
|
||||
{/* Quick Actions */}
|
||||
<section>
|
||||
<h2 className="text-lg font-semibold text-gray-800 mb-3">快捷入口</h2>
|
||||
<div className="grid grid-cols-2 gap-3">
|
||||
<Link key="0" href="/home" className="bg-white rounded-xl border border-gray-100 p-4 text-center hover:shadow-md transition-shadow">
|
||||
<span className="text-2xl block mb-1">📋</span>
|
||||
<span className="text-sm text-gray-600">文章发布</span>
|
||||
</Link>
|
||||
<Link key="1" href="/home" className="bg-white rounded-xl border border-gray-100 p-4 text-center hover:shadow-md transition-shadow">
|
||||
<span className="text-2xl block mb-1">📅</span>
|
||||
<span className="text-sm text-gray-600">分类标签</span>
|
||||
</Link>
|
||||
<Link key="2" href="/home" className="bg-white rounded-xl border border-gray-100 p-4 text-center hover:shadow-md transition-shadow">
|
||||
<span className="text-2xl block mb-1">📝</span>
|
||||
<span className="text-sm text-gray-600">评论管理</span>
|
||||
</Link>
|
||||
<Link key="3" href="/home" className="bg-white rounded-xl border border-gray-100 p-4 text-center hover:shadow-md transition-shadow">
|
||||
<span className="text-2xl block mb-1">📸</span>
|
||||
<span className="text-sm text-gray-600">媒体库</span>
|
||||
</Link>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
{/* Recent Activity / Stats */}
|
||||
<section>
|
||||
<h2 className="text-lg font-semibold text-gray-800 mb-3">今日概览</h2>
|
||||
<div className="bg-white rounded-xl border border-gray-100 p-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<p className="text-sm text-gray-500">欢迎使用 SocialApp</p>
|
||||
<p className="text-xs text-gray-400 mt-1">开始探索功能吧</p>
|
||||
</div>
|
||||
<span className="text-3xl">👋</span>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { EmptyState } from "@/components/ui/EmptyState";
|
||||
|
||||
|
||||
export default function PagePage() {
|
||||
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-xl font-bold text-gray-800">个人</h1>
|
||||
<p className="text-sm text-gray-500 mt-1">个人主页</p>
|
||||
</div>
|
||||
<Button onClick={() => setOpen(true)} variant="primary" size="sm">新增</Button>
|
||||
</div>
|
||||
|
||||
{<Card>
|
||||
<p className="text-gray-500 text-sm">个人 页面内容</p>
|
||||
<p className="text-xs text-gray-400 mt-1">路由: /profile/:userId</p>
|
||||
</Card>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { EmptyState } from "@/components/ui/EmptyState";
|
||||
|
||||
|
||||
export default function PagePage() {
|
||||
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-xl font-bold text-gray-800">发布</h1>
|
||||
<p className="text-sm text-gray-500 mt-1">编辑器、相册选择</p>
|
||||
</div>
|
||||
<Button onClick={() => setOpen(true)} variant="primary" size="sm">新增</Button>
|
||||
</div>
|
||||
|
||||
{<Card>
|
||||
<p className="text-gray-500 text-sm">发布 页面内容</p>
|
||||
<p className="text-xs text-gray-400 mt-1">路由: /publish</p>
|
||||
</Card>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { EmptyState } from "@/components/ui/EmptyState";
|
||||
import { useTags } from "@/hooks/useTags";
|
||||
|
||||
export default function PagePage() {
|
||||
const { data, loading, error, refetch } = useTags();
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-xl font-bold text-gray-800">分类标签</h1>
|
||||
<p className="text-sm text-gray-500 mt-1">分类标签</p>
|
||||
</div>
|
||||
<Button onClick={() => setOpen(true)} variant="primary" size="sm">新增</Button>
|
||||
</div>
|
||||
|
||||
{loading ? (
|
||||
<div className="flex justify-center py-12">
|
||||
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-500" />
|
||||
</div>
|
||||
) : error ? (
|
||||
<Card className="text-center py-8">
|
||||
<p className="text-red-500 text-sm">{error}</p>
|
||||
<Button onClick={refetch} variant="secondary" size="sm" className="mt-2">重试</Button>
|
||||
</Card>
|
||||
) : data.length === 0 ? (
|
||||
<EmptyState
|
||||
icon="📭"
|
||||
title="暂无数据"
|
||||
description="还没有任何记录,点击上方按钮开始"
|
||||
action={<Button onClick={() => setOpen(true)} variant="primary" size="sm">创建第一条</Button>}
|
||||
/>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
{data.map((item: any) => (
|
||||
<Card key={item.id}>
|
||||
<h3 className="font-medium text-gray-800">{item.title || item.name || `Item ${item.id.slice(0, 8)}`}</h3>
|
||||
<p className="text-xs text-gray-400 mt-1">{item.createdAt || ""}</p>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { EmptyState } from "@/components/ui/EmptyState";
|
||||
import { use分类标签 } from "@/hooks/use分类标签";
|
||||
|
||||
export default function PagePage() {
|
||||
const { data, loading, error, refetch } = use分类标签();
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-xl font-bold text-gray-800">分类标签</h1>
|
||||
<p className="text-sm text-gray-500 mt-1">分类标签</p>
|
||||
</div>
|
||||
<Button onClick={() => setOpen(true)} variant="primary" size="sm">新增</Button>
|
||||
</div>
|
||||
|
||||
{loading ? (
|
||||
<div className="flex justify-center py-12">
|
||||
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-500" />
|
||||
</div>
|
||||
) : error ? (
|
||||
<Card className="text-center py-8">
|
||||
<p className="text-red-500 text-sm">{error}</p>
|
||||
<Button onClick={refetch} variant="secondary" size="sm" className="mt-2">重试</Button>
|
||||
</Card>
|
||||
) : data.length === 0 ? (
|
||||
<EmptyState
|
||||
icon="📭"
|
||||
title="暂无数据"
|
||||
description="还没有任何记录,点击上方按钮开始"
|
||||
action={<Button onClick={() => setOpen(true)} variant="primary" size="sm">创建第一条</Button>}
|
||||
/>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
{data.map((item: any) => (
|
||||
<Card key={item.id}>
|
||||
<h3 className="font-medium text-gray-800">{item.title || item.name || `Item ${item.id.slice(0, 8)}`}</h3>
|
||||
<p className="text-xs text-gray-400 mt-1">{item.createdAt || ""}</p>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { EmptyState } from "@/components/ui/EmptyState";
|
||||
import { use媒体库 } from "@/hooks/use媒体库";
|
||||
|
||||
export default function PagePage() {
|
||||
const { data, loading, error, refetch } = use媒体库();
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-xl font-bold text-gray-800">媒体库</h1>
|
||||
<p className="text-sm text-gray-500 mt-1">媒体库</p>
|
||||
</div>
|
||||
<Button onClick={() => setOpen(true)} variant="primary" size="sm">新增</Button>
|
||||
</div>
|
||||
|
||||
{loading ? (
|
||||
<div className="flex justify-center py-12">
|
||||
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-500" />
|
||||
</div>
|
||||
) : error ? (
|
||||
<Card className="text-center py-8">
|
||||
<p className="text-red-500 text-sm">{error}</p>
|
||||
<Button onClick={refetch} variant="secondary" size="sm" className="mt-2">重试</Button>
|
||||
</Card>
|
||||
) : data.length === 0 ? (
|
||||
<EmptyState
|
||||
icon="📭"
|
||||
title="暂无数据"
|
||||
description="还没有任何记录,点击上方按钮开始"
|
||||
action={<Button onClick={() => setOpen(true)} variant="primary" size="sm">创建第一条</Button>}
|
||||
/>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
{data.map((item: any) => (
|
||||
<Card key={item.id}>
|
||||
<h3 className="font-medium text-gray-800">{item.title || item.name || `Item ${item.id.slice(0, 8)}`}</h3>
|
||||
<p className="text-xs text-gray-400 mt-1">{item.createdAt || ""}</p>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { EmptyState } from "@/components/ui/EmptyState";
|
||||
import { use文章发布 } from "@/hooks/use文章发布";
|
||||
|
||||
export default function PagePage() {
|
||||
const { data, loading, error, refetch } = use文章发布();
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-xl font-bold text-gray-800">文章发布</h1>
|
||||
<p className="text-sm text-gray-500 mt-1">文章发布</p>
|
||||
</div>
|
||||
<Button onClick={() => setOpen(true)} variant="primary" size="sm">新增</Button>
|
||||
</div>
|
||||
|
||||
{loading ? (
|
||||
<div className="flex justify-center py-12">
|
||||
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-500" />
|
||||
</div>
|
||||
) : error ? (
|
||||
<Card className="text-center py-8">
|
||||
<p className="text-red-500 text-sm">{error}</p>
|
||||
<Button onClick={refetch} variant="secondary" size="sm" className="mt-2">重试</Button>
|
||||
</Card>
|
||||
) : data.length === 0 ? (
|
||||
<EmptyState
|
||||
icon="📭"
|
||||
title="暂无数据"
|
||||
description="还没有任何记录,点击上方按钮开始"
|
||||
action={<Button onClick={() => setOpen(true)} variant="primary" size="sm">创建第一条</Button>}
|
||||
/>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
{data.map((item: any) => (
|
||||
<Card key={item.id}>
|
||||
<h3 className="font-medium text-gray-800">{item.title || item.name || `Item ${item.id.slice(0, 8)}`}</h3>
|
||||
<p className="text-xs text-gray-400 mt-1">{item.createdAt || ""}</p>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { Card } from "@/components/ui/Card";
|
||||
import { Button } from "@/components/ui/Button";
|
||||
import { EmptyState } from "@/components/ui/EmptyState";
|
||||
import { use评论管理 } from "@/hooks/use评论管理";
|
||||
|
||||
export default function PagePage() {
|
||||
const { data, loading, error, refetch } = use评论管理();
|
||||
const [open, setOpen] = useState(false);
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
<div className="flex items-center justify-between">
|
||||
<div>
|
||||
<h1 className="text-xl font-bold text-gray-800">评论管理</h1>
|
||||
<p className="text-sm text-gray-500 mt-1">评论管理</p>
|
||||
</div>
|
||||
<Button onClick={() => setOpen(true)} variant="primary" size="sm">新增</Button>
|
||||
</div>
|
||||
|
||||
{loading ? (
|
||||
<div className="flex justify-center py-12">
|
||||
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-blue-500" />
|
||||
</div>
|
||||
) : error ? (
|
||||
<Card className="text-center py-8">
|
||||
<p className="text-red-500 text-sm">{error}</p>
|
||||
<Button onClick={refetch} variant="secondary" size="sm" className="mt-2">重试</Button>
|
||||
</Card>
|
||||
) : data.length === 0 ? (
|
||||
<EmptyState
|
||||
icon="📭"
|
||||
title="暂无数据"
|
||||
description="还没有任何记录,点击上方按钮开始"
|
||||
action={<Button onClick={() => setOpen(true)} variant="primary" size="sm">创建第一条</Button>}
|
||||
/>
|
||||
) : (
|
||||
<div className="space-y-3">
|
||||
{data.map((item: any) => (
|
||||
<Card key={item.id}>
|
||||
<h3 className="font-medium text-gray-800">{item.title || item.name || `Item ${item.id.slice(0, 8)}`}</h3>
|
||||
<p className="text-xs text-gray-400 mt-1">{item.createdAt || ""}</p>
|
||||
</Card>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { usePathname } from "next/navigation";
|
||||
|
||||
interface NavItem {
|
||||
name: string;
|
||||
href: string;
|
||||
icon: string;
|
||||
}
|
||||
|
||||
export function BottomNav({ items }: { items: NavItem[] }) {
|
||||
const pathname = usePathname();
|
||||
|
||||
if (!items.length) return null;
|
||||
|
||||
return (
|
||||
<nav className="lg:hidden fixed bottom-0 inset-x-0 bg-white border-t border-gray-100 z-40">
|
||||
<div className="flex items-center justify-around h-16">
|
||||
{items.map((item) => {
|
||||
const active = pathname === item.href;
|
||||
return (
|
||||
<Link
|
||||
key={item.href}
|
||||
href={item.href}
|
||||
className={`flex flex-col items-center gap-0.5 px-3 py-1 ${
|
||||
active ? "text-blue-600" : "text-gray-400"
|
||||
}`}
|
||||
>
|
||||
<span className="text-xl">{item.icon}</span>
|
||||
<span className="text-[10px]">{item.name}</span>
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</nav>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { usePathname } from "next/navigation";
|
||||
|
||||
interface NavItem {
|
||||
name: string;
|
||||
href: string;
|
||||
icon: string;
|
||||
}
|
||||
|
||||
interface SidebarProps {
|
||||
items: NavItem[];
|
||||
projectName: string;
|
||||
}
|
||||
|
||||
export function Sidebar({ items, projectName }: SidebarProps) {
|
||||
const pathname = usePathname();
|
||||
|
||||
return (
|
||||
<aside className="hidden lg:flex lg:flex-col lg:w-64 lg:fixed lg:inset-y-0 bg-white border-r border-gray-100">
|
||||
<div className="px-6 py-5 border-b border-gray-50">
|
||||
<h1 className="text-lg font-bold text-gray-800">{projectName}</h1>
|
||||
</div>
|
||||
<nav className="flex-1 px-3 py-4 space-y-1 overflow-y-auto">
|
||||
{items.map((item) => {
|
||||
const active = pathname === item.href;
|
||||
return (
|
||||
<Link
|
||||
key={item.href}
|
||||
href={item.href}
|
||||
className={`flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm transition-colors ${
|
||||
active ? "bg-blue-50 text-blue-700 font-medium" : "text-gray-600 hover:bg-gray-50"
|
||||
}`}
|
||||
>
|
||||
<span className="text-lg">{item.icon}</span>
|
||||
<span>{item.name}</span>
|
||||
</Link>
|
||||
);
|
||||
})}
|
||||
</nav>
|
||||
</aside>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
"use client";
|
||||
|
||||
import { type ButtonHTMLAttributes } from "react";
|
||||
|
||||
interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
|
||||
variant?: "primary" | "secondary" | "danger" | "ghost";
|
||||
size?: "sm" | "md" | "lg";
|
||||
loading?: boolean;
|
||||
}
|
||||
|
||||
export function Button({ variant = "primary", size = "md", loading, children, className = "", disabled, ...props }: ButtonProps) {
|
||||
const base = "inline-flex items-center justify-center font-medium rounded-lg transition-colors focus:outline-none focus:ring-2 focus:ring-offset-2 disabled:opacity-50 disabled:cursor-not-allowed";
|
||||
const variants = {
|
||||
primary: "bg-blue-600 text-white hover:bg-blue-700 focus:ring-blue-500",
|
||||
secondary: "bg-gray-100 text-gray-700 hover:bg-gray-200 focus:ring-gray-400",
|
||||
danger: "bg-red-600 text-white hover:bg-red-700 focus:ring-red-500",
|
||||
ghost: "text-gray-600 hover:bg-gray-100 focus:ring-gray-400",
|
||||
};
|
||||
const sizes = {
|
||||
sm: "px-3 py-1.5 text-sm",
|
||||
md: "px-4 py-2 text-sm",
|
||||
lg: "px-6 py-3 text-base",
|
||||
};
|
||||
|
||||
return (
|
||||
<button className={`${base} ${variants[variant]} ${sizes[size]} ${className}`} disabled={disabled || loading} {...props}>
|
||||
{loading && <svg className="animate-spin -ml-1 mr-2 h-4 w-4" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24"><circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" /><path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z" /></svg>}
|
||||
{children}
|
||||
</button>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import type { ReactNode } from "react";
|
||||
|
||||
interface CardProps {
|
||||
children: ReactNode;
|
||||
className?: string;
|
||||
onClick?: () => void;
|
||||
}
|
||||
|
||||
export function Card({ children, className = "", onClick }: CardProps) {
|
||||
return (
|
||||
<div
|
||||
className={`bg-white rounded-xl shadow-sm border border-gray-100 p-4 ${onClick ? "cursor-pointer hover:shadow-md transition-shadow" : ""} ${className}`}
|
||||
onClick={onClick}
|
||||
>
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { ReactNode } from "react";
|
||||
|
||||
interface EmptyStateProps {
|
||||
icon?: string;
|
||||
title: string;
|
||||
description?: string;
|
||||
action?: ReactNode;
|
||||
}
|
||||
|
||||
export function EmptyState({ icon = "📭", title, description, action }: EmptyStateProps) {
|
||||
return (
|
||||
<div className="flex flex-col items-center justify-center py-16 text-center">
|
||||
<span className="text-5xl mb-4">{icon}</span>
|
||||
<h3 className="text-lg font-medium text-gray-700 mb-1">{title}</h3>
|
||||
{description && <p className="text-sm text-gray-500 mb-4">{description}</p>}
|
||||
{action}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
"use client";
|
||||
|
||||
import { type InputHTMLAttributes } from "react";
|
||||
|
||||
interface InputProps extends InputHTMLAttributes<HTMLInputElement> {
|
||||
label?: string;
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export function Input({ label, error, className = "", id, ...props }: InputProps) {
|
||||
return (
|
||||
<div className="w-full">
|
||||
{label && <label htmlFor={id} className="block text-sm font-medium text-gray-700 mb-1">{label}</label>}
|
||||
<input
|
||||
id={id}
|
||||
className={`w-full px-3 py-2 border rounded-lg text-sm focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent ${error ? "border-red-300" : "border-gray-300"} ${className}`}
|
||||
{...props}
|
||||
/>
|
||||
{error && <p className="mt-1 text-xs text-red-500">{error}</p>}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, type ReactNode } from "react";
|
||||
|
||||
interface ModalProps {
|
||||
open: boolean;
|
||||
onClose: () => void;
|
||||
title: string;
|
||||
children: ReactNode;
|
||||
}
|
||||
|
||||
export function Modal({ open, onClose, title, children }: ModalProps) {
|
||||
useEffect(() => {
|
||||
if (open) document.body.style.overflow = "hidden";
|
||||
else document.body.style.overflow = "";
|
||||
return () => { document.body.style.overflow = ""; };
|
||||
}, [open]);
|
||||
|
||||
if (!open) return null;
|
||||
|
||||
return (
|
||||
<div className="fixed inset-0 z-50 flex items-center justify-center">
|
||||
<div className="absolute inset-0 bg-black/40" onClick={onClose} />
|
||||
<div className="relative bg-white rounded-2xl shadow-xl max-w-lg w-full mx-4 p-6">
|
||||
<div className="flex items-center justify-between mb-4">
|
||||
<h2 className="text-lg font-semibold">{title}</h2>
|
||||
<button onClick={onClose} className="text-gray-400 hover:text-gray-600 text-xl leading-none">×</button>
|
||||
</div>
|
||||
{children}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
|
||||
// Generic data type for articles
|
||||
type Article = Record<string, unknown>;
|
||||
|
||||
/**
|
||||
* Fetch and manage articles data.
|
||||
*/
|
||||
export function useArticles() {
|
||||
const [data, setData] = useState<Article[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const fetchData = useCallback(async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
const res = await fetch(`/api/articles`);
|
||||
const json = await res.json();
|
||||
setData(Array.isArray(json) ? json : json?.data || []);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : "Failed to load data");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
}, [fetchData]);
|
||||
|
||||
return { data, loading, error, refetch: fetchData };
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
|
||||
// Generic data type for auth
|
||||
type Auth = Record<string, unknown>;
|
||||
|
||||
/**
|
||||
* Fetch and manage auth data.
|
||||
*/
|
||||
export function useAuth() {
|
||||
const [data, setData] = useState<Auth[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const fetchData = useCallback(async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
const res = await fetch(`/api/auth`);
|
||||
const json = await res.json();
|
||||
setData(Array.isArray(json) ? json : json?.data || []);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : "Failed to load data");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
}, [fetchData]);
|
||||
|
||||
return { data, loading, error, refetch: fetchData };
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
|
||||
// Generic data type for comments
|
||||
type Comment = Record<string, unknown>;
|
||||
|
||||
/**
|
||||
* Fetch and manage comments data.
|
||||
*/
|
||||
export function useComments() {
|
||||
const [data, setData] = useState<Comment[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const fetchData = useCallback(async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
const res = await fetch(`/api/comments`);
|
||||
const json = await res.json();
|
||||
setData(Array.isArray(json) ? json : json?.data || []);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : "Failed to load data");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
}, [fetchData]);
|
||||
|
||||
return { data, loading, error, refetch: fetchData };
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect } from "react";
|
||||
|
||||
export function useDebounce<T>(value: T, delay: number = 300): T {
|
||||
const [debounced, setDebounced] = useState(value);
|
||||
|
||||
useEffect(() => {
|
||||
const timer = setTimeout(() => setDebounced(value), delay);
|
||||
return () => clearTimeout(timer);
|
||||
}, [value, delay]);
|
||||
|
||||
return debounced;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
|
||||
// Generic data type for feed
|
||||
type Feed = Record<string, unknown>;
|
||||
|
||||
/**
|
||||
* Fetch and manage feed data.
|
||||
*/
|
||||
export function useFeed() {
|
||||
const [data, setData] = useState<Feed[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const fetchData = useCallback(async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
const res = await fetch(`/api/feed`);
|
||||
const json = await res.json();
|
||||
setData(Array.isArray(json) ? json : json?.data || []);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : "Failed to load data");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
}, [fetchData]);
|
||||
|
||||
return { data, loading, error, refetch: fetchData };
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useCallback } from "react";
|
||||
|
||||
interface UseFormOptions<T> {
|
||||
initialValues: T;
|
||||
onSubmit: (values: T) => Promise<void>;
|
||||
}
|
||||
|
||||
export function useForm<T extends Record<string, unknown>>({ initialValues, onSubmit }: UseFormOptions<T>) {
|
||||
const [values, setValues] = useState<T>(initialValues);
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const setField = useCallback(<K extends keyof T>(field: K, value: T[K]) => {
|
||||
setValues(prev => ({ ...prev, [field]: value }));
|
||||
}, []);
|
||||
|
||||
const handleSubmit = useCallback(async (e: React.FormEvent) => {
|
||||
e.preventDefault();
|
||||
try {
|
||||
setSubmitting(true);
|
||||
setError(null);
|
||||
await onSubmit(values);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : "Submit failed");
|
||||
} finally {
|
||||
setSubmitting(false);
|
||||
}
|
||||
}, [values, onSubmit]);
|
||||
|
||||
return { values, setField, submitting, error, handleSubmit };
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
|
||||
// Generic data type for 评论管理
|
||||
type Item = Record<string, unknown>;
|
||||
|
||||
/**
|
||||
* Fetch and manage 评论管理 data.
|
||||
*/
|
||||
export function useItem() {
|
||||
const [data, setData] = useState<Item[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const fetchData = useCallback(async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
const res = await fetch(`/api/评论管理`);
|
||||
const json = await res.json();
|
||||
setData(Array.isArray(json) ? json : json?.data || []);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : "Failed to load data");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
}, [fetchData]);
|
||||
|
||||
return { data, loading, error, refetch: fetchData };
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
|
||||
// Generic data type for media
|
||||
type Media = Record<string, unknown>;
|
||||
|
||||
/**
|
||||
* Fetch and manage media data.
|
||||
*/
|
||||
export function useMedia() {
|
||||
const [data, setData] = useState<Media[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const fetchData = useCallback(async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
const res = await fetch(`/api/media`);
|
||||
const json = await res.json();
|
||||
setData(Array.isArray(json) ? json : json?.data || []);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : "Failed to load data");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
}, [fetchData]);
|
||||
|
||||
return { data, loading, error, refetch: fetchData };
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
|
||||
// Generic data type for posts
|
||||
type Post = Record<string, unknown>;
|
||||
|
||||
/**
|
||||
* Fetch and manage posts data.
|
||||
*/
|
||||
export function usePosts() {
|
||||
const [data, setData] = useState<Post[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const fetchData = useCallback(async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
const res = await fetch(`/api/posts`);
|
||||
const json = await res.json();
|
||||
setData(Array.isArray(json) ? json : json?.data || []);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : "Failed to load data");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
}, [fetchData]);
|
||||
|
||||
return { data, loading, error, refetch: fetchData };
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
|
||||
// Generic data type for tags
|
||||
type Tag = Record<string, unknown>;
|
||||
|
||||
/**
|
||||
* Fetch and manage tags data.
|
||||
*/
|
||||
export function useTags() {
|
||||
const [data, setData] = useState<Tag[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const fetchData = useCallback(async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
const res = await fetch(`/api/tags`);
|
||||
const json = await res.json();
|
||||
setData(Array.isArray(json) ? json : json?.data || []);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : "Failed to load data");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
}, [fetchData]);
|
||||
|
||||
return { data, loading, error, refetch: fetchData };
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
"use client";
|
||||
|
||||
import { useState, useEffect, useCallback } from "react";
|
||||
|
||||
// Generic data type for users
|
||||
type User = Record<string, unknown>;
|
||||
|
||||
/**
|
||||
* Fetch and manage users data.
|
||||
*/
|
||||
export function useUsers() {
|
||||
const [data, setData] = useState<User[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const fetchData = useCallback(async () => {
|
||||
try {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
const res = await fetch(`/api/users`);
|
||||
const json = await res.json();
|
||||
setData(Array.isArray(json) ? json : json?.data || []);
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : "Failed to load data");
|
||||
} finally {
|
||||
setLoading(false);
|
||||
}
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
fetchData();
|
||||
}, [fetchData]);
|
||||
|
||||
return { data, loading, error, refetch: fetchData };
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
/// <reference types="next" />
|
||||
/// <reference types="next/image-types/global" />
|
||||
/// <reference path="./.next/types/routes.d.ts" />
|
||||
|
||||
// NOTE: This file should not be edited
|
||||
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
|
||||
@@ -0,0 +1,7 @@
|
||||
import type { NextConfig } from "next";
|
||||
|
||||
const nextConfig: NextConfig = {
|
||||
/* config options here */
|
||||
};
|
||||
|
||||
export default nextConfig;
|
||||
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"name": "socialapp",
|
||||
"version": "0.1.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"dev": "next dev",
|
||||
"build": "next build",
|
||||
"start": "next start",
|
||||
"lint": "next lint",
|
||||
"type-check": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"next": "^15.0.0",
|
||||
"react": "^19.0.0",
|
||||
"react-dom": "^19.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.0.0",
|
||||
"@types/react": "^19.0.0",
|
||||
"typescript": "^5.6.0",
|
||||
"tailwindcss": "^3.4.0",
|
||||
"postcss": "^8.4.0",
|
||||
"autoprefixer": "^10.4.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
module.exports = {
|
||||
plugins: {
|
||||
tailwindcss: {},
|
||||
autoprefixer: {},
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,47 @@
|
||||
// Auto-generated API client for SocialApp
|
||||
|
||||
const API_BASE = process.env.NEXT_PUBLIC_API_URL || "http://localhost:3001/api";
|
||||
|
||||
interface RequestOptions extends RequestInit {
|
||||
params?: Record<string, string | number>;
|
||||
}
|
||||
|
||||
async function request<T>(endpoint: string, options: RequestOptions = {}): Promise<T> {
|
||||
const { params, ...init } = options;
|
||||
let url = `${API_BASE}${endpoint}`;
|
||||
|
||||
if (params) {
|
||||
const searchParams = new URLSearchParams();
|
||||
Object.entries(params).forEach(([k, v]) => searchParams.set(k, String(v)));
|
||||
url += `?${searchParams.toString()}`;
|
||||
}
|
||||
|
||||
const res = await fetch(url, {
|
||||
...init,
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
...init.headers,
|
||||
},
|
||||
});
|
||||
|
||||
if (!res.ok) {
|
||||
const err = await res.json().catch(() => ({ message: res.statusText }));
|
||||
throw new Error(err.message || `API Error: ${res.status}`);
|
||||
}
|
||||
|
||||
return res.json();
|
||||
}
|
||||
|
||||
export const api = {
|
||||
get: <T>(url: string, params?: Record<string, string | number>) =>
|
||||
request<T>(url, { method: "GET", params }),
|
||||
|
||||
post: <T>(url: string, data?: unknown) =>
|
||||
request<T>(url, { method: "POST", body: JSON.stringify(data) }),
|
||||
|
||||
put: <T>(url: string, data?: unknown) =>
|
||||
request<T>(url, { method: "PUT", body: JSON.stringify(data) }),
|
||||
|
||||
delete: <T>(url: string) =>
|
||||
request<T>(url, { method: "DELETE" }),
|
||||
};
|
||||
@@ -0,0 +1,10 @@
|
||||
// Auto-generated articles service
|
||||
import { api } from "./api";
|
||||
|
||||
export function get() {
|
||||
return api.get("/api/articles");
|
||||
}
|
||||
|
||||
export function post(data: Record<string, unknown>) {
|
||||
return api.post("/api/articles", data);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Auto-generated auth service
|
||||
import { api } from "./api";
|
||||
|
||||
export function postlogin(data: Record<string, unknown>) {
|
||||
return api.post("/api/auth", data);
|
||||
}
|
||||
|
||||
export function getme() {
|
||||
return api.get("/api/auth");
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Auto-generated comments service
|
||||
import { api } from "./api";
|
||||
|
||||
export function get() {
|
||||
return api.get("/api/comments");
|
||||
}
|
||||
|
||||
export function post(data: Record<string, unknown>) {
|
||||
return api.post("/api/comments", data);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
// Auto-generated feed service
|
||||
import { api } from "./api";
|
||||
|
||||
export function get() {
|
||||
return api.get("/api/feed");
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Auto-generated media service
|
||||
import { api } from "./api";
|
||||
|
||||
export function get() {
|
||||
return api.get("/api/media");
|
||||
}
|
||||
|
||||
export function post(data: Record<string, unknown>) {
|
||||
return api.post("/api/media", data);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Auto-generated posts service
|
||||
import { api } from "./api";
|
||||
|
||||
export function post(data: Record<string, unknown>) {
|
||||
return api.post("/api/posts", data);
|
||||
}
|
||||
|
||||
export function post_id_like(data: Record<string, unknown>) {
|
||||
return api.post("/api/posts", data);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
// Auto-generated tags service
|
||||
import { api } from "./api";
|
||||
|
||||
export function get() {
|
||||
return api.get("/api/tags");
|
||||
}
|
||||
|
||||
export function post(data: Record<string, unknown>) {
|
||||
return api.post("/api/tags", data);
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user