Skip to content

Commit

Permalink
Updated README and added one more sad path test
Browse files Browse the repository at this point in the history
  • Loading branch information
jdmchugh111 committed Aug 23, 2024
1 parent 3e5b70b commit 49682bc
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 2 deletions.
136 changes: 135 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,25 @@ This is the backend API repository for TurLink. TurLink is a link shortener app
"id": "1",
"type": "user",
"attributes": {
"email": "[email protected]"
"email": "[email protected]",
"links": [
{
"id": 1,
"original": "testlink.com",
"short": "tur.link/4a7c204baeacaf2c",
"user_id": 1,
"created_at": "2024-08-23T15:51:38.866Z",
"updated_at": "2024-08-23T15:51:38.866Z"
},
{
"id": 2,
"original": "testlink.com",
"short": "tur.link/67c758fc",
"user_id": 1,
"created_at": "2024-08-23T15:53:08.573Z",
"updated_at": "2024-08-23T15:53:08.573Z"
},
]
}
}
}
Expand Down Expand Up @@ -89,3 +107,119 @@ This is the backend API repository for TurLink. TurLink is a link shortener app
]
}
```
### Shorten a Link
- **POST** `/api/v1/users/:id/links?link={original link}`
- Description: Creates a shortened link associated with an exisiting user
- Example Request: POST `https://turlink-be-53ba7254a7c1.herokuapp.com/users/1/links?link=testlink.com`
- Successful Response (200 OK):
```json
{
"data": {
"id": "1",
"type": "link",
"attributes": {
"original": "testlink.com",
"short": "tur.link/4a7c204baeacaf2c",
"user_id": 1
}
}
}
```
- Error Response (422 Unprocessable Entity):
```json
{
"errors": [
{
"status": "unprocessable_entity",
"message": "Original can't be blank"
}
]
}
```
- Error Response (404 Not Found):
```json
{
"errors": [
{
"status": "unprocessable_entity",
"message": "User must exist"
}
]
}
```

### All Links for a User
- **GET** `/api/v1/users/:id/links`
- Description: Gets an index of all links for a user
- Example Request: GET `https://turlink-be-53ba7254a7c1.herokuapp.com/users/1/links`
- Successful Response (200 OK):
```json
{
"data": {
"id": "1",
"type": "user",
"attributes": {
"email": "[email protected]",
"links": [
{
"id": 1,
"original": "testlink.com",
"short": "tur.link/4a7c204baeacaf2c",
"user_id": 1,
"created_at": "2024-08-23T15:51:38.866Z",
"updated_at": "2024-08-23T15:51:38.866Z"
},
{
"id": 2,
"original": "testlink.com",
"short": "tur.link/67c758fc",
"user_id": 1,
"created_at": "2024-08-23T15:53:08.573Z",
"updated_at": "2024-08-23T15:53:08.573Z"
},
]
}
}
}
```
- Error Response (422 Unprocessable Entity):
```json
{
"errors": [
{
"status": "unprocessable_entity",
"message": "User must exist"
}
]
}
```

### Return full link when short link is given
- **GET** `/api/v1/links?short={shortened link}`
- Description: Gets full link object when given shortened link
- Example Request: GET `https://turlink-be-53ba7254a7c1.herokuapp.com/links?short=tur.link/4a7c204baeacaf2c`
- Successful Response (200 OK):
```json
{
"data": {
"id": "1",
"type": "link",
"attributes": {
"original": "testlink.com",
"short": "tur.link/4a7c204baeacaf2c",
"user_id": 1
}
}
}
```
- Error Response (404 Not Found):
```json
{
"errors": [
{
"status": "unprocessable_entity",
"message": "User must exist"
}
]
}
```
6 changes: 5 additions & 1 deletion app/controllers/api/v1/links_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ def index

def show
link = Link.find_by(short: params[:short])
render json: LinkSerializer.new(link)
if link != nil
render json: LinkSerializer.new(link)
else
not_found_error
end
end

private
Expand Down
13 changes: 13 additions & 0 deletions spec/requests/api/v1/links_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,18 @@
expect(link2[:attributes][:short]).to eq(short)
expect(link2[:attributes][:user_id]).to eq(user1.id)
end

describe "sad path" do
it "will return a 404 if shortened link doesn't exist" do
get "/api/v1/links?short=tur.link/12345678"

expect(response).to_not be_successful
expect(response.status).to eq(404)

error = JSON.parse(response.body, symbolize_names: true)[:errors][0]

expect(error[:message]).to eq("Record not found")
end
end
end
end

0 comments on commit 49682bc

Please sign in to comment.