diff --git a/staff/marti-herms/project/G-HUB/api/index.js b/staff/marti-herms/project/G-HUB/api/index.js index 9cdaee267..6dcec472b 100644 --- a/staff/marti-herms/project/G-HUB/api/index.js +++ b/staff/marti-herms/project/G-HUB/api/index.js @@ -60,7 +60,7 @@ mongoose.connect(process.env.MONGODB_URI) api.delete('/reviews/:reviewId', jwtVerifier, handle.deleteReview) - api.get('/chat/:targeUserId', jwtVerifier, handle.openChat) + api.get('/chat/:targetUserId', jwtVerifier, handle.openChat) api.post('/chat/:chatId/messages', jwtVerifier, jsonBodyParser, handle.sendMessage) diff --git a/staff/marti-herms/project/G-HUB/app/logic/getChatMessages.js b/staff/marti-herms/project/G-HUB/app/logic/getChatMessages.js new file mode 100644 index 000000000..3ce8a3fc6 --- /dev/null +++ b/staff/marti-herms/project/G-HUB/app/logic/getChatMessages.js @@ -0,0 +1,29 @@ +import { validate, errors } from 'com' + +const { SystemError } = errors + +export default (chatId) => { + validate.string(chatId, 'chatId') + + return fetch(`${import.meta.env.VITE_API_URL}/chat/${chatId}/messages`, { + headers: { Authorization: `Bearer ${sessionStorage.token}` } + }) + .catch(error => { throw new SystemError(error.message) }) + .then(response => { + const { status } = response + + if (status === 200) { + return response.json() + .then(messages => messages) + } + + return response.json() + .then(body => { + const { error, message } = body + + const constructor = errors[error] + + throw new constructor(message) + }) + }) +} \ No newline at end of file diff --git a/staff/marti-herms/project/G-HUB/app/logic/index.js b/staff/marti-herms/project/G-HUB/app/logic/index.js index 9311eb1c4..06a9b5c2e 100644 --- a/staff/marti-herms/project/G-HUB/app/logic/index.js +++ b/staff/marti-herms/project/G-HUB/app/logic/index.js @@ -22,11 +22,15 @@ import getUserAvatar from './getUserAvatar.js' import getUser from './getUser.js' import editUserAvatar from './editUserAvatar.js' import editUserUsername from './editUserUsername.js' +import openChat from './openChat.js' +import sendMessage from './sendMessage.js' +import getChatMessages from './getChatMessages.js' const logic = { deleteReview, editUserAvatar, editUserUsername, + getChatMessages, getDevUserGames, getGameById, getGameReviews, @@ -41,10 +45,12 @@ const logic = { loginUser, logoutUser, makeReview, + openChat, registerGame, registerUser, searchGame, searchUser, + sendMessage, toggleAddGame, toggleFavGame, toggleFollowUser, diff --git a/staff/marti-herms/project/G-HUB/app/logic/openChat.js b/staff/marti-herms/project/G-HUB/app/logic/openChat.js new file mode 100644 index 000000000..d2882e8c7 --- /dev/null +++ b/staff/marti-herms/project/G-HUB/app/logic/openChat.js @@ -0,0 +1,29 @@ +import { validate, errors } from 'com' + +const { SystemError } = errors + +export default (targetUserId) => { + validate.string(targetUserId, 'targetUserId') + + return fetch(`${import.meta.env.VITE_API_URL}/chat/${targetUserId}`, { + headers: { Authorization: `Bearer ${sessionStorage.token}` } + }) + .catch(error => { throw new SystemError(error.message) }) + .then(response => { + const { status } = response + + if (status === 200) { + return response.json() + .then(chatId => chatId) + } + + return response.json() + .then(body => { + const { error, message } = body + + const constructor = errors[error] + + throw new constructor(message) + }) + }) +} \ No newline at end of file diff --git a/staff/marti-herms/project/G-HUB/app/logic/sendMessage.js b/staff/marti-herms/project/G-HUB/app/logic/sendMessage.js new file mode 100644 index 000000000..347b951d0 --- /dev/null +++ b/staff/marti-herms/project/G-HUB/app/logic/sendMessage.js @@ -0,0 +1,31 @@ +import { validate, errors } from 'com' + +const { SystemError } = errors + +export default (chatId, content) => { + validate.string(chatId, 'chatId') + validate.string(content, 'content') + + if (content.trim().length === 0) throw new Error('empty content') + + return fetch(`${import.meta.env.VITE_API_URL}/chat/${chatId}/messages`, { + method: 'POST', + headers: { Authorization: `Bearer ${sessionStorage.token}` }, + body: JSON.stringify({ content }) + }) + .catch(error => { throw new SystemError(error.message) }) + .then(response => { + const { status } = response + + if (status === 200) return + + return response.json() + .then(body => { + const { error, message } = body + + const constructor = errors[error] + + throw new constructor(message) + }) + }) +} \ No newline at end of file diff --git a/staff/marti-herms/project/G-HUB/app/src/home/AddGame.jsx b/staff/marti-herms/project/G-HUB/app/src/home/AddGame.jsx index a4976f2c4..c6833394d 100644 --- a/staff/marti-herms/project/G-HUB/app/src/home/AddGame.jsx +++ b/staff/marti-herms/project/G-HUB/app/src/home/AddGame.jsx @@ -55,7 +55,7 @@ export default function AddGame({ onAddGame }) { {image && } -