Skip to content
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

Implement like api #61

Merged
merged 3 commits into from
Mar 19, 2021
Merged
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
71 changes: 71 additions & 0 deletions docs/yas_api_like_doc.md
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"
}
~~~
2 changes: 2 additions & 0 deletions src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as express from 'express';
import auth from './v1/auth';
import user from './v1/user';
import contents from './v1/contents';
import like from './v1/like';
import post from './v1/post';
import logoffedPostList from './v1/logoffedPostList';

Expand All @@ -13,6 +14,7 @@ const router = express.Router();
router.use('/v1/auth', auth);
router.use('/v1/user', middleware.validateToken, user);
router.use('/v1/contents', middleware.validateToken, middleware.getGoogleAccessToken, contents);
router.use('/v1/like', middleware.validateToken, like);
// handle middleware in post
router.use('/v1/post', post);
router.use('/v1/logoffed-post-list', logoffedPostList);
Expand Down
3 changes: 3 additions & 0 deletions src/api/v1/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ router.get('/login', async (request: express.Request, response: express.Response
}
} catch(e) {
errorSend(response, 'internal_server_error', e);
return;
}

const data = {
Expand Down Expand Up @@ -102,6 +103,7 @@ router.get('/login', async (request: express.Request, response: express.Response
});
} catch(e) {
errorSend(response, 'fail_create_token', null);
return;
}

response.json({
Expand Down Expand Up @@ -143,6 +145,7 @@ router.get('/access-token', async (request: express.Request, response: express.R
});
} catch(e) {
errorSend(response, 'cannot_find_token', null);
return;
}

// no token found from database
Expand Down
91 changes: 91 additions & 0 deletions src/api/v1/like.ts
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;
1 change: 1 addition & 0 deletions src/api/v1/logoffedPostList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ router.get('/recent-videos', async (request: express.Request, response: express.
}
} catch(e) {
errorSend(response, 'fail_get_video', null);
return;
}

const recentVideoList = [];
Expand Down
4 changes: 4 additions & 0 deletions src/api/v1/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ router.get('/user-videos/:userId', async (request: express.Request, response: ex
}
} catch(e) {
errorSend(response, 'not_found', null);
return;
}

const userVideos = [];
Expand Down Expand Up @@ -143,6 +144,7 @@ router.post('/video', middleware.validateToken, async (request: express.Request,
});
} catch(e) {
errorSend(response, 'internal_server_error', e);
return;
}
return;
});
Expand Down Expand Up @@ -192,6 +194,7 @@ router.put('/video', middleware.validateToken, async (request: express.Request,
tagService.addVideoHasTag(videoPostId, tagIds);
} catch(e) {
errorSend(response, 'internal_server_error', null);
return;
}

response.json({
Expand Down Expand Up @@ -230,6 +233,7 @@ router.delete('/video', middleware.validateToken, async (request: express.Reques
// video_has_tag will be deleted automatically by Database setting.
} catch(e) {
errorSend(response, 'internal_server_error', null);
return;
}

response.json({
Expand Down
2 changes: 2 additions & 0 deletions src/api/v1/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ router.put('/user-info', async (request: express.Request, response: express.Resp
);
} catch(e) {
errorSend(response, 'fail_update_user', null);
return;
}

response.json({
Expand Down Expand Up @@ -75,6 +76,7 @@ router.put('/profile-image', async (request: express.Request, response: express.
);
} catch(e) {
errorSend(response, 'fail_update_user', null);
return;
}

response.json({
Expand Down
2 changes: 2 additions & 0 deletions src/model/Like.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ class Like extends Model {
{
userId: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false
},
videoId: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false
}
},
Expand Down