-
Notifications
You must be signed in to change notification settings - Fork 79
g596.kozlova.task4 #324
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
Open
nkozlova
wants to merge
8
commits into
fediq:master
Choose a base branch
from
nkozlova:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
g596.kozlova.task4 #324
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
d6b0018
Homework 4
nkozlova a3334db
Fixed code style
nkozlova 56e57f2
Fixed code style
nkozlova ebaeb29
g596.kozlova.task4
nkozlova b4317e5
Fixed code style
nkozlova a85f6e5
Fixed comments
nkozlova c834538
Fixed code style
nkozlova 14f5a22
Fixed code style
nkozlova File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
...-g596-kozlova/src/main/java/ru/mipt/java2016/homework/g596/kozlova/task4/Application.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package ru.mipt.java2016.homework.g596.kozlova.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; | ||
|
|
||
| @EnableAutoConfiguration | ||
| @Configuration | ||
| @ComponentScan(basePackageClasses = Application.class) | ||
| public class Application { | ||
|
|
||
| @Bean | ||
| public Calculator calculator() { | ||
| return Calculator.INSTANCE; | ||
| } | ||
|
|
||
| @Bean | ||
| public EmbeddedServletContainerCustomizer customizer( | ||
| @Value("${ru.mipt.java2016.homework.g596.kozlova.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); | ||
| } | ||
| } |
159 changes: 159 additions & 0 deletions
159
...k-g596-kozlova/src/main/java/ru/mipt/java2016/homework/g596/kozlova/task4/BillingDao.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| package ru.mipt.java2016.homework.g596.kozlova.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.stereotype.Repository; | ||
| import ru.mipt.java2016.homework.base.task1.ParsingException; | ||
|
|
||
| import javax.annotation.PostConstruct; | ||
| import javax.sql.DataSource; | ||
| import java.sql.ResultSet; | ||
| import java.util.List; | ||
| import java.util.Arrays; | ||
| import java.util.Map; | ||
| import java.util.HashMap; | ||
|
|
||
| @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); | ||
| LOG.trace("Initializing"); | ||
| jdbcTemplate.execute("CREATE SCHEMA IF NOT EXISTS billing"); | ||
| jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS billing.users " + | ||
| "(username VARCHAR PRIMARY KEY, password VARCHAR, enabled BOOLEAN)"); | ||
| jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS billing.variables " + | ||
| "(username VARCHAR, name VARCHAR, value DOUBLE, expression VARCHAR)"); | ||
| jdbcTemplate.execute("CREATE TABLE IF NOT EXISTS billing.functions " + | ||
| "(username VARCHAR, name VARCHAR, arguments VARCHAR, expression VARCHAR)"); | ||
| createNewUser("userName", "password", true); | ||
| } | ||
|
|
||
| public boolean createNewUser(String userName, String password, boolean enabled) { | ||
| try { | ||
| loadUser(userName); | ||
| return false; | ||
| } catch (EmptyResultDataAccessException e) { | ||
| jdbcTemplate.update("INSERT INTO billing.users VALUES (?, ?, ?)", userName, password, enabled); | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| public BillingUser loadUser(String userName) throws EmptyResultDataAccessException { | ||
| LOG.trace("Querying for user " + userName); | ||
| return jdbcTemplate.queryForObject( | ||
| "SELECT username, password, enabled FROM billing.users WHERE username = ?", | ||
| new Object[]{userName}, | ||
| (ResultSet resultSet, int numberOfRow) -> { | ||
| return new BillingUser(resultSet.getString("userName"), resultSet.getString("password"), | ||
| resultSet.getBoolean("enabled")); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| public Variable getVariable(String userName, String variable) { | ||
| return jdbcTemplate.queryForObject( | ||
| "SELECT userName, name, value, expression FROM billing.variables WHERE userName = ? AND name = ?", | ||
| new Object[]{userName, variable}, | ||
| (ResultSet resultSet, int numberOfRow) -> { | ||
| return new Variable( | ||
| resultSet.getString("userName"), | ||
| resultSet.getString("name"), | ||
| resultSet.getDouble("value"), | ||
| resultSet.getString("expression")); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| public Map<String, String> getVariables(String userName) { | ||
| try { | ||
| return jdbcTemplate.queryForObject( | ||
| "SELECT userName, name, value, expression FROM billing.variables WHERE userName = ?", | ||
| new Object[]{userName}, | ||
| (ResultSet resultSet, int numberOfRow) -> { | ||
| Map<String, String> map = new HashMap<>(); | ||
| while (!resultSet.next()) { | ||
| map.put(resultSet.getString("name"), Double.toString(resultSet.getDouble("value"))); | ||
| } | ||
| return map; | ||
| } | ||
| ); | ||
| } catch (EmptyResultDataAccessException e) { | ||
| return new HashMap<>(); | ||
| } | ||
| } | ||
|
|
||
| public void deleteVariable(String userName, String name) throws ParsingException { | ||
| try { | ||
| getVariable(userName, name); | ||
| jdbcTemplate.update("DELETE FROM billing.variables WHERE userName = ? AND name = ?", userName, name); | ||
| } catch (EmptyResultDataAccessException e) { | ||
| throw new ParsingException("Can't delete"); | ||
| } | ||
| } | ||
|
|
||
| public void addVariable(String userName, String name, Double value, String expression) throws ParsingException { | ||
| jdbcTemplate.update("MERGE INTO billing.variables VALUES (?, ?, ?, ?)", userName, name, value, expression); | ||
| } | ||
|
|
||
| public Function getFunction(String userName, String function) { | ||
| return jdbcTemplate.queryForObject( | ||
| "SELECT userName, name, arguments, expression FROM billing.functions WHERE userName = ? AND name = ?", | ||
|
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. А где ты создаешь billing.functions? |
||
| new Object[]{userName, function}, | ||
| (ResultSet resultSet, int numberOfRow) -> { | ||
| String name = resultSet.getString("name"); | ||
| List<String> arguments = Arrays.asList(resultSet.getString("arguments").split(" ")); | ||
| String expression = resultSet.getString("expression"); | ||
| return new Function(userName, name, arguments, expression); | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| public Map<String, Function> getFunctions(String userName) { | ||
| try { | ||
| return jdbcTemplate.queryForObject( | ||
| "SELECT userName, name, arguments, expression FROM billing.functions WHERE userName = ?", | ||
| new Object[]{userName}, | ||
| (ResultSet resultSet, int numberOfRow) -> { | ||
| Map<String, Function> map = new HashMap<>(); | ||
| while (true) { | ||
| String name = resultSet.getString("name"); | ||
| List<String> arguments = Arrays.asList(resultSet.getString("arguments").split(" ")); | ||
| String expression = resultSet.getString("expression"); | ||
| map.put(name, new Function(userName, name, arguments, expression)); | ||
| if (!resultSet.next()) { | ||
| break; | ||
| } | ||
| } | ||
| return map; | ||
| } | ||
| ); | ||
| } catch (EmptyResultDataAccessException e) { | ||
| return new HashMap<>(); | ||
| } | ||
| } | ||
|
|
||
| public void deleteFunction(String userName, String name) throws ParsingException { | ||
| try { | ||
| getFunction(userName, name); | ||
| jdbcTemplate.update("DELETE FROM billing.functions WHERE userName = ? AND name = ?", userName, name); | ||
| } catch (EmptyResultDataAccessException e) { | ||
| throw new ParsingException("Can't delete"); | ||
| } | ||
| } | ||
|
|
||
| public void addFunction(String userName, String name, List<String> arguments, String expression) | ||
| throws ParsingException { | ||
| jdbcTemplate.update("MERGE INTO billing.functions VALUES (?, ?, ?, ?)", userName, name, arguments, expression); | ||
| } | ||
| } | ||
26 changes: 26 additions & 0 deletions
26
.../main/java/ru/mipt/java2016/homework/g596/kozlova/task4/BillingDatabaseConfiguration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package ru.mipt.java2016.homework.g596.kozlova.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; | ||
|
|
||
| @Configuration | ||
| public class BillingDatabaseConfiguration { | ||
| @Bean | ||
| public DataSource billingDataSource( | ||
| @Value("${ru.mipt.java2016.homework.g596.kozlova.task4.jdbcUrl}") String jdbcUrlAdress, | ||
| @Value("${ru.mipt.java2016.homework.g596.kozlova.task4.userName:}") String userName, | ||
| @Value("${ru.mipt.java2016.homework.g596.kozlova.task4.password:}") String password | ||
| ) { | ||
| HikariConfig config = new HikariConfig(); | ||
| config.setDriverClassName(org.h2.Driver.class.getName()); | ||
| config.setJdbcUrl(jdbcUrlAdress); | ||
| config.setUsername(userName); | ||
| config.setPassword(password); | ||
| return new HikariDataSource(config); | ||
| } | ||
| } |
53 changes: 53 additions & 0 deletions
53
...-g596-kozlova/src/main/java/ru/mipt/java2016/homework/g596/kozlova/task4/BillingUser.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package ru.mipt.java2016.homework.g596.kozlova.task4; | ||
|
|
||
| public class BillingUser { | ||
|
|
||
| private final String userName; | ||
| private final String password; | ||
| private final boolean enabled; | ||
|
|
||
| public BillingUser(String u, String p, boolean e) { | ||
| if (u.equals(null) || p.equals(null)) { | ||
| throw new IllegalArgumentException("Incorrect data"); | ||
| } | ||
| userName = u; | ||
| password = p; | ||
| enabled = e; | ||
| } | ||
|
|
||
| public String getUserName() { | ||
| return userName; | ||
| } | ||
|
|
||
| public String getPassword() { | ||
| return password; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "BillingUser{ userName='" + userName + "\', password='" + password + '\'' + ", enabled=" + enabled + "}"; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object obj) { | ||
| if (this == obj) { | ||
| return true; | ||
| } | ||
| if (obj == null || getClass() != obj.getClass()) { | ||
| return false; | ||
| } | ||
| BillingUser that = (BillingUser) obj; | ||
| if (!userName.equals(that.userName) || !password.equals(that.password) || enabled != that.enabled) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| int result = userName.hashCode(); | ||
| result = 31 * result + password.hashCode(); | ||
| result = 31 * result + (enabled ? 1 : 0); | ||
| return result; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Вообще есть merge. C ним можно обновлять существующие значения и добавлять новые - не нужно будет удалять