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

Generate clear message when recipe exists during creation #702

Merged
merged 8 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
[#699](https://github.com/nextcloud/cookbook/pull/699) @christianlupus
- Enable stalebot
[#700](https://github.com/nextcloud/cookbook/pull/700) @christianlupus
- Correct error messages when recipe already exists
[#702](https://github.com/nextcloud/cookbook/pull/702) @christianlupus

## 0.8.4 - 2021-03-08

Expand Down
9 changes: 9 additions & 0 deletions lib/Controller/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
use OCA\Cookbook\Service\DbCacheService;
use OCA\Cookbook\Helper\RestParameterParser;
use OCA\Cookbook\Exception\UserFolderNotWritableException;
use OCA\Cookbook\Exception\RecipeExistsException;
use OCP\AppFramework\Http\JSONResponse;

class MainController extends Controller {
protected $appName;
Expand Down Expand Up @@ -353,6 +355,13 @@ public function import() {
$this->dbCacheService->addRecipe($recipe_file);

return new DataResponse($recipe_json, Http::STATUS_OK, ['Content-Type' => 'application/json']);
} catch (RecipeExistsException $ex) {
$json = [
'msg' => $ex->getMessage(),
'line' => $ex->getLine(),
'file' => $ex->getFile(),
];
return new JSONResponse($json, Http::STATUS_CONFLICT);
} catch (\Exception $e) {
return new DataResponse($e->getMessage(), 400);
}
Expand Down
19 changes: 15 additions & 4 deletions lib/Controller/RecipeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use OCA\Cookbook\Service\RecipeService;
use OCP\IURLGenerator;
use OCA\Cookbook\Service\DbCacheService;
use OCA\Cookbook\Exception\RecipeExistsException;
use OCA\Cookbook\Helper\RestParameterParser;
use OCP\AppFramework\Http\JSONResponse;

class RecipeController extends Controller {
/**
Expand Down Expand Up @@ -117,10 +119,19 @@ public function create() {
$this->dbCacheService->triggerCheck();

$recipeData = $this->restParser->getParameters();
$file = $this->service->addRecipe($recipeData);
$this->dbCacheService->addRecipe($file);

return new DataResponse($file->getParent()->getId(), Http::STATUS_OK, ['Content-Type' => 'application/json']);
try {
$file = $this->service->addRecipe($recipeData);
$this->dbCacheService->addRecipe($file);

return new DataResponse($file->getParent()->getId(), Http::STATUS_OK, ['Content-Type' => 'application/json']);
} catch (RecipeExistsException $ex) {
$json = [
'msg' => $ex->getMessage(),
'file' => $ex->getFile(),
'line' => $ex->getLine(),
];
return new JSONResponse($json, Http::STATUS_CONFLICT);
}
}

/**
Expand Down
9 changes: 9 additions & 0 deletions lib/Exception/RecipeExistsException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace OCA\Cookbook\Exception;

class RecipeExistsException extends \Exception {
public function __construct($message = null, $code = null, $previous = null) {
parent::__construct($message, $code, $previous);
}
}
5 changes: 3 additions & 2 deletions lib/Service/RecipeService.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use OCP\PreConditionNotMetException;
use Psr\Log\LoggerInterface;
use OCA\Cookbook\Exception\UserFolderNotWritableException;
use OCA\Cookbook\Exception\RecipeExistsException;

/**
* Main service class for the cookbook app.
Expand Down Expand Up @@ -687,7 +688,7 @@ public function addRecipe($json) {
// The recipe is being renamed, move the folder
if ($old_path !== $new_path) {
if ($user_folder->nodeExists($json['name'])) {
throw new Exception('Another recipe with that name already exists');
throw new RecipeExistsException($this->il10n->t('Another recipe with that name already exists'));
}

$recipe_folder->move($new_path);
Expand All @@ -698,7 +699,7 @@ public function addRecipe($json) {
$json['dateCreated'] = $now;

if ($user_folder->nodeExists($json['name'])) {
throw new Exception('Another recipe with that name already exists');
throw new RecipeExistsException($this->il10n->t('Another recipe with that name already exists'));
}

$recipe_folder = $user_folder->newFolder($json['name']);
Expand Down
11 changes: 9 additions & 2 deletions src/components/AppNavi.vue
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,15 @@ export default {
e2.response.status >= 400 &&
e2.response.status < 500
) {
// eslint-disable-next-line no-alert
alert(e2.response.data)
if (e2.response.status == 409) {
// There was a recipe found with the same name

// eslint-disable-next-line no-alert
alert(e2.response.data.msg)
} else {
// eslint-disable-next-line no-alert
alert(e2.response.data)
}
} else {
console.error(e2)
alert(
Expand Down
3 changes: 2 additions & 1 deletion src/components/AppSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ export default {
this.resetPrintImage = false
if (config) {
this.printImage = config.print_image
this.showTagCloudInRecipeList = this.$store.state.localSettings.showTagCloudInRecipeList
this.showTagCloudInRecipeList =
this.$store.state.localSettings.showTagCloudInRecipeList
this.updateInterval = config.update_interval
this.recipeFolder = config.folder
} else {
Expand Down
85 changes: 52 additions & 33 deletions src/components/RecipeEdit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -564,47 +564,66 @@ export default {
this.$store.dispatch("setSavingRecipe", { saving: true })
const $this = this

if (this.recipe.id) {
this.$store
.dispatch("updateRecipe", { recipe: this.recipe })
.then((response) => {
$this.$window.goTo(`/recipe/${response.data}`)
const request = (() => {
if (this.recipe_id) {
return this.$store.dispatch("updateRecipe", {
recipe: this.recipe,
})
.catch((e) => {
// error
} else {
return this.$store.dispatch("createRecipe", {
recipe: this.recipe,
})
}
})()

request
.then((response) => {
$this.$window.goTo(`/recipe/${response.data}`)
})
.catch((e) => {
// error

if (e.response) {
// Non 2xx state returned

switch (e.response.status) {
case 409:
alert(e.response.data.msg)
break

default:
// eslint-disable-next-line no-alert
alert(
// prettier-ignore
t("cookbook","Unknown answer returned from server. See logs.")
)
// eslint-disable-next-line no-console
console.log(e.response)
}
} else if (e.request) {
// eslint-disable-next-line no-alert
alert(t("cookbook", "Recipe could not be saved"))
alert(
t("cookbook", "No answer for request was received.")
)
// eslint-disable-next-line no-console
console.log(e)
})
.then(() => {
// finally
$this.$store.dispatch("setSavingRecipe", {
saving: false,
})
$this.savingRecipe = false
})
} else {
this.$store
.dispatch("createRecipe", { recipe: this.recipe })
.then((response) => {
$this.$window.goTo(`/recipe/${response.data}`)
})
.catch((e) => {
// error
} else {
// eslint-disable-next-line no-alert
alert(t("cookbook", "Recipe could not be saved"))
alert(
// prettier-ignore
t("cookbook","Could not start request to save recipe.")
)
// eslint-disable-next-line no-console
console.log(e)
}
})
.then(() => {
// finally
$this.$store.dispatch("setSavingRecipe", {
saving: false,
})
.then(() => {
// finally
$this.$store.dispatch("setSavingRecipe", {
saving: false,
})
$this.savingRecipe = false
})
}
$this.savingRecipe = false
})
},
setup() {
this.fetchCategories()
Expand Down
15 changes: 6 additions & 9 deletions src/components/RecipeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,8 @@ export default {
}

if (this.$store.state.recipe.cookTime) {
const cookT = this.$store.state.recipe.cookTime.match(
/PT(\d+?)H(\d+?)M/
)
const cookT =
this.$store.state.recipe.cookTime.match(/PT(\d+?)H(\d+?)M/)
const hh = parseInt(cookT[1], 10)
const mm = parseInt(cookT[2], 10)
if (hh > 0 || mm > 0) {
Expand All @@ -366,9 +365,8 @@ export default {
}

if (this.$store.state.recipe.prepTime) {
const prepT = this.$store.state.recipe.prepTime.match(
/PT(\d+?)H(\d+?)M/
)
const prepT =
this.$store.state.recipe.prepTime.match(/PT(\d+?)H(\d+?)M/)
const hh = parseInt(prepT[1], 10)
const mm = parseInt(prepT[2], 10)
if (hh > 0 || mm > 0) {
Expand All @@ -377,9 +375,8 @@ export default {
}

if (this.$store.state.recipe.totalTime) {
const totalT = this.$store.state.recipe.totalTime.match(
/PT(\d+?)H(\d+?)M/
)
const totalT =
this.$store.state.recipe.totalTime.match(/PT(\d+?)H(\d+?)M/)
const hh = parseInt(totalT[1], 10)
const mm = parseInt(totalT[2], 10)
if (hh > 0 || mm > 0) {
Expand Down
5 changes: 3 additions & 2 deletions src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,14 @@ export default new Vuex.Store({
url: `${window.baseUrl}/api/recipes`,
data: recipe,
})
request.then(() => {
return request.then((v) => {
// Refresh navigation to display changes
c.dispatch("setAppNavigationRefreshRequired", {
isRequired: true,
})

return v
})
return request
},
/**
* Delete recipe on the server
Expand Down