diff --git a/README.md b/README.md index 62ef748..2dc7e12 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ dokku run bit cli --create-user=Admin | `/api/ping` | GET | Ping the API to check if it's running | - | | `/:slug` | GET | Retrieve a link by its slug | - | | `/api/links` | GET | Retrieve all links | - | +| `/api/links/:id` | GET | Retrieve a link by its ID | - | | `/api/links` | POST | Create a new link | `{"url": "https://example.com"}` | | `/api/links/:id` | PUT | Update an existing link by its ID | `{"url": "https://newexample.com"}` | | `/api/links/:id` | DELETE | Delete a link by its ID | - | diff --git a/app/controllers/link.cr b/app/controllers/link.cr index c871af2..947e392 100644 --- a/app/controllers/link.cr +++ b/app/controllers/link.cr @@ -102,6 +102,26 @@ module App::Controllers::Link end end + class Get < App::Lib::BaseController + include App::Models + include App::Lib + + def call(env) + user = env.get("user").as(User) + link_id = env.params.url["id"] + + query = Database::Query.where(id: link_id.as(String), user_id: user.id.as(String)).limit(1) + links = Database.all(Link, query, preload: [:clicks]) + + if links.empty? + raise App::NotFoundException.new(env) + end + + response = {"data" => App::Serializers::Link.new(links.first)} + response.to_json + end + end + class Update < App::Lib::BaseController include App::Models include App::Lib diff --git a/app/routes.cr b/app/routes.cr index aed584e..302d084 100644 --- a/app/routes.cr +++ b/app/routes.cr @@ -23,6 +23,10 @@ module App Controllers::Link::All.new.call(env) end + get "/api/links/:id" do |env| + Controllers::Link::Get.new.call(env) + end + post "/api/links" do |env| Controllers::Link::Create.new.call(env) end