DEV Community

Discussion on: Creating a JSON API with Athena & Granite

Collapse
 
blacksmoke16 profile image
Blacksmoke16

This is a result of some breaking changes that happened in the new Granite version. See github.com/amberframework/granite/...

I'll update the guide to reflect those changes.

Collapse
 
phangs profile image
phangs

Thank you! Looking forward to the update

Thread Thread
 
blacksmoke16 profile image
Blacksmoke16

Should be good to go now, let me know if you run into any trouble.

Thread Thread
 
phangs profile image
phangs

tried the source again from the repo, new error:

Athena::Routing::Converters::Athena::Routing::Converters::RequestBody(Blog::Models::Article, Nil).new.convert val
--------------------------------------------------------------------
Error: undefined constant Athena::Routing::Converters::Athena::Routing::Converters::RequestBody

I'm still figuring out the problem just would like to let you know.

Thread Thread
 
blacksmoke16 profile image
Blacksmoke16 • Edited

Make sure you do a shards update I updated some dependencies and pinned the versions so it'll always use the correct version.

Thread Thread
 
phangs profile image
phangs

tried again with the shards update. I still couldn't figure out. sorry, I am new to programming, I am a business analyst, and was thinking I could grow with crystal as my realy programming language that's why I am trying to learn it.

Thread Thread
 
phangs profile image
phangs

@routes.add "/POST/user", RouteAction(

846 | # Map Nil return type to Noop to avoid github.com/crystal-lang/crystal/is...
847 | Proc(HTTP::Server::Context, Hash(String, String?), Blog::Models::User), Athena::Routing::Renderers::JSONRenderer, Blog::Controllers::UserController)
848 | .new(
849 | __temp_647,
850 | RouteDefinition.new("/POST/user", nil),
851 | Callbacks.new([] of CallbackBase, [] of CallbackBase),
852 | "new_user",
853 | ["default"],
854 | [Athena::Routing::Parameters::BodyParameter(Blog::Models::User).new("body")] of Athena::Routing::Parameters::Param
855 | )

Thread Thread
 
blacksmoke16 profile image
Blacksmoke16

what's the actual error you're getting? Should be towards the very bottom.

I ran through the tutorial on crystal 0.31.1 and everything was fine, so also make sure that's up to date.

Thread Thread
 
phangs profile image
phangs • Edited

`wilbert@wilbert-UX360CAK:~/Documents/Development/crystal/athena-blog-tutorial$ crystal ./src/blog.cr
Showing last frame. Use --error-trace for full trace.

There was a problem expanding macro 'macro_140521444230352'

Code in lib/athena/src/routing/handlers/route_handler.cr:19:7

19 | {% for klass in Athena::Routing::Controller.all_subclasses %}
^
Called macro defined in lib/athena/src/routing/handlers/route_handler.cr:19:7

19 | {% for klass in Athena::Routing::Controller.all_subclasses %}

Which expanded to:

140 | arr << if val = vals[key]?
141 |

142 | Athena::Routing::Converters::Athena::Routing::Converters::RequestBody(Blog::Models::Article, Nil).new.convert val
--------------------------------------------------------------------
Error: undefined constant Athena::Routing::Converters::Athena::Routing::Converters::RequestBody
wilbert@wilbert-UX360CAK:~/Documents/Development/crystal/athena-blog-tutorial$`

Thread Thread
 
blacksmoke16 profile image
Blacksmoke16 • Edited

Ahhh I figured it out. Apparently shards update doesn't actually update the directory in ./lib, thus my version locally was still using the older Athena version. I'll push a fix right now.

Thread Thread
 
phangs profile image
phangs

that's great! thank you very much. I will check in a bit and clone the project again.

I really appreciate your help

Thread Thread
 
phangs profile image
phangs

Code is now running, just noticed:

  1. After cloning, user must create the logs/development.log directory and file. The code will look for it and will not compile it does not exist.

  2. Was not yet able to find the problem when send post request to localhost:8888/user, error in logs is:
    [2019-11-25T05:37:51.194843000Z] main.CRITICAL: Unhandled exception: relation "users" does not exist in Blog::Controllers::UserController at src/controllers/user_controller.cr:6:107 {"cause":null,"cause_class":"Nil"}

I will try later to figure this out

Thread Thread
 
blacksmoke16 profile image
Blacksmoke16

Thanks, I pushed a fix for #1. The other error would be because Granite can't find a table called users in the database you're connected to. Be sure you ran the few SQL scripts I've included if you're using PG. Otherwise, be sure you create tables in your DB of choice.