-
Notifications
You must be signed in to change notification settings - Fork 79
Spirin [group 597] - Task 4 #323
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
base: master
Are you sure you want to change the base?
Changes from all commits
b1d20ab
d665de2
bb3d94c
2e251a7
462bf09
8dc4a5a
132f443
2629413
a976d6d
282558d
d4506b8
9945a32
88e35f2
d069165
cd1f67a
68f1cae
cc4699a
0c2aa38
c437248
473aaef
69e58a3
e91dd4e
4516fe5
a6663e2
5ebb5fd
7a33651
6320c23
9d56185
e6879e0
ef122aa
4052bf7
a67ce89
52b3176
7392232
369e713
2a3669e
1a953ad
729dcf1
075ba37
16817b1
a56520d
663b34d
330a846
1e8f059
0ce6e72
97adc68
1ca0882
9babca2
7e190e2
e6a31f3
fe74464
661d46a
a94b977
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package ru.mipt.java2016.homework.g597.spirin.task4; | ||
|
|
||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.boot.Banner; | ||
| import org.springframework.boot.SpringApplication; | ||
| import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
| import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.ComponentScan; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| import ru.mipt.java2016.homework.base.task1.Calculator; | ||
|
|
||
| /** | ||
| * Created by whoami on 12/13/16. | ||
| */ | ||
|
|
||
| @EnableAutoConfiguration | ||
| @Configuration | ||
| @ComponentScan(basePackageClasses = Application.class) | ||
| public class Application { | ||
|
|
||
| @Bean | ||
| public Calculator calculator() { | ||
| return new Evaluator(); | ||
| } | ||
|
|
||
| @Bean | ||
| public EmbeddedServletContainerCustomizer customizer( | ||
| @Value("${ru.mipt.java2016.homework.g597.spirin.task4.httpPort:9001}") int port) { | ||
| return container -> container.setPort(port); | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| SpringApplication application = new SpringApplication(Application.class); | ||
| application.setBannerMode(Banner.Mode.OFF); | ||
| application.run(args); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,163 @@ | ||
| package ru.mipt.java2016.homework.g597.spirin.task4; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.dao.EmptyResultDataAccessException; | ||
| import org.springframework.jdbc.core.JdbcTemplate; | ||
| import org.springframework.jdbc.core.RowMapper; | ||
| import org.springframework.stereotype.Repository; | ||
|
|
||
| import javax.annotation.PostConstruct; | ||
| import javax.sql.DataSource; | ||
| import java.sql.ResultSet; | ||
| import java.sql.SQLException; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Created by whoami on 12/13/16. | ||
| */ | ||
|
|
||
| @Repository | ||
| public class BillingDao { | ||
| private static final Logger LOG = LoggerFactory.getLogger(BillingDao.class); | ||
|
|
||
| @Autowired | ||
| private DataSource dataSource; | ||
|
|
||
| private JdbcTemplate jdbcTemplate; | ||
|
|
||
| @PostConstruct | ||
| public void postConstruct() { | ||
| jdbcTemplate = new JdbcTemplate(dataSource, false); | ||
| initSchema(); | ||
| } | ||
|
|
||
| public void initSchema() { | ||
| LOG.info("Initializing schema"); | ||
| jdbcTemplate.execute("CREATE SCHEMA IF NOT EXISTS billing"); | ||
|
|
||
| // User table | ||
| jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS billing.users " + | ||
| "(username VARCHAR PRIMARY KEY, password VARCHAR)"); | ||
|
|
||
| // Update initial (username, password) | ||
| jdbcTemplate.execute("DELETE FROM billing.users WHERE username = 'username'"); | ||
| jdbcTemplate.execute("INSERT INTO billing.users VALUES ('username', 'password')"); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно было бы использовать |
||
|
|
||
| // Variable table | ||
| jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS billing.variables " + | ||
| "(username VARCHAR, variable VARCHAR, value FLOAT)"); | ||
|
|
||
| // Function table | ||
| jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS billing.functions " + | ||
| "(username VARCHAR, function VARCHAR, arity INTEGER, body VARCHAR)"); | ||
| } | ||
|
|
||
| // Load user from user table | ||
| public BillingUser loadUser(String username) throws EmptyResultDataAccessException { | ||
| LOG.info("Querying for user " + username); | ||
| return jdbcTemplate.queryForObject( | ||
| "SELECT username, password FROM billing.users WHERE username = ?", | ||
| new Object[]{username}, | ||
| new RowMapper<BillingUser>() { | ||
| @Override | ||
| public BillingUser mapRow(ResultSet rs, int rowNum) throws SQLException { | ||
| return new BillingUser( | ||
| rs.getString("username"), | ||
| rs.getString("password") | ||
| ); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| // Put user into user table | ||
| public void putUser(String username, String password) { | ||
| LOG.info("Putting user (username, password) into table"); | ||
| jdbcTemplate.execute("INSERT INTO billing.users VALUES ('" | ||
| + username + "', '" + password + "')"); | ||
| } | ||
|
|
||
| // Get value of variable of particular user | ||
| public Double getVariable(String username, String variable) throws EmptyResultDataAccessException { | ||
| LOG.info("Querying for variable " + variable + " of user " + username); | ||
| return jdbcTemplate.queryForObject( | ||
| "SELECT value FROM billing.variables WHERE username = '" + username + | ||
| "' AND variable = '" + variable + "'", | ||
| new Double[]{}, | ||
| new RowMapper<Double>() { | ||
| @Override | ||
| public Double mapRow(ResultSet rs, int rowNum) throws SQLException { | ||
| return new Double( | ||
| rs.getString("value").toString() | ||
| ); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| // Put variable of particular user | ||
| public void putVariable(String username, String variable, Double value) { | ||
| LOG.info("Putting variable " + variable + " of user " + username); | ||
| deleteVariable(username, variable); | ||
| jdbcTemplate.execute("INSERT INTO billing.variables VALUES ('" | ||
| + username + "', '" + variable + "', " + value.toString() + ")"); | ||
| } | ||
|
|
||
| // Delete variable of particular user | ||
| public void deleteVariable(String username, String variable) { | ||
| LOG.info("Deleting variable " + variable + " of user " + username); | ||
| jdbcTemplate.execute("DELETE FROM billing.variables WHERE username = '" | ||
| + username + "' AND variable = '" + variable + "'"); | ||
| } | ||
|
|
||
| // Get all variables from the service of particular user | ||
| public String[] getAllVariables(String username) { | ||
| LOG.info("Querying for all variables of user " + username); | ||
|
|
||
| List queryResult = jdbcTemplate.queryForList( | ||
| "SELECT variable FROM billing.variables WHERE username = '" + | ||
| username + "'"); | ||
|
|
||
| String[] variables = new String[queryResult.size()]; | ||
|
|
||
| for (int i = 0; i < queryResult.size(); ++i) { | ||
| variables[i] = queryResult.get(i).toString(); | ||
| } | ||
|
|
||
| return variables; | ||
| } | ||
|
|
||
| // Get function of particular user | ||
| public String getFunction(String username, String function) { | ||
| LOG.info("Querying for function " + function + " of user " + username); | ||
| return jdbcTemplate.queryForObject( | ||
| "SELECT body FROM billing.functions WHERE username = '" + username + | ||
| "' AND function = '" + function + "'", | ||
| new String[]{}, | ||
| new RowMapper<String>() { | ||
| @Override | ||
| public String mapRow(ResultSet rs, int rowNum) throws SQLException { | ||
| return rs.getString("body"); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| // Put function of particular user | ||
| public void putFunction(String username, String function, Integer arity, String body) { | ||
| LOG.info("Putting function " + function + " of user " + username); | ||
| deleteFunction(username, function); | ||
| jdbcTemplate.execute("INSERT INTO billing.functions VALUES ('" | ||
| + username + "', '" + function + "', " + arity.toString() + ", '" | ||
| + body + "')"); | ||
| } | ||
|
|
||
| // Delete function of particular user | ||
| public void deleteFunction(String username, String function) { | ||
| LOG.info("Deleting function " + function + " of user " + username); | ||
| jdbcTemplate.execute("DELETE FROM billing.functions WHERE username = '" + | ||
| username + "' AND function = '" + function + "'"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package ru.mipt.java2016.homework.g597.spirin.task4; | ||
|
|
||
| import com.zaxxer.hikari.HikariConfig; | ||
| import com.zaxxer.hikari.HikariDataSource; | ||
| import org.springframework.beans.factory.annotation.Value; | ||
| import org.springframework.context.annotation.Bean; | ||
| import org.springframework.context.annotation.Configuration; | ||
|
|
||
| import javax.sql.DataSource; | ||
|
|
||
| /** | ||
| * Created by whoami on 12/13/16. | ||
| */ | ||
|
|
||
| @Configuration | ||
| public class BillingDatabaseConfiguration { | ||
| @Bean | ||
| public DataSource billingDataSource( | ||
| @Value("jdbc:h2:mem:testdb") String jdbcUrl, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Можно создать в |
||
| @Value("${ru.mipt.java2016.homework.g597.spirin.task4.username:}") String username, | ||
| @Value("${ru.mipt.java2016.homework.g597.spirin.task4.password:}") String password | ||
| ) { | ||
| HikariConfig config = new HikariConfig(); | ||
| config.setDriverClassName(org.h2.Driver.class.getName()); | ||
| config.setJdbcUrl(jdbcUrl); | ||
| config.setUsername(username); | ||
| config.setPassword(password); | ||
| return new HikariDataSource(config); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| package ru.mipt.java2016.homework.g597.spirin.task4; | ||
|
|
||
| /** | ||
| * Created by whoami on 12/13/16. | ||
| */ | ||
|
|
||
| public class BillingUser { | ||
| private final String username; | ||
| private final String password; | ||
|
|
||
| public BillingUser(String username, String password) { | ||
| if (username == null) { | ||
| throw new IllegalArgumentException("Null username is not allowed"); | ||
| } | ||
| if (password == null) { | ||
| throw new IllegalArgumentException("Null password is not allowed"); | ||
| } | ||
| this.username = username; | ||
| this.password = password; | ||
| } | ||
|
|
||
| public String getUsername() { | ||
| return username; | ||
| } | ||
|
|
||
| public String getPassword() { | ||
| return password; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "BillingUser{" + | ||
| "username='" + username + '\'' + | ||
| ", password='" + password + '\'' + | ||
| '}'; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
|
|
||
| BillingUser that = (BillingUser) o; | ||
|
|
||
| if (!username.equals(that.username)) { | ||
| return false; | ||
| } | ||
| return password.equals(that.password); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int result = username.hashCode(); | ||
| result = 31 * result + password.hashCode(); | ||
| return result; | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ну хоть бы коммент удалил при копипасте :)