This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public func register(request: HTTPRequest, response: HTTPResponse) { | |
do { | |
let registerUserDto = try request.getObjectFromRequest(RegisterUserDto.self) | |
let user = registerUserDto.toUser() | |
try self.usersService.add(entity: user) | |
guard let registeredUser = try self.usersService.get(byId: user.id) else { | |
return response.sendNotFoundError() | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// swift-tools-version:4.0 | |
// The swift-tools-version declares the minimum version of Swift required to build this package. | |
import PackageDescription | |
let package = Package( | |
name: "TaskerServer", | |
dependencies: [ | |
// Dependencies declare other packages that this package depends on. | |
.package(url: "https://github.com/PerfectlySoft/Perfect-HTTPServer.git", from: "3.0.0"), |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TasksRepository : TasksRepositoryProtocol { | |
private let databaseContext: DatabaseContextProtocol | |
init(databaseContext: DatabaseContextProtocol) { | |
self.databaseContext = databaseContext | |
} | |
func getTasks() throws -> [Task] { | |
let tasks = try self.databaseContext.set(Task.self).select() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Run migrations. | |
let databaseContext = try! container.resolve() as DatabaseContextProtocol | |
databaseContext.executeMigrations() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func registerDatabase(container: DependencyContainer) { | |
container.register(.singleton) { SQLiteConnection(configuration: $0) as SqlConnectionProtocol } | |
container.register { DatabaseContext(sqlConnection: $0) as DatabaseContextProtocol } | |
} | |
private func registerRepositories(container: DependencyContainer) { | |
container.register { TasksRepository(databaseContext: $0) as TasksRepositoryProtocol } | |
container.register { UsersRepository(databaseContext: $0) as UsersRepositoryProtocol } | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public protocol SqlConnectionProtocol { | |
func getDatabaseConfiguration() -> DatabaseConfigurationProtocol | |
func isValidConnection() -> Bool | |
} | |
class SQLiteConnection : SqlConnectionProtocol { | |
private let connectionString: String | |
private var configuration: SQLiteDatabaseConfiguration? | |
private let lock = NSLock() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DatabaseContext: DatabaseContextProtocol { | |
private var sqlConnection: SqlConnectionProtocol | |
private var database: Database<SQLiteDatabaseConfiguration> | |
private let lock = NSLock() | |
public func set<T: Codable>(_ type: T.Type) -> Table<T, Database<SQLiteDatabaseConfiguration>> { | |
self.validateConnection() | |
return database.table(type) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public protocol DatabaseContextProtocol { | |
func executeMigrations() | |
func set<T: Codable>(_ type: T.Type) -> Table<T, Database<SQLiteDatabaseConfiguration>> | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"serverName": "www.example.com", | |
"serverPort": 8181, | |
"logFile": "logs/log.txt", | |
"databaseHost": "localhost", | |
"datanaseName": "tasker", | |
"databaseUser": "tasker_user", | |
"databasePassword": "P@ssw0rd!" | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var builder = new ConfigurationBuilder() | |
.SetBasePath(env.ContentRootPath) | |
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) | |
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true) | |
.AddEnvironmentVariables(); | |
Configuration = builder.Build(); |