Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Services, Controllers & View to display jokes. #52

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
48 changes: 29 additions & 19 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,23 +1,33 @@
# Compiled class file
*.class
HELP.md
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

# Log file
*.log
### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

# BlueJ files
*.ctxt
### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

# Mobile Tools for Java (J2ME)
.mtj.tmp/
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

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

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
### VS Code ###
.vscode/
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package guru.springframework.chucknorris.controllers;

import guru.springframework.chucknorris.services.JokeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class JokeController {

private final JokeService jokeService;

public JokeController(JokeService jokeService) {
this.jokeService = jokeService;
}

@RequestMapping({"/", ""})
public String showJoke(Model model) {
model.addAttribute("joke", jokeService.getJoke());

return "index";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package guru.springframework.chucknorris.services;

public interface JokeService {

String getJoke();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package guru.springframework.chucknorris.services;

import guru.springframework.norris.chuck.ChuckNorrisQuotes;
import org.springframework.stereotype.Service;

@Service
public class JokeServiceImpl implements JokeService {

private final ChuckNorrisQuotes chuckNorrisQuotes;

public JokeServiceImpl() {
this.chuckNorrisQuotes = new ChuckNorrisQuotes();
}

@Override
public String getJoke() {
return chuckNorrisQuotes.getRandomQuote();
}
}
11 changes: 11 additions & 0 deletions src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Chuck Norris Jokes</title>
</head>
<body>
<h1>Chuck Norris Joke</h1>
<span th:text="${joke}"></span>
</body>
</html>