Skip to content

Commit

Permalink
delete recipe backend b00tc4mp#407
Browse files Browse the repository at this point in the history
  • Loading branch information
berlem committed Apr 16, 2024
1 parent 26e3231 commit c96ac57
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 4 deletions.
21 changes: 21 additions & 0 deletions staff/belen-ivars/project/api/handlers/deleteRecipeHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ContentError, NotFoundError } from "com/errors.js"
import logic from "../logic/index.js"

export default async (req, res) => {
const userId = req.params.id
const recipeId = req.body

try {
await logic.deleteRecipe(userId, recipeId)

res.status(204).send()
} catch (error) {
let status = 500
if (error instanceof NotFoundError)
status = 404
if (error instanceof ContentError || error instanceof TypeError)
status = 500

res.status(status).json({ error: error.constructor.name, message: error.message })
}
}
4 changes: 3 additions & 1 deletion staff/belen-ivars/project/api/handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ import authenticateUserHandler from './authenticateUserHandler.js'
import retrieveUserHandler from './retrieveUserHandler.js'
import createRecipeHandler from './createRecipeHandler.js'
import retrieveRecipesHandler from './retrieveRecipesHandler.js'
import deleteRecipeHandler from './deleteRecipeHandler.js'


export {
registerUserHandler,
authenticateUserHandler,
retrieveUserHandler,
createRecipeHandler,
retrieveRecipesHandler
retrieveRecipesHandler,
deleteRecipeHandler
}
5 changes: 4 additions & 1 deletion staff/belen-ivars/project/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
authenticateUserHandler,
retrieveUserHandler,
createRecipeHandler,
retrieveRecipesHandler
retrieveRecipesHandler,
deleteRecipeHandler

} from './handlers/index.js'

Expand All @@ -34,6 +35,8 @@ mongoose.connect(process.env.MONGODB_URL)

server.get('/recipes/:id', retrieveRecipesHandler)

server.delete('/recipes/:id', deleteRecipeHandler)

server.listen(process.env.PORT, () => console.log(`server running on port ${process.env.PORT}`))
})

Expand Down
1 change: 0 additions & 1 deletion staff/belen-ivars/project/api/logic/createRecipe.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ dotenv.config()
import mongoose, { mongo } from 'mongoose'

import { expect } from 'chai'
import bcrypt from 'bcryptjs'
import random from './helpers/random.js'
import createRecipe from './createRecipe.js'
import { errors } from 'com'
Expand Down
26 changes: 26 additions & 0 deletions staff/belen-ivars/project/api/logic/deleteRecipe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { validate } from "com"
import { User, Recipe } from '../data/models.js'
import { ContentError, NotFoundError } from "com/errors.js"

async function deleteRecipe(userId, recipeId) {
validate.id(userId, 'user id')
validate.id(recipeId, 'recipe id')

const user = await User.findById(userId)

if (!user)
throw new NotFoundError('user not found')
console.log('user founded')

const recipe = await Recipe.findById(recipeId)
if (user.id !== recipe.author.toString())
throw new NotFoundError('recipe does not belong to that user')

try {
await Recipe.findByIdAndDelete(recipe.id)
} catch (error) {
throw new ContentError('recipe cannot be deleted')
}
}

export default deleteRecipe
80 changes: 80 additions & 0 deletions staff/belen-ivars/project/api/logic/deleteRecipe.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import dotenv from 'dotenv'
dotenv.config()

import mongoose, { mongo } from 'mongoose'

import { expect } from 'chai'
import random from './helpers/random.js'
import deleteRecipe from './deleteRecipe.js'
import { errors } from 'com'
import { User, Recipe } from '../data/models.js'
import { NotFoundError } from 'com/errors.js'
const { ObjectId } = mongoose.Types
const { DuplicityError } = errors

describe('deleteRecipe', () => {
before(async () => await mongoose.connect(process.env.TEST_MONGODB_URL))
beforeEach(async () => await User.deleteMany())

it('on success', async () => {
const name = random.name()
const email = random.email()
const password = random.password()
const user = await User.create({ name, email, password })

const title = random.name()
const description = random.text()
const image = random.image()
const author = user.id
const recipe = await Recipe.create({ author, title, description, image })

try {
await deleteRecipe(user.id, recipe.id)
} catch (error) {
expect(recipe).not.exist
}

})

it('fails on non existing user', async () => {

const title = random.name()
const description = random.text()
const image = random.image()
const author = new ObjectId().toString()
const recipe = await Recipe.create({ author, title, description, image })

try {
await deleteRecipe(recipe.id, new ObjectId().toString())
throw new Error('should not reach this point')
} catch (error) {
expect(error).to.be.instanceOf(NotFoundError)
expect(error.message).to.equal('user not found')
}
})

it('fails on non author user', async () => {

const name = random.name()
const email = random.email()
const password = random.password()
const user = await User.create({ name, email, password })

const title = random.name()
const description = random.text()
const image = random.image()
const author = new ObjectId().toString()
const recipe = await Recipe.create({ author, title, description, image })

try {
await deleteRecipe(user.id, recipe.id)
throw new Error('should not reach this point')
} catch (error) {
expect(error).to.be.instanceOf(NotFoundError)
expect(error.message).to.equal('recipe does not belong to that user')
}
})


after(async () => await mongoose.disconnect())
})
16 changes: 16 additions & 0 deletions staff/belen-ivars/project/api/logic/deleteRecipe.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import dotenv from 'dotenv'
dotenv.config()
import mongoose from 'mongoose'

import deleteRecipe from './deleteRecipe.js'

(async () => {
await mongoose.connect(process.env.MONGODB_URL)
try {
await deleteRecipe('65d656eaa92e85c9f9fa6d71', '65f97adbd699fa7945c5d178')

console.log('recipe has been deleted')
} catch (error) {
console.log(error)
}
})()
4 changes: 3 additions & 1 deletion staff/belen-ivars/project/api/logic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import authenticateUser from './authenticateUser.js'
import retrieveUser from './retrieveUser.js'
import createRecipe from './createRecipe.js'
import retrieveRecipes from './retrieveRecipes.js'
import deleteRecipe from './deleteRecipe.js'

const logic = {
registerUser,
authenticateUser,
retrieveUser,
createRecipe,
retrieveRecipes
retrieveRecipes,
deleteRecipe
}

export default logic
52 changes: 52 additions & 0 deletions staff/belen-ivars/project/api/test/delete-recipe.test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
source pepetest.sh

TEST 'delete-recipe'

CASE 'success on current user'

curl 'http://localhost:9000/recipes/65d655fac1dd88f9aee917d6' \
-H 'Authorization: Bearer 65d655fac1dd88f9aee917d6' \
-d '{"recipeId": "ObjectId('65f97adbd699fa7945c5d178')"}' \
-X DELETE \
-v

# > POST /recipes HTTP/1.1
# > Host: localhost:9000
# > User-Agent: curl/8.4.0
# > Accept: */*
# > Content-Type: application/json
# > Content-Length: 187
# >
# < HTTP/1.1 201 Created
# < X-Powered-By: Express
# < Access-Control-Allow-Origin: *
# < Date: Thu, 21 Mar 2024 17:45:59 GMT
# < Connection: keep-alive
# < Keep-Alive: timeout=5
# < Content-Length: 0

CASE "fail on non existing user"

curl 'http://localhost:9000/recipes' \
-H 'Content-Type: application/json' \
-v

# > POST /recipes HTTP/1.1
# > Host: localhost:9000
# > User-Agent: curl/8.4.0
# > Accept: */*
# > Content-Type: application/json
# > Content-Length: 187
# >
# < HTTP/1.1 404 Not Found
# < X-Powered-By: Express
# < Access-Control-Allow-Origin: *
# < Content-Type: application/json; charset=utf-8
# < Content-Length: 52
# < ETag: W/"34-Cs2INrsYwSHLSHCKVUFPEWh9NjI"
# < Date: Thu, 21 Mar 2024 17:45:59 GMT
# < Connection: keep-alive
# < Keep-Alive: timeout=5
# <
# * Connection #0 to host localhost left intact
# {"error":"NotFoundError","message":"user not found"}%

0 comments on commit c96ac57

Please sign in to comment.