Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

From JOY SINHA

Assignment 1:

1: Creating a Database
Let us start by creating a test database. I Named it "joy_sinha"

using the following command in the MongoDB shell, or through a


database manager like MongoDB Compass:

use joy_sinha;

2: Adding a MongoDB Collection


Let's create the collection with the following command:

db.createCollection("cars");

data to the collection with the following command:

db.cars.insertMany([
{
"car_name" : "Audi",
"date_of_sale" : "11 Jan 2019",
"model_no" : "A3"
"price" : "700000"
},
{
"car_name" : "XYZ",
"date_of_sale" : "10 Jan 2018",
"model_no" : "ABC",
"price" : "900000"
},
{
"car_name" : "ABC",
"date_of_sale" : "10 Jan 2018",
"model_no" : "XYZ",
"price" : "1000000"
}
]);

Querying the collection with db.cars.find({});


MongoDB automatically assigns a unique _id
3:
creating a new folder in src/main/java/[package name]/ called "models".
In the new "models" folder, we can create a file called cars.java

code:

package io.codementor.gtommee.joy_sinha.models;

import org.bson.types.ObjectId;
import org.springframework.data.annotation.Id;

public class Cars {


@Id
public ObjectId _id;

public String car_name;


public String date_of_sale;
public String model;
public String price;

// Constructors
public Cars() {}

public Cars(ObjectId _id, String car_name, String


date_of_sale, String model, String price) {
this._id = _id;
this.car_name = car_name;
this.date_of_sale = date_of_sale;
this.model = model;
this.price = price;

// ObjectId needs to be converted to string


public String get_id() { return _id.toHexString(); }
public void set_id(ObjectId _id) { this._id = _id; }

public String getName() { return car_name; }


public void setName(String car_name) { this.car_name =
car_name; }

public String getDos() { return date_of_sale; }


public void setDos(String date_of_sale) { this.date_of_sale =
date_of_sale; }

public String getModel() { return model; }


public void setModel(String model) { this.model = model; }

public String getPrice() { return price; }


public void setPrice(String price) { this.price = price; }
}
Adding Repository to Spring Boot Project
making a new folder called "repositories" in src/main/java/[package
name]/.

In the new "repositories" folder, we can create a file


called CarsRepository.java

Code:

package io.codementor.gtommee.rest_tutorial.repositories;

import io.codementor.gtommee.rest_tutorial.models.Pets;
import org.bson.types.ObjectId;
import
org.springframework.data.mongodb.repository.MongoRepository;

public interface PetsRepository extends MongoRepository<Cars,


String> {
Pets findBy_id(ObjectId _id);
}

Adding the MongoDB Connection Info


Connection information for our MongoDB, we will need to add conection
details to the application.properties file, located in the
"src/main/resources" folder.

MongoDB instance:

spring.data.mongodb.host=[host]
spring.data.mongodb.port=[port]
spring.data.mongodb.authentication-
database=[authentication_database]
spring.data.mongodb.username=[username]
spring.data.mongodb.password=[password]
spring.data.mongodb.database=joy_sinha
Creating REST Controller
Spring should now be able to connect to MongoDB

We can create a Rest Controller by adding a file


called CarsController.java to the src/main/java/[package
name]/ folder.

Code:

package io.codementor.gtommee.joy_sinha;

import io.codementor.gtommee.joy_sinha.models.Cars;
import
io.codementor.gtommee.joy_sinha.repositories.CarsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/cars")
public class CarsController {
@Autowired
private CarsRepository repository;
}

Adding the REST Endpoints

following enpoints will be added directly into the CarsController class

Completed Controller:

package io.codementor.gtommee.joy_sinha;

import io.codementor.gtommee.joy_sinha.models.Cars;
import
io.codementor.gtommee.joy_sinha.repositories.CarsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/cars")
public class CarsController {
@Autowired
private CarsRepository repository;

@RequestMapping(value = "/", method = RequestMethod.GET)


public List<Cars> getAllCars() {
return repository.findAll();
}

@RequestMapping(value = "/{id}", method = RequestMethod.GET)


public Pets getCarById(@PathVariable("id") ObjectId id) {
return repository.findBy_id(id);
}

@RequestMapping(value = "/{id}", method = RequestMethod.PUT)


public void modifyCarById(@PathVariable("id") ObjectId id,
@Valid @RequestBody Cars cars) {
pets.set_id(id);
repository.save(cars);
}

@RequestMapping(value = "/", method = RequestMethod.POST)


public Cars createCar(@Valid @RequestBody Cars cars) {
pets.set_id(ObjectId.get());
repository.save(cars);
return cars;
}

@RequestMapping(value = "/{id}", method =


RequestMethod.DELETE)
public void deleteCar(@PathVariable ObjectId id) {
repository.delete(repository.findBy_id(id));
}
}

Testing Your API

run the mvn spring-boot:run


POST 'http://localhost:8080/cars'

With body : {"car_name" : "IJK", "date_of_sale" : "22 Feb 2018",


"model" : "ABC", “price” : “2000000”}

and header : 'Content-Type: application/json'

Returns:

"_id": "5aecef5b6d55754834124df3",

"car_name": "IJK",

"date_of_sale": "22 feb 2018",

"model": "ABC"",

"price": "2000000",

PUT 'http://localhost:8080/cars/5aecef5b6d55754834124df3'

With body : {"car_name" : "IJK", "date_of_sale" : "22 Feb 2018",


"model" : "ABC", “price” : “2000000”}

and header : Content-Type : application/json

Returns:

empty response

GET 'http://localhost:8080/cars/5aecef5b6d55754834124df3'
Returns:

"_id": "5aecef5b6d55754834124df3",

"car_name": "IJK",

"date_of_sale": "22 feb 2018",

"model": "ABC"",

"price": "2000000",

DELETE 'http://localhost:8080/cars/5aecef5b6d55754834124df3'

Returns:

empty response

GET 'http://localhost:8080/cars'

Returns:

"_id": "5aeccb0a18365ba07414356c",

"car_name": "Audi",
"date_of_sale": "11 Jan 2019",

"model": "A3",

"price": "700000"

},

"_id": "5aeccb0a18365ba07414356c",

"car_name": "XYZ",

"date_of_sale": "10 Jan 2018",

"model": "ABC",

"price": "900000"

},

"_id": "5aeccb0a18365ba07414356c",

"car_name": "ABC",

"date_of_sale": "10 Jan 2018",

"model": "XYZ",

"price": "1000000"

},
]

You might also like