Skip to content

Instantly share code, notes, and snippets.

@adaam2
Last active September 23, 2020 16:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adaam2/12ad410153c9c8339a2b51789751933a to your computer and use it in GitHub Desktop.
Save adaam2/12ad410153c9c8339a2b51789751933a to your computer and use it in GitHub Desktop.

Intro

I want you to build a Rails API that helps a logistics company to manage their fleet of vehicles.

The company has many depots around the UK, and each depot has a set of vehicles that serve the depot.

Entities

You will be responsible for modelling the entities and their relationships above.

To start off, it's probably best to do some research about the underlying database tables and their links to one another. Usually you would create something called a ERD (entity relationship diagram) to model your database tables prior to actually jumping into coding the Rails models that generate the database tables. There are many tools for diagramming online that you can use to do this for free.

I want you to use Rails models to represent each of the entities below. The entities and their properties are detailed below:

Company

The company, in this case, is called Sam Incorporated:

  • It has many depots
  • It has a name e.g. Sam Incorporated

Depot

A depot has:

  • Many vehicles attached to it.
  • It's location e.g. Bristol

location is required, and there must be at least one vehicle attached to each depot.

Vehicle

A vehicle:

  • Belongs to only one depot
  • Has a registration number e.g AD55 GS0
  • Has a make e.g. Vauxhall
  • Has a model e.g Astra
  • Has a mileage e.g. 30000 miles

make, model and mileage, registration_number are required fields

Public facing API

I want you to create two new endpoints which will allow:

  • Creation of a new vehicle within a particular depot. This will be a HTTP POST request. The path should be api/depots/{depot_id}/vehicles
  • Retrieve a list of vehicles for a particular depot. This will be a HTTP GET request. The path should be api/depots/{depot_id}/vehicles

The JSON response payload returned for both endpoints and the request payload (for the POST) is up to you. But when you are creating a vehicle, the endpoint must allow you to create a vehicle and set all of its required fields.

Getting started

Check you have a recent version of Rails (5 or 6):

rails -v

You will need to create a brand new Ruby on Rails application from scratch. I want you to create an API only Rails app (e.g with no views) and I want you to use postgresql as the database.

You can do this by running:

rails new sams-company --api --database=postgresql

Beyond this, you are on your own (although obviously I will help when you get stuck).

Testing

I would expect you to add unit tests for the endpoints, and also for all of the models that you have created to test any non trivial functionality.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment