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

Import/Export functions #54

Merged
merged 14 commits into from
Oct 17, 2021
31 changes: 31 additions & 0 deletions app/Controllers/ExportController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { getAllGroupVocabulary, getAllLanguagePackageVocabulary } = require('../Services/ExportServiceProvider.js');
const catchAsync = require('../utils/catchAsync');

const exportGroup = catchAsync(async (req, res) => {
// get userId from request
const userId = req.user.id;

// get language package id from params
const { groupId } = req.params;

const group = await getAllGroupVocabulary(userId, groupId);

res.send(group);
});

const exportLanguagePackage = catchAsync(async (req, res) => {
// get userId from request
const userId = req.user.id;

// get language package id from params
const { languagePackageId } = req.params;

const languagePackage = await getAllLanguagePackageVocabulary(userId, languagePackageId);

res.send(languagePackage);
});

module.exports = {
exportGroup,
exportLanguagePackage,
};
32 changes: 32 additions & 0 deletions app/Controllers/ImportController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const { storeGroupVocabulary, storeLanguagePackageVocabulary } = require('../Services/ImportServiceProvider.js');
const catchAsync = require('../utils/catchAsync');

const importGroup = catchAsync(async(req, res) => {
// get userId from request
const userId = req.user.id;

const { active, activate, languagePackageId } = req.query;

await storeGroupVocabulary(req.body, userId, languagePackageId, active, activate);

res.send('stored');
luwol03 marked this conversation as resolved.
Show resolved Hide resolved
});

const importLanguagePackage = catchAsync(async(req, res) => {
// get userId from request
const userId = req.user.id;

// get group with vocabs from request body
const { active, activate } = req.query;

await storeLanguagePackageVocabulary(req.body, userId, active, activate);

// await storeLanguagePackageVocabulary(group);
noctera marked this conversation as resolved.
Show resolved Hide resolved

res.send('stored');
});

module.exports = {
importGroup,
importLanguagePackage,
};
12 changes: 10 additions & 2 deletions app/Controllers/VocabularyController.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,21 @@ const addVocabularyCard = catchAsync(async (req, res) => {
// get userId from request
const userId = req.user.id;
const { name, description, active, translations } = req.body;
const { languagePackageId } = req.params;
const { languagePackageId, groupId } = req.params;

// check if user wants to train vocabulary card directly
const activate = req.query.activate === 'true';

// create vocabulary card
const vocabularyCard = await createVocabularyCard(req.params, name, description, userId, active, activate);
const vocabularyCard = await createVocabularyCard(
languagePackageId,
groupId,
name,
description,
userId,
active,
activate
);
noctera marked this conversation as resolved.
Show resolved Hide resolved

// parse vocabulary card id from response and create translations
await createTranslations(translations, userId, languagePackageId, vocabularyCard.id);
Expand Down
62 changes: 62 additions & 0 deletions app/Services/ExportServiceProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const { VocabularyCard, Translation, Group, LanguagePackage } = require('../../database');

// get every vocabulary of a group
async function getAllGroupVocabulary(userId, groupId) {
const group = await Group.findOne({
attributes: ['name', 'description'],
include: [
{
model: VocabularyCard,
attributes: ['name', 'description'],
include: [
{
model: Translation,
attributes: ['name'],
},
],
},
],
where: {
id: groupId,
userId,
},
});
return group;
}

async function getAllLanguagePackageVocabulary(userId, languagePackageId) {
// Get user with email from database
const languagePackage = await LanguagePackage.findOne({
// if groups is true, return groups to every language package
include: [
{
model: Group,
attributes: ['name', 'description'],
include: [
{
model: VocabularyCard,
attributes: ['name', 'description'],
include: [
{
model: Translation,
attributes: ['name'],
},
],
},
],
},
],
attributes: ['name', 'foreignWordLanguage', 'translatedWordLanguage', 'vocabsPerDay', 'rightWords'],
where: {
userId,
id: languagePackageId,
},
});

return languagePackage;
}

module.exports = {
getAllGroupVocabulary,
getAllLanguagePackageVocabulary,
};
103 changes: 103 additions & 0 deletions app/Services/ImportServiceProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const { Group } = require('../../database');
const { ForeignKeyConstraintError } = require('sequelize');
const ApiError = require('../utils/ApiError.js');
const httpStatus = require('http-status');
const { createDrawers } = require('./DrawerServiceProvider.js');
const { createLanguagePackage } = require('./LanguagePackageServiceProvider.js');
const { drawers } = require('../utils/constants.js');
const { createVocabularyCard, createTranslations } = require('./VocabularyServiceProvider.js');

async function storeGroupVocabulary(
{ name, description, VocabularyCards },
userId,
languagePackageId,
active,
activate
) {
try {
const group = await Group.create({
userId,
languagePackageId,
name,
description,
active,
});

VocabularyCards.forEach(async (vocabularyCard) => {
const createdCard = await createVocabularyCard(
languagePackageId,
group.id,
vocabularyCard.name,
vocabularyCard.description,
userId,
active,
activate
);
// parse vocabulary card id from response and create translations
createTranslations(vocabularyCard.Translations, userId, languagePackageId, createdCard.id);
});
noctera marked this conversation as resolved.
Show resolved Hide resolved
} catch (error) {
if (error instanceof ForeignKeyConstraintError) {
throw new ApiError(httpStatus.BAD_REQUEST, 'Error importing vocabs');
}

throw error;
}
}

async function storeLanguagePackageVocabulary(
{ name, foreignWordLanguage, translatedWordLanguage, vocabsPerDay, rightWords, Groups },
userId,
active,
activate
) {
// ------------------------------------------//
// TO DO: ADD THESE TWO FUNCTIONS TOGETHER
// ------------------------------------------//

try {
// create language Package
const createdLanguagePackage = await createLanguagePackage(
{ name, foreignWordLanguage, translatedWordLanguage, vocabsPerDay, rightWords },
userId
);

// store drawers for language package in database
await createDrawers(drawers, createdLanguagePackage.id, userId);

Groups.forEach(async (group, index) => {
noctera marked this conversation as resolved.
Show resolved Hide resolved
const createdGroup = await Group.create({
userId,
languagePackageId: createdLanguagePackage.id,
name: group.name,
description: group.description,
active,
});

Groups[index].VocabularyCards.forEach(async (vocabularyCard) => {
const createdCard = await createVocabularyCard(
createdLanguagePackage.id,
createdGroup.id,
vocabularyCard.name,
vocabularyCard.description,
userId,
active,
activate
);
// parse vocabulary card id from response and create translations
createTranslations(vocabularyCard.Translations, userId, createdLanguagePackage.id, createdCard.id);
});
});
} catch (error) {
if (error instanceof ForeignKeyConstraintError) {
throw new ApiError(httpStatus.BAD_REQUEST, 'Error importing vocabs');
}

throw error;
}
}

module.exports = {
storeGroupVocabulary,
storeLanguagePackageVocabulary,
};
2 changes: 1 addition & 1 deletion app/Services/VocabularyServiceProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const ApiError = require('../utils/ApiError.js');
const httpStatus = require('http-status');

// create language package
async function createVocabularyCard({ languagePackageId, groupId }, name, description, userId, active, activate) {
async function createVocabularyCard(languagePackageId, groupId, name, description, userId, active, activate) {
// if activate = false store vocabulary card in drawer 0 directly

// select drawer id depending on the activate state
Expand Down
8 changes: 8 additions & 0 deletions routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const LanguageController = require('../app/Controllers/LanguageController.js');
const DocsController = require('../app/Controllers/DocsController.js');
const StatsController = require('../app/Controllers/StatsController.js');
const InfoController = require('../app/Controllers/InfoController.js');
const ImportController = require('../app/Controllers/ImportController.js');
const ExportController = require('../app/Controllers/ExportController.js');

const router = express.Router();

Expand Down Expand Up @@ -62,6 +64,12 @@ router.patch('/vocabulary/:vocabularyId', ProtectMiddleware, QueryController.che
// Language
router.get('/language', ProtectMiddleware, LanguageController.sendLanguages);

// Import / Export
router.get('/export/group/:groupId', ProtectMiddleware, ExportController.exportGroup);
noctera marked this conversation as resolved.
Show resolved Hide resolved
router.get('/export/languagePackage/:languagePackageId', ProtectMiddleware, ExportController.exportLanguagePackage);
router.post('/import/group/', ProtectMiddleware, ImportController.importGroup);
router.post('/import/languagePackage/', ProtectMiddleware, ImportController.importLanguagePackage);

// Docs
router.get('/swagger.json', DocsController.document);
router.use('/swagger', DocsController.swagger);
Expand Down