diff --git a/src/main/java/com/workshop/labspringbootfundamentals/LabSpringBootFundamentalsApplication.java b/src/main/java/com/workshop/labspringbootfundamentals/LabSpringBootFundamentalsApplication.java new file mode 100644 index 0000000..e5318ca --- /dev/null +++ b/src/main/java/com/workshop/labspringbootfundamentals/LabSpringBootFundamentalsApplication.java @@ -0,0 +1,13 @@ +package com.workshop.labspringbootfundamentals; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class LabSpringBootFundamentalsApplication { + + public static void main(String[] args) { + SpringApplication.run(LabSpringBootFundamentalsApplication.class, args); + } + +} diff --git a/src/main/java/com/workshop/labspringbootfundamentals/controller/GreetingController.java b/src/main/java/com/workshop/labspringbootfundamentals/controller/GreetingController.java new file mode 100644 index 0000000..47e7b6c --- /dev/null +++ b/src/main/java/com/workshop/labspringbootfundamentals/controller/GreetingController.java @@ -0,0 +1,29 @@ +package com.workshop.labspringbootfundamentals.controller; + +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +@RestController + public class GreetingController { + + @GetMapping("/hello") + public String sayHello() { + return "Hello World!"; + } + + @GetMapping("/hello/{name}") + public String sayHelloName(@PathVariable String name) { + return "Hello " + name + "!"; + } + + @GetMapping("/add/{num1}/{num2}") + public int add(@PathVariable int num1, @PathVariable int num2) { + return num1 + num2; + } + + @GetMapping("/multiply/{num1}/{num2}") + public int multiply(@PathVariable int num1, @PathVariable int num2) { + return num1 * num2; + } + } diff --git a/src/main/java/com/workshop/labspringbootfundamentals/controller/TimeController.java b/src/main/java/com/workshop/labspringbootfundamentals/controller/TimeController.java new file mode 100644 index 0000000..dbce9da --- /dev/null +++ b/src/main/java/com/workshop/labspringbootfundamentals/controller/TimeController.java @@ -0,0 +1,38 @@ +package com.workshop.labspringbootfundamentals.controller; + +import com.workshop.labspringbootfundamentals.model.Time; +import com.workshop.labspringbootfundamentals.service.TimeService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping +public class TimeController { + + private final TimeService timeService; + + public TimeController(TimeService timeService) { + this.timeService = timeService; + } + + @GetMapping("/time") + public String getTime() { + return timeService.getTime(); + } + + @GetMapping("/date") + public String getDate() { + return timeService.getDate(); + } + + @GetMapping("/day") + public String getDay() { + return timeService.getCurrentDayOfWeek(); + } + + @GetMapping("/all") + public String getAll() { + return timeService.getTime() + timeService.getDate() + timeService.getCurrentDayOfWeek(); + } + } \ No newline at end of file diff --git a/src/main/java/com/workshop/labspringbootfundamentals/controller/WeatherController.java b/src/main/java/com/workshop/labspringbootfundamentals/controller/WeatherController.java new file mode 100644 index 0000000..982a5a0 --- /dev/null +++ b/src/main/java/com/workshop/labspringbootfundamentals/controller/WeatherController.java @@ -0,0 +1,38 @@ +package com.workshop.labspringbootfundamentals.controller; + +import com.workshop.labspringbootfundamentals.model.Weather; +import com.workshop.labspringbootfundamentals.service.WeatherService; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/weather") +public class WeatherController { + + private final WeatherService weatherService; + + public WeatherController(WeatherService weatherService) { + this.weatherService = weatherService; + } + + @GetMapping("/temperature") + public int getTemperature() { + return weatherService.getTemperature(); + } + + @GetMapping("/condition") + public String getCondition() { + return weatherService.getCondition(); + } + + @GetMapping("/wind") + public int getWindSpeed() { + return weatherService.getWindSpeed(); + } + + @GetMapping("/all") + public Weather getAll(){ + return weatherService.getAll(); + } + } diff --git a/src/main/java/com/workshop/labspringbootfundamentals/model/Time.java b/src/main/java/com/workshop/labspringbootfundamentals/model/Time.java new file mode 100644 index 0000000..1073c76 --- /dev/null +++ b/src/main/java/com/workshop/labspringbootfundamentals/model/Time.java @@ -0,0 +1,26 @@ +package com.workshop.labspringbootfundamentals.model; + +public class Time { + + private String time; + private String date; + private String day; + + public Time(String time, String date, String day) { + this.time = time; + this.date = date; + this.day = day; + } + + public String getTime() { + return time; + } + + public String getDate() { + return date; + } + + public String getDay() { + return day; + } +} \ No newline at end of file diff --git a/src/main/java/com/workshop/labspringbootfundamentals/model/Weather.java b/src/main/java/com/workshop/labspringbootfundamentals/model/Weather.java new file mode 100644 index 0000000..23914bd --- /dev/null +++ b/src/main/java/com/workshop/labspringbootfundamentals/model/Weather.java @@ -0,0 +1,37 @@ +package com.workshop.labspringbootfundamentals.model; + +public class Weather { + private int temperature; + private String condition; + private int windspeed; + + public Weather(int temperature, String condition, int speed) { + this.temperature = temperature; + this.condition = condition; + this.windspeed = speed; + } + + public int getTemperature() { + return temperature; + } + + public void setTemperature(int temperature) { + this.temperature = temperature; + } + + public String getCondition() { + return condition; + } + + public void setCondition(String condition) { + this.condition = condition; + } + + public int getWindspeed() { + return windspeed; + } + + public void setWindspeed(int windspeed) { + this.windspeed = windspeed; + } +} \ No newline at end of file diff --git a/src/main/java/com/workshop/labspringbootfundamentals/service/TimeService.java b/src/main/java/com/workshop/labspringbootfundamentals/service/TimeService.java new file mode 100644 index 0000000..8463a41 --- /dev/null +++ b/src/main/java/com/workshop/labspringbootfundamentals/service/TimeService.java @@ -0,0 +1,22 @@ +package com.workshop.labspringbootfundamentals.service; + +import org.springframework.stereotype.Service; + +import java.time.LocalDate; +import java.time.LocalTime; + +@Service +public class TimeService { + + public String getTime() { + return LocalTime.now().toString(); + } + + public String getDate() { + return LocalDate.now().toString(); + } + + public String getCurrentDayOfWeek() { + return LocalDate.now().getDayOfWeek().toString(); + } +} \ No newline at end of file diff --git a/src/main/java/com/workshop/labspringbootfundamentals/service/WeatherService.java b/src/main/java/com/workshop/labspringbootfundamentals/service/WeatherService.java new file mode 100644 index 0000000..94330c2 --- /dev/null +++ b/src/main/java/com/workshop/labspringbootfundamentals/service/WeatherService.java @@ -0,0 +1,45 @@ +package com.workshop.labspringbootfundamentals.service; + +import com.workshop.labspringbootfundamentals.model.Weather; +import org.springframework.stereotype.Service; + +import java.util.Random; + +@Service +public class WeatherService { + + private final Random random = new Random(); + + private int temp = random.nextInt(-10, 41); + private int condition = random.nextInt(0, 4); + private int windSpeed = random.nextInt(0, 101); + + public int getTemperature() { + return temp; + } + + public String getCondition() { + + if (condition == 0) { + return "Sunny"; + } else if (condition == 1) { + return "Rainy"; + } else if (condition == 2) { + return "Cloudy"; + } else { + return "Windy"; + } + } + + public int getWindSpeed() { + return windSpeed; + } + + public Weather getAll() { + return new Weather( + getTemperature(), + getCondition(), + getWindSpeed() + ); + } +} \ No newline at end of file diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties new file mode 100644 index 0000000..e7ead59 --- /dev/null +++ b/src/main/resources/application.properties @@ -0,0 +1 @@ +spring.application.name=LabSpringBootFundamentals diff --git a/src/test/java/com/workshop/labspringbootfundamentals/LabSpringBootFundamentalsApplicationTests.java b/src/test/java/com/workshop/labspringbootfundamentals/LabSpringBootFundamentalsApplicationTests.java new file mode 100644 index 0000000..d92129c --- /dev/null +++ b/src/test/java/com/workshop/labspringbootfundamentals/LabSpringBootFundamentalsApplicationTests.java @@ -0,0 +1,13 @@ +package com.workshop.labspringbootfundamentals; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class LabSpringBootFundamentalsApplicationTests { + + @Test + void contextLoads() { + } + +}