DEV Community

Cover image for MY JOURNEY AS A BACKEND ENGINEER INTERN.
Ifeoma Eunice Ugwu
Ifeoma Eunice Ugwu

Posted on

MY JOURNEY AS A BACKEND ENGINEER INTERN.

MY JOURNEY AS A BACKEND ENGINEER INTERN.
I am Ifeoma Eunice Ugwu, a goal-getter, hardworking, determined, an easy-going person. I studied Metallurgical and Materials Engineering at Nnamdi Azikiwe University. I also studied courses on Backend Engineering (NodeJs, Typescript, NestJs) and frontend courses like HTML, CSS, JavaScript.
Joining HNG for internship program was what I looked forward to. I jumped into the offer when a friend sent me their link https://hng.tech/internship. I registered through this link: https://hng.tech/premium. I registered and followed their procedure judiciously in order to be a part of it knowing that I will be able to learn to build different APIs, collaborate with others, connect with my fellow engineers, learn to work under pressure and meet up with targets and more.
My work as a Backend Engineer has been a smooth journey until I encountered this particular problem in NodeJS when I was building a Todo API application. I was stocked trying to query the registered tasks in my database for the app.
Firstly, I created tasks using the below code and logging in tasks in order to save them in my database with the help of taskSchema.
// to create tasks
app.post("/task", (req, res) => {
let tasks = req.body;
tasksModel.create(tasks)
.then((doc) => {
res.status(201).send({ message: "Task Registered successfully" })
})
.catch((err) => {
console.log(err);
res.status({ message: "Some problem occured" })
})
})

I then tried to get all tasks using the below code.
// get all tasks
app.get("/tasks, (req, res) => {
tasksModel.findAll({ tasks })
.then((tasks) => {
res.status(200).json({ tasks, message: "Tasks returned successfully"})
console.log(tasks);
}).catch(err=>{
console.log(err)
})
})

Here is the code for getting tasks by id:
// get task by id
app.get("/tasks/:_id", (req, res) => {
const _id = req.params._id
console.log(_id)
tasksModel.findOne({ _id })
.then((tasks) => {
res.status(200).json({ tasks, message: "Task returned successfully"})
// console.log(tasks);
}).catch(err=>{
console.log(err)
})
})

Here is the code for updating the registered tasks.
// to update task by id
app.put("/tasks/:_id", (req, res) =>{
const _id = req.params._id
const Name = req.body.Name
const Status = req.body.Status
tasksModel.updateOne({ _id }, { Name , Status })
.then((tasks) => {
res.status(200).json({ tasks, message: "Task updated successfully"})
console.log(tasks);
})
})

Finally, I was able to solve the problem using the below code to delete the required task by id.
// to delete tasks by id
app.delete("/tasks/:_id", (req, res) => {
const _id = req.params._id
tasksModel.deleteOne({ _id})
.then((tasks) => {
res.status(200).json({ tasks, message: "Task deleted successfully"})
console.log(tasks);
})
})

Top comments (0)