Coder Social home page Coder Social logo

phase-4-rails-practice-challenge-cosmic-challenge's Introduction

Rails Mock Challenge - Cosmic Travel

It is the year 2100 and you run an interplanetary space travel agency. You are building a website to book scientists on missions to other planets.

In this repo, there is a Rails application with some features built out. There is also a fully built React frontend application, so you can test if your API is working.

Your job is to build out the Rails API to add the functionality described in the deliverables below.

Setup

To download the dependencies for the frontend and backend, run:

$ bundle install
$ npm install --prefix client

There is some starter code in the db/seeds.rb file so that once you've generated the models, you'll be able to create data to test your application.

You can run your Rails API on localhost:3000 by running:

$ rails s

You can run your React app on localhost:4000 by running:

$ npm start --prefix client

You are not being assessed on React, and you don't have to update any of the React code; the frontend code is available just so that you can test out the behavior of your API in a realistic setting.

There are also tests included which you can run using rspec to check your work.

Depending on your preference, you can either check your progress by:

  • Running rspec and seeing if your code passes the tests
  • Running the React application in the browser and interacting with the API via the frontend
  • Running the Rails server and using Postman (or equivalent API client) to make requests

Models

It is your job to build out Planet, Scientist, and Mission models so that scientists can book their missions. In a given mission, one scientist will visit one planet. Over their careers, scientists will visit many planets and planets will be visited by many scientists.

You need to create the following relationships:

  • A Scientist has many Missions, and has many Planets through Missions
  • An Planet has many Missions, and has many Scientists through Missions
  • A Mission belongs to a Scientist and belongs to a Planet

Start by creating the models and migrations for the following database tables:

cosmic_erd

If you use a Rails generator to create the models, make sure to use the --no-test-framework flag to avoid overwriting the test files.

Add any code needed in the model files to establish the relationships.

Then, run the migrations and seed file:

$ rails db:migrate db:seed

If you aren't able to get the provided seed file working, you are welcome to generate your own seed data to test the application.

Validations

Add validations to the Scientist model:

  • must have a name, and a field_of_study
  • names must be unique

Add validations to the Mission model:

  • must have a name, a scientist and a planet
  • a scientist cannot join the same mission twice

Routes

Set up the following routes. Make sure to return JSON data in the format specified along with the appropriate HTTP verb.

GET /scientists

Return JSON data in the format below. Note: you should return a JSON response in this format, without any additional nested data related to each scientist.

[
  {
    "id": 1,
    "name": "Mel T. Valent",
    "field_of_study": "xenobiology",
    "avatar": "https://robohash.org/mel_t_valent?set=set5"
  },
  {
    "id": 2,
    "name": "P. Legrange",
    "field_of_study": "orbital mechanics",
    "avatar": "https://robohash.org/p_legrange?set=set5"
  }
]

GET /scientists/:id

If the Scientist exists, return JSON data in the format below. Note: you will need to serialize the data for this response differently than for the GET /scientists route. Make sure to include an array of missions for each scientist.

{
  "id": 1,
  "name": "Mel T. Valent",
  "field_of_study": "xenobiology",
  "avatar": "https://robohash.org/mel_t_valent?set=set5",
  "planets": [
    {
      "id": 1,
      "name": "TauCeti E",
      "distance_from_earth": "12 light years",
      "nearest_star": "TauCeti",
      "image": "planet3"
    },
    {
      "id": 2,
      "name": "Maxxor",
      "distance_from_earth": "9 parsecs",
      "nearest_star": "Canus Minor",
      "image": "planet7"
    }
  ]
}

If the Scientist does not exist, return the following JSON data, along with the appropriate HTTP status code:

{
  "error": "Scientist not found"
}

POST /scientists

This route should create a new Scientist. It should accept an object with the following properties in the body of the request:

{
  "name": "Evan T'Horizon",
  "field_of_study": "astronavigation",
  "avatar": "https://robohash.org/evan_thorizon?set=set5"
}

If the Scientist is created successfully, send back a response with the new Scientist:

{
  "id": 3,
  "name": "Evan T'Horizon",
  "field_of_study": "astronavigation",
  "avatar": "https://robohash.org/evan_thorizon?set=set5"
}

If the Scientist is not created successfully, return the following JSON data, along with the appropriate HTTP status code:

{
  "errors": ["validation errors"]
}

PATCH /scientists/:id

This route should update an existing Scientist. It should accept an object with one or more of the following properties in the body of the request:

{
  "name": "Bevan T'Horizon",
  "field_of_study": "warp drive tech",
  "avatar": "https://robohash.org/bevan_thorizon?set=set5"
}

If the Scientist is updated successfully, send back a response with the updated Scientist and a 202 :accepted status code:

{
  "id": 2,
  "name": "Bevan T'Horizon",
  "field_of_study": "warp drive tech",
  "avatar": "https://robohash.org/bevan_thorizon?set=set5"
}

If the Scientist is not updated successfully, return the following JSON data, along with the appropriate HTTP status code:

{
  "errors": ["validation errors"]
}

OR, given an invalid ID, the appropriate HTTP status code, and the following JSON:

{
  "error": "Scientist not found"
}

DELETE /scientists/:id

If the Scientist exists, it should be removed from the database, along with any Missions that are associated with it (a Mission belongs to an Scientist, so you need to delete the Missions before the Scientist can be deleted).

After deleting the Scientist, return an empty response body, along with the appropriate HTTP status code.

If the Scientist does not exist, return the following JSON data, along with the appropriate HTTP status code:

{
  "error": "Scientist not found"
}

GET /planets

Return JSON data in the format below. Note: you should return a JSON response in this format, without any additional nested data related to each planet.

[
  {
    "id": 1,
    "name": "TauCeti E",
    "distance_from_earth": "12 light years",
    "nearest_star": "TauCeti",
    "image": "planet3"
  },
  {
    "id": 2,
    "name": "Maxxor",
    "distance_from_earth": "9 parsecs",
    "nearest_star": "Canus Minor",
    "image": "planet7"
  }
]

POST /missions

This route should create a new Missions. It should accept an object with the following properties in the body of the request:

{
  "name": "Project Terraform",
  "scientist_id": 1,
  "planet_id": 2
}

If the Mission is created successfully, send back a response with the planet associated with the new Mission (contrary to convention, which normally dictates the response would include data about the mission that was created):

{
  "id": 2,
  "name": "Maxxor",
  "distance_from_earth": "9 parsecs",
  "nearest_star": "Canus Minor",
  "image": "planet7"
}

If the Mission is not created successfully, return the following JSON data, along with the appropriate HTTP status code:

{
  "errors": ["validation errors"]
}

phase-4-rails-practice-challenge-cosmic-challenge's People

Contributors

betalantz avatar darkcohiba avatar lizbur10 avatar morgvanny avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

phase-4-rails-practice-challenge-cosmic-challenge's Issues

Various issues with "Rails Mock Challenge - Cosmic Travel"

Canvas Link

https://learning.flatironschool.com/courses/5195/assignments/195171?module_item_id=438811

Concern

Various issues you might want to consider for this mock code challenge:

  • It would be nice if "byebug" or "pry" were in the Gemfile. (Although it's not out of bounds at this point for that to be a step in the challenge.)

  • change this:
    An Planet has many Missions,
    to this:
    A Planet has many Missions,

  • This one has probably been raised already:
    Change this:
    include an array of missions for each scientist.
    to this:
    include an array of planets for each scientist.

  • The way the validation errors are written up seems misleading. (Three instances in this challenge. There is a similar issue (one time) in "Rails Code Challenge - Late Show". Not being exact and clear in the environment of an actual code challenge adds unnecessary stress.)
    The lab guide directs to present multi-error validation notices as a one-element array containing the string "validation errors". As such, maybe the tests should test for this single-element array? I'm guessing the student should return the actual error messages in these arrays. Can that point be stated more clearly, maybe with multiple actual error messages in "the following JSON data" instead of a single generic string there? Or an additional qualifier statement saying something like:
    ("You will show actual validation errors instead of the string "validation errors".)

If the Scientist is not created successfully, return the following JSON data, along with the appropriate HTTP status code:

{
  "errors": ["validation errors"]
}

If the Scientist is not updated successfully, return the following JSON data, along with the appropriate HTTP status code:

{
  "errors": ["validation errors"]
}

If the Mission is not created successfully, return the following JSON data, along with the appropriate HTTP status code:

{
  "errors": ["validation errors"]
}
  • change this:
    This route should create a new Missions.
    to this:
    This route should create a new Mission.

  • In places where "Missions" is the proper term, you might change its formatting to highlight "Mission" and tack on a letter "s" at the end as happens for Scientist-s and Planet-s. e.g. here: A Scientist has many Missions,
    (There are not actual "-" chars, but placing them here emphasizes the effect achieved.)

  • Another that's likely fixed already:

In the code that is provided:
In the lab code's "client/src/components/AddMission.jsx" we had to add "await" on line 54 to get:

        } else {
            const messages = await res.json()
            setErrors(messages.errors)
  • The solution branch for "Rails Mock Challenge - Cosmic Travel" appears to have a validation that's too strict, i.e. validates :scientist, :uniqueness: {scope: :name}

When considering the cosmic traveler validation of "a scientist cannot join the same mission twice", what does our client mean by the term "mission"?
Well, if records in the mission table define missions, such that each mission is the combination of a scientist, a planet, and a mission name,
then the client is specifying that "a scientist cannot join the same [combination of a scientist, a planet, and a mission name] twice". (It would be nice to ask, but we have to go with what's written.)

This would mean:
Brannan can't go on a GoldDigger mission to Mars twice.
Brannan can't go on a GoldDigger mission to Jupiter twice.
Brannan can't go on a SilverMining mission to Mars twice.
Brannan can't go on a SilverMining mission to Jupiter twice.

This rule ("a scientist cannot join the same mission twice") does not say:
Brannan can go on a GoldDigger mission to Mars
but
Brannan can't also go on a GoldDigger mission to Jupiter.

The rule says nothing about mission names, it only covers missions (Combo of name/scientist/planet.)
As such, a rule in models/mission.rb like
validates :scientist, :uniqueness: {scope: :name}
is too strict since it prevents Brannan from going on GoldDigger missions to multiple planets.
For the rule as stated, it seems that a validation like
validates :scientist, uniqueness: {scope: [:name, :planet]}
is more appropriate.
"validates :scientist, :uniqueness: {scope: :name}" would be ok if we change the constraint to "a scientist cannot join a mission with the same name twice."

Another approach: It's an important job skill for developers to talk with the Product Owner or the customer about aspects of a story that are vague. It would be an interesting twist for the lab/challenge to direct the student to talk to the client/instructor about any aspects of the story that seem unclear. In particular, it could say "As part of this challenge, you will need to ask your client (your instructor) to clarify what they mean by the term "mission". Their response might change how you validate certain information." This trains the student to be sure they are in agreement about what important words mean. When asked, the instructor might say "A mission name can be reused provided the scientist is unique." This new information would lead to scoping scientist uniqueness in missions to only name rather than both name and planet.

Additional Context

No response

Suggested Changes

See "Concern" above.

Rails mock challenge - cosmic travel

Canvas Link

https://learning.flatironschool.com/courses/5851/assignments/217042?module_item_id=505197

Concern

I think someone accidentally updated the GitHub repo, because when I forked and cloned the repo and opened it in vs code, it had all the necessary models, routes, validations, etc...
Additionally, some strange things are happening with the tests relating to the Planet model. I tried running learn test, and it said I needed to add routes for the show, create, update (put, patch), and destroy. When I add them, I fail a different test saying to not add unnecessary routes, so there is something happening in the spec files as well.

Additional Context

image

image

After changing routes from resources :planet, only: :index -----> resources :planet

image

image

Suggested Changes

Remove all code that completes lab, and fix the wonky tests I guess....

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.