-
Notifications
You must be signed in to change notification settings - Fork 4
Feature/favorite rooms #422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
7181346
add feature favorite rooms #332
nmate980829 fc9daa4
fix add eslint comment
nmate980829 2fa386e
fix indentation
nmate980829 d2c07e3
make favorite button clickable
nmate980829 ce5c8a1
remove feature floor
nmate980829 a22c825
Merge branch 'master' into feature/favorite-rooms
nmate980829 af90ffc
add requested changes
nmate980829 8b192d0
add feature favorite rooms #332
nmate980829 468bff1
fix add eslint comment
nmate980829 846c055
fix indentation
nmate980829 87131d1
conflict
nmate980829 a5d2f38
remove feature floor
nmate980829 86c4dd8
add requested changes
nmate980829 4e8cc06
Merge branch 'feature/favorite-rooms' of github.com:kir-dev/tanulo-ne…
nmate980829 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as Knex from 'knex' | ||
|
||
|
||
export async function up(knex: Knex): Promise<void> { | ||
return knex.schema | ||
.createTable('favorites', table => { | ||
table.increments('id').primary() | ||
|
||
table.unique(['userId', 'room']) | ||
|
||
table | ||
.integer('userId') | ||
.unsigned() | ||
.references('id') | ||
.inTable('users') | ||
.index() | ||
|
||
table | ||
.integer('room') | ||
.notNullable() | ||
}) | ||
} | ||
|
||
|
||
export async function down(knex: Knex): Promise<void> { | ||
return knex.schema | ||
.dropTableIfExists('favorites') | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import * as Knex from 'knex' | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
return knex.schema.table('users', table => { | ||
table.dropColumn('floor') | ||
}) | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
return knex.schema.table('users', table => { | ||
table.integer('floor') | ||
.unsigned() | ||
.nullable() | ||
}) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
function deleteFavorite(room) { | ||
fetch(`/favorites/${room}`, { | ||
method: 'DELETE' | ||
}) | ||
.then(async res => { | ||
switch (res.status) { | ||
case 201: | ||
location.reload() | ||
break | ||
case 401: | ||
displayMessage('danger', UNAUTHORIZED_MESSAGE) | ||
break | ||
case 403: | ||
displayMessage('danger', FORBIDDEN_MESSAGE) | ||
break | ||
case 404: | ||
data = await res.json() | ||
displayMessage('danger', data.message) | ||
break | ||
} | ||
}) | ||
.catch(err => displayMessage('danger', err)) | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
function addFavorite(room) { | ||
if (!room) { | ||
displayMessage('danger', err) | ||
} else { | ||
fetch('/favorites', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ | ||
room | ||
}), | ||
}) | ||
.then(async (res) => { | ||
switch (res.status) { | ||
case 201: | ||
location.reload() | ||
break | ||
case 401: | ||
displayMessage('danger', UNAUTHORIZED_MESSAGE) | ||
break | ||
case 403: | ||
displayMessage('danger', FORBIDDEN_MESSAGE) | ||
break | ||
case 404: | ||
data = await res.json() | ||
displayMessage('danger', data.message) | ||
break | ||
} | ||
}) | ||
.catch((err) => displayMessage('danger', err)) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +0,0 @@ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
function updateUser(id) { | ||
const floorEl = document.getElementById('floor-input') | ||
const floor = parseInt(floorEl.value) | ||
if (floorEl.value !== '' && (floor < 3 || floor > 18)) { | ||
displayMessage('danger', 'A szint üres vagy 3 és 18 közötti szám lehet') | ||
} else { | ||
fetch(`/users/${id}`, { | ||
method: 'PATCH', | ||
headers: { | ||
'Content-Type': 'application/json' | ||
}, | ||
body: JSON.stringify({ floor }), | ||
}) | ||
.then(async res => { | ||
switch (res.status) { | ||
case 200: | ||
const user = await res.json() | ||
floorEl.value = user.floor | ||
displayMessage('success', 'Sikeres mentés!') | ||
break | ||
case 400: | ||
const data = await res.json() | ||
clearMessages() | ||
data.errors.forEach(err => displayMessage('danger', err.msg)) | ||
break | ||
case 401: | ||
displayMessage('danger', UNAUTHORIZED_MESSAGE) | ||
break | ||
default: | ||
displayMessage('danger', 'Nem várt hiba történt') | ||
break | ||
} | ||
}) | ||
.catch((err) => console.error(err)) | ||
} | ||
} | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Router } from 'express' | ||
import { check } from 'express-validator' | ||
|
||
import { isAuthenticated } from '../../config/passport' | ||
import { handleValidationError } from '../../util/validators' | ||
import { addFavorite, removeFavorite } from './favorite.service' | ||
|
||
const router = Router() | ||
|
||
router.post('/', | ||
isAuthenticated, | ||
check('room') | ||
.notEmpty() | ||
.isInt({ gt: 2, lt: 19 }) | ||
.withMessage('A szint csak üres vagy 3 és 18 közötti szám lehet'), | ||
handleValidationError(400), | ||
addFavorite, | ||
(req, res) => res.sendStatus(201) | ||
) | ||
|
||
router.delete('/:id', | ||
isAuthenticated, | ||
removeFavorite, | ||
(req, res) => res.sendStatus(201) | ||
) | ||
|
||
export default router |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Request, Response, NextFunction } from 'express' | ||
import { asyncWrapper } from '../../util/asyncWrapper' | ||
import { User } from '../users/user' | ||
|
||
export const addFavorite = asyncWrapper( | ||
async (req: Request, res: Response, next: NextFunction) => { | ||
const favorites = await User.relatedQuery('favorites').for( | ||
(req.user as User).id | ||
) | ||
if ( | ||
favorites && | ||
!favorites.some((element) => element.room == parseInt(req.params.id)) | ||
) { | ||
await User.relatedQuery('favorites') | ||
.for((req.user as User).id) | ||
.insert({ | ||
room: parseInt(req.body.room), | ||
}) | ||
next() | ||
} else { | ||
res.sendStatus(404) | ||
} | ||
} | ||
) | ||
|
||
export const removeFavorite = asyncWrapper( | ||
async (req: Request, res: Response, next: NextFunction) => { | ||
const favorites = await User.relatedQuery('favorites').for( | ||
(req.user as User).id | ||
) | ||
if ( | ||
req.params.id && | ||
favorites && | ||
favorites.some((element) => element.room == parseInt(req.params.id)) | ||
) { | ||
await User.relatedQuery('favorites') | ||
.for((req.user as User).id) | ||
.delete() | ||
.where({ room: parseInt(req.params.id) }) | ||
next() | ||
} else { | ||
res.sendStatus(404) | ||
} | ||
} | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Model } from 'objection' | ||
|
||
import { User } from '../users/user' | ||
|
||
export class Favorite extends Model { | ||
id!: number | ||
room: number | ||
user: User | ||
static get tableName() { | ||
return 'favorites' | ||
} | ||
|
||
static get relationMappings() { | ||
return { | ||
user: { | ||
relation: Model.BelongsToOneRelation, | ||
modelClass: User, | ||
|
||
join: { | ||
from: 'favorites.userId', | ||
to: 'users.id' | ||
} | ||
} | ||
} | ||
} | ||
|
||
static get jsonSchema() { | ||
return { | ||
type: 'object', | ||
required: ['room'], | ||
|
||
properties: { | ||
id: { type: 'integer' }, | ||
room: { type: 'integer' }, | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.