Skip to content

Commit 9a84682

Browse files
authored
Merge pull request #17 from flutter-news-app-full-source-code/sync-api-server-docs
Sync api server docs
2 parents e3906bc + a1f815d commit 9a84682

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

src/content/docs/api-server/architecture/data-access-flow.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ This flow applies to requests for a collection of items.
4040
This crucial middleware uses the `ModelConfig` to determine the required permission for a `GET` collection request. It checks if the authenticated user has this permission. If not, a `403 Forbidden` error is thrown.
4141

4242
6. **Route Handler (`/routes/api/v1/data/index.dart`)**
43-
The request finally reaches the handler. The handler uses the `DataOperationRegistry` to find the correct `readAll` function for the specified model and executes it, passing along any filter, sort, or pagination parameters.
43+
The request finally reaches the handler. The handler uses the `DataOperationRegistry` to find the correct `readAll` function for the specified model and executes it. For the `country` model, if a `usage` filter is present, the request is delegated to the `CountryService` for specialized, non-paginated results; otherwise, it proceeds with standard pagination and sorting.
4444

4545
7. **Response**
4646
The handler wraps the data from the repository in a standard `SuccessApiResponse` and sends it back to the client.

src/content/docs/api-server/architecture/data-seeding-and-fixtures.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ The primary goal of this system is to **guarantee a consistent and predictable s
1313

1414
This process is designed to be **lean and focused on system setup, not content population**. It handles two critical, one-time tasks:
1515

16-
1. **Ensuring Database Indexes:** It creates all necessary MongoDB indexes, including TTL (Time-To-Live) indexes for temporary data like verification codes and text indexes for search functionality.
16+
1. **Ensuring Database Indexes:** It creates all necessary MongoDB indexes, including TTL (Time-To-Live) indexes for temporary data like verification codes, text indexes for search functionality, and compound indexes to optimize country-related aggregation queries.
1717
2. **Initializing the Admin User:** It securely sets up the single administrator account based on the `OVERRIDE_ADMIN_EMAIL` environment variable.
1818

1919
### Idempotent and Safe to Run

src/content/docs/api-server/features/data-management-api.mdx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ GET /api/v1/data?model=headline
2323
GET /api/v1/data/some-topic-id?model=topic
2424
```
2525

26+
**Example: Fetching countries by usage**
27+
```http
28+
GET /api/v1/data?model=country&filter={"usage":"eventCountry"}
29+
```
30+
This fetches countries that are referenced as 'event countries' in headlines. Similarly, `filter={"usage":"headquarters"}` can be used to fetch countries that are headquarters for sources.
31+
32+
<Aside type="note" title="Why use the 'usage' filter for countries?">
33+
The `usage` filter for the `country` model is designed to enhance the client-side user experience and optimize data retrieval. Instead of fetching a comprehensive list of all countries globally, this filter allows client applications to retrieve only those countries that are *relevant* to existing content.
34+
35+
For instance, when a user wants to filter headlines by the country where the news event occurred, it's often more practical to present a list of countries for which actual headlines exist. Similarly, when filtering by the headquarters of news sources, displaying only countries that host active sources provides a more focused and actionable selection. This approach avoids presenting users with a long list of irrelevant options, streamlining navigation and improving the efficiency of data display.
36+
</Aside>
37+
2638
### The Dual Registry System
2739

2840
The power behind this generic endpoint comes from two central registries:
@@ -41,7 +53,7 @@ The generic data endpoint can manage the following models out-of-the-box:
4153
- `headline`
4254
- `topic`
4355
- `source`
44-
- `country`
56+
- `country` (supports specialized filtering by `usage` for 'eventCountry' or 'headquarters')
4557
- `language`
4658
- `user`
4759
- `user_app_settings`

src/content/docs/api-server/reference/data-access.mdx

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ The following model names are supported:
2323
- `headline`
2424
- `topic`
2525
- `source`
26-
- `country`
26+
- `country` (supports specialized filtering by `usage` for 'eventCountry' or 'headquarters')
2727
- `language`
2828
- `user`
2929
- `user_app_settings`
@@ -32,7 +32,7 @@ The following model names are supported:
3232
- `dashboard_summary`
3333

3434
<Aside type="caution" title="Read-Only Models">
35-
The `country` and `language` models are configured to be **read-only** through the API (i.e., they do not support `POST`, `PUT`, or `DELETE` requests). This is an intentional design choice, as this foundational data is managed via the database seeding process.
35+
The `country` and `language` models are configured to be **read-only** through the API (i.e., they do not support `POST`, `PUT`, or `DELETE` requests). This is an intentional design choice. This foundational data is managed via the database seeding process.
3636
</Aside>
3737

3838
<Aside type="caution">
@@ -51,6 +51,14 @@ Retrieves a paginated list of items for a specific model.
5151
#### Query Parameters
5252

5353
- **`filter`** (string, optional): A URL-encoded JSON string representing a MongoDB-style query.
54+
<Aside type="note" title="Special Filter for Country Model: 'usage'">
55+
For the `country` model, the `filter` parameter supports a special `usage` key. This allows you to retrieve a curated list of countries that are relevant to existing content, rather than all countries globally. This is particularly useful for populating dynamic dropdowns or filters in client applications.
56+
57+
- `filter={"usage":"eventCountry"}`: Returns countries referenced as `eventCountry` in headlines.
58+
- `filter={"usage":"headquarters"}`: Returns countries referenced as `headquarters` in sources.
59+
60+
When using the `usage` filter for the `country` model, the response will **not** be paginated and will return the complete filtered set of countries.
61+
</Aside>
5462
- **`sort`** (string, optional): A comma-separated list of fields to sort by (e.g., `createdAt:desc,title:asc`).
5563
- **`limit`** (integer, optional): The maximum number of items to return.
5664
- **`cursor`** (string, optional): The pagination cursor from a previous response to fetch the next page.
@@ -74,6 +82,13 @@ GET /api/v1/data?model=headline&filter={"topic.id":{"$in":["topicId1","topicId2"
7482
```
7583
This fetches headlines belonging to specific topics, sorted by creation date.
7684
</TabItem>
85+
<TabItem label="Country Usage Filter">
86+
**Request:**
87+
```
88+
GET /api/v1/data?model=country&filter={"usage":"eventCountry"}
89+
```
90+
This fetches all countries that are referenced as 'event countries' in headlines.
91+
</TabItem>
7792
</Tabs>
7893

7994
- **Success Response:**

0 commit comments

Comments
 (0)