Skip to content

Commit

Permalink
vote and favorite
Browse files Browse the repository at this point in the history
  • Loading branch information
mudroljub committed Dec 13, 2024
1 parent 57e8e78 commit d1c3031
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 12 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ Feel free to get involved, suggest or implement new features.

Access to these routes requires a valid token.

#### POST `/api/quotes/favorite/:id`
- **Description**: Add quote to favorites

#### POST `/api/quotes/vote`
- **Description**: Votes for a specific quote.
- **Required Parameters** (in JSON body):
- `quoteId`: The ID of the quote.
- `newVote`: A numeric value (1–5).

#### POST `/api/quotes`
- **Description**: Adds a new quote.
- **Required Parameters** (in JSON body):
Expand All @@ -45,12 +54,6 @@ Access to these routes requires a valid token.
- **Parameters** (in JSON body):
- `author`, `text`, or `source`.

#### POST `/api/quotes/vote`
- **Description**: Votes for a specific quote.
- **Required Parameters** (in JSON body):
- `quoteId`: The ID of the quote.
- `newVote`: A numeric value (1–5).

#### DELETE `/api/quotes/:id`
- **Description**: Deletes a quote by its unique ID.

Expand Down
15 changes: 13 additions & 2 deletions src/controllers/QuoteController.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,24 @@ const vote = async(req, res) => {

try {
const quote = await QuoteService.applyVote(quoteId, vote, req.user)
await UserService.updateVoted(req.user.id, quoteId)

res.json({ message: 'SUCCESS', quote })
} catch (err) {
handleError(res, err)
}
}

const favorite = async(req, res) => {
if (!req.user)
return res.status(400).send({ message: 'NO_USER' })

try {
await UserService.addToFavorites(req.user.id, req.params.id)
res.json({ message: 'SUCCESS' })
} catch (err) {
handleError(res, err)
}
}

const deleteQuote = async(req, res) => {
try {
await QuoteService.delete(req.params.id)
Expand All @@ -107,4 +117,5 @@ export default {
update,
vote,
delete: deleteQuote,
favorite,
}
2 changes: 1 addition & 1 deletion src/dto/UserResponseDTO.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ export default class UserResponseDTO {
this.email = user.email
this.privilege = user.privilege
this.memberSince = user.memberSince
// this.voted = user.voted
this.favorites = user.favorites
}
}
2 changes: 1 addition & 1 deletion src/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const userSchema = new Schema({
required: true,
maxlength: 100,
},
voted: [
favorites: [
{
type: Schema.Types.ObjectId,
ref: 'Quote',
Expand Down
1 change: 1 addition & 0 deletions src/routes/quotesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const router = new Router()
// preceding routes
router.get('/random', QuoteController.random)
router.post('/vote', authenticate, QuoteController.vote)
router.post('/favorite/:id', authenticate, QuoteController.favorite)

// query params: page, quotesPerPage, author
router.get('/', normalizeQueryParams, compression(), QuoteController.getQuotes)
Expand Down
5 changes: 3 additions & 2 deletions src/services/UserService.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ const addPrivilege = async(id, privilege) => {
return new UserResponseDTO(user)
}

const updateVoted = (userId, quoteId) =>
User.updateOne({ _id: userId }, { $addToSet: { voted: quoteId } })
const addToFavorites = (userId, quoteId) =>
User.updateOne({ _id: userId }, { $addToSet: { favorites: quoteId } })

const findOrCreate = async(email, password) =>
await getMyUser(email, password) || await create(email, password)
Expand All @@ -67,4 +67,5 @@ export default {
getByEmail,
findOrCreate,
addPrivilege,
addToFavorites,
}

0 comments on commit d1c3031

Please sign in to comment.