8.3 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d824a367417b2b2512c43 | Biblioteca Personale | 4 | 301571 | personal-library |
--description--
Costruisci un'app JavaScript full-stack che sia funzionalmente simile a questa: https://personal-library.freecodecamp.rocks/. Lavorare su questo progetto ti porterà a scrivere il tuo codice utilizzando uno dei seguenti metodi:
- Clonare questo repository GitHub e completare il tuo progetto localmente.
- Usare la nostra bozza di progetto su Replit per completare il tuo progetto.
- Usare un costruttore di siti a tua scelta per completare il progetto. Assicurati di incorporare tutti i file del nostro repository GitHub.
Quando hai finito, assicurati che una demo funzionante del tuo progetto sia ospitata in qualche percorso pubblico. Quindi invia l'URL nel campo Solution Link
. Facoltativamente, invia anche un link al codice sorgente del tuo progetto nel campo GitHub Link
.
--instructions--
- Aggiungi la stringa di connessione a MongoDB a
.env
senza virgolette comeDB
Esempio:DB=mongodb://admin:pass@1234.mlab.com:1234/fccpersonallib
- Nel tuo file
.env
impostaNODE_ENV
atest
, senza virgolette - È necessario creare tutte le rotte dentro
routes/api.js
- Creerai tutti i test funzionali in
tests/2_functional-tests.js
--hints--
È necessario fornire il proprio progetto, non l'URL di esempio.
(getUserInput) => {
assert(
!/.*\/personal-library\.freecodecamp\.rocks/.test(getUserInput('url'))
);
};
Puoi inviare una richiesta POST a /api/books
con title
come parte dei dati del modulo per aggiungere un libro. La risposta restituita sarà un oggetto con un title
e un _id
unico come chiavi. Se title
non è incluso nella richiesta, la risposta restituita dovrebbe essere la stringa missing required field title
.
async (getUserInput) => {
try {
let data1 = await $.post(getUserInput('url') + '/api/books', {
title: 'Faux Book 1'
});
assert.isObject(data1);
assert.property(data1, 'title');
assert.equal(data1.title, 'Faux Book 1');
assert.property(data1, '_id');
let data2 = await $.post(getUserInput('url') + '/api/books');
assert.isString(data2);
assert.equal(data2, 'missing required field title');
} catch (err) {
throw new Error(err.responseText || err.message);
}
};
Puoi inviare una richiesta GET a /api/books
e ricevere una risposta JSON che rappresenta tutti i libri. La risposta JSON sarà un array di oggetti con ogni oggetto (libro) contenente le proprietà title
, _id
e commentcount
.
async (getUserInput) => {
try {
let url = getUserInput('url') + '/api/books';
let a = $.post(url, { title: 'Faux Book A' });
let b = $.post(url, { title: 'Faux Book B' });
let c = $.post(url, { title: 'Faux Book C' });
await Promise.all([a, b, c]).then(async () => {
let data = await $.get(url);
assert.isArray(data);
assert.isAtLeast(data.length, 3);
data.forEach((book) => {
assert.isObject(book);
assert.property(book, 'title');
assert.isString(book.title);
assert.property(book, '_id');
assert.property(book, 'commentcount');
assert.isNumber(book.commentcount);
});
});
} catch (err) {
throw new Error(err.responseText || err.message);
}
};
Puoi inviare una richiesta GET a /api/books/{_id}
per recuperare un singolo oggetto di un libro contenente le proprietà title
, _id
e un array di commenti comments
(array vuoto se nessun commento è presente). Se non viene trovato nessun libro, restituisci la stringa no book exists
.
async (getUserInput) => {
try {
let url = getUserInput('url') + '/api/books';
let noBook = await $.get(url + '/5f665eb46e296f6b9b6a504d');
assert.isString(noBook);
assert.equal(noBook, 'no book exists');
let sampleBook = await $.post(url, { title: 'Faux Book Alpha' });
assert.isObject(sampleBook);
let bookId = sampleBook._id;
let bookQuery = await $.get(url + '/' + bookId);
assert.isObject(bookQuery);
assert.property(bookQuery, 'title');
assert.equal(bookQuery.title, 'Faux Book Alpha');
assert.property(bookQuery, 'comments');
assert.isArray(bookQuery.comments);
} catch (err) {
throw new Error(err.responseText || err.message);
}
};
È possibile inviare una richiesta POST contenente comment
come dati del corpo del modulo a /api/books/{_id}
per aggiungere un commento a un libro. La risposta restituita sarà l'oggetto libri simile alla richiesta GET /api/books/{_id}
in un test precedente. Se comment
non è incluso nella richiesta, restituisci la stringa missing required field comment
. Se non viene trovato nessun libro, restituisci la stringa no book exists
.
async (getUserInput) => {
try {
let url = getUserInput('url') + '/api/books';
let commentTarget = await $.post(url, { title: 'Notable Book' });
assert.isObject(commentTarget);
let bookId = commentTarget._id;
let bookCom1 = await $.post(url + '/' + bookId, {
comment: 'This book is fab!'
});
let bookCom2 = await $.post(url + '/' + bookId, {
comment: 'I did not care for it'
});
assert.isObject(bookCom2);
assert.property(bookCom2, '_id');
assert.property(bookCom2, 'title');
assert.property(bookCom2, 'comments');
assert.lengthOf(bookCom2.comments, 2);
bookCom2.comments.forEach((comment) => {
assert.isString(comment);
assert.oneOf(comment, ['This book is fab!', 'I did not care for it']);
});
let commentErr = await $.post(url + '/' + bookId);
assert.isString(commentErr);
assert.equal(commentErr, 'missing required field comment');
let failingComment = await $.post(url + '/5f665eb46e296f6b9b6a504d', {
comment: 'Never Seen Comment'
});
assert.isString(failingComment);
assert.equal(failingComment, 'no book exists');
} catch (err) {
throw new Error(err.responseText || err.message);
}
};
Puoi inviare una richiesta DELETE a /api/books/{_id}
per eliminare un libro dalla collezione. La risposta restituita sarà la stringa delete successful
se la richiesta ha avuto successo. Se non viene trovato nessun libro, restituisce la stringa no book exists
.
async (getUserInput) => {
try {
let url = getUserInput('url') + '/api/books';
let deleteTarget = await $.post(url, { title: 'Deletable Book' });
assert.isObject(deleteTarget);
let bookId = deleteTarget._id;
let doDelete = await $.ajax({ url: url + '/' + bookId, type: 'DELETE' });
assert.isString(doDelete);
assert.equal(doDelete, 'delete successful');
let failingDelete = await $.ajax({
url: url + '/5f665eb46e296f6b9b6a504d',
type: 'DELETE'
});
assert.isString(failingDelete);
assert.equal(failingDelete, 'no book exists');
} catch (err) {
throw new Error(err.responseText || err.message);
}
};
Puoi inviare una richiesta DELETE a /api/books
per eliminare tutti i libri nel database. La risposta restituita sarà la stringa 'complete delete successful
se la richiesta ha avuto successo.
async (getUserInput) => {
try {
const deleteAll = await $.ajax({
url: getUserInput('url') + '/api/books',
type: 'DELETE'
});
assert.isString(deleteAll);
assert.equal(deleteAll, 'complete delete successful');
} catch (err) {
throw new Error(err.responseText || err.message);
}
};
Tutti i 10 test funzionali richiesti sono completi e superati.
async (getUserInput) => {
try {
const getTests = await $.get(getUserInput('url') + '/_api/get-tests');
assert.isArray(getTests);
assert.isAtLeast(getTests.length, 10, 'At least 10 tests passed');
getTests.forEach((test) => {
assert.equal(test.state, 'passed', 'Test in Passed State');
assert.isAtLeast(
test.assertions.length,
1,
'At least one assertion per test'
);
});
} catch (err) {
throw new Error(err.responseText || err.message);
}
};
--solutions--
/**
Backend challenges don't need solutions,
because they would need to be tested against a full working project.
Please check our contributing guidelines to learn more.
*/