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

Task 4 Kochukov #344

Open
wants to merge 3 commits into
base: master
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
33 changes: 33 additions & 0 deletions homework-g597-kochukov/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,46 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>homework-g597-kochukov</artifactId>
<properties>
<spring.boot.version>1.4.2.RELEASE</spring.boot.version>
</properties>

<dependencies>
<dependency>
<groupId>ru.mipt.java2016</groupId>
<artifactId>homework-base</artifactId>
<version>1.0.0</version>
</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>org.xerial</groupId>
<artifactId>sqlite-jdbc</artifactId>
<version>3.8.11.1</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>ru.mipt.java2016</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package ru.mipt.java2016.homework.g597.kochukov.task4;

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

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

public String getUsername() {
return username;
}

public String getPassword() {
return password;
}

public Integer getUserId() {
return userid;
}

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;
}
if (!userid.equals(that.userid)) {
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,152 @@
package ru.mipt.java2016.homework.g597.kochukov.task4;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import ru.mipt.java2016.homework.base.task1.ParsingException;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.LinkedHashMap;

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

private DBWorker dbWorker = DBWorker.getInstance();

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

@RequestMapping(path = "/", method = RequestMethod.GET, produces = "text/html")
public String main(@RequestParam(required = false) String name) {
if (name == null) {
name = "world";
}
return "<html>" +
"<head><title>tna0y_App</title></head>" +
"<body><h1>Hello, " + name + "!</h1></body>" +
"</html>";
}

@RequestMapping(path = "/eval", method = RequestMethod.POST, consumes = "text/plain", produces = "text/plain")
public String eval(Authentication authentication, @RequestBody String expr) throws ParsingException {
Integer userId = dbWorker.getUser(authentication.getName()).getUserId();
LOG.debug("Evaluation request: [" + expr + "]");
LinkedHashMap<String, Double> scope = dbWorker.getUserScope(userId).getResult();
Expression expression = new Expression(expr, scope);
double result;
try {
calculator.setUserid(userId);
result = calculator.calculate(expression);
LOG.trace("Result: " + result);
return Double.toString(result) + "\n";
} catch (SQLException sqle) {
LOG.trace("Failed due to unknown functions");
return "Using undeclared functions: " + sqle.getLocalizedMessage();
}
}

@RequestMapping(path = "/signup", method = RequestMethod.POST, consumes = "text/plain", produces = "text/plain")
public void signUp(@RequestParam(value = "args") String[] arguments) throws ParsingException {
String username = arguments[0];
String password = arguments[1];

LOG.debug("user added:" + username);
System.out.println("USER ADDED: " + username);
LOG.trace(username);
LOG.trace(password);

dbWorker.register(username, password);
}

@RequestMapping(path = "/variable/{variableName}", method = RequestMethod.PUT,
consumes = "text/plain", produces = "text/plain")
public void putVariable(Authentication authentication,
@PathVariable String variableName,
@RequestBody String expr) {
LOG.trace("Creating variable");
Integer userId = dbWorker.getUser(authentication.getName()).getUserId();
try {
calculator.setUserid(userId);
double d = calculator.calculate(new Expression(expr, dbWorker.getUserScope(0).getResult()));

dbWorker.setVariable(variableName, d, userId);
} catch (SQLException e) {
LOG.trace("Failed due to unknown functions");
} catch (ParsingException e) {
LOG.trace("Failed due to incorrect syntax");
}
}


@RequestMapping(path = "/variable/{variableName}", method = RequestMethod.GET,
consumes = "text/plain", produces = "text/plain")
public String getVariable(Authentication authentication, @PathVariable String variableName) {
Integer userId = dbWorker.getUser(authentication.getName()).getUserId();
Double res = dbWorker.getVariable(variableName, userId).getResult();
return res.toString() + "\n";
}

@RequestMapping(path = "/variable/{variableName}", method = RequestMethod.DELETE,
consumes = "text/plain", produces = "text/plain")
public void deleteVariable(Authentication authentication, @PathVariable String variableName) {
Integer userId = dbWorker.getUser(authentication.getName()).getUserId();
dbWorker.deleteVariable(variableName, userId);
}

@RequestMapping(path = "/variable", method = RequestMethod.GET, consumes = "text/plain", produces = "text/plain")
public String getAllVariables(Authentication authentication) {
Integer userId = dbWorker.getUser(authentication.getName()).getUserId();
ArrayList<String> strings = dbWorker.listVariables(userId).getResult();
String res = "";
for (String i : strings) {
res += i;
res += ";";
}
return res + "\n";
}

@RequestMapping(path = "/function/{functionName}", method = RequestMethod.PUT,
consumes = "text/plain", produces = "text/plain")
public void putFunction(Authentication authentication, @PathVariable String functionName,
@RequestParam(value = "arity") Integer arity,
@RequestParam(value = "vars") String vars,
@RequestBody String body) {
Integer userId = dbWorker.getUser(authentication.getName()).getUserId();

try {
dbWorker.setFunction(functionName, body, arity, vars, userId);
} catch (SQLException e) {
LOG.trace("Failed to add function: " + e.getMessage());
}
}

@RequestMapping(path = "/function/{functionName}", method = RequestMethod.DELETE,
consumes = "text/plain", produces = "text/plain")
public void deleteFunction(Authentication authentication,
@RequestParam(value = "arity") Integer argc,
@PathVariable String functionName) {
Integer userId = dbWorker.getUser(authentication.getName()).getUserId();
dbWorker.deleteFunction(functionName, argc, userId);
}

@RequestMapping(path = "/function/{functionName}", method = RequestMethod.GET,
consumes = "text/plain", produces = "text/plain")
public String getFunction(Authentication authentication,
@RequestParam(value = "arity") Integer argc,
@PathVariable String functionName) {
Integer userId = dbWorker.getUser(authentication.getName()).getUserId();
Expression res = dbWorker.getFunction(functionName, argc, userId).getResult();

return res.getName() + " " + res.getExpression() + "\n";
}


}
Loading