Skip to content

Instantly share code, notes, and snippets.

@panayotkulchev
Created April 29, 2015 10:56
Show Gist options
  • Save panayotkulchev/e2d9aa55bc5c922ccd76 to your computer and use it in GitHub Desktop.
Save panayotkulchev/e2d9aa55bc5c922ccd76 to your computer and use it in GitHub Desktop.
Class
import adapter.db.ConnectionProvider;
import adapter.db.JdbcConnectionProvider;
import adapter.db.PersistentFundsRepository;
import adapter.db.PersistentSessionRepository;
import adapter.db.PersistentUserRepository;
import adapter.http.ConnectionFilter;
import adapter.http.LoginFilter;
import adapter.http.SecurityFilter;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.servlet.GuiceServletContextListener;
import com.google.inject.servlet.ServletModule;
import com.google.sitebricks.SitebricksModule;
import core.FundsRepository;
import core.SessionRepository;
import core.UserRepository;
import pages.Deposit;
import pages.Login;
import pages.Logout;
import pages.Menu;
import pages.Register;
import pages.Welcome;
import pages.Withdraw;
/**
* Created by Panayot Kulchev on 15-4-27.
* e-mail: [email protected]
* happy coding ...
*/
public class AppConfig extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
return Guice.createInjector(
new ServletModule() {
@Override
protected void configureServlets() {
filter("/*").through(ConnectionFilter.class);
filter("/login").through(LoginFilter.class);
filter("/welcome").through(SecurityFilter.class);
filter("/withdraw").through(SecurityFilter.class);
filter("/deposit").through(SecurityFilter.class);
filter("/menu").through(SecurityFilter.class);
}
},
new SitebricksModule() {
@Override
protected void configureSitebricks() {
at("/register").show(Register.class);
at("/login").show(Login.class);
at("/welcome").show(Welcome.class);
at("/deposit").show(Deposit.class);
at("/withdraw").show(Withdraw.class);
at("/logout").show(Logout.class);
at("/menu").show(Menu.class);
embed(Menu.class).as("Hello");
}
},
new AbstractModule() {
@Override
protected void configure() {
bind(ConnectionProvider.class).to(JdbcConnectionProvider.class);
bind(SessionRepository.class).to(PersistentSessionRepository.class);
bind(UserRepository.class).to(PersistentUserRepository.class);
bind(FundsRepository.class).to(PersistentFundsRepository.class);
}
});
}
}
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title>Deposit funds</title>
<link rel="stylesheet" href="css/main.css"/>
</head>
<body style="background-color: #f0f0f0">
<div id="includedContent"></div>
<div id="container-wide">
<!--<p>${message}</p>-->
<div align="center">
<h6 style="color: #5e5e5e">DEPOSIT FUNDS</h6>
<form action="/deposit" method="post">
<input type="text" name="amount" placeholder="0"/>
<p></p>
<input type="submit" value="DEPOSIT"/>
</form>
</div>
</div>
<div>
@Hello
<span>
This text is replaced by the embed.
</span>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(function () {
$("#includedContent").load("/menu");
});
</script>
</body>
</html>
package pages;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.sitebricks.Show;
import com.google.sitebricks.http.Get;
import com.google.sitebricks.rendering.EmbedAs;
import core.FundsRepository;
import core.SessionRepository;
import core.SidProvider;
import core.UserRepository;
import javax.servlet.http.HttpServletRequest;
/**
* Created by Panayot Kulchev on 15-4-29.
* e-mail: [email protected]
* happy coding ...
*/
@Show("menu.html")
@EmbedAs("Hello")
public class Menu {
public Integer onlineUsersNumber = 0;
public Integer userAmount = 0;
public String userEmail = "toBeImplemented";
private SessionRepository sessionRepository;
private FundsRepository fundsRepository;
private Provider<HttpServletRequest> requestProvider;
private UserRepository userRepository;
@Inject
public Menu(SessionRepository sessionRepository,
FundsRepository fundsRepository,
Provider<HttpServletRequest> requestProvider,
UserRepository userRepository) {
this.sessionRepository = sessionRepository;
this.fundsRepository = fundsRepository;
this.requestProvider = requestProvider;
this.userRepository = userRepository;
}
@Get
private void doGet() {
HttpServletRequest request = requestProvider.get();
String sid = SidProvider.getSid(request);
Integer userId = sessionRepository.getUserIdBySid(sid);
onlineUsersNumber = sessionRepository.count();
userAmount = fundsRepository.getAmount(userId);
userEmail = userRepository.getById(userId).email;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment