当前位置: 首页 > 工具软件 > Vapor > 使用案例 >

【Vapor】05 Chapter 7: CRUD Database Operations

墨阳羽
2023-12-01

0x00 Chapter 7: CRUD Database Operations

routes.swift 文件内写各种路由
操作数据库的记录

1.create
创建记录,之前的文章已经写过了
需要提交数据

url: http://127.0.0.1:8080/api/acronyms
method: POST
parameters: {"short": "TGD", "long":"Tomorrow is a good day"}

    app.post("api", "acronyms") { req -> EventLoopFuture<Acronym> in
        let acronym = try req.content.decode(Acronym.self)
        return acronym.save(on: req.db).map {
            acronym
        }
    }

2.retrieve
获取所有记录

url: http://127.0.0.1:8080/api/acronyms
method: GET
parameters:

    app.get("api", "acronyms") { req -> EventLoopFuture<[Acronym]> in
        Acronym.query(on: req.db).all()
    }

3.retrieve a single acronym
根据 ID 查询记录

url: http://127.0.0.1:8080/api/acronyms/<ID>
method: GET
parameters: ID

    app.get("api", "acronyms", ":acronymID") { req -> EventLoopFuture<Acronym> in
        Acronym.find(req.parameters.get("acronymID"), on: req.db)
            .unwrap(or: Abort(.notFound))
    }

4.update
根据 ID 更新记录

url: http://127.0.0.1:8080/api/acronyms/<ID>
method: UPDATE
parameters: {"short": "TGD1", "long":"Tomorrow is a good day1"}

    app.put("api", "acronyms", ":acronymID") { req -> EventLoopFuture<Acronym> in
        let updatedAcronym = try req.content.decode(Acronym.self)
        return Acronym.find(req.parameters.get("acronymID"), on: req.db)
            .unwrap(or: Abort(.notFound)).flatMap { acronym in
                acronym.short = updatedAcronym.short
                acronym.long = updatedAcronym.long
                return acronym.save(on: req.db).map {
                    acronym
                }
            }
    }

5.delete
根据 ID 删除记录

url: http://127.0.0.1:8080/api/acronyms/<ID>
method: DELETE
parameters: ID

    app.delete("api", "acronyms", ":acronymID") { req -> EventLoopFuture<HTTPStatus> in
        Acronym.find(req.parameters.get("acronymID"), on: req.db)
            .unwrap(or: Abort(.notFound))
            .flatMap { acronym in
                acronym.delete(on: req.db)
                    .transform(to: .noContent)
            }
    }

6.filter
根据参数查询记录

url: http://127.0.0.1:8080/api/acronyms/search?term=OMG
method: GET
parameters:

    app.get("api", "acronyms", "search") { req -> EventLoopFuture<[Acronym]> in
        guard let searchTerm = req.query[String.self, at: "term"] else {
            throw Abort(.badRequest)
        }
        return Acronym.query(on: req.db)
            .filter(\.$short == searchTerm)
            .all()
    }

filter group
url: http://127.0.0.1:8080/api/acronyms/search?term=Oh+My+God
method: GET
parameters:

    app.get("api", "acronyms", "search") { req -> EventLoopFuture<[Acronym]> in
        guard let searchTerm = req.query[String.self, at: "term"] else {
            throw Abort(.badRequest)
        }
        return Acronym.query(on: req.db).group(.or) { or in
            or.filter(\.$short == searchTerm)
            or.filter(\.$long == searchTerm)
        }.all()
    }

7.first result
查询第一条记录

url: http://127.0.0.1:8080/api/acronyms/first
method: GET
parameters:

    app.get("api", "acronyms", "first") { req -> EventLoopFuture<Acronym> in
        Acronym.query(on: req.db).first().unwrap(or: Abort(.notFound))
    }

8.sorting results
对所有查询记录进行排序

url: http://127.0.0.1:8080/api/acronyms/sorted
method: GET
parameters:

    app.get("api", "acronyms", "sorted") { req -> EventLoopFuture<[Acronym]> in
        Acronym.query(on: req.db).sort(\.$short, .ascending).all()
    }

0x01 我的作品

欢迎体验我的作品之一:小笔记
一步到位,好帮手!
App Store 搜索即可~


 类似资料:

相关阅读

相关文章

相关问答