Skip to content

Commit

Permalink
fixed deletion of plants
Browse files Browse the repository at this point in the history
  • Loading branch information
repolevedavaj committed Apr 25, 2021
1 parent 8c06e81 commit d788f33
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 13 deletions.
15 changes: 10 additions & 5 deletions src/repositories/ImageRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ class ImageRepository {
return uri;
}

await FileSystem.makeDirectoryAsync(PLANT_AVATARS_DIRECTORY,{intermediates:true});
await FileSystem.makeDirectoryAsync(PLANT_AVATARS_DIRECTORY, {intermediates: true});

const existingImage = (await FileSystem.readDirectoryAsync(PLANT_AVATARS_DIRECTORY)).find(entry => entry.startsWith(`${id}_`));
if (ObjectUtils.isDefined(existingImage)) {
await FileSystem.deleteAsync(`${PLANT_AVATARS_DIRECTORY}/${existingImage}`)
}
await this.deleteImageIfExists(id);

const filename = this.createFilename(id, uri);
const destinationUri = `${PLANT_AVATARS_DIRECTORY}/${filename}`;
Expand All @@ -28,6 +25,14 @@ class ImageRepository {
return destinationUri;
}

async deleteImageIfExists(id: number) {
await FileSystem.makeDirectoryAsync(PLANT_AVATARS_DIRECTORY, {intermediates: true});
const existingImage = (await FileSystem.readDirectoryAsync(PLANT_AVATARS_DIRECTORY)).find(entry => entry.startsWith(`${id}_`));
if (ObjectUtils.isDefined(existingImage)) {
await FileSystem.deleteAsync(`${PLANT_AVATARS_DIRECTORY}/${existingImage}`)
}
}

private createFilename(id: number, uri: string) {
return `${id}_${new Date().getTime()}.${this.getFileExtension(uri)}`;
}
Expand Down
8 changes: 3 additions & 5 deletions src/repositories/PlantRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,11 @@ const PLANT_BASE_SELECT_STATEMENT = `select id,
last_time_watered,
last_time_fertilised,
last_time_sprayed
from plants
where deleted = false`
from plants`

const PLANT_SELECT_ALL_STATEMENT = `${PLANT_BASE_SELECT_STATEMENT} order by name`

const PLANT_SELECT_STATEMENT = `${PLANT_BASE_SELECT_STATEMENT} and id = ?`
const PLANT_SELECT_STATEMENT = `${PLANT_BASE_SELECT_STATEMENT} where id = ?`

const PLANT_UPDATE_STATEMENT = `update plants
set name = ?,
Expand All @@ -56,8 +55,7 @@ const PLANT_UPDATE_STATEMENT = `update plants
last_time_sprayed = ?
where id = ?`

const PLANT_DELETE_STATEMENT = `update plants
set deleted = true
const PLANT_DELETE_STATEMENT = `delete from plants
where id = ?`

class PlantRepository {
Expand Down
3 changes: 2 additions & 1 deletion src/services/PlantService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ class PlantService {
}

async deletePlant(plant: Plant): Promise<void> {
return PlantRepository.deletePlant(plant);
await PlantRepository.deletePlant(plant);
await ImageRepository.deleteImageIfExists(plant.id!);
}

}
Expand Down
5 changes: 4 additions & 1 deletion src/services/database/GrowBuddyDatabaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ const MIGRATIONS = [
`alter table plants
add column planted date`,
`alter table plants
add column amount integer`
add column amount integer`,
`delete
from plants
where deleted = true`
];

class GrowBuddyDatabaseService {
Expand Down
2 changes: 1 addition & 1 deletion src/stacks/default/tabs/plants/screens/PlantEditScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default () => {
}

const deletePlant = async () => {
await PlantService.savePlant(plant);
await PlantService.deletePlant(plant);

navigation.dispatch(StackActions.popToTop());
}
Expand Down

0 comments on commit d788f33

Please sign in to comment.