Skip to content

this is self project to implement golang basics for creating backend solution

License

Notifications You must be signed in to change notification settings

Prachi-Thakre/todobackend

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

todobackend

This is a Simple ToDo Backend Code

One Instance of code i have alreday deployed on the lambda and created API GAteway Endpoints That can be be found in the below postman collection

https://www.getpostman.com/collections/526963b4efd334bca962

for Running Code in the local system follow steps written below

  1. clone the repository
  2. set the mysql credential in conf->env.yml file ( make sure runmode shoud be dev )
  3. create databse and tables, please refer mysql->dbSchema.sql ( just copy paste those command in your workbench nothing more :) )
  4. run the code and follow the below postman collection

https://www.getpostman.com/collections/e1a846af5ec01f9e0172

--------------------------------------------------------------------------------------------------------------

Code Overview

we have two table is our database tododb, one table is task, to store details of each task while other is user to store user details.
Schema is given below

--
-- Table structure for table `User`
--
create table user(
  id int auto_increment not null,
  first_name varchar(50) not null,
  last_name varchar(50) not null,
   primary key(id)
  

);

--
-- Table structure for table `Task`
--

create table task(
  id int auto_increment not null,
  user_id  int not null,
  task_name  varchar(100) not null,
  description text,
  status varchar(50) not null,
  due_date datetime NOT NULL DEFAULT current_timestamp(),
  primary key(id)
);
 

Endpoint

  1. add user :

    POST /todo/user

    Request Body:
    {
    "firstName": "hardic",
    "lastName": "pandya"
    }

this end point will add the the user in the user table
Response Body:

        
  {
   "code": 200,
   "msg": "success",
   "model": {
       "id": 7,
       "firstName": "hardic",
       "lastName": "pandya"
        }
   }

you will get user id on adding a user(in this case user id is 7)

  1. get all users:

    Get /todo/user

it will basically fetch all the user from the table
Resopnse Body

{
  "code": 200,
  "msg": "success",
  "model": [
      {
          "id": 1,
          "firstName": "fname",
          "lastName": "lname"
      },
      {
          "id": 2,
          "firstName": "Rahul",
          "lastName": "Bareliya"
      },
      {
          "id": 3,
          "firstName": "test",
          "lastName": "user"
      },
      {
          "id": 4,
          "firstName": "virat",
          "lastName": "kohli"
      },
      {
          "id": 5,
          "firstName": "rohit",
          "lastName": "sharma"
      },
      {
          "id": 6,
          "firstName": "hardic",
          "lastName": "pandya"
      },
      {
          "id": 7,
          "firstName": "hardic",
          "lastName": "pandya"
      }
  ]
}
  1. get a user by user id

    Get todo/user?id=1

    it will filter the table by user id and will return the details corrospondig to the userid
    Response Body

       {
     "code": 200,
     "msg": "success",
     "model": {
         "id": 1,
         "firstName": "fname",
         "lastName": "lname"
        }
      }
  2. Add task

    Post todo/tsak

Request Body:

{
 "userId": 6,
 "taskName": "BackEnd",
 "description": "an incredible task 2",
 "dueDate": "2022-02-23T17:58:53+05:30"
 }

task are added corrosponding to user
so we need user id in request body (int above case is ,6) if we pass invalid user id we will get error accordingly Response Body:

{
    "code": 200,
    "msg": "success",
    "model": {
        "id": 10,
        "userId": 6,
        "taskname": "BackEnd",
        "description": "an incredible task 2",
        "status": "pending",
        "dueDate": "2022-02-23T17:58:53+05:30"
    }
}
  1. Update Task.

    PUT todo/task

    Request Body:
    {
      "id": 1,
        "taskname": "updated Task Name",
        "description": "an incredible task 2 updated",
        "dueDate": "2022-02-23T17:58:53+05:30"
    }

In this Case we must give a valid task id to update(in this case it is 1) other wise we will get error accordingly

Response Body:

{
    "code": 200,
    "msg": "success",
    "model": {
        "id": 1,
        "userId": 0,
        "taskname": "updated Task Name",
        "description": "an incredible task 2 updated",
        "status": "pending",
        "dueDate": "2022-02-23T17:58:53+05:30"
    }
}
  1. Change Status

    PUT todo/task

    Requst Body :
    {
     "id": 7,
     "status": "completed"
    }

Agsin we need a valid task id to update the status
response Body:

{
    "code": 200,
    "msg": "success",
    "model": {
        "id": 7,
        "userId": 0,
        "taskname": "",
        "description": "an incredible task 2",
        "status": "completed",
        "dueDate": "2022-02-23T17:58:53+05:30"
    }
  }
  1. delete task

    DELETE todo/task

    request body:
    {
     "id": 6
    
    }

we need to pass just task id in the request body
response body:

{
    "code": 200,
    "msg": "success",
    "model": null
  }
  1. get all task

    GET todo/task

    Response Body:

    {
     "code": 200,
     "msg": "success",
     "model": [
         {
             "id": 1,
             "userId": 0,
             "taskname": "updated Task Name",
             "description": "an incredible task 2 updated",
             "status": "pending",
             "dueDate": "2022-02-23T17:58:53+05:30"
         },
         {
             "id": 2,
             "userId": 1,
             "taskname": "",
             "description": "an incredible task 2",
             "status": "completed",
             "dueDate": "2022-02-23T17:58:53+05:30"
         },
         {
             "id": 3,
             "userId": 1,
             "taskname": " TaskName",
             "description": "an incredible task 2",
             "status": "pending",
             "dueDate": "2022-02-23T17:58:53+05:30"
         },
         {
             "id": 4,
             "userId": 3,
             "taskname": "BournVita",
             "description": "an incredible task 2",
             "status": "pending",
             "dueDate": "2022-02-23T17:58:53+05:30"
         },
         {
             "id": 7,
             "userId": 0,
             "taskname": "",
             "description": "an incredible task 2",
             "status": "completed",
             "dueDate": "2022-02-23T17:58:53+05:30"
         },
         {
             "id": 8,
             "userId": 5,
             "taskname": "FrontEnd",
             "description": "an incredible task 2",
             "status": "pending",
             "dueDate": "2022-02-23T17:58:53+05:30"
         },
         {
             "id": 9,
             "userId": 6,
             "taskname": "BackEnd",
             "description": "an incredible task 2",
             "status": "pending",
             "dueDate": "2022-02-23T17:58:53+05:30"
         },
         {
             "id": 10,
             "userId": 6,
             "taskname": "BackEnd",
             "description": "an incredible task 2",
             "status": "pending",
             "dueDate": "2022-02-23T17:58:53+05:30"
         }
        ]
      }
  2. get the task by user id

    GET todo/tsak?userId=1

{
  "code": 200,
  "msg": "success",
  "model": [
      {
          "id": 2,
          "userId": 1,
          "taskname": "",
          "description": "an incredible task 2",
          "status": "completed",
          "dueDate": "2022-02-23T17:58:53+05:30"
      },
      {
          "id": 3,
          "userId": 1,
          "taskname": " TaskName",
          "description": "an incredible task 2",
          "status": "pending",
          "dueDate": "2022-02-23T17:58:53+05:30"
      }
  ]
}

we will get the all the task created by the user
10. get the task by task id.
### GET task/todo?id=1
response body:

{
 "code": 200,
 "msg": "success",
 "model": {
     "id": 1,
     "userId": 0,
     "taskname": "updated Task Name",
     "description": "an incredible task 2 updated",
     "status": "pending",
     "dueDate": "2022-02-23T17:58:53+05:30"
 }
}

we will get the task corrosponding to the id

  1. get the task by user is and status

    GET todo/task?userId=1&status=pending

reponse body:

{
  "code": 200,
  "msg": "success",
  "model": [
      {
          "id": 3,
          "userId": 1,
          "taskname": " TaskName",
          "description": "an incredible task 2",
          "status": "pending",
          "dueDate": "2022-02-23T17:58:53+05:30"
      }
  ]
}

this will give us the task have the given status corrosponding to the user id

About

this is self project to implement golang basics for creating backend solution

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages