Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Test #24

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
40 changes: 34 additions & 6 deletions 01.framework-introductions/springboot-in-10-steps/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,26 @@
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-hal-browser</artifactId>
</dependency>

<!--

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
-->


<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -57,7 +65,27 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
<scope>provided</scope>
</dependency>




<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.5.9</version>
</dependency>

<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.12.10</version>
</dependency>
</dependencies>

<build>
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.in28minutes.springboot.basics.springbootin10steps;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter @Setter @AllArgsConstructor
public class Book {
long id;
String name;
String author;

// public Book(long id, String name, String author) {
// super();
// this.id = id;
// this.name = name;
// this.author = author;
// }

// public long getId() {
// return id;
// }
//
// public String getName() {
// return name;
// }
//
// public String getAuthor() {
// return author;
// }

@Override
public String toString() {
return String.format("Book [id=%s, name=%s, author=%s]", id, name, author);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.in28minutes.springboot.basics.springbootin10steps;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.NOT_IMPLEMENTED)
public class BookNotFoundException extends RuntimeException {
BookNotFoundException(){
super("Book not found");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.in28minutes.springboot.basics.springbootin10steps;

import java.util.Arrays;
import java.util.List;

import org.bson.Document;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RestController;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;



@RestController
public class BooksController {


@Operation(summary = "Get a book by its id")
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "Found the book",
content = { @Content(mediaType = "application/json",
schema = @Schema(implementation = Book.class)) }),
@ApiResponse(responseCode = "400", description = "Invalid id supplied",
content = @Content),
@ApiResponse(responseCode = "404", description = "Book not found",
content = @Content) })


@GetMapping("/books")
public List<Book> getAllBooks() {

MongoClient mongoClient = new MongoClient(
new MongoClientURI(
//MongoDBURL
)
);
MongoDatabase database = mongoClient.getDatabase("sample_airbnb");
MongoCollection<Document> collection = database.getCollection("listingsAndReviews");

AggregateIterable<Document> result = collection.aggregate(Arrays.asList(
new Document("$group",new Document("_id", "$address.country").append("c", new Document("$sum", 1L))),
new Document("$sort", new Document("c", -1L))));

Document first = result.first();

return Arrays.asList(
new Book(1l, "Mastering Spring 5.2", "Ranga Karanam"));
}

@DeleteMapping("/books")
public List<Book> getAllBooks1() {
throw new BookNotFoundException();
}

@PostMapping("/books")
public List<Book> getAllBooks1(@Validated @RequestBody Book book) {
//throw new BookNotFoundException();
return Arrays.asList(book);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.in28minutes.springboot.basics.springbootin10steps;

import java.util.Date;

import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
import org.springframework.web.util.WebUtils;

@ControllerAdvice
@RestController
public class CustomResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(Exception.class)
public final ResponseEntity<Object> handleExceptionInternal(Exception ex, WebRequest request) {
ExceptionResponse exResponse = new ExceptionResponse(new Date(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity<>(exResponse, HttpStatus.INTERNAL_SERVER_ERROR);
}

@ExceptionHandler(BookNotFoundException.class)
public final ResponseEntity<Object> handleExceptionInternal1(Exception ex, WebRequest request) {
ExceptionResponse exResponse = new ExceptionResponse(new Date(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity<>(exResponse, HttpStatus.NOT_IMPLEMENTED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.in28minutes.springboot.basics.springbootin10steps;

import java.util.Date;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

@Getter @Setter @AllArgsConstructor
public class ExceptionResponse {
private Date timestamp;
private String message;
private String details;
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
#logging.level.org.springframework = DEBUG
management.security.enabled=false
logging.level.org.springframework = DEBUG
management.security.enabled=false
management.endpoints.web.exposure.include=*
security.user.name=username
security.user.password=password

spring.datasource.url=jdbc:h2:mem:testdb
spring.jpa.defer-datasource-initialization=true
spring.jpa.show-sql=true
spring.h2.console.enabled=true

This file was deleted.