Last week I wrote about how to extend your Ecto model. I wanted to figure it out because I thought this week, I’ll add to mother other flight related data. Turns out, I did it in completely different way. Not sure if this is the correct approach, so feel free to bash mi in comments (in a constructive way!).
One of the data I have on my flights so far is a departure and destination airport IATA code. Those are 3 letters code, commonly used in civil aviation (SFO = San Francisco, CPH = Copenhagen etc.). I wanted to use some simple API to get more data based on this code. So I decided to write my own using Azure Functions and F#. It became a separate adventure. I’ll write another post about it.
When I had my API ready, I started figuring out, how to put it in Phoenix app. My first approach was a bit like you do stuff in ASP.NET MVC – adding those extra fields to the model, and trying to update them from within the controller. Unfortunately, I couldn’t make it work. I couldn’t even access fields of the model.
The solution I ended up doing, is putting all the web calls and logic for parsing them within the view file. It feels dirty for me. It would be very dirty in ASP.NET MVC. So my intuition is, that it’s not the best way to do it. But so far the only one I made working. After those changes my Flight view file looks like that:
https://gist.github.com/mlusiak/b00a756b891e701354cba66fae55bd78
And then I use those functions within the template that renders the view:
https://gist.github.com/mlusiak/ecb3acf26baaad25d32f78af89080319
Immediately you can see another problem with the code – for every information that I want to show, I’m making a separate web request. But it works, which is a huge improvement. Done is better than perfect.
Screenshot to prove that it does, what it should:
That’s all for today. Tune in next week for another part. Also, check previous episodes. And if you’re interested in machine learning, look into my weekly link drop.
You probably do not need to download airport information each time. Also i would recommend some kind of abstraction over airport information structure.
Hey Arkadiusz, Thank you for your comment! As I mentioned, downloading it each time is definitely a bad part of this solution. Do you have some good examples how that kind of abstraction would look like? I’m relatively fresh to the language and framework.
Sure. On http://theerlangelist.com/article/beyond_taskasync you have nice example with Aggregator module. You can check this repository: https://github.com/elpikel/board_game_platform where you can find something similar in gryplanszowe24 application.
Also i would recommend some kind of abstraction over airport information structure. You can check this repository: <a href="https://github.