Skip to content

Commit

Permalink
feat: add ShipController
Browse files Browse the repository at this point in the history
  • Loading branch information
clemlatz committed Feb 21, 2024
1 parent b2d698a commit 73c60b6
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { ShipController } from './controllers/ShipController';
import ShipRepository from './repositories/ShipRepository';
import { PrismaClient } from '@prisma/client';

@Module({
imports: [],
controllers: [AppController],
providers: [AppService],
controllers: [AppController, ShipController],
providers: [AppService, PrismaClient, ShipRepository],
})
export class AppModule {}
60 changes: 60 additions & 0 deletions src/controllers/ShipController.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { Test, TestingModule } from '@nestjs/testing';
import { PrismaClient } from '@prisma/client';
import { Response } from 'express';

import { ShipController } from './ShipController';
import ShipRepository from '../repositories/ShipRepository';
import Ship from '../models/Ship';
import Position from '../models/Position';
import Location from '../models/Location';

describe('ShipController', () => {
let shipController: ShipController;
let shipRepository: ShipRepository;

beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [ShipController],
providers: [PrismaClient, ShipRepository],
}).compile();

shipController = app.get<ShipController>(ShipController);
shipRepository = app.get<ShipRepository>(ShipRepository);
});

describe('index', () => {
it('it returns a list of ship', async () => {
// given
const response = {
json: jest.fn(),
} as unknown as Response;
const destination = new Location('europa', 'Europa', new Position(5, 6));
const ships = [
new Ship(1, 'Discovery One', new Position(1, 2), null),
new Ship(2, 'Europa Report', new Position(3, 4), destination),
];
jest
.spyOn(shipRepository, 'getAll')
.mockImplementation(async () => ships);

// when
await shipController.index(response);

// then
expect(response.json).toHaveBeenCalledWith([
{
id: 1,
name: 'Discovery One',
position: { x: 1, y: 2 },
destination: null,
},
{
id: 2,
name: 'Europa Report',
position: { x: 3, y: 4 },
destination: { name: 'Europa' },
},
]);
});
});
});
22 changes: 22 additions & 0 deletions src/controllers/ShipController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Controller, Get, Res } from '@nestjs/common';
import { Response } from 'express';
import ShipRepository from '../repositories/ShipRepository';

@Controller()
export class ShipController {
constructor(private readonly shipRepository: ShipRepository) {}

@Get('ships')
async index(@Res() res: Response): Promise<void> {
const ships = await this.shipRepository.getAll();
const shipsResponse = ships.map((ship) => {
return {
id: ship.id,
name: ship.name,
position: { x: ship.currentPosition.x, y: ship.currentPosition.y },
destination: ship.isStationary ? null : { name: ship.destination.name },
};
});
res.json(shipsResponse);
}
}
2 changes: 2 additions & 0 deletions src/repositories/ShipRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { PrismaClient } from '@prisma/client';
import Position from '../models/Position';
import Ship from '../models/Ship';
import LocationRepository from './LocationRepository';
import { Injectable } from '@nestjs/common';

type ShipDTO = {
id: number;
Expand All @@ -12,6 +13,7 @@ type ShipDTO = {
destinationCode: string;
};

@Injectable()
export default class ShipRepository {
private prisma: PrismaClient;

Expand Down

0 comments on commit 73c60b6

Please sign in to comment.