Skip to content

Instantly share code, notes, and snippets.

@sandrabosk
Last active February 2, 2021 06:01
Show Gist options
  • Save sandrabosk/2eb5ef5ec7a52e2f4c9842c7d3d9a483 to your computer and use it in GitHub Desktop.
Save sandrabosk/2eb5ef5ec7a52e2f4c9842c7d3d9a483 to your computer and use it in GitHub Desktop.

logo_ironhack_blue 7

Object Oriented Programming - the core

Introduction

For this challenge, you are going to build a mock comments section.

Instructions

We're going to focus on two aspects:

Users

  • Users can be: normal users, moderators, and admins. Normal users can only create new comments, and edit their own comments. Moderators have the added ability to delete comments, while admins have the ability to edit or delete any comment.

  • Users can log in and out, and we track when they last logged in.

Comments

  • Comments are represented by the message, the timestamp, and the author (user who created it).

  • Comments can also be a reply, so we'll store what the parent comment was.

Your Challenge

This is challenge is not about building a fully functional API, but more about focusing on the design from an object-oriented point-of-view.

While working on the assessment, please keep in mind the following OOP concepts:

  • Encapsulation of Properties
  • All classes should have no publicly accessible fields

You should make sure you at least "hide" the required fields, for example, using _name instead of name. Alternatively, feel free to use a better solution as extra credit.

Additional methods are allowed, though remember to keep read-only properties read-only.

Instantiation

Classes should be instantiated with properties (as provided), to create instances with values already assigned.

User/Moderator/Admin defaults:

  • Should be marked as not logged in
  • Should return null for the last logged in at property

Comment defaults:

  • Should set the current timestamp for the created at property upon instantiation
  • Replied To is optional, and should be null if not provided

Inheritance & Access Control

User

  • Users can be logged in and out.
  • When logging in, set the lastLoggedInAt timestamp. Do not modify this timestamp when logging out
  • Users can only edit their own comments
  • Users cannot delete any comments
  • Moderator is a User
  • Moderators can only edit their own comments
  • Moderators can delete any comments
  • Admin is both a User and a Moderator
  • Admins can edit any comments
  • Admins can delete any comments

Composition

  • Comments contain a reference to the User who created it (author)
  • Comments optionally contain a reference to another comment (repliedTo)
  • When converting to a string (toString), the following format is used: «message» by «author.name» With replied to: «message» by «author.name» (replied to «repliedTo.author.name»)
class User {
    constructor(name) {}
    isLoggedIn() {}
    getLastLoggedInAt() {}
    logIn() {}
    logOut() {}
    getName() {}
    setName(name) {}
    canEdit(comment) {}
    canDelete(comment) {}
}

class Moderator {}

class Admin {}

class Comment {
    constructor(author, message, repliedTo) {}
    getMessage() {}
    setMessage(message) {}
    getCreatedAt() {}
    getAuthor() {}
    getRepliedTo() {}
    toString() {}
}

Submission

Please send us the link to the public CodePen or Repl.it file and don't hesitate to add comments to your code to guide us through your thinking process.

We wish you nothing but good luck!

Happy coding! ❤️

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