diff --git a/README.md b/README.md index 38f738a..389d7b9 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,11 @@ # RememberMe -A database to save the data of people that I've met. -## Features (Included Data) +A database to save the data of people that I've met. I've created this project because of my personal needs and also I wanted to learn backend. I tried to write the code as much as clean and understandable as I could. If you want to add features please feel free to do so. + +I want to implement this to a terminal based program and also a webapp in the future. + +## Features +You'll be able to save the data specified below using REST API and MongoDB. + 1. Name 2. Where you've met 3. Place (that they live) @@ -18,3 +23,70 @@ A database to save the data of people that I've met. 11. When shold I contact next 12. Additional notes 13. Tags + +## Usage +1. `npm install` +2. You'll need to install MongoDB either locally or from the cloud. Once you get your connection string change `./startup/db.js` connection string to your MongoDB adress. +3. You'll need token in order to use REST API. Your private key comes from `process.env.rememberMe_jwtkey` so you want to `setenv process.env.rememberMe_jwtkey "whatever"` + 1. To get your jwtKey write `node -e 'require("./token").createToken()'` to terminal. This token expires in 30 days. +4. Run `node index.js` and you are ready. + 1. Server port starts at `3002` if undefined. You can define the port by using `process.env.port` environment. + +### Rest API +You are going to need to set your jwt key header `x-auth-token: yourjwtkey` in order to make requests. + +Here are the example requests you can make. + +#### GET + +##### GET all entries +`http://localhost:3002/api/person/` + +##### GET by name +`http://localhost:3002/api/person/get/Mike` + +##### GET by ID +`http://localhost:3002/api/person/60fabc01465fd12839091b25` + +#### POST +`http://localhost:3002/api/person/` + +``` +{ + "name": "Mike", + "metAt": "Wall Maria Cafe", + "place": "Üsküdar" + "contact": { + "phone": "12345", + "email": "123456@gmail.com", + "socialmedia": { + "instagram": "mike123", + "youtube": "mike123", + "twitter": "mike123" + } + }, + "birth": "10.05.1998", + "age": 22, + "likes": ["football", "video games"], + "dislikes": ["cats", "spiders"], + "occupation": "student", + "lastseen": "12.07.2021", + "nextcontact": "15.07.2021", + "notes": "He is a nice guy.", + "createdAt": "23.07.2021" +} +``` +#### PUT by ID +`http://localhost:3002/api/person/60fabc01465fd12839091b25` + +``` +{ + "name": "Jimmy", + "metAt": "Taksim" +} +``` +#### DELETE by ID +`http://localhost:3002/api/person/60fabc01465fd12839091b25` + +### Required Values +Required values are `name`, `metAt` and `createdAt`. For all rules checkout `./models/person-model.js` diff --git a/models/person-model.js b/models/person-model.js index ae6714b..3e19411 100644 --- a/models/person-model.js +++ b/models/person-model.js @@ -9,12 +9,17 @@ const personSchema = new mongoose.Schema({ trim: true, required: true, }, - place: { + metAt: { type: String, minlength: 3, maxlength: 50, required: true, }, + place: { + type: String, + minlength: 3, + maxlength: 50, + }, contact: { type: new mongoose.Schema({ phone: { @@ -101,6 +106,7 @@ const personSchema = new mongoose.Schema({ createdAt: { type: String, maxlength: 25, + required: true, }, editedAt: { type: Date, @@ -114,7 +120,8 @@ const Person = mongoose.model("Person", personSchema); function validatePerson(value) { const schema = Joi.object({ name: Joi.string().min(3).max(50).required(), - place: Joi.string().min(3).max(50).required(), + metAt: Joi.string().min(3).max(50).required(), + place: Joi.string().min(3).max(50), contact: Joi.object({ phone: Joi.string().min(5).max(20), email: Joi.string().email().min(5).max(255), @@ -134,7 +141,7 @@ function validatePerson(value) { nextcontact: Joi.string().min(3).max(50), notes: Joi.string().min(3).max(500), tags: Joi.array().max(6), - createdAt: Joi.string().max(25), + createdAt: Joi.string().max(25).required(), }); return schema.validate(value, { allowUnknown: true }); } diff --git a/routes/person.js b/routes/person.js index c411e21..9615c32 100644 --- a/routes/person.js +++ b/routes/person.js @@ -32,6 +32,7 @@ router.post("/", auth, async (req, res) => { const { name, + metAt, place, contact, birth, @@ -50,6 +51,7 @@ router.post("/", auth, async (req, res) => { const person = new Person({ name, place, + metAt, contact, birth, age, @@ -100,7 +102,6 @@ router.put("/:id", auth, validateObjectId, async (req, res) => { if (!req.body.notes) req.body.notes = notes; if (!req.body.tags) req.body.tags = tags; - console.log(req.body); const { error } = validate(req.body); if (error) return res.status(400).send(error.details[0].message); diff --git a/tests/integration/person.test.js b/tests/integration/person.test.js index 95c6036..cc6c8aa 100644 --- a/tests/integration/person.test.js +++ b/tests/integration/person.test.js @@ -23,7 +23,8 @@ describe("person route", () => { person = new Person({ _id: personId, name: "Mike", - place: "Taksim", + metAt: "Taksim", + createdAt: "10.10.2000", }); await person.save(); @@ -150,11 +151,12 @@ describe("person route", () => { }); describe("POST /api/person", () => { - let person; + let person2; beforeEach(() => { - person = { + person2 = { name: "Skips", - place: "Car Maintanance", + metAt: "Car Maintanance", + createdAt: "20.12.2020", }; }); @@ -162,7 +164,7 @@ describe("person route", () => { return await request(server) .post(`/api/person/`) .set({ "x-auth-token": token }) - .send(person); + .send(person2); }; it("should return 401 if no token provided", async () => { @@ -178,7 +180,7 @@ describe("person route", () => { }); it("joi should return 400 if input is invalid", async () => { - person.name = ""; + person2.name = ""; const res = await exec(); expect(res.status).toBe(400); }); @@ -196,7 +198,8 @@ describe("person route", () => { beforeEach(() => { person2 = { name: "Jimmy", - place: "Texas", + metAt: "Texas", + createdAt: "11.11.2021", }; }); diff --git a/token.js b/token.js index 64f9314..e95e373 100644 --- a/token.js +++ b/token.js @@ -4,7 +4,7 @@ module.exports.createToken = function () { expiresIn: "30d", }); - if (!process.env.NODE_ENV === "test") { + if (process.env.NODE_ENV !== "test") { console.log(token); }