Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

node js rest with flutter

var qs = require('querystring');
const Task = require("../models/task.model.js");
// Create and Save a new Task

exports.create = (req, res) => {
  console.log(req.body);
  // Validate request
  if (!req.body) {
    res.status(400).send({
      message: "Content can not be empty!"
    });
  }
  // Create a Task
  const task = new Task({
    task_name: req.body.task_name,
    task_detail: req.body.task_detail,
    date: req.body.date,
    published:req.body.published || 0
  });
  // Save Task in the database
  Task.create(task, (err, data) => {
    if (err)
      res.status(500).send({
        message:
          err.message || "Some error occurred while creating the Task."
      });
    else res.send(data);
  });
};
// Retrieve all tasks from the database (with condition).
exports.findAll = (req, res) => {
  const title = req.query.name;
  Task.getAll(title, (err, data) => {
    if (err)
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving tasks."
      });
    else res.send(data);
  });
};
exports.findAllPublished = (req, res) => {
  Task.getAllPublished((err, data) => {
    if (err)
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving tasks."
      });
    else res.send(data);
  });


};
// Find a single Task with a id
exports.findOne = (req, res) => {
  console.log(req.query.id);
  Task.findById(req.query.id, (err, data) => {
    if (err) {
      if (err.kind === "not_found") {
        res.status(404).send({
          message: `Not found Task with id ${req.query.id}.`
        });
      } else {
        res.status(500).send({
          message: "Error retrieving Task with id " + req.params.id
        });
      }
    } else res.send(data);
  });
};

// Update a Task identified by the id in the request
exports.update = (req, res) => {
  // Validate Request
  if (!req.body) {
    res.status(400).send({
      message: "Content can not be empty!"
    });
  }


  console.log(req.body);
  Task.updateById(
    req.query.id,
    new Task(req.body),
    (err, data) => {
      if (err) {
        if (err.kind === "not_found") {
          res.status(404).send({
            message: `Not found Tutorial with id ${req.query.id}.`
          });
        } else {
          res.status(500).send({
            message: "Error updating Tutorial with id " + req.query.id
          });
        }
      } else res.send(data);
    }
  );

};
// Delete a Task with the specified id in the request
exports.delete = (req, res) => {
  Task.remove(req.query.id, (err, data) => {
    if (err) {
      if (err.kind === "not_found") {
        res.status(404).send({
          message: `Not found Task with id ${req.query.id}.`
        });
      } else {
        res.status(500).send({
          message: "Could not delete Task with id " + req.query.id
        });
      }
    } else res.send({ message: `Task was deleted successfully!` });
  });

};
// Delete all tasks from the database.
exports.deleteAll = (req, res) => {
  Task.removeAll((err, data)=>{
    if(err){
      res.status(500).send({
        message:err.message || "Could not finish the operation"
      });

    }else{
      res.send({
        message:"Removed all task successfully"
      });
    }
  })
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: select inputs without specific type js 
Javascript :: multiple variables in one live javascript 
Javascript :: node js knex sqlite query 
Javascript :: js 2 varibale points on same values 
Javascript :: class angular dynamic template 
Javascript :: json format in .net core 
Javascript :: check if content is overflowing react 
Javascript :: javascrript Wrap all individual words in a span tag based on their first letter 
Javascript :: How to escape specific JSON characters in Powershell 
Javascript :: angularjs checking array of objects 
Javascript :: Why is this forEach code snippet invalid in AngularJS 
Javascript :: angularjs trying to fix a rack lint error and 500 on GET /cable 
Javascript :: Angular after click add active class and remove from siblings 
Javascript :: How to hide div based on select the dropdown in angular js 
Javascript :: Changing Component File location in React native does not show in main App 
Javascript :: In React Native / Expo, is there any way to save a specific part of an image 
Javascript :: Browser globals 
Javascript :: Store input values in array javascript 
Javascript :: javascript polyIntersect 
Javascript :: morgan 
Javascript :: json array form to list object java 
Javascript :: div auto extend win righting in 
Javascript :: Bare Minimum Passport Login Pages App.js 
Javascript :: how to add random color in chart in react j 
Javascript :: sol.common.MapTable elo 
Javascript :: javascript palindrome check 
Javascript :: add array and sort 
Javascript :: prisma is and isNot 
Javascript :: PostManDocs 
Javascript :: connect nextjs to google sheets 
ADD CONTENT
Topic
Content
Source link
Name
1+6 =