diff --git a/Backend/src/models/pricealarms/pricealarms.router.ts b/Backend/src/models/pricealarms/pricealarms.router.ts index 6a73bca..8e0114a 100644 --- a/Backend/src/models/pricealarms/pricealarms.router.ts +++ b/Backend/src/models/pricealarms/pricealarms.router.ts @@ -106,3 +106,29 @@ pricealarmsRouter.put('/', async (req: Request, res: Response) => { res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); } }); + +// DELETE pricealarms/:id +pricealarmsRouter.delete('/:id', async (req, res) => { + try { + // Authenticate user + const user_ip = req.connection.remoteAddress ?? ''; + const session_id = (req.query.session_id ?? '').toString(); + const session_key = (req.query.session_key ?? '').toString(); + const user = await UserService.checkSession(session_id, session_key, user_ip); + + const id: number = parseInt(req.params.id, 10); + + const success = await PriceAlarmsService.deletePriceAlarm(id, user.user_id); + + if (success) { + res.status(200).send(JSON.stringify({success: true})); + return; + } else { + res.status(500).send(JSON.stringify({success: false})); + return; + } + } catch (e) { + console.log('Error handling a request: ' + e.message); + res.status(500).send(JSON.stringify({'message': 'Internal Server Error. Try again later.'})); + } +}); diff --git a/Backend/src/models/pricealarms/pricealarms.service.ts b/Backend/src/models/pricealarms/pricealarms.service.ts index 7d975fb..6b59146 100644 --- a/Backend/src/models/pricealarms/pricealarms.service.ts +++ b/Backend/src/models/pricealarms/pricealarms.service.ts @@ -92,3 +92,24 @@ export const updatePriceAlarm = async (alarm_id: number, user_id: number, define } } }; + +/** + * Deletes the given price alarm + * @param alarm_id The id of the price alarm to update + * @param user_id The id of the user that wants to update the price alarm + */ +export const deletePriceAlarm = async (alarm_id: number, user_id: number): Promise => { + let conn; + try { + conn = await pool.getConnection(); + const res = await conn.query('DELETE FROM price_alarms WHERE alarm_id = ? AND user_id = ?', [alarm_id, user_id]); + + return res.affectedRows === 1; + } catch (err) { + throw err; + } finally { + if (conn) { + conn.end(); + } + } +}; diff --git a/CucumberTests/src/test/java/stepdefs/SearchProduct.java b/CucumberTests/src/test/java/stepdefs/SearchProduct.java index af32faa..73ab146 100644 --- a/CucumberTests/src/test/java/stepdefs/SearchProduct.java +++ b/CucumberTests/src/test/java/stepdefs/SearchProduct.java @@ -16,7 +16,7 @@ public class SearchProduct { //throw new PendingException(); Preconditions.driver.get("https://betterzon.xyz"); WebElement logo = (new WebDriverWait(Preconditions.driver, 10)) - .until(ExpectedConditions.elementToBeClickable(By.cssSelector(".logo"))); + .until(ExpectedConditions.elementToBeClickable(By.cssSelector(".navbar-brand"))); } @When("^the user enters the search term \"([^\"]*)\" and clicks search$") @@ -25,7 +25,7 @@ public class SearchProduct { searchField.sendKeys(searchTerm); searchField.sendKeys(Keys.ENTER); WebElement logo = (new WebDriverWait(Preconditions.driver, 10)) - .until(ExpectedConditions.elementToBeClickable(By.cssSelector(".logo"))); + .until(ExpectedConditions.elementToBeClickable(By.cssSelector(".navbar-brand"))); } @Then("^the user should see the error page \"([^\"]*)\"$") diff --git a/Frontend/src/app/services/api.service.ts b/Frontend/src/app/services/api.service.ts index 953c229..4839482 100644 --- a/Frontend/src/app/services/api.service.ts +++ b/Frontend/src/app/services/api.service.ts @@ -445,6 +445,25 @@ export class ApiService { } } + /** + * Deletes the given price alarm + * @param alarmId the price alarm to delete + * @return Observable The observable response of the api + */ + deletePriceAlarm(alarmId: number): Observable { + try { + const sessionInfo = this.getSessionInfoFromLocalStorage(); + + let params = new HttpParams(); + params = params.append('session_id', sessionInfo.session_id); + params = params.append('session_key', sessionInfo.session_key); + + return this.http.delete((this.apiUrl + '/pricealarms/' + alarmId), {params}); + } catch (exception) { + process.stderr.write(`ERROR received from ${this.apiUrl}: ${exception}\n`); + } + } + /* __ __ / / / /_______ __________