avltree / task-menu

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Menu manager

Table of Contents

Task Description

Fork or Download this repository and implement the logic to manage a menu.

A Menu has a depth of N and maximum number of items per layer M. Consider N and M to be dynamic for bonus points.

It should be possible to manage the menu by sending API requests. Do not implement a frontend for this task.

Feel free to add comments or considerations when submitting the response at the end of the README.

Example menu

  • Home
    • Home sub1
      • Home sub sub
        • [N]
    • Home sub2
    • [M]
  • About
  • [M]

Routes

POST /menus

Create a menu.

Input

{
    "field": "value"
}
Bonus
{
    "field": "value",
    "max_depth": 5,
    "max_children": 5
}

Output

{
    "field": "value"
}
Bonus
{
    "field": "value",
    "max_depth": 5,
    "max_children": 5
}

GET /menus/{menu}

Get the menu.

Output

{
    "field": "value"
}
Bonus
{
    "field": "value",
    "max_depth": 5,
    "max_children": 5
}

PUT|PATCH /menus/{menu}

Update the menu.

Input

{
    "field": "value"
}
Bonus
{
    "field": "value",
    "max_depth": 5,
    "max_children": 5
}

Output

{
    "field": "value"
}
Bonus
{
    "field": "value",
    "max_depth": 5,
    "max_children": 5
}

DELETE /menus/{menu}

Delete the menu.

POST /menus/{menu}/items

Create menu items.

Input

[
    {
        "field": "value"
    },
    {
        "field": "value"
    }
]
Bonus
[
    {
        "field": "value",
        "children": [
            {
                "field": "value",
                "children": []
            },
            {
                "field": "value"
            }
        ]
    },
    {
        "field": "value"
    }
]

Output

[
    {
        "field": "value"
    },
    {
        "field": "value"
    }
]
Bonus
[
    {
        "field": "value",
        "children": [
            {
                "field": "value",
                "children": []
            },
            {
                "field": "value"
            }
        ]
    },
    {
        "field": "value"
    }
]

GET /menus/{menu}/items

Get all menu items.

Output

[
    {
        "field": "value"
    },
    {
        "field": "value"
    }
]
Bonus
[
    {
        "field": "value",
        "children": [
            {
                "field": "value",
                "children": []
            },
            {
                "field": "value"
            }
        ]
    },
    {
        "field": "value"
    }
]

DELETE /menus/{menu}/items

Remove all menu items.

POST /items

Create an item.

Input

{
    "field": "value"
}

Output

{
    "field": "value"
}

GET /items/{item}

Get the item.

Output

{
    "field": "value"
}

PUT|PATCH /items/{item}

Update the item.

Input

{
    "field": "value"
}

Output

{
    "field": "value"
}

DELETE /items/{item}

Delete the item.

POST /items/{item}/children

Create item's children.

Input

[
    {
        "field": "value"
    },
    {
        "field": "value"
    }
]
Bonus
[
    {
        "field": "value",
        "children": [
            {
                "field": "value",
                "children": []
            },
            {
                "field": "value"
            }
        ]
    },
    {
        "field": "value"
    }
]

Output

[
    {
        "field": "value"
    },
    {
        "field": "value"
    }
]
Bonus
[
    {
        "field": "value",
        "children": [
            {
                "field": "value",
                "children": []
            },
            {
                "field": "value"
            }
        ]
    },
    {
        "field": "value"
    }
]

GET /items/{item}/children

Get all item's children.

Output

[
    {
        "field": "value"
    },
    {
        "field": "value"
    }
]
Bonus
[
    {
        "field": "value",
        "children": [
            {
                "field": "value",
                "children": []
            },
            {
                "field": "value"
            }
        ]
    },
    {
        "field": "value"
    }
]

DELETE /items/{item}/children

Remove all children.

GET /menus/{menu}/layers/{layer}

Get all menu items in a layer.

Output

[
    {
        "field": "value"
    },
    {
        "field": "value"
    }
]

DELETE /menus/{menu}/layers/{layer}

Remove a layer and relink layer + 1 with layer - 1, to avoid dangling data.

GET /menus/{menu}/depth

Output

Get depth of menu.

{
    "depth": 5
}

Bonus points

  • 10 vs 1.000.000 menu items - what would you do differently?
  • Write documentation
  • Use PhpCS | PhpCsFixer | PhpStan
  • Use cache
  • Use data structures
  • Use docker

Comments and considerations

The task has taken more time than I expected, mostly because I'm not used to Laravel's and Eloquent's quirks and conventions and I sometimes had to work around them. The code probably needs some slight refactoring to make it perfect, because is not ideal as of now.

Known issues

They exist mostly because of the time constraints. For a proper project I would have ironed them all out, of course.

  • The tests use production database
  • There are some missing feature tests, I have decided to give them up for time constraints
  • There are no unit tests for the service classes
  • There are some quirks in the code that probably need refactoring:
    • The MenuRegistry service class is basically a mediator pattern implementation, and it violates the single responsibility principle. Can be reworked into dedicated classes.
    • Some database operations could use transactions to be atomic
    • Complex store request validation should be refactored into custom rule objects
    • The container structure should use a dedicated server container like nginx, not the php development server
    • Php 7.4 can be used instead of 7.3
    • Instead of using the simple database tree implementation, a dedicated nested set can be used for future development
    • Cache isn't used for database requests, I ran out of time.
  • Ultra simple Makefile
  • No CI, phpcs and such must be run manually

Running the app / tests

After cloning run:

make init

To start the HTTP server:

make up

To run the tests

make test

About


Languages

Language:PHP 99.5%Language:Makefile 0.3%Language:Dockerfile 0.2%