Taxi fare refactor (#2328)
This commit is contained in:
parent
7f22e3dc35
commit
e8df7f8116
@ -1,32 +0,0 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>org.baeldung.springboot.rest</groupId>
|
||||
<artifactId>difference-uri-url-rest</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>war</packaging>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.5.2.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</project>
|
@ -1,21 +0,0 @@
|
||||
package com.baeldung.springboot.rest;
|
||||
|
||||
public class Greeting {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Integer id = null;
|
||||
private String content = null;
|
||||
|
||||
public Greeting(Integer id) {
|
||||
this.id = id;
|
||||
this.content = "Hello World";
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
package com.baeldung.springboot.rest;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController("/")
|
||||
@Component
|
||||
public class GreetingController {
|
||||
|
||||
private final AtomicLong counter = new AtomicLong();
|
||||
|
||||
@RequestMapping(value = "/greetings", method = RequestMethod.GET)
|
||||
public Greeting greeting() {
|
||||
|
||||
return new Greeting(new Integer((int) counter.incrementAndGet()));
|
||||
}
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package com.baeldung.springboot.rest;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.web.support.SpringBootServletInitializer;
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@SpringBootApplication
|
||||
public class SpringBootWebApplication extends SpringBootServletInitializer {
|
||||
|
||||
// method for explicit deployment on Application Server
|
||||
@Override
|
||||
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
|
||||
return application.sources(SpringBootWebApplication.class);
|
||||
}
|
||||
|
||||
// run it as standalone JAVA application
|
||||
public static void main(String[] args) throws Exception {
|
||||
SpringApplication.run(SpringBootWebApplication.class, args);
|
||||
}
|
||||
|
||||
//Samples
|
||||
// http://localhost:8080/greetings
|
||||
// http://localhost:8989/difference-uri-url-rest/greetings
|
||||
}
|
@ -1,27 +0,0 @@
|
||||
package com.baeldung.springboot.rest.client;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
public class ApplicationClient {
|
||||
//private static final Logger log = LoggerFactory.getLogger(ApplicationClient.class);
|
||||
final static String URI_STRING = "http://localhost:8080/difference-uri-url-rest/greetings";
|
||||
|
||||
|
||||
public ApplicationClient() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Greeting init() {
|
||||
RestTemplate restTemplate = new RestTemplate();
|
||||
Greeting greeting = restTemplate.getForObject(ApplicationClient.URI_STRING, Greeting.class);
|
||||
//log.info(greeting.toString());
|
||||
return greeting;
|
||||
}
|
||||
public static void main(String args[]) {
|
||||
Greeting greeting = new ApplicationClient().init();
|
||||
System.out.println(greeting.toString());
|
||||
}
|
||||
|
||||
}
|
@ -1,33 +0,0 @@
|
||||
package com.baeldung.springboot.rest.client;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Greeting implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private Integer id = null;
|
||||
private String content = null;
|
||||
|
||||
/** Default constructor is mandatory for client */
|
||||
public Greeting() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Greeting(Integer id) {
|
||||
this.id = id;
|
||||
this.content = "Hello World";
|
||||
}
|
||||
|
||||
public Integer getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Id: " + getId().toString() + " Content: " + getContent();
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package com.baeldung.springboot.rest.test;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import com.baeldung.springboot.rest.client.Greeting;
|
||||
|
||||
public class DifferenceURIURLRESTTest {
|
||||
final static String URI_STRING = "http://localhost:8080/difference-uri-url-rest/greetings";
|
||||
static RestTemplate restTemplate;
|
||||
Greeting greeting;
|
||||
|
||||
@BeforeClass
|
||||
public static void setupTest() {
|
||||
restTemplate = new RestTemplate();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenRestTenplate_whenIsNotNull_thenSuccess() {
|
||||
assertNotNull("Rest Template not null", restTemplate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWiredConstructorParam_whenIsNotNull_thenSuccess() {
|
||||
greeting = restTemplate.getForObject(URI_STRING, Greeting.class);
|
||||
assertNotNull("Greeting class is not null", greeting);
|
||||
}
|
||||
|
||||
}
|
@ -17,7 +17,7 @@ public class DataProducerController {
|
||||
return "Hello world";
|
||||
}
|
||||
|
||||
@GetMapping(value = "/get-image")
|
||||
@GetMapping("/get-image")
|
||||
public @ResponseBody byte[] getImage() throws IOException {
|
||||
final InputStream in = getClass().getResourceAsStream("/com/baeldung/produceimage/image.jpg");
|
||||
return IOUtils.toByteArray(in);
|
||||
|
@ -14,5 +14,4 @@ public class Application extends SpringBootServletInitializer {
|
||||
public static void main(final String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -14,11 +14,11 @@ import com.baeldung.web.log.util.RequestLoggingUtil;
|
||||
@Component
|
||||
public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter {
|
||||
|
||||
Logger LOGGER = LoggerFactory.getLogger(TaxiFareRequestInterceptor.class);
|
||||
private final static Logger LOGGER = LoggerFactory.getLogger(TaxiFareRequestInterceptor.class);
|
||||
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
||||
String postData = null;
|
||||
String postData;
|
||||
HttpServletRequest requestCacheWrapperObject = null;
|
||||
try {
|
||||
// Uncomment to produce the stream closed issue
|
||||
|
@ -1,18 +1,17 @@
|
||||
package com.baeldung.web.log.config;
|
||||
|
||||
import com.baeldung.web.log.app.TaxiFareRequestInterceptor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
|
||||
|
||||
import com.baeldung.web.log.app.TaxiFareRequestInterceptor;
|
||||
|
||||
@Configuration
|
||||
public class TaxiFareMVCConfig extends WebMvcConfigurerAdapter {
|
||||
|
||||
@Autowired
|
||||
private TaxiFareRequestInterceptor taxiFareRequestInterceptor;
|
||||
|
||||
|
||||
@Override
|
||||
public void addInterceptors(InterceptorRegistry registry) {
|
||||
registry.addInterceptor(taxiFareRequestInterceptor).addPathPatterns("/**/taxifare/**/");
|
||||
|
@ -14,8 +14,9 @@ import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import com.baeldung.web.log.data.RateCard;
|
||||
import com.baeldung.web.log.data.TaxiRide;
|
||||
import com.baeldung.web.log.service.TaxiFareCalculatorService;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Controller
|
||||
@RestController
|
||||
public class TaxiFareController {
|
||||
|
||||
@Autowired
|
||||
@ -23,15 +24,13 @@ public class TaxiFareController {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(TaxiFareController.class);
|
||||
|
||||
@GetMapping(value = "/taxifare/get/")
|
||||
@ResponseBody
|
||||
@GetMapping("/taxifare/get/")
|
||||
public RateCard getTaxiFare() {
|
||||
LOGGER.debug("getTaxiFare() - START");
|
||||
return new RateCard();
|
||||
}
|
||||
|
||||
@PostMapping(value = "/taxifare/calculate/")
|
||||
@ResponseBody
|
||||
@PostMapping("/taxifare/calculate/")
|
||||
public String calculateTaxiFare(@RequestBody @Valid TaxiRide taxiRide) {
|
||||
LOGGER.debug("calculateTaxiFare() - START");
|
||||
String totalFare = taxiFareCalculatorService.calculateFare(taxiRide);
|
||||
|
@ -4,25 +4,28 @@ public class RateCard {
|
||||
|
||||
private String nightSurcharge;
|
||||
private String ratePerMile;
|
||||
|
||||
public RateCard(){
|
||||
nightSurcharge="Extra $ 100";
|
||||
ratePerMile="$ 10 Per Mile";
|
||||
|
||||
public RateCard() {
|
||||
nightSurcharge = "Extra $ 100";
|
||||
ratePerMile = "$ 10 Per Mile";
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public String getNightSurcharge() {
|
||||
return nightSurcharge;
|
||||
}
|
||||
|
||||
public void setNightSurcharge(String nightSurcharge) {
|
||||
this.nightSurcharge = nightSurcharge;
|
||||
}
|
||||
|
||||
public String getRatePerMile() {
|
||||
return ratePerMile;
|
||||
}
|
||||
|
||||
public void setRatePerMile(String ratePerMile) {
|
||||
this.ratePerMile = ratePerMile;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -5,14 +5,15 @@ public class TaxiRide {
|
||||
private Boolean isNightSurcharge;
|
||||
private Long distanceInMile;
|
||||
|
||||
public TaxiRide(){}
|
||||
|
||||
public TaxiRide(Boolean isNightSurcharge, Long distanceInMile){
|
||||
public TaxiRide() {
|
||||
}
|
||||
|
||||
public TaxiRide(Boolean isNightSurcharge, Long distanceInMile) {
|
||||
this.isNightSurcharge = isNightSurcharge;
|
||||
this.distanceInMile = distanceInMile;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public Boolean getIsNightSurcharge() {
|
||||
return isNightSurcharge;
|
||||
}
|
||||
|
@ -1,20 +1,14 @@
|
||||
package com.baeldung.web.log.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baeldung.web.log.data.TaxiRide;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class TaxiFareCalculatorService {
|
||||
|
||||
public String calculateFare(TaxiRide taxiRide) {
|
||||
Long fare = 0l;
|
||||
if (taxiRide.getIsNightSurcharge()) {
|
||||
fare = taxiRide.getDistanceInMile() * 10 + 100;
|
||||
} else {
|
||||
fare = taxiRide.getDistanceInMile() * 10;
|
||||
}
|
||||
return String.valueOf(fare);
|
||||
return String.valueOf((Long) (taxiRide.getIsNightSurcharge()
|
||||
? taxiRide.getDistanceInMile() * 10 + 100
|
||||
: taxiRide.getDistanceInMile() * 10));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -12,5 +12,4 @@ public class MainApplication extends WebMvcConfigurerAdapter {
|
||||
public static void main(final String[] args) {
|
||||
SpringApplication.run(MainApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
@ -1,11 +1,11 @@
|
||||
package org.baeldung.repository;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.baeldung.web.dto.HeavyResource;
|
||||
import org.baeldung.web.dto.HeavyResourceAddressOnly;
|
||||
|
||||
public class HeavyResourceRepository {
|
||||
import java.util.Map;
|
||||
|
||||
public class HeavyResourceRepository {
|
||||
|
||||
public void save(HeavyResource heavyResource) {
|
||||
}
|
||||
@ -21,6 +21,7 @@ public class HeavyResourceRepository {
|
||||
public void save(HeavyResource heavyResource, String id) {
|
||||
|
||||
}
|
||||
|
||||
public void save(HeavyResourceAddressOnly partialUpdate, String id) {
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user