You may find this useful as doing it in the backend will be way more scaleable, elegant, and maintainable.