feat: update link
This commit is contained in:
+1
-1
@@ -1,2 +1,2 @@
|
||||
DATABASE_URL=sqlite3://./sqlite/data.db
|
||||
APP_URL=http://localhost:3000
|
||||
APP_URL=http://localhost:4000
|
||||
|
||||
+30
-5
@@ -36,8 +36,7 @@ module App::Controllers::Link
|
||||
def call(env)
|
||||
slug = env.params.url["slug"]
|
||||
|
||||
link = Database.get_by(Link, slug: slug)
|
||||
raise App::NotFoundException.new(env) if !link
|
||||
link = Database.get_by!(Link, slug: slug)
|
||||
|
||||
spawn do
|
||||
link.click_counter = link.click_counter! + 1
|
||||
@@ -59,13 +58,39 @@ module App::Controllers::Link
|
||||
def call(env)
|
||||
id = env.params.url["id"]
|
||||
|
||||
link = Database.get(Link, id)
|
||||
raise App::NotFoundException.new(env) if !link
|
||||
link = Database.get!(Link, id)
|
||||
|
||||
response = {"data" => App::Serializers::Link.new(link)}
|
||||
response.to_json
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: update, delete
|
||||
class Update < App::Lib::BaseController
|
||||
include App::Models
|
||||
include App::Lib
|
||||
|
||||
def call(env)
|
||||
id = env.params.url["id"]
|
||||
|
||||
json_params = env.params.json.to_h
|
||||
url = json_params.has_key?("url") ? json_params["url"] : nil
|
||||
raise App::BadRequestException.new(env, {"url" => "Required field"}) if !url
|
||||
|
||||
link = Database.get!(Link, id)
|
||||
link.url = url.to_s
|
||||
link.click_counter = 0
|
||||
|
||||
changeset = Database.update(link)
|
||||
|
||||
if !changeset.valid?
|
||||
errors = {"errors" => map_changeset_errors(changeset.errors)}
|
||||
raise App::UnprocessableEntityException.new(env, errors)
|
||||
end
|
||||
|
||||
response = {"data" => App::Serializers::Link.new(link)}
|
||||
response.to_json
|
||||
end
|
||||
end
|
||||
|
||||
# TODO: delete
|
||||
end
|
||||
|
||||
@@ -9,13 +9,6 @@ module App
|
||||
end
|
||||
end
|
||||
|
||||
class NotFoundException < Kemal::Exceptions::CustomException
|
||||
def initialize(context)
|
||||
context.response.status_code = 404
|
||||
super(context)
|
||||
end
|
||||
end
|
||||
|
||||
class UnprocessableEntityException < Kemal::Exceptions::CustomException
|
||||
def initialize(context, message = Hash(String, String))
|
||||
context.response.status_code = 422
|
||||
|
||||
@@ -20,4 +20,8 @@ module App
|
||||
post "/api/links" do |env|
|
||||
Controllers::Link::Create.new.call(env)
|
||||
end
|
||||
|
||||
put "/api/links/:id" do |env|
|
||||
Controllers::Link::Update.new.call(env)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
meta {
|
||||
name: Create Link - Malformed
|
||||
type: http
|
||||
seq: 3
|
||||
seq: 2
|
||||
}
|
||||
|
||||
post {
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
meta {
|
||||
name: Create Link
|
||||
type: http
|
||||
seq: 2
|
||||
seq: 1
|
||||
}
|
||||
|
||||
post {
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
meta {
|
||||
name: Get Link
|
||||
type: http
|
||||
seq: 3
|
||||
}
|
||||
|
||||
get {
|
||||
url: {{baseUrl}}/api/links/0da9dc9d-c56c-4d4e-942e-1ebf05ed7090
|
||||
body: none
|
||||
auth: none
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
meta {
|
||||
name: Index Link
|
||||
type: http
|
||||
seq: 4
|
||||
}
|
||||
|
||||
get {
|
||||
url: {{baseUrl}}/3JQV8w
|
||||
body: none
|
||||
auth: none
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
meta {
|
||||
name: Update Link
|
||||
type: http
|
||||
seq: 5
|
||||
}
|
||||
|
||||
put {
|
||||
url: {{baseUrl}}/api/links/0da9dc9d-c56c-4d4e-942e-1ebf05ed7090
|
||||
body: json
|
||||
auth: none
|
||||
}
|
||||
|
||||
body:json {
|
||||
{
|
||||
"url": "https://idonthavespotify.donado.co"
|
||||
}
|
||||
}
|
||||
+4
-3
@@ -7,8 +7,9 @@ require "./app/serializers/*"
|
||||
|
||||
require "./app/routes"
|
||||
|
||||
error 500 { |env| {"error" => "Internal Server Error"}.to_json }
|
||||
error 401 { |env| {"error" => "Unauthorized"}.to_json }
|
||||
error 403 { |env| {"error" => "Forbidden"}.to_json }
|
||||
error 500 { |env| { "error" => "Internal Server Error" }.to_json }
|
||||
error 401 { |env| { "error" => "Unauthorized" }.to_json }
|
||||
error 403 { |env| { "error" => "Forbidden" }.to_json }
|
||||
error 404 { |env| { "error" => "Not Found" }.to_json }
|
||||
|
||||
Kemal.run
|
||||
|
||||
Reference in New Issue
Block a user