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 homework-g597-kirilenko/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
</parent>
<modelVersion>4.0.0</modelVersion>

<properties>
<spring.boot.version>1.4.2.RELEASE</spring.boot.version>
</properties>

<artifactId>homework-g597-kirilenko</artifactId>

<dependencies>
Expand All @@ -24,6 +28,33 @@
<version>1.0.0</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>2.5.1</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.193</version>
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,4 +221,4 @@ private boolean checkIncorrectExpression(String expres) {
return true;

}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package ru.mipt.java2016.homework.g597.kirilenko.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;

@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.trace("Initializing schema");
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.update("INSERT INTO billing.users VALUES ('username', 'password', 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},
new RowMapper<BillingUser>() {
@Override
public BillingUser mapRow(ResultSet rs, int rowNum) throws SQLException {
return new BillingUser(
rs.getString("username"),
rs.getString("password"),
rs.getBoolean("enabled")
);
}
}
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ru.mipt.java2016.homework.g597.kirilenko.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.g597.kirilenko.task4.username:}") String username,
@Value("${ru.mipt.java2016.homework.g597.kirilenko.task4.password:}") String password
) {
HikariConfig config = new HikariConfig();
config.setDriverClassName(org.h2.Driver.class.getName());
String jdbcUrl = "jdbc:h2:~/task4.db";
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,68 @@
package ru.mipt.java2016.homework.g597.kirilenko.task4;

public class BillingUser {
private final String username;
private final String password;
private final boolean enabled;

public BillingUser(String username, String password, boolean enabled) {
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;
this.enabled = enabled;
}

public String getUsername() {
return username;
}

public String getPassword() {
return password;
}

public boolean isEnabled() {
return enabled;
}

@Override
public String toString() {
return "BillingUser{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", enabled=" + enabled +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

BillingUser that = (BillingUser) o;

if (enabled != that.enabled) {
return false;
}
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();
result = 31 * result + (enabled ? 1 : 0);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package ru.mipt.java2016.homework.g597.kirilenko.task4;

import javafx.util.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import ru.mipt.java2016.homework.base.task1.ParsingException;

import java.util.ArrayList;

@RestController
public class CalculatorController {
private static final Logger LOG = LoggerFactory.getLogger(CalculatorController.class);
@Autowired
private MyCalculator calculator;


@RequestMapping(path = "/variable/{variableName}", method = RequestMethod.GET)
public String getVariable(@PathVariable String variableName) {
String result = calculator.getVariableExpression(variableName);
return result;
}

@RequestMapping(path = "/function/{functionName}", method = RequestMethod.GET)
public String getFunction(@PathVariable String functionName) {
Pair<ArrayList<String>, String> info = calculator.getFunctionInfo(functionName);
String result = info.getValue() + "&";
for (int i = 0; i < info.getKey().size(); i++) {
result += info.getKey().get(i);
if (i != info.getKey().size() - 1) {
result += ",";
}
}
return result + "\n";
}

@RequestMapping(path = "/variable/{variableName}", method = RequestMethod.PUT,
consumes = "text/plain", produces = "text/plain")
public void putVariable(@PathVariable String variableName,
@RequestBody String expr) throws ParsingException {
calculator.setVariableExpression(variableName, expr);
}

@RequestMapping(path = "/variable/{variableName}", method = RequestMethod.DELETE)
public void deleteVariable(@PathVariable String variableName) {
calculator.deleteVariable(variableName);
}

@RequestMapping(path = "/variable", method = RequestMethod.GET)
public ArrayList<String> getVariables() {
return calculator.getAllVariables();
}


@RequestMapping(path = "/function/{functionName}", method = RequestMethod.PUT)
public void putFunction(@PathVariable String functionName,
@RequestParam(value = "args") ArrayList<String> args,
@RequestBody String functionBody) throws ParsingException {
calculator.setFunction(functionName, new ArrayList<>(args), functionBody);
}

@RequestMapping(path = "/function/{functionName}", method = RequestMethod.DELETE)
public Boolean deleteFunction(@PathVariable String functionName) {
return calculator.deleteFunction(functionName);
}

@RequestMapping(path = "/function", method = RequestMethod.GET)
public ArrayList<String> getFunctionsNames() {
return calculator.getAllFunctions();
}

@RequestMapping(path = "/ping", method = RequestMethod.GET, produces = "text/plain")
public String echo() {
return "OK\n";
}

@RequestMapping(path = "/eval", method = RequestMethod.POST, consumes = "text/plain", produces = "text/plain")
public String eval(@RequestBody String expression) throws ParsingException {
double result = calculator.evaluateExpression(expression);
return Double.toString(result) + "\n";
}
}
Loading