Skip to content

Instantly share code, notes, and snippets.

@efleming969
Created March 21, 2018 17:25
Show Gist options
  • Save efleming969/de854ea577676fe62b5431072236427d to your computer and use it in GitHub Desktop.
Save efleming969/de854ea577676fe62b5431072236427d to your computer and use it in GitHub Desktop.
export type GameInfo = { code: string }
export type GameEntity = { id: string, code: string, date: string, rolls: string }
export type RemoteGameData = { datePlayed: string, rolls: string }
export interface RemoteDataService {
fetch ( code: String ): Promise<RemoteGameData>
}
export interface IdentityService {
generate (): string
}
export interface GameRepository {
add ( game: GameEntity ): Promise<void>
checkExisting ( code: string ): Promise<boolean>
}
export class AddGameService {
constructor ( private identity_service: IdentityService,
private remote_data_service: RemoteDataService,
private game_repository: GameRepository ) {
}
execute ( game_info: GameInfo ) {
if ( game_info.code.length !== 4 )
throw new Error( "invalid_code" )
const game_repository = this.game_repository
const remote_data_service = this.remote_data_service
const identity_service = this.identity_service
return this.game_repository.checkExisting( game_info.code )
.then( function ( exists ) {
if ( exists )
throw new Error( "game_already_exists" )
return remote_data_service.fetch( game_info.code )
.then( remote_game_data => ({
id: identity_service.generate(),
code: game_info.code,
date: remote_game_data.datePlayed,
rolls: remote_game_data.rolls
}) ).then( game => game_repository.add( game ) )
} )
}
}
import { AddGameService } from "./add-game-service"
describe( "adding a new game", function () {
let remote_data_service
let game_repository
let identity_service
let add_game_service
beforeEach( function () {
remote_data_service = {
fetch: function ( code ) {
return Promise.resolve( {
datePlayed: "2018/01/01",
rolls: "11111111111111111111"
} )
}
}
game_repository = {
add: function () {
},
checkExisting: function ( code ) {
return Promise.resolve( false )
}
}
identity_service = {
generate: function () {
return "1"
}
}
add_game_service = new AddGameService(
identity_service, remote_data_service, game_repository )
} )
it( "throws an error, when game code is invalid", function () {
expect( () => add_game_service.execute( { code: "111" } ) )
.toThrow( "invalid_code" )
} )
it( "saves new game to repository, when game info is valid", function () {
game_repository.add = jest.fn()
return add_game_service.execute( { code: "1111" } ).then( function () {
expect( game_repository.add ).toBeCalledWith( {
id: "1",
code: "1111",
date: "2018/01/01",
rolls: "11111111111111111111"
} )
} )
} )
it( "throws an error, when game was already added", function () {
game_repository.checkExisting = () => Promise.resolve( true )
return add_game_service.execute( { code: "1111" } ).catch( function ( error ) {
expect( error.message ).toEqual( "game_already_exists" )
} )
} )
} )
import * as Express from "express"
import * as BodyParser from "body-parser"
import * as UUID from "uuid"
import { Client as PostgresClient } from "pg"
import { AddGameService, GameEntity, GameRepository } from "./add-game-service"
const app = Express()
app.use( BodyParser.json() )
const postgres_client = new PostgresClient( {
host: "localhost", database: "postgres", user: "postgres", password: "password"
} )
postgres_client.connect()
class PostgresGameRepository implements GameRepository {
add ( game: GameEntity ): Promise<void> {
const sql = "insert into games(id, code, date, rolls) values($1, $2, $3, $4)"
return postgres_client.query( sql, [
game.id, game.code, game.date, game.rolls
] )
}
checkExisting ( code: string ): Promise<boolean> {
return Promise.resolve( false );
}
}
const gameRepository = new PostgresGameRepository()
const identity_service = {
generate: () => UUID.v1()
}
app.post( "/games", function ( req, res ) {
const remote_data_service = {
fetch: function ( code ) {
return Promise.resolve( {
datePlayed: "2018/01/01",
rolls: "11111111111111111111"
} )
}
}
const game_repository = new PostgresGameRepository()
const add_game_service = new AddGameService(
identity_service, remote_data_service, game_repository )
add_game_service.execute( req.body )
.then( () => res.send( "done" ) )
.catch( () => res.send( "error" ) )
} )
app.listen( 8081, function () {
console.log( "server started.." )
} )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment