Skip to content

Commit

Permalink
Update Controller-Middlewares.md
Browse files Browse the repository at this point in the history
  • Loading branch information
KazaiMazai authored Jun 23, 2024
1 parent 79a00bb commit 595b9a9
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions Docs/Controller-Middlewares.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@
#### How to add custom business logic to Resource Controller


#### Override ResourceUpdateModel method
#### Override ResourceUpdateModel methods

Default implementation of that method is:
It can be as simple as:

```swift
func update(_ model: Model, req: Request, database: Database) -> EventLoopFuture<Model> {
return database.eventLoop.tryFuture { try update(model) }
database.eventLoop.tryFuture { try update(model) }
}
```
or its swift-concurrency compatible version:

```swift
func update(_ model: Model, req: Request, database: Database) async throws -> Model {
try await database.eventLoop.tryFuture { try update(model) }.get()
}
```

It can be made complex, but with the following restrictions:
It can be made more complex, but with the following restrictions:
- **All database requests should be performed with provided database instance parameter**
- **Database instance parameter should be used for obtaining event loop**

Expand All @@ -26,11 +32,19 @@ Default implementation of that method is:

```swift
func patch(_ model: Model, req: Request, database: Database) -> EventLoopFuture<Model> {
return database.eventLoop.tryFuture { try patch(model) }
database.eventLoop.tryFuture { try patch(model) }
}

```

or its swift-concurrency compatible version:

```swift
func patch(_ model: Model, req: Request, database: Database) async throws -> Model {
try await database.eventLoop.tryFuture { try patch(model) }.get()
}
```

It can be made as complicated as you wish, but with the following restrictions:
- **All database requests should be performed with provided database instance**
- **Database instance parameter should be used for obtaining event loop**
Expand Down

0 comments on commit 595b9a9

Please sign in to comment.