linkShortener: Count clicks

This commit is contained in:
divlo 2020-08-04 12:12:27 +02:00
parent 9f068614f5
commit 55c467f1e4
4 changed files with 23 additions and 3 deletions

View File

@ -9,5 +9,10 @@ module.exports = sequelize.define('short_link', {
shortcut: { shortcut: {
type: Sequelize.TEXT, type: Sequelize.TEXT,
allowNull: false allowNull: false
},
count: {
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 0
} }
}) })

View File

@ -51,7 +51,18 @@ app.get('/:shortcut', (req, res, next) => {
return res.redirect('/error/404') return res.redirect('/error/404')
} }
return res.redirect(result.url) const count = (result.count += 1)
database.query(
'UPDATE short_links SET count = ? WHERE id = ?',
[count, result.id],
error => {
if (error != null) {
return next(error)
}
return res.redirect(result.url)
}
)
} }
) )
}) })

View File

@ -277,6 +277,9 @@ const LinksList = ({
<th className='table-row' scope='col'> <th className='table-row' scope='col'>
Nom Nom
</th> </th>
<th className='table-row' scope='col'>
Compteur de clics
</th>
<th className='table-row' scope='col'> <th className='table-row' scope='col'>
Modifier Modifier
</th> </th>
@ -297,6 +300,7 @@ const LinksList = ({
{link.shortcut} {link.shortcut}
</a> </a>
</td> </td>
<td className='table-row'>{link.count}</td>
<td <td
style={{ cursor: 'pointer' }} style={{ cursor: 'pointer' }}
onClick={() => handleEditLink(link)} onClick={() => handleEditLink(link)}

View File

@ -41,8 +41,8 @@ const Profile = props => {
event.target.files != null event.target.files != null
? event.target.files[0] ? event.target.files[0]
: isTypeCheck : isTypeCheck
? event.target.checked ? event.target.checked
: event.target.value : event.target.value
setInputState(inputStateNew) setInputState(inputStateNew)
} }