Skip to content

Commit f93843d

Browse files
committed
Reorganized to better provide realistic DB example
1 parent d2b3f7c commit f93843d

File tree

7 files changed

+136
-60
lines changed

7 files changed

+136
-60
lines changed

Package.swift.doc

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// swift-tools-version:5.5
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "VaporShell",
8+
dependencies: [
9+
.package(url: "/usr/local/lib/merlin/VaporLibrary-0.0.1/VaporLibrary", branch: "master"),
10+
],
11+
targets: [
12+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
13+
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
14+
// .target(name: "CBase32"),
15+
// .target(name: "CBcrypt"),
16+
17+
.executableTarget(name: "VaporShell", dependencies: [
18+
.product(name: "VaporLibrary", package: "VaporLibrary"),
19+
// .target(name: "CBase32"),
20+
// .target(name: "CBcrypt")
21+
])
22+
]
23+
)

Package.swift.dynamic

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// swift-tools-version:5.5
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
import PackageDescription
5+
6+
let package = Package(
7+
name: "VaporShell",
8+
dependencies: [
9+
10+
],
11+
targets: [
12+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
13+
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
14+
.target(name: "CBase32"),
15+
.target(name: "CBcrypt"),
16+
17+
.executableTarget(name: "VaporShell", dependencies: [
18+
.target(name: "CBase32"),
19+
.target(name: "CBcrypt")
20+
])
21+
]
22+
)

README.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
# VaporShell
1+
# This document provides information for the API Endpoints related to the Employee database
2+
3+
# Documentation Standards
4+
* Return values are documented in their respective classes
5+
* Each class also provides a sample representation in JSON
6+
* The API Endpoint indicates variables using braces, e.g. {id}
7+
* Query parameters refer to supplementary arguments specified in the URL as part of the query string, e.g. ?page=3&per=25
28

3-
An empty shell to serve as a starting point for using Vapor.
49

5-
* To serve files from the /Public folder, search for "UNCOMMENT-PUBLIC" and uncomment
6-
* For a database example, search for "UNCOMMENT-DATABASE" and uncomment.
710

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import Vapor
2+
import Fluent
3+
import FluentMySQLDriver
4+
5+
/// Provides API Endpoints for accessing ``Employee`` related data
6+
public class EmployeesController {
7+
8+
/// Retrieves the employee record specified by the ID
9+
///
10+
/// * API Endpoint: /employees/{id}
11+
/// * Method: GET
12+
/// * Query parameters: None
13+
/// * Status codes:
14+
/// * 200 Successful
15+
/// * 400 Bad request
16+
/// * 404 Not found
17+
///
18+
/// Returns: ``Employee``
19+
///
20+
public func getEmployeeById(_ app: Application) throws {
21+
app.get("employees", ":id") { req -> Employee in
22+
23+
guard let id = req.parameters.get("id", as: Int.self) else {
24+
throw Abort(.badRequest)
25+
}
26+
27+
guard let employee = try await Employee.query(on: req.db)
28+
.filter(\.$id == id)
29+
.first() else {
30+
throw Abort(.notFound)
31+
}
32+
return employee
33+
}
34+
}
35+
}

Sources/VaporShell/App/Models/Employee.swift

+22-22
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,33 @@ import Vapor
1717
import Fluent
1818
import FluentMySQLDriver
1919

20-
// UNCOMMENT-DATABASE to configure database example
21-
// // Content conformance will ensure that the object can be encoded and decoded from HTTP messages.
22-
// final class Employee: Model, Content {
23-
// // Name of the table or collection.
24-
// static let schema = "employees"
20+
/// This class provides the model for an Employee
21+
final public class Employee: Model, Content {
22+
// Name of the table or collection.
23+
public static let schema = "employees"
2524

26-
// // Unique identifier for this Employee.
27-
// @ID(custom: "emp_no", generatedBy: .database)
28-
// var id: Int?
25+
/// Unique identifier for this Employee.
26+
@ID(custom: "emp_no", generatedBy: .database)
27+
public var id: Int?
2928

30-
// // Additional fields for this Employee.
31-
// @Field(key: "first_name")
32-
// var firstName: String
29+
/// First name of employee
30+
@Field(key: "first_name")
31+
public var firstName: String
3332

34-
// @Field(key: "last_name")
35-
// var lastName: String
33+
/// Last name of employee
34+
@Field(key: "last_name")
35+
public var lastName: String
3636

37-
// @Field(key: "gender")
38-
// var gender: String
37+
@Field(key: "gender")
38+
public var gender: String
3939

40-
// @Field(key: "birth_date")
41-
// var birthDate: Date
40+
@Field(key: "birth_date")
41+
public var birthDate: Date
4242

43-
// @Field(key: "hire_date")
44-
// var hireDate: Date
43+
@Field(key: "hire_date")
44+
public var hireDate: Date
4545

46-
// // Creates a new, empty Employee.
47-
// init() { }
48-
// }
46+
// Creates a new, empty Employee.
47+
public init() { }
48+
}
4949

Sources/VaporShell/App/configure.swift

+13-13
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,25 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
import Vapor
1717

1818
// UNCOMMENT-DATABASE to configure database example
19-
// import Fluent
20-
// import FluentMySQLDriver
19+
import Fluent
20+
import FluentMySQLDriver
2121

2222
// configures your application
23-
public func configure(_ app: Application) throws {
23+
func configure(_ app: Application) throws {
2424
// UNCOMMENT-PUBLIC to serve files from /Public folder
2525
// app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))
2626

2727
// UNCOMMENT-DATABASE to configure database example
28-
// var tls = TLSConfiguration.makeClientConfiguration()
29-
// tls.certificateVerification = .none
30-
// app.databases.use(.mysql(
31-
// hostname: "db",
32-
// port: MySQLConfiguration.ianaPortNumber,
33-
// username: "employees_user",
34-
// password: "tAn?*4YKX3,xk?PH",
35-
// database: "employees",
36-
// tlsConfiguration: tls
37-
// ), as: .mysql)
28+
var tls = TLSConfiguration.makeClientConfiguration()
29+
tls.certificateVerification = .none
30+
app.databases.use(.mysql(
31+
hostname: "db",
32+
port: MySQLConfiguration.ianaPortNumber,
33+
username: "employees_user",
34+
password: "tAn?*4YKX3,xk?PH",
35+
database: "employees",
36+
tlsConfiguration: tls
37+
), as: .mysql)
3838

3939
// Set local port
4040
guard let portString = Environment.get("VAPOR_LOCAL_PORT"),

Sources/VaporShell/App/routes.swift

+14-21
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
1616
import Vapor
1717

1818
// UNCOMMENT-DATABASE to configure database example
19-
// import Fluent
20-
// import FluentMySQLDriver
19+
import Fluent
20+
import FluentMySQLDriver
21+
22+
let employeesController = EmployeesController()
2123

2224
func routes(_ app: Application) throws {
2325

@@ -26,24 +28,15 @@ func routes(_ app: Application) throws {
2628
}
2729

2830
// UNCOMMENT-DATABASE to configure database example
29-
// // Find an employee with the specified ID
30-
// app.get ("employees", ":id") { req -> Employee in
31-
// guard let id = req.parameters.get("id", as: Int.self) else {
32-
// throw Abort(.badRequest)
33-
// }
34-
35-
// guard let employee = try await Employee.query(on: req.db)
36-
// .filter(\.$id == id)
37-
// .first() else {
38-
// throw Abort(.notFound)
39-
// }
40-
// return employee
41-
// }
31+
// Find an employee with the specified ID
32+
try employeesController.getEmployeeById(app)
4233

43-
// // List all employees using paging
44-
// app.get("employees") { req -> Page<Employee> in
45-
// let employees = try await Employee.query(on: req.db)
46-
// .paginate(for: req)
47-
// return employees
48-
// }
34+
/// This API endpoint provides a list of all employees
35+
/// Paging is supported
36+
/// Endpoint URI: /employees
37+
app.get("employees") { req -> Page<Employee> in
38+
let employees = try await Employee.query(on: req.db)
39+
.paginate(for: req)
40+
return employees
41+
}
4942
}

0 commit comments

Comments
 (0)