Skip to main content

Overview

The Favorite Tours API allows tourists to save their favorite tours for easy reference and future booking. This resource uses a composite key consisting of touristId and tourId to uniquely identify favorite relationships.

Composite Key Structure

Favorite tour records are identified by a composite primary key:
  • touristId: The ID of the tourist user
  • tourId: The ID of the tour
This composite key ensures that each tourist-tour pair can only exist once in the system, preventing duplicate favorites.

Endpoints

Create Favorite Tour

curl -X POST http://localhost:8080/api/favorite_tours \
  -H "Content-Type: application/json" \
  -d '{
    "touristId": 123,
    "tourId": 789
  }'
Adds a tour to a tourist’s favorites list.
touristId
Long
required
The ID of the tourist user marking the tour as favorite
tourId
Long
required
The ID of the tour being marked as favorite
createdAt
datetime
Optional timestamp. If not provided, the server will set it automatically.
touristId
Long
The ID of the tourist user
tourId
Long
The ID of the tour
createdAt
datetime
Timestamp when the favorite was created

Get All Favorite Tours

curl http://localhost:8080/api/favorite_tours
Retrieves a list of all favorite tour relationships.
favorites
array
Array of favorite tour objects

Get Favorite Tour by Composite Key

curl http://localhost:8080/api/favorite_tours/123/789
Retrieves a specific favorite tour relationship using the composite key.
touristId
Long
required
The ID of the tourist user
tourId
Long
required
The ID of the tour
touristId
Long
The ID of the tourist user
tourId
Long
The ID of the tour
createdAt
datetime
Timestamp when the favorite was created

Update Favorite Tour

curl -X PUT http://localhost:8080/api/favorite_tours/123/789 \
  -H "Content-Type: application/json" \
  -d '{
    "createdAt": "2024-03-15T14:30:00"
  }'
Updates an existing favorite tour relationship. Typically used to update metadata like the creation timestamp.
touristId
Long
required
The ID of the tourist user
tourId
Long
required
The ID of the tour
createdAt
datetime
Updated timestamp for when the favorite was created
touristId
Long
The ID of the tourist user
tourId
Long
The ID of the tour
createdAt
datetime
Updated timestamp

Delete Favorite Tour

curl -X DELETE http://localhost:8080/api/favorite_tours/123/789
Removes a tour from a tourist’s favorites list using the composite key.
touristId
Long
required
The ID of the tourist user
tourId
Long
required
The ID of the tour
status
204
No content returned on successful deletion

Usage Examples

Check if Tour is Favorited

To check if a specific tour is in a tourist’s favorites:
curl http://localhost:8080/api/favorite_tours/123/789
  • If the relationship exists, you’ll receive a 200 response with the favorite data
  • If it doesn’t exist, you’ll receive a 404 error

Get Tourist’s Favorite Tours

To retrieve all tours favorited by a specific tourist, filter the results from the get all endpoint:
curl http://localhost:8080/api/favorite_tours | jq '.[] | select(.touristId == 123)'

Get Tourists Who Favorited a Tour

To see which tourists have favorited a specific tour (useful for analytics):
curl http://localhost:8080/api/favorite_tours | jq '.[] | select(.tourId == 789)'

Toggle Favorite Status

To implement a toggle favorite feature in your application:
  1. First, check if the tour is already favorited (GET request)
  2. If it exists (200 response), delete it (DELETE request)
  3. If it doesn’t exist (404 response), create it (POST request)
#!/bin/bash

TOURIST_ID=123
TOUR_ID=789
API_URL="http://localhost:8080/api/favorite_tours"

# Check if favorite exists
if curl -s -o /dev/null -w "%{http_code}" "$API_URL/$TOURIST_ID/$TOUR_ID" | grep -q "200"; then
  # Delete if exists
  curl -X DELETE "$API_URL/$TOURIST_ID/$TOUR_ID"
  echo "Tour removed from favorites"
else
  # Create if doesn't exist
  curl -X POST "$API_URL" \
    -H "Content-Type: application/json" \
    -d "{\"touristId\": $TOURIST_ID, \"tourId\": $TOUR_ID}"
  echo "Tour added to favorites"
fi

Relationships

The FavoriteTour entity maintains relationships with:
  • Tourist User: References the user table via touristIduser_id
  • Tour: References the tours table via tourIdtour_id
Both IDs must reference valid records in their respective tables.

Use Cases

  • Wishlist: Tourists can build a wishlist of tours they’re interested in
  • Quick Access: Easy retrieval of saved tours for comparison and booking
  • Recommendations: Analyze favorite patterns to improve tour recommendations
  • Marketing: Notify tourists about updates or deals on their favorited tours

Build docs developers (and LLMs) love