Skip to content

Commit

Permalink
Removed DTO references from irrigation programs service
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewpetro committed Mar 2, 2024
1 parent d9ffeba commit fb16ed2
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 46 deletions.
16 changes: 9 additions & 7 deletions src/irrigation-programs/entities/irrigation-program.entity.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
export interface IrrigationProgramEntity {
duration: number
wateringPeriod: number
startTime: string
switches: number[]
simultaneousIrrigation: boolean
nextRunDate?: string
export class IrrigationProgramEntity {
constructor(
public readonly duration: number,
public readonly wateringPeriod: number,
public readonly startTime: string,
public readonly switches: number[],
public readonly simultaneousIrrigation: boolean,
public readonly nextRunDate?: string
) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface IrrigationProgram {
id: string
duration: number
wateringPeriod: number
startTime: string
switches: number[]
simultaneousIrrigation: boolean
nextRunDate?: string
}
20 changes: 13 additions & 7 deletions src/irrigation-programs/irrigation-programs.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
UsePipes,
} from '@nestjs/common'
import { IrrigationProgramsService } from './irrigation-programs.service'
import type { CreateIrrigationProgram, UpdateIrrigationProgram } from './types'
import { IrrigationProgramDto } from './dto/irrigation-program.dto'
import { CreateIrrigationProgramDto } from './dto/create-irrigation-program.dto'
import { UpdateIrrigationProgramDto } from './dto/update-irrigation-program.dto'

Expand All @@ -23,16 +25,17 @@ export class IrrigationProgramsController {

@Post()
async create(@Body() createIrrigationProgramDto: CreateIrrigationProgramDto) {
const result = this.irrigationProgramsService.create(createIrrigationProgramDto)
const result = await this.irrigationProgramsService.create(createIrrigationProgramDto as CreateIrrigationProgram)
if (result === null) {
throw new HttpException('Failed to create irrigation program', HttpStatus.INTERNAL_SERVER_ERROR)
}
return result
return result as IrrigationProgramDto
}

@Get()
async findAll() {
return this.irrigationProgramsService.findAll()
const result = await this.irrigationProgramsService.findAll()
return result as IrrigationProgramDto[]
}

@Get(':id')
Expand All @@ -41,23 +44,26 @@ export class IrrigationProgramsController {
if (irrigationProgram === null) {
throw new HttpException(`Irrigation program with ID ${id} not found`, HttpStatus.NOT_FOUND)
}
return irrigationProgram
return irrigationProgram as IrrigationProgramDto
}

@Patch(':id')
async update(
@Param('id', new ParseUUIDPipe({ version: '4' })) id: string,
@Body() updateIrrigationProgramDto: UpdateIrrigationProgramDto
) {
const irrigationProgram = this.irrigationProgramsService.update(id, updateIrrigationProgramDto)
const irrigationProgram = await this.irrigationProgramsService.update(
id,
updateIrrigationProgramDto as UpdateIrrigationProgram
)
if (irrigationProgram === null) {
throw new HttpException(`Irrigation program with ID ${id} not found`, HttpStatus.NOT_FOUND)
}
return irrigationProgram
return irrigationProgram as IrrigationProgramDto
}

@Delete(':id')
async remove(@Param('id', new ParseUUIDPipe({ version: '4' })) id: string) {
this.irrigationProgramsService.remove(id)
await this.irrigationProgramsService.remove(id)
}
}
10 changes: 5 additions & 5 deletions src/irrigation-programs/irrigation-programs.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { IrrigationProgramsService } from './irrigation-programs.service'
import { DatabaseModule } from '@/database/database.module'
import { ConfigModule } from '@nestjs/config'
import { v4 as uuidv4 } from 'uuid'
import { IrrigationProgram } from './interfaces/irrigation-program.interface'
import { IrrigationProgramEntity } from './entities/irrigation-program.entity'
import { IrrigationProgramDto } from './dto/irrigation-program.dto'
import { CreateIrrigationProgramDto } from './dto/create-irrigation-program.dto'
import { UpdateIrrigationProgramDto } from './dto/update-irrigation-program.dto'
import { HttpStatus } from '@nestjs/common'
Expand Down Expand Up @@ -78,7 +78,7 @@ describe('IrrigationProgramsService', () => {
expect(irrigationProgram).toEqual({
id: expect.stringMatching(uuidv4Regex),
...mockCreateDto,
})
} as IrrigationProgram)
})

it('should not create an irrigation program because the result is not ok', async () => {
Expand Down Expand Up @@ -143,7 +143,7 @@ describe('IrrigationProgramsService', () => {
expect(irrigationPrograms).toEqual([
{ id: uuid1, ...mockIrrigationProgram1 },
{ id: uuid2, ...mockIrrigationProgram2 },
] as IrrigationProgramDto[])
] as IrrigationProgram[])
})

it('should not find any irrigation programs', async () => {
Expand Down Expand Up @@ -180,7 +180,7 @@ describe('IrrigationProgramsService', () => {
expect(irrigationProgram).toEqual({
id: id,
...mockPartialIrrigationProgram,
} as IrrigationProgramDto)
} as IrrigationProgram)
})

it('should not find a deleted irrigation program', async () => {
Expand Down Expand Up @@ -241,7 +241,7 @@ describe('IrrigationProgramsService', () => {
id,
...mockIrrigationProgram,
...mockUpdateDto,
} as IrrigationProgramDto)
} as IrrigationProgram)
})

it('should not update an irrigation program because the program has been deleted', async () => {
Expand Down
44 changes: 17 additions & 27 deletions src/irrigation-programs/irrigation-programs.service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
import { HttpException, HttpStatus, Injectable, OnModuleInit } from '@nestjs/common'
import { v4 as uuidv4 } from 'uuid'
import { CreateIrrigationProgramDto } from './dto/create-irrigation-program.dto'
import { UpdateIrrigationProgramDto } from './dto/update-irrigation-program.dto'
import { IrrigationProgramDto } from './dto/irrigation-program.dto'
import type { CreateIrrigationProgram, UpdateIrrigationProgram } from './types'
import { IrrigationProgramEntity } from './entities/irrigation-program.entity'
import { IrrigationProgram } from './interfaces/irrigation-program.interface'
import { ConfigService } from '@nestjs/config'
import EnvironmentVariables from '@/environment-variables'
import { DatabaseService } from '@/database/database.service'
import { DocumentScope, IdentifiedDocument, MaybeDocument } from 'nano'

type IrrigationProgramDocument = IrrigationProgramEntity & IdentifiedDocument
type IrrigationProgramEntityWithId = IrrigationProgramEntity & IdentifiedDocument

type IrrigationProgramMaybeDocument = IrrigationProgramEntity & MaybeDocument

const irrigationDocumentToDto = (irrigationProgram: IrrigationProgramDocument): IrrigationProgramDto => ({
const irrigationEntityToIrrigationInterface = (
irrigationProgram: IrrigationProgramEntityWithId
): IrrigationProgram => ({
id: irrigationProgram._id,
duration: irrigationProgram.duration,
wateringPeriod: irrigationProgram.wateringPeriod,
Expand All @@ -23,18 +22,9 @@ const irrigationDocumentToDto = (irrigationProgram: IrrigationProgramDocument):
nextRunDate: irrigationProgram.nextRunDate,
})

const creationDtoToDocument = (irrigationProgramDto: CreateIrrigationProgramDto): IrrigationProgramMaybeDocument => ({
duration: irrigationProgramDto.duration,
wateringPeriod: irrigationProgramDto.wateringPeriod,
startTime: irrigationProgramDto.startTime,
switches: irrigationProgramDto.switches,
simultaneousIrrigation: irrigationProgramDto.simultaneousIrrigation,
nextRunDate: irrigationProgramDto.nextRunDate,
})

@Injectable()
export class IrrigationProgramsService implements OnModuleInit {
private db: DocumentScope<IrrigationProgramMaybeDocument>
private db: DocumentScope<IrrigationProgramEntity & MaybeDocument>

public constructor(
private configService: ConfigService<EnvironmentVariables, true>,
Expand All @@ -47,13 +37,13 @@ export class IrrigationProgramsService implements OnModuleInit {
)
}

async create(createIrrigationProgramDto: CreateIrrigationProgramDto) {
async create(createIrrigationProgram: CreateIrrigationProgram) {
try {
const result = await this.db.insert(creationDtoToDocument(createIrrigationProgramDto), uuidv4())
const result = await this.db.insert(createIrrigationProgram as IrrigationProgramEntity, uuidv4())
if (!result.ok) {
throw new HttpException('Failed to create irrigation program', HttpStatus.INTERNAL_SERVER_ERROR)
}
return { id: result.id, ...createIrrigationProgramDto } as IrrigationProgramDto
return { id: result.id, ...createIrrigationProgram } as IrrigationProgram
} catch (error) {
if (error instanceof HttpException) {
throw error
Expand All @@ -65,7 +55,7 @@ export class IrrigationProgramsService implements OnModuleInit {
async findAll() {
try {
const result = await this.db.list({ include_docs: true })
return result.rows.map((row) => irrigationDocumentToDto(row.doc!))
return result.rows.map((row) => irrigationEntityToIrrigationInterface(row.doc! as IrrigationProgramEntityWithId))
} catch (error) {
throw new HttpException('Failed to retrieve irrigation programs', HttpStatus.INTERNAL_SERVER_ERROR)
}
Expand All @@ -77,7 +67,7 @@ export class IrrigationProgramsService implements OnModuleInit {
if (result._deleted) {
throw new HttpException(`Irrigation program with ID ${id} not found`, HttpStatus.NOT_FOUND)
}
return result
return result as IrrigationProgramEntityWithId
} catch (error) {
if (error instanceof HttpException) {
throw error
Expand All @@ -91,21 +81,21 @@ export class IrrigationProgramsService implements OnModuleInit {

async findOne(id: string) {
const result = await this.findIrrigationProgramById(id)
return irrigationDocumentToDto(result)
return irrigationEntityToIrrigationInterface(result)
}

async update(id: string, updateIrrigationProgramDto: UpdateIrrigationProgramDto) {
async update(id: string, updateIrrigationProgram: UpdateIrrigationProgram) {
const currentDocument = await this.findIrrigationProgramById(id)
const newDocument = {
const newDocument: IrrigationProgramEntityWithId = {
...currentDocument,
...updateIrrigationProgramDto,
...updateIrrigationProgram,
}
try {
const result = await this.db.insert(newDocument)
if (!result.ok) {
throw new HttpException(`Failed to update irrigation program with ID ${id}`, HttpStatus.INTERNAL_SERVER_ERROR)
}
return irrigationDocumentToDto(newDocument)
return irrigationEntityToIrrigationInterface(newDocument)
} catch (error) {
if (!(error instanceof HttpException)) {
throw new HttpException(`Failed to update irrigation program with ID ${id}`, HttpStatus.INTERNAL_SERVER_ERROR)
Expand Down
5 changes: 5 additions & 0 deletions src/irrigation-programs/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { IrrigationProgram } from '../interfaces/irrigation-program.interface'

export type CreateIrrigationProgram = Omit<IrrigationProgram, 'id'>

export type UpdateIrrigationProgram = Partial<Omit<IrrigationProgram, 'id'>>

0 comments on commit fb16ed2

Please sign in to comment.