Skip to content

Latest commit

 

History

History
213 lines (186 loc) · 12.8 KB

File metadata and controls

213 lines (186 loc) · 12.8 KB

Assignment: REST Service

Description

Let's try to create a Home Library Service! Users can create, read, update, delete data about Artists, Tracks and Albums, add them to Favorites in their own Home Library!

NB! You must create new repository from template for this task. Its name must be nodejs2022Q2-service i.e. full link to the repository must be https://github.com/%your-gihub-id%/nodejs2022Q2-service.

Create an application, the application should operate with the following resources:

  • User (with attributes):

    interface User {
      id: string; // uuid v4
      login: string;
      password: string;
      version: number; // integer number, increments on update
      createdAt: number; // timestamp of creation
      updatedAt: number; // timestamp of last update
    }
  • Artist (with attributes):

    interface Artist {
      id: string; // uuid v4
      name: string;
      grammy: boolean;
    }
  • Track (with attributes):

    interface Track {
      id: string; // uuid v4
      name: string;
      artistId: string | null; // refers to Artist
      albumId: string | null; // refers to Album
      duration: number; // integer number
    }
  • Album (with attributes):

    interface Album {
      id: string; // uuid v4
      name: string;
      year: number;
      artistId: string | null; // refers to Artist
    }
  • Favorites (with attributes):

    interface Favorites {
      artists: string[]; // favorite artists ids
      albums: string[]; // favorite albums ids
      tracks: string[]; // favorite tracks ids
    }

Details:

  1. For Users, Artists, Albums, Tracks and Favorites REST endpoints with separate router paths should be created
  • Users (/user route)

    • GET /user - get all users
      • Server should answer with status code 200 and all users records
    • GET /user/:id - get single user by id
      • Server should answer with status code 200 and and record with id === userId if it exists
      • Server should answer with status code 400 and corresponding message if userId is invalid (not uuid)
      • Server should answer with status code 404 and corresponding message if record with id === userId doesn't exist
    • POST /user - create user (following DTO should be used) CreateUserDto
          interface CreateUserDto {
            login: string;
            password: string;
          }
      • Server should answer with status code 201 and newly created record if request is valid
      • Server should answer with status code 400 and corresponding message if request body does not contain required fields
    • PUT /user/:id - update user's password UpdatePasswordDto (with attributes):
      interface UpdatePasswordDto {
        oldPassword: string; // previous password
        newPassword: string; // new password
      }
      • Server should answer with status code 200 and updated record if request is valid
      • Server should answer with status code 400 and corresponding message if userId is invalid (not uuid)
      • Server should answer with status code 404 and corresponding message if record with id === userId doesn't exist
      • Server should answer with status code 403 and corresponding message if oldPassword is wrong
    • DELETE /user/:id - delete user
      • Server should answer with status code 204 if the record is found and deleted
      • Server should answer with status code 400 and corresponding message if userId is invalid (not uuid)
      • Server should answer with status code 404 and corresponding message if record with id === userId doesn't exist
  • Tracks (/track route)

    • GET /track - get all tracks
      • Server should answer with status code 200 and all tracks records
    • GET /track/:id - get single track by id
      • Server should answer with status code 200 and and record with id === trackId if it exists
      • Server should answer with status code 400 and corresponding message if trackId is invalid (not uuid)
      • Server should answer with status code 404 and corresponding message if record with id === trackId doesn't exist
    • POST /track - create new track
      • Server should answer with status code 201 and newly created record if request is valid
      • Server should answer with status code 400 and corresponding message if request body does not contain required fields
    • PUT /track/:id - update track info
      • Server should answer with status code 200 and updated record if request is valid
      • Server should answer with status code 400 and corresponding message if trackId is invalid (not uuid)
      • Server should answer with status code 404 and corresponding message if record with id === trackId doesn't exist
    • DELETE /track/:id - delete track
      • Server should answer with status code 204 if the record is found and deleted
      • Server should answer with status code 400 and corresponding message if trackId is invalid (not uuid)
      • Server should answer with status code 404 and corresponding message if record with id === trackId doesn't exist
  • Artists (/artist route)

    • GET /artist - get all artists
      • Server should answer with status code 200 and all artists records
    • GET /artist/:id - get single artist by id
      • Server should answer with status code 200 and and record with id === artistId if it exists
      • Server should answer with status code 400 and corresponding message if artistId is invalid (not uuid)
      • Server should answer with status code 404 and corresponding message if record with id === artistId doesn't exist
    • POST /artist - create new artist
      • Server should answer with status code 201 and newly created record if request is valid
      • Server should answer with status code 400 and corresponding message if request body does not contain required fields
    • PUT /artist/:id - update artist info
      • Server should answer with status code 200 and updated record if request is valid
      • Server should answer with status code 400 and corresponding message if artist is invalid (not uuid)
      • Server should answer with status code 404 and corresponding message if record with id === artistId doesn't exist
    • DELETE /artist/:id - delete album
      • Server should answer with status code 204 if the record is found and deleted
      • Server should answer with status code 400 and corresponding message if artistId is invalid (not uuid)
      • Server should answer with status code 404 and corresponding message if record with id === artistId doesn't exist
  • Albums (/album route)

    • GET /album - get all albums
      • Server should answer with status code 200 and all albums records
    • GET /album/:id - get single album by id
      • Server should answer with status code 200 and and record with id === albumId if it exists
      • Server should answer with status code 400 and corresponding message if albumId is invalid (not uuid)
      • Server should answer with status code 404 and corresponding message if record with id === albumId doesn't exist
    • POST /album - create new album
      • Server should answer with status code 201 and newly created record if request is valid
      • Server should answer with status code 400 and corresponding message if request body does not contain required fields
    • PUT /album/:id - update album info
      • Server should answer with status code 200 and updated record if request is valid
      • Server should answer with status code 400 and corresponding message if albumId is invalid (not uuid)
      • Server should answer with status code 404 and corresponding message if record with id === albumId doesn't exist
    • DELETE /album/:id - delete album
      • Server should answer with status code 204 if the record is found and deleted
      • Server should answer with status code 400 and corresponding message if albumId is invalid (not uuid)
      • Server should answer with status code 404 and corresponding message if record with id === albumId doesn't exist
  • Favorites

    • GET /favs - get all favorites
      • Server should answer with status code 200 and all favorite records (not their ids), split by entity type:
      interface FavoritesRepsonse{
        artists: Artist[];
        albums: Album[];
        tracks: Track[];
      }
    • POST /favs/track/:id - add track to the favorites
      • Server should answer with status code 201 and corresponding message if track with id === trackId exists
      • Server should answer with status code 400 and corresponding message if trackId is invalid (not uuid)
      • Server should answer with status code 422 and corresponding message if track with id === trackId doesn't exist
    • DELETE /favs/track/:id - delete track from favorites
      • Server should answer with status code 204 if the track was in favorites and now it's deleted id is found and deleted
      • Server should answer with status code 400 and corresponding message if trackId is invalid (not uuid)
      • Server should answer with status code 404 and corresponding message if corresponding track is not favorite
    • POST /favs/album/:id - add album to the favorites
      • Server should answer with status code 201 and corresponding message if album with id === albumId exists
      • Server should answer with status code 400 and corresponding message if albumId is invalid (not uuid)
      • Server should answer with status code 422 and corresponding message if album with id === albumId doesn't exist
    • DELETE /favs/album/:id - delete album from favorites
      • Server should answer with status code 204 if the album was in favorites and now it's deleted id is found and deleted
      • Server should answer with status code 400 and corresponding message if albumId is invalid (not uuid)
      • Server should answer with status code 404 and corresponding message if corresponding album is not favorite
    • POST /favs/artist/:id - add artist to the favorites
      • Server should answer with status code 201 and corresponding message if artist with id === artistId exists
      • Server should answer with status code 400 and corresponding message if artistId is invalid (not uuid)
      • Server should answer with status code 422 and corresponding message if artist with id === artistId doesn't exist
    • DELETE /favs/artist/:id - delete artist from favorites
      • Server should answer with status code 204 if the artist was in favorites and now it's deleted id is found and deleted
      • Server should answer with status code 400 and corresponding message if artistId is invalid (not uuid)
      • Server should answer with status code 404 and corresponding message if corresponding artist is not favorite
  1. For now, these endpoints should operate only with in-memory (hardcoded) data, in the next tasks we will use a DB for it. You should organize your modules with the consideration that the data source will be changed soon.

  2. An application/json format should be used for request and response body.

  3. Do not put everything in one file - use a separate file for application creation (bootstrapping), for controllers (routers) and code related to business logic. Also split files to different modules depends on a domain (user-related, artist-related, etc...).

  4. User's password should be excluded from server response.

  5. When you delete Artist, Album or Track, it's id should be deleted from favorites (if was there) and references to it in other entities should become null. For example: Artist is deleted => this artistId in corresponding Albums's and Track's become null + this artist's id is deleted from favorites, same logic for Album and Track.

  6. Non-existing entity can't be added to Favorites.

  7. To run the service npm start command should be used.

  8. Service should listen on PORT 4000 by default, PORT value is stored in .env file.

  9. Incoming requests should be validated.

  10. You can fix and use OpenAPI file in doc folder.

Hints