Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,10 @@ module.exports = {
'react', 'react-hooks', 'jest'
],
rules: {
},
settings: {
react: {
version: '17.0.2'
}
}
}
19 changes: 16 additions & 3 deletions 01-basic-cs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,20 @@ const assert = require('assert')

const database = require('./database.json')

const total = 0 // TODO
// Combine all users's hats
const allHats = _.flatMap(database, 'hats')

// Obtain the frequency of each hat by its id
const hatsFrequency = _.countBy(allHats, 'id')

// Obtain a ordered array with the frequency of each hat
const orderedHatsByFrequency = _.orderBy(_.toPairs(hatsFrequency), (pair) => pair[1], 'desc')

// Get the first 3 elements
const highers = _.take(orderedHatsByFrequency, 3)

// Sume it
const total = _.sumBy(highers, (el) => el[1])

// Throws error on failure
assert.equal(total, 23, `Invalid result: ${total} != 23`)
Expand All @@ -14,6 +27,6 @@ console.log('Success!')

/**
* Time and space complexity in O() notation is:
* - time complexity: TODO
* - space complexity: TODO
* - time complexity: O(m log m) - m represents the array with the frequency, which is ordered
* - space complexity: O(n)
*/
8 changes: 8 additions & 0 deletions 02-nodejs/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
mediastream:
image: mongo:5.0.0
container_name: mediastream-challenge
ports:
- 27017:27017
volumes:
- ./mongo:/data/db
28 changes: 28 additions & 0 deletions 02-nodejs/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
'use strict'

const express = require('express')
const csv = require('fast-csv')
const { Transform } = require('stream')

const User = require('./models/User')

// Setup Express.js app
const app = express()

// TODO: everything else
app.get('/users', async (req, res) => {
try {
// Get all users from DB and select only the id, name and email
const dbUsers = await User.find({}).lean().select('-__v')

// Setup the headers to download
res.setHeader('Content-Type', 'text/csv')
res.setHeader('Content-Disposition', 'attachment filename=users.csv')

// Create a transform stream and in the _transform function add each register to the transform stream
const transformStream = new Transform({ objectMode: true })
transformStream._transform = function (user, _, done) {
this.push(user)
done()
}

// Finally use csv.write to generate the cvs file from our dbUsers
// and through the pipe connect it to the transform stream
csv.write(dbUsers, { headers: true })
.pipe(transformStream)
.pipe(res)
} catch (error) {
console.log(error)
res.status(500).send('Error while download the CSV file')
}
})

app.listen(3000)
34 changes: 28 additions & 6 deletions 03-react/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions 03-react/src/App.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React from 'react'
import { BrowserRouter, Route, Switch } from 'react-router-dom'
import Exercise from './components/pages/Exercise'
import { MovieShop } from './pages/movies-shop/MovieShop'

function App () {
return (
<BrowserRouter>
<Switch>
<Route path="/" component={Exercise} exact />
<Route path="/" component={MovieShop} exact />
</Switch>
</BrowserRouter>
)
Expand Down
40 changes: 40 additions & 0 deletions 03-react/src/components/MovieCard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react'
import PropTypes from 'prop-types'

export const MovieCard = ({ item, isInCart, addItem, removeItem }) => {
return (
<li key={item.id} className='movies__list-card'>
<ul>
<li>ID: {item.id}</li>
<li>Name: {item.name}</li>
<li>Price: ${item.price}</li>
</ul>
{
isInCart
? (
<div className="movies__cart-card-quantity">
<button onClick={() => removeItem(item)}>
-
</button>
<span>
{item.quantity}
</span>
<button onClick={() => addItem(item)}>
+
</button>
</div>
)
: (
<button onClick={() => addItem(item)}>Add to cart</button>
)
}
</li>
)
}

MovieCard.propTypes = {
item: PropTypes.object.isRequired,
isInCart: PropTypes.bool,
addItem: PropTypes.func.isRequired,
removeItem: PropTypes.func
}
113 changes: 0 additions & 113 deletions 03-react/src/components/pages/Exercise/index.js

This file was deleted.

37 changes: 37 additions & 0 deletions 03-react/src/constants/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
export const movies = [
{
id: 1,
name: 'Star Wars',
price: 20
},
{
id: 2,
name: 'Minions',
price: 25
},
{
id: 3,
name: 'Fast and Furious',
price: 10
},
{
id: 4,
name: 'The Lord of the Rings',
price: 5
}
]

export const discountRules = [
{
m: [3, 2],
discount: 0.25
},
{
m: [2, 4, 1],
discount: 0.5
},
{
m: [4, 2],
discount: 0.1
}
]
Loading