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
64 changes: 63 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,63 @@
/.idea/
/.idea/

# Compiled class files
*.class

# Log files
*.log

# Package files
*.jar
*.war
*.nar
*.ear
*.zip
*.tar.gz
*.rar

# Maven
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar

# Gradle
build/
.gradle/
!gradle/wrapper/gradle-wrapper.jar

# Eclipse
.classpath
.project
.settings/
bin/

# IntelliJ
*.iml
*.iws
*.ipr
out/

# NetBeans
nbproject/private/
nbbuild/
dist/
nbdist/
.nb-gradle/

# VS Code
.vscode/

# OS files
.DS_Store
Thumbs.db

# Virtual machine crash logs
hs_err_pid*
replay_pid*
1 change: 0 additions & 1 deletion .gitignore.txt

This file was deleted.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ buildscript {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.2.RELEASE")
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.7.18")
}
}

Expand Down
18 changes: 16 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

<groupId>org.springframework</groupId>
<artifactId>gs-spring-boot</artifactId>
<packaging>pom</packaging>
<packaging>jar</packaging>
<version>0.1.0</version>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.2.RELEASE</version>
<version>2.7.18</version>
</parent>

<dependencies>
Expand All @@ -28,10 +28,24 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<properties>
Expand Down
23 changes: 0 additions & 23 deletions src/main/java/hello/Application.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@
import java.util.stream.Collectors;

import hello.model.Customer;
import hello.model.Quote;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
public class Application implements CommandLineRunner {
Expand All @@ -34,25 +30,6 @@ public static void main(String[] args) {
for (String beanName : beanNames) {
System.out.println(beanName);
}

RestTemplate restTemplate = new RestTemplate();
Quote quote = restTemplate.getForObject("http://gturnquist-quoters.cfapps.io/api/random", Quote.class);
log.info(quote.toString());
}


@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {
return builder.build();
}

@Bean
public CommandLineRunner run(RestTemplate restTemplate) throws Exception {
return args -> {
Quote quote = restTemplate.getForObject(
"http://gturnquist-quoters.cfapps.io/api/random", Quote.class);
log.info(quote.toString());
};
}


Expand Down
17 changes: 5 additions & 12 deletions src/main/java/hello/controller/HelloController.java
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
package hello.controller;

import hello.declaration.SimpleTimeClient;
import hello.declaration.TimeClient;
import hello.model.SimpleTimeClient;
import hello.model.Topic;
import hello.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.chrono.ChronoPeriod;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@RestController
public class HelloController {
Expand All @@ -43,7 +36,7 @@ public class HelloController {
*
* @return
*/
@RequestMapping("/datetime")
@GetMapping("/datetime")
public String index() {
TimeClient myTimeClient = new SimpleTimeClient();
LocalDateTime localDateTime = LocalDateTime.now();
Expand All @@ -63,7 +56,7 @@ public String index() {
*
* @return
*/
@RequestMapping("/topic/string/operation")
@GetMapping("/topic/string/operation")
public String showStringOperation() {

String join = topicService.returnAllTopicIDWithStringSlicing();
Expand All @@ -84,7 +77,7 @@ public String showStringOperation() {
* File Operation in Java 8
* @return
*/
@RequestMapping("/topic/file/operation")
@GetMapping("/topic/file/operation")
public String showFileOperation() {
String findAllFilesInPathAndSort = topicService.findAllFilesInPathAndSort();
String findParticularFileInPathAndSort = topicService.findParticularFileInPathAndSort();
Expand Down
52 changes: 32 additions & 20 deletions src/main/java/hello/controller/TopicController.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,17 @@
import hello.model.Topic;
import hello.service.TopicService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
Expand All @@ -18,68 +27,71 @@ public class TopicController {
* Get all Topic
* @return
*/
@RequestMapping("/topic")
public List<Topic> getAllTopics() {
return topicService.getAllTopics();
@GetMapping("/topic")
public ResponseEntity<List<Topic>> getAllTopics() {
return ResponseEntity.ok(topicService.getAllTopics());
}

/**
* get Topic with ID
* @param id
* @return
*/
@RequestMapping("/topic/{id}")
public Topic getTopicWithID(@PathVariable String id) {
return topicService.getTopicWithId(id);
@GetMapping("/topic/{id}")
public ResponseEntity<Topic> getTopicWithID(@PathVariable String id) {
return ResponseEntity.ok(topicService.getTopicWithId(id));
}

/**
* Add a new topic in list
* @param topic
*/
@RequestMapping(method = RequestMethod.POST, value = "/topic")
public void addTopic(@RequestBody Topic topic) {
@PostMapping("/topic")
public ResponseEntity<Void> addTopic(@Valid @RequestBody Topic topic) {
topicService.addTopic(topic);
return ResponseEntity.status(HttpStatus.CREATED).build();
}

/**
* Update Topic in List with id
* @param id
* @param topic
*/
@RequestMapping(method = RequestMethod.PUT, value = "/topic/{id}")
public void updateTopic(@PathVariable String id, @RequestBody Topic topic) {
@PutMapping("/topic/{id}")
public ResponseEntity<Void> updateTopic(@PathVariable String id, @Valid @RequestBody Topic topic) {
topicService.updateTopic(id, topic);
return ResponseEntity.ok().build();
}


/**
* Delete a topic with ID
* @param id
*/
@RequestMapping(method = RequestMethod.DELETE, value = "/topic/{id}")
public void deleteTopic(@PathVariable String id) {
@DeleteMapping("/topic/{id}")
public ResponseEntity<Void> deleteTopic(@PathVariable String id) {
topicService.deleteTopic(id);
return ResponseEntity.noContent().build();
}

/**
* Get all topics with Id length greater then minimum length
* @param minLength
* @return
*/
@RequestMapping(value = "/topic/minimum/length/{minLength}")
public List<Topic> filterMinimumLengthForId(@PathVariable Integer minLength) {
return topicService.filterMinimumLengthForId(minLength);
@GetMapping("/topic/minimum/length/{minLength}")
public ResponseEntity<List<Topic>> filterMinimumLengthForId(@PathVariable Integer minLength) {
return ResponseEntity.ok(topicService.filterMinimumLengthForId(minLength));
}


/**
* Sort with Id
* @return
*/
@RequestMapping("/topic/sort")
public List<Topic> sortTopicsWithID() {
return topicService.sortTopicsWithID();
@GetMapping("/topic/sort")
public ResponseEntity<List<Topic>> sortTopicsWithID() {
return ResponseEntity.ok(topicService.sortTopicsWithID());
}


Expand Down
1 change: 1 addition & 0 deletions src/main/java/hello/declaration/CustomPredicate.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Use as a Functional Interface
* @param <T>
*/
@FunctionalInterface
public interface CustomPredicate <T>{
boolean test(T t);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package hello.model;

import hello.declaration.TimeClient;
package hello.declaration;

import java.time.LocalDate;
import java.time.LocalDateTime;
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/hello/exception/GlobalExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package hello.exception;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler {

private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);

@ExceptionHandler(TopicNotFoundException.class)
public ResponseEntity<Map<String, Object>> handleTopicNotFound(TopicNotFoundException ex) {
return buildResponse(HttpStatus.NOT_FOUND, ex.getMessage());
}

@ExceptionHandler(Exception.class)
public ResponseEntity<Map<String, Object>> handleGenericException(Exception ex) {
log.error("Unhandled exception", ex);
return buildResponse(HttpStatus.INTERNAL_SERVER_ERROR, ex.getMessage());
}

private ResponseEntity<Map<String, Object>> buildResponse(HttpStatus status, String message) {
Map<String, Object> body = new HashMap<>();
body.put("timestamp", LocalDateTime.now().toString());
body.put("status", status.value());
body.put("error", status.getReasonPhrase());
body.put("message", message);
return ResponseEntity.status(status).body(body);
}
}
12 changes: 12 additions & 0 deletions src/main/java/hello/exception/TopicNotFoundException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package hello.exception;

public class TopicNotFoundException extends RuntimeException {

public TopicNotFoundException(String message) {
super(message);
}

public TopicNotFoundException(String message, Throwable cause) {
super(message, cause);
}
}
Loading