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
31 changes: 31 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Maven
target/
*.jar
*.war
*.ear
*.zip
*.tar.gz

# IDE
.idea/
.vscode/
*.iml
*.iws
*.ipr
.eclipse
.classpath
.project
.settings/

# OS
.DS_Store
Thumbs.db

# Dependencies
*.class
*.log

# Spring Boot
logs/
*.log.*

54 changes: 54 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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
http://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.2.0</version>
<relativePath/>
</parent>

<groupId>com.ironhack</groupId>
<artifactId>springboot-fundamentals</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBoot Fundamentals</name>
<description>Lab for SpringBoot Fundamentals</description>

<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,14 @@
package com.ironhack.springbootfundamentals;

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

@SpringBootApplication
public class SpringBootFundamentalsApplication {

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

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.ironhack.springbootfundamentals.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 greetByName(@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;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.ironhack.springbootfundamentals.controller;

import com.ironhack.springbootfundamentals.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 Map<String, String> getTime() {
Map<String, String> response = new HashMap<>();
response.put("time", timeService.getCurrentTime());
return response;
}

@GetMapping("/date")
public Map<String, String> getDate() {
Map<String, String> response = new HashMap<>();
response.put("date", timeService.getCurrentDate());
return response;
}

@GetMapping("/day")
public Map<String, String> getDay() {
Map<String, String> response = new HashMap<>();
response.put("day", timeService.getCurrentDayOfWeek());
return response;
}

@GetMapping("/all")
public Map<String, String> getAllTimeInfo() {
Map<String, String> response = new HashMap<>();
response.put("time", timeService.getCurrentTime());
response.put("date", timeService.getCurrentDate());
response.put("day", timeService.getCurrentDayOfWeek());
return response;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.ironhack.springbootfundamentals.controller;

import com.ironhack.springbootfundamentals.service.WeatherService;
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 WeatherController {

private final WeatherService weatherService;

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

@GetMapping("/weather/temperature")
public Map<String, Object> getTemperature() {
Map<String, Object> response = new HashMap<>();
response.put("temperature", weatherService.getCurrentTemperature());
response.put("unit", "Celsius");
return response;
}

@GetMapping("/weather/condition")
public Map<String, Object> getCondition() {
Map<String, Object> response = new HashMap<>();
response.put("condition", weatherService.getWeatherCondition());
return response;
}

@GetMapping("/weather/wind")
public Map<String, Object> getWindSpeed() {
Map<String, Object> response = new HashMap<>();
response.put("windSpeed", weatherService.getWindSpeed());
response.put("unit", "km/h");
return response;
}

@GetMapping("/weather/all")
public Map<String, Object> getAllWeatherInfo() {
Map<String, Object> response = new HashMap<>();
response.put("temperature", weatherService.getCurrentTemperature());
response.put("temperatureUnit", "Celsius");
response.put("condition", weatherService.getWeatherCondition());
response.put("windSpeed", weatherService.getWindSpeed());
response.put("windSpeedUnit", "km/h");
return response;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.ironhack.springbootfundamentals.service;

import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.time.LocalDate;
import java.time.DayOfWeek;
import java.time.format.DateTimeFormatter;

@Service
public class TimeService {

public String getCurrentTime() {
LocalDateTime now = LocalDateTime.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HH:mm:ss");
return now.format(formatter);
}

public String getCurrentDate() {
LocalDate today = LocalDate.now();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
return today.format(formatter);
}

public String getCurrentDayOfWeek() {
LocalDate today = LocalDate.now();
DayOfWeek dayOfWeek = today.getDayOfWeek();
return dayOfWeek.toString();
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.ironhack.springbootfundamentals.service;

import org.springframework.stereotype.Service;
import java.util.Random;

@Service
public class WeatherService {

private final Random random = new Random();

public int getCurrentTemperature() {
return random.nextInt(51) - 10; // Rango entre -10 y 40
}

public String getWeatherCondition() {
String[] conditions = {"Sunny", "Rainy", "Cloudy", "Windy"};
return conditions[random.nextInt(conditions.length)];
}

public int getWindSpeed() {
return random.nextInt(101); // Rango entre 0 y 100
}

}

4 changes: 4 additions & 0 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
spring.application.name=springboot-fundamentals
server.port=8080
spring.devtools.restart.enabled=true