first draft
This commit is contained in:
parent
6d5ea19088
commit
e5ad2b045b
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.boot.count;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;
|
||||
|
||||
@SpringBootApplication
|
||||
@EnableMongoRepositories(basePackages = { "com.baeldung.boot.count" })
|
||||
public class SpringBootCountApplication {
|
||||
public static void main(String... args) {
|
||||
SpringApplication.run(SpringBootCountApplication.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,16 @@
|
|||
package com.baeldung.boot.count.dao;
|
||||
|
||||
import org.springframework.data.mongodb.repository.MongoRepository;
|
||||
import org.springframework.data.mongodb.repository.Query;
|
||||
|
||||
import com.baeldung.boot.count.data.Car;
|
||||
|
||||
public interface CarRepository extends MongoRepository<Car, String> {
|
||||
@Query(value = "{brand: ?0}", count = true)
|
||||
public long countBrand(String brand);
|
||||
|
||||
Long countByBrand(String brand);
|
||||
|
||||
@Query(value = "{}", count = true)
|
||||
Long countWithAnnotation();
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.boot.count.data;
|
||||
|
||||
import org.springframework.data.mongodb.core.mapping.Document;
|
||||
@Document
|
||||
public class Car {
|
||||
private String name;
|
||||
|
||||
private String brand;
|
||||
|
||||
public Car() {
|
||||
}
|
||||
|
||||
public Car(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
public String getBrand() {
|
||||
return brand;
|
||||
}
|
||||
|
||||
public void setBrand(String brand) {
|
||||
this.brand = brand;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,69 @@
|
|||
package com.baeldung.boot.count.service;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.boot.count.dao.CarRepository;
|
||||
import com.baeldung.boot.count.data.Car;
|
||||
|
||||
@Service
|
||||
public class CountCarService {
|
||||
|
||||
@Autowired
|
||||
private CarRepository repo;
|
||||
|
||||
@Autowired
|
||||
private MongoTemplate mongo;
|
||||
|
||||
public List<Car> findCars() {
|
||||
return repo.findAll();
|
||||
}
|
||||
|
||||
public Optional<Car> findCar(String id) {
|
||||
return repo.findById(id);
|
||||
}
|
||||
|
||||
public Car insertCar(Car item) {
|
||||
return repo.insert(item);
|
||||
}
|
||||
|
||||
public long getCountWithQueryAnnotation() {
|
||||
return repo.countWithAnnotation();
|
||||
}
|
||||
|
||||
public long getCountWithCrudRepository() {
|
||||
return repo.count();
|
||||
}
|
||||
|
||||
public long getCountBrandWithQueryMethod(String brand) {
|
||||
return repo.countByBrand(brand);
|
||||
}
|
||||
|
||||
public long getCountWithExample(Car item) {
|
||||
return repo.count(Example.of(item));
|
||||
}
|
||||
|
||||
public long getCountWithExampleCriteria(Car item) {
|
||||
Query query = new Query();
|
||||
query.addCriteria(Criteria.byExample(item));
|
||||
return mongo.count(query, Car.class);
|
||||
}
|
||||
|
||||
public long getCountBrandWithQueryAnnotation(String brand) {
|
||||
return repo.countBrand(brand);
|
||||
}
|
||||
|
||||
public long getCountBrandWithCriteria(String brand) {
|
||||
Query query = new Query();
|
||||
query.addCriteria(Criteria.where("brand")
|
||||
.is(brand));
|
||||
return mongo.count(query, Car.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
package com.baeldung.boot.count.web;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Example;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.query.Criteria;
|
||||
import org.springframework.data.mongodb.core.query.Query;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
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 com.baeldung.boot.count.dao.CarRepository;
|
||||
import com.baeldung.boot.count.data.Car;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/car")
|
||||
public class CarController {
|
||||
@Autowired
|
||||
private CarRepository carRepo;
|
||||
|
||||
@Autowired
|
||||
private MongoTemplate mongo;
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Optional<Car> getCar(@PathVariable String id) {
|
||||
return carRepo.findById(id);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public Car postCar(@RequestBody Car item) {
|
||||
return carRepo.insert(item);
|
||||
}
|
||||
|
||||
@GetMapping("/count/{brand}")
|
||||
public Long getCountCarBrand(@PathVariable String brand) {
|
||||
return carRepo.countByBrand(brand);
|
||||
}
|
||||
|
||||
@GetMapping("/count2/{brand}")
|
||||
public Long getCountCarBrand2(@PathVariable String brand) {
|
||||
return carRepo.countBrand(brand);
|
||||
}
|
||||
|
||||
@GetMapping("/count")
|
||||
public Long getCountCar() {
|
||||
return carRepo.countWithAnnotation();
|
||||
}
|
||||
|
||||
@GetMapping("/count2")
|
||||
public Long getCountCar2() {
|
||||
// default do repo
|
||||
return carRepo.count();
|
||||
}
|
||||
|
||||
@PostMapping("/count")
|
||||
public Long postCount(@RequestBody Car item) {
|
||||
return carRepo.count(Example.of(item));
|
||||
}
|
||||
|
||||
@PostMapping("/count/criteria")
|
||||
public Long postCountCriteria(@RequestBody Car item) {
|
||||
Query query = new Query();
|
||||
query.addCriteria(Criteria.byExample(item));
|
||||
return mongo.count(query, Car.class);
|
||||
}
|
||||
|
||||
@GetMapping("/count/criteria/{brand}")
|
||||
public Long getCountCarCriteria(@PathVariable String brand) {
|
||||
Query query = new Query();
|
||||
query.addCriteria(Criteria.where("brand")
|
||||
.is(brand));
|
||||
return mongo.count(query, Car.class);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,118 @@
|
|||
package com.baeldung.boot.count.service;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.boot.count.data.Car;
|
||||
|
||||
@SpringBootTest
|
||||
@DirtiesContext
|
||||
@RunWith(SpringRunner.class)
|
||||
public class CountCarServiceIntegrationTest {
|
||||
@Autowired
|
||||
private CountCarService service;
|
||||
|
||||
Car car1 = new Car("B-A");
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
service.insertCar(car1);
|
||||
service.insertCar(new Car("B-B"));
|
||||
service.insertCar(new Car("B-C"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAllDocs_whenQueryAnnotationCount_thenCountEqualsSize() {
|
||||
List<Car> all = service.findCars();
|
||||
|
||||
long count = service.getCountWithQueryAnnotation();
|
||||
|
||||
assertEquals(count, all.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAllDocs_whenCrudRepositoryCount_thenCountEqualsSize() {
|
||||
List<Car> all = service.findCars();
|
||||
|
||||
long count = service.getCountWithCrudRepository();
|
||||
|
||||
assertEquals(count, all.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFilteredDocs_whenCriteriaCountByBrand_thenCountEqualsSize() {
|
||||
String filter = "B-A";
|
||||
long all = service.findCars()
|
||||
.stream()
|
||||
.filter(car -> car.getBrand()
|
||||
.equals(filter))
|
||||
.count();
|
||||
|
||||
long count = service.getCountBrandWithCriteria(filter);
|
||||
|
||||
assertEquals(count, all);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenQueryAnnotation_whenCountingByBrand_thenCountEqualsSize() {
|
||||
String filter = "B-A";
|
||||
long all = service.findCars()
|
||||
.stream()
|
||||
.filter(car -> car.getBrand()
|
||||
.equals(filter))
|
||||
.count();
|
||||
|
||||
long count = service.getCountBrandWithQueryAnnotation(filter);
|
||||
|
||||
assertEquals(count, all);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFilteredDocs_whenQueryMethodCountByBrand_thenCountEqualsSize() {
|
||||
String filter = "B-A";
|
||||
long all = service.findCars()
|
||||
.stream()
|
||||
.filter(car -> car.getBrand()
|
||||
.equals(filter))
|
||||
.count();
|
||||
|
||||
long count = service.getCountBrandWithQueryMethod(filter);
|
||||
|
||||
assertEquals(count, all);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFilteredDocs_whenExampleCount_thenCountEqualsSize() {
|
||||
long all = service.findCars()
|
||||
.stream()
|
||||
.filter(car -> car.getBrand()
|
||||
.equals(car1.getBrand()))
|
||||
.count();
|
||||
|
||||
long count = service.getCountWithExample(car1);
|
||||
|
||||
assertEquals(count, all);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFilteredDocs_whenExampleCriteriaCount_thenCountEqualsSize() {
|
||||
long all = service.findCars()
|
||||
.stream()
|
||||
.filter(car -> car.getBrand()
|
||||
.equals(car1.getBrand()))
|
||||
.count();
|
||||
|
||||
long count = service.getCountWithExampleCriteria(car1);
|
||||
|
||||
assertEquals(count, all);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue