Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resolucao exercicio #1

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remover enum
flpnascto committed Nov 9, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit e33cb80c2ff0357c801a29639940d05d086af6ab
11 changes: 4 additions & 7 deletions src/models/HandleFile.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import fs from 'fs/promises';
import path from 'path';

export enum FileType {
Plants = 'plants',
OpsInfo = 'opsInfo',
}
export type FileType = 'plants' | 'opsInfo';

const PATHS = {
plants: path.join(__dirname, 'database', 'plantsData.json'),
@@ -14,13 +11,13 @@ const PATHS = {
export class HandleFile {
private PATHS = PATHS;

public async saveFile<DataType>(type: FileType, data: DataType): Promise<void> {
public async saveFile<T>(type: FileType, data: T): Promise<void> {
await fs.writeFile(this.PATHS[`${type}`], JSON.stringify(data, null, 2));
}

public async readFile<DataType>(type: FileType): Promise<DataType> {
public async readFile<T>(type: FileType): Promise<T> {
const dataRaw = await fs.readFile(this.PATHS[`${type}`], { encoding: 'utf8' });
const data: DataType = JSON.parse(dataRaw);
const data: T = JSON.parse(dataRaw);
return data;
}
}
18 changes: 11 additions & 7 deletions src/models/PlantModel.ts
Original file line number Diff line number Diff line change
@@ -2,19 +2,23 @@ import { HandleFile, FileType } from './HandleFile';
import { IPlantModel, IPlant, IOpsInfo } from '../interfaces';

class PlantModel implements IPlantModel {
private handleFile = new HandleFile();
private fileTypePlant: FileType = 'plants';

private fileTypeOpsInfo: FileType = 'opsInfo';

constructor(private handleFile = new HandleFile()) {}

private async updateOpsInfo(incrementAmount = 1): Promise<number> {
const opsInfo = await this.handleFile.readFile<IOpsInfo>(FileType.OpsInfo);
const opsInfo = await this.handleFile.readFile<IOpsInfo>(this.fileTypeOpsInfo);
opsInfo.createdPlants += incrementAmount;

await this.handleFile.saveFile(FileType.OpsInfo, opsInfo);
await this.handleFile.saveFile(this.fileTypeOpsInfo, opsInfo);

return opsInfo.createdPlants;
}

public async getAll(): Promise<IPlant[]> {
const plants = await this.handleFile.readFile<IPlant[]>(FileType.Plants);
const plants = await this.handleFile.readFile<IPlant[]>(this.fileTypePlant);
return plants;
}

@@ -25,7 +29,7 @@ class PlantModel implements IPlantModel {
const newPlant = { id: newPlantId, ...plant };
plants.push(newPlant);

await this.handleFile.saveFile(FileType.Plants, plants);
await this.handleFile.saveFile(this.fileTypePlant, plants);

return newPlant;
}
@@ -45,7 +49,7 @@ class PlantModel implements IPlantModel {
if (!removedPlant) return false;

const newPlants = plants.filter((plant) => plant.id !== parseInt(id, 10));
this.handleFile.saveFile(FileType.Plants, newPlants);
this.handleFile.saveFile(this.fileTypePlant, newPlants);

return true;
}
@@ -58,7 +62,7 @@ class PlantModel implements IPlantModel {
return editPlant;
});

await this.handleFile.saveFile(FileType.Plants, updatedPlants);
await this.handleFile.saveFile(this.fileTypePlant, updatedPlants);

return plant;
}