Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ro.raffa.curs.configuration.database;

import java.math.BigDecimal;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import ro.raffa.curs.model.Car;
import ro.raffa.curs.repository.DbCarRepository;

@Configuration
@Profile("database")
public class DataBaseInit {
@Autowired
DbCarRepository carRepository;

@Bean
public void DbInit(){
if(carRepository.count()==0){
loadDb();
}

}

private void loadDb() {
carRepository.save(createCar("Mercedes","Red", "CN2", 2020, "EUR", 250000));
carRepository.save(createCar("BMW", "Cream", "M2", 2023, "EUR", 300000));
carRepository.save(createCar("Toyota", "Dark Chocolate", "Heisenberg", 2018, "EUR", 80000));
}

private Car createCar(String maker,String color,String model, Integer year, String currency, Integer price) {
Car car = new Car();
car.setMaker(maker);
car.setColor(color);
car.setModel(model);
car.setYear(year);
car.setCurrency(currency);
car.setPrice(BigDecimal.valueOf(price));
return car;
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use right click -> format document for all files

}
2 changes: 2 additions & 0 deletions src/main/java/ro/raffa/curs/controller/v1/CarController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Profile;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand All @@ -16,6 +17,7 @@

@RestController
@RequestMapping("/v1")
@Profile("local")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

controllers should not be profile specific !

public class CarController {

@Autowired
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ro.raffa.curs.controller.v1;

import org.springframework.context.annotation.Profile;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -11,6 +12,7 @@

@RestController
@RequestMapping("/v1")
@Profile("local")
public class HelloWorldController {

@GetMapping(path = "/helloworld/hello1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import netscape.javascript.JSObject;
import ro.raffa.curs.model.Car;
import ro.raffa.curs.repository.DbCarRepository;

Expand All @@ -19,6 +21,7 @@ public class CarControllerV2 {
@Autowired
DbCarRepository carRepository;


@GetMapping("/car/list")
public List<Car> getCars() {
List<Car> list=carRepository.findAll();
Expand Down