-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #61 from webdonalds/feature/like
Implement like api
- Loading branch information
Showing
8 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,71 @@ | ||
## POST - /v1/like/ | ||
|
||
set like | ||
|
||
#### Header | ||
|
||
| KEY | VALUE | REQUIRED | | ||
| -------------- | ---------------- | -------- | | ||
| Content-type | application/json | O | | ||
| x-access-token | ACCESS_TOKEN | O | | ||
|
||
|
||
#### Body Parameters | ||
|
||
| KEY | VALUE | REQUIRED | | ||
| ----------- | ---------------- | -------- | | ||
| videoId | VIDEO_ID | O | | ||
|
||
|
||
#### URL Parameters | ||
|
||
X | ||
|
||
|
||
#### Success Response | ||
|
||
Code: 200 (success) | ||
|
||
Content: | ||
~~~ | ||
{ | ||
"message": "success" | ||
} | ||
~~~ | ||
|
||
|
||
|
||
## DELETE - /v1/like | ||
|
||
unset like | ||
|
||
|
||
#### Header | ||
|
||
| KEY | VALUE | REQUIRED | | ||
| -------------- | ---------------- | -------- | | ||
| Content-type | application/json | O | | ||
| x-access-token | ACCESS_TOKEN | O | | ||
|
||
|
||
#### Body Parameters | ||
|
||
| KEY | VALUE | REQUIRED | | ||
| ------- | -------- | -------- | | ||
| videoId | VIDEO_ID | O | | ||
|
||
|
||
#### URL Parameters | ||
|
||
X | ||
|
||
|
||
#### Success Response | ||
|
||
Code: 200 (success) | ||
Content: | ||
~~~ | ||
{ | ||
"message": "success" | ||
} | ||
~~~ |
This file contains 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 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 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,91 @@ | ||
import * as express from 'express'; | ||
import { Like, Video } from '../../model/index'; | ||
import { errorSend } from '../../error/errorUtil'; | ||
import { Sequelize } from 'sequelize'; | ||
|
||
const router = express.Router(); | ||
|
||
|
||
// post like (set like) | ||
router.post('/', async (request: express.Request, response: express.Response) => { | ||
const userId = request.body.userInfo.userId; | ||
const videoId = request.body.videoId; | ||
|
||
try { | ||
await Like.create({ | ||
userId: userId, | ||
videoId: videoId | ||
}); | ||
} | ||
catch (e) { | ||
errorSend(response, 'videoId is invalid', null); | ||
return; | ||
} | ||
|
||
try { | ||
Video.update({ | ||
totalLikes: Sequelize.literal('totalLikes + 1') | ||
}, { | ||
where: { | ||
id: videoId | ||
} | ||
}); | ||
response.json({ | ||
message: 'success' | ||
}); | ||
} catch (e) { | ||
await Like.destroy({ | ||
where: { | ||
userId: userId, | ||
videoId: videoId | ||
} | ||
}); | ||
errorSend(response, 'fail to update video totalLikes', null); | ||
return; | ||
} | ||
}); | ||
|
||
|
||
// delete like (unset like) | ||
router.delete('/', async (request: express.Request, response: express.Response) => { | ||
const userId = request.body.userInfo.userId; | ||
const videoId = request.body.videoId; | ||
|
||
try { | ||
const deleteCount = await Like.destroy({ | ||
where: { | ||
userId: userId, | ||
videoId: videoId | ||
} | ||
}); | ||
if(deleteCount == 0) { | ||
throw ''; | ||
} | ||
} catch (e) { | ||
errorSend(response, 'videoId is invalid', null); | ||
return; | ||
} | ||
|
||
try { | ||
await Video.update({ | ||
totalLikes: Sequelize.literal('totalLikes - 1') | ||
}, { | ||
where: { | ||
id: videoId | ||
} | ||
}); | ||
response.json({ | ||
message: 'success' | ||
}); | ||
} catch (e) { | ||
await Like.create({ | ||
userId: userId, | ||
videoId: videoId | ||
}); | ||
errorSend(response, 'fail to update video totalLikes', null); | ||
return; | ||
} | ||
}); | ||
|
||
|
||
export default router; |
This file contains 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 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 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 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