Skip to content

Commit

Permalink
get contracts local server and service (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonborisoff authored Feb 29, 2024
1 parent 785b313 commit 3488b67
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 0 deletions.
13 changes: 13 additions & 0 deletions localServer/contracts.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {
Request,
Response
} from 'express'
import {
CONTRACTS
} from './contracts'

export const contractRoutes = {
getContracts: function (req: Request, res: Response): void {
res.status(200).json(CONTRACTS)
}
}
12 changes: 12 additions & 0 deletions localServer/contracts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const CONTRACTS = [
{
id: 'contract_1',
number: 'APX100',
conditions: 'one year labour contract'
},
{
id: 'contract_2',
number: 'APX200',
conditions: 'two year labour contract'
}
]
3 changes: 3 additions & 0 deletions localServer/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
import {
checkAuth
} from './auth.middleware'
import { contractRoutes } from './contracts.route'

const app: Application = express()

Expand All @@ -23,6 +24,8 @@ app.use(checkAuth)
app.route('/api/auth/login').post(authRoutes.login)
app.route('/api/auth/logout').post(authRoutes.logout)

app.route('/api/contracts').get(contractRoutes.getContracts)

const port = 9000
app.listen(port, () => {
console.log(`HTTP REST API Server running at http://localhost: ${port}`)
Expand Down
5 changes: 5 additions & 0 deletions src/app/interfaces/contract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Contract {
id: string
number: string
conditions: string
}
59 changes: 59 additions & 0 deletions src/app/services/contracts/contract.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import {
TestBed
} from '@angular/core/testing'

import {
ContractService
} from './contract.service'
import {
HttpClientTestingModule,
HttpTestingController
} from '@angular/common/http/testing'
import {
Contract
} from '../../interfaces/contract'

describe('ContractService', () => {
let service: ContractService
let httpTestingController: HttpTestingController

beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientTestingModule]
})
service = TestBed.inject(ContractService)
httpTestingController = TestBed.inject(HttpTestingController)
})

afterEach(() => {
httpTestingController.verify()
})

it('getContracts dispatches request properly and returns expected data', () => {
const expectedContracts: Contract[] = [
{
id: 'test_a',
number: 'testA',
conditions: 'test a'
},
{
id: 'test_b',
number: 'testB',
conditions: 'test b'
}
]
let actualContracts: Contract[] = []

service.getContracts().subscribe((contracts) => {
actualContracts = contracts
})
const testRequest = httpTestingController.expectOne('/api/contracts')
expect(testRequest.request.method).toBe('GET')

testRequest.flush(expectedContracts)
expect(actualContracts.length).toBe(expectedContracts.length)
expectedContracts.forEach((expectedContract) => {
expect(actualContracts).toContain(jasmine.objectContaining(expectedContract))
})
})
})
23 changes: 23 additions & 0 deletions src/app/services/contracts/contract.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
HttpClient
} from '@angular/common/http'
import {
Injectable
} from '@angular/core'
import {
Observable
} from 'rxjs'
import {
Contract
} from '../../interfaces/contract'

@Injectable({
providedIn: 'root'
})
export class ContractService {
public constructor(private http: HttpClient) { }

public getContracts(): Observable<Contract[]> {
return this.http.get<Contract[]>('/api/contracts')
}
}

0 comments on commit 3488b67

Please sign in to comment.