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
60 changes: 60 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.4.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ironhack</groupId>
<artifactId>springBootApp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springBootApp</name>
<description>Demo project for Spring Boot</description>
<url/>
<licenses>
<license/>
</licenses>
<developers>
<developer/>
</developers>
<scm>
<connection/>
<developerConnection/>
<tag/>
<url/>
</scm>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.ironhack.springBootApp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SpringBootAppApplication {

public static void main(String[] args) {
SpringApplication.run(SpringBootAppApplication.class, args);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.ironhack.springBootApp.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

@RequestMapping("/hello")
public String hello() {
return "Hello World!";
}

// Endpoint: /hello/{name}
@RequestMapping("/hello/{name}")
public String helloName(@PathVariable String name) {
return "Hello " + name + "!";
}

// Endpoint: /add/{num1}/{num2}
@RequestMapping("/add/{num1}/{num2}")
public String add(@PathVariable int num1, @PathVariable int num2) {
int sum = num1 + num2;
return "Sum: " + sum;
}

// Endpoint: /multiply/{num1}/{num2}
@RequestMapping("/multiply/{num1}/{num2}")
public String multiply(@PathVariable int num1, @PathVariable int num2) {
int product = num1 * num2;
return "Product: " + product;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.ironhack.springBootApp.controller;

import com.ironhack.springBootApp.service.TimeService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class TimeController {

private final TimeService timeService;

public TimeController(TimeService timeService) {
this.timeService = timeService;
}

@GetMapping("/time")
public String getTime() {
return "Current Time: " + timeService.getCurrentTime().toString();
}

@GetMapping("/date")
public String getDate() {
return "Current Date: " + timeService.getCurrentDate().toString();
}

@GetMapping("/day")
public String getDayOfWeek() {
return "Today is: " + timeService.getCurrentDayOfWeek().toString();
}

@GetMapping("/all")
public Map<String, String> getAllTimeInfo() {
Map<String, String> timeInfo = new HashMap<>();
timeInfo.put("time", timeService.getCurrentTime().toString());
timeInfo.put("date", timeService.getCurrentDate().toString());
timeInfo.put("dayOfWeek", timeService.getCurrentDayOfWeek().toString());
return timeInfo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package com.ironhack.springBootApp.controller;

import com.ironhack.springBootApp.service.WeatherService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("weather")
public class WeatherController {
private final WeatherService weatherService;

// Constructor injection
public WeatherController(WeatherService weatherService) {
this.weatherService = weatherService;
}

@GetMapping("/temperature")
public int getCurrentTemperature() {
return weatherService.getCurrentTemperature();
}

@GetMapping("/condition")
public String getWeatherCondition() {
return weatherService.getWeatherCondition();
}

@GetMapping("/wind")
public int getWindSpeed() {
return weatherService.getWindSpeed();
}

@GetMapping("/all")
public Map<String, Object> getAllWeatherInfo() {
Map<String, Object> weatherInfo = new HashMap<>();
weatherInfo.put("temperature", weatherService.getCurrentTemperature());
weatherInfo.put("condition", weatherService.getWeatherCondition());
weatherInfo.put("windSpeed", weatherService.getWindSpeed());
return weatherInfo;
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/ironhack/springBootApp/service/TimeService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.ironhack.springBootApp.service;

import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;

@Service
public class TimeService {
public LocalTime getCurrentTime() {
return LocalTime.now();
}

// Get current date
public LocalDate getCurrentDate() {
return LocalDate.now();
}

// Get current day of the week
public DayOfWeek getCurrentDayOfWeek() {
return LocalDate.now().getDayOfWeek();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.ironhack.springBootApp.service;

import org.springframework.stereotype.Service;

import java.util.Random;

@Service
public class WeatherService {

private final Random random = new Random();

// Method to get current temperature (random between -10 and 40)
public int getCurrentTemperature() {
return random.nextInt(51) - 10; // Generates a number between -10 and 40
}

// Method to get weather condition (random from list)
public String getWeatherCondition() {
String[] conditions = {"Sunny", "Rainy", "Cloudy", "Windy"};
int index = random.nextInt(conditions.length);
return conditions[index];
}

// Method to get wind speed (random between 0 and 100)
public int getWindSpeed() {
return random.nextInt(101); // Generates a number between 0 and 100
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
spring.application.name=springBootApp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.ironhack.springBootApp;

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringBootAppApplicationTests {

@Test
void contextLoads() {
}

}