Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
f72b251afa | |||
4a8c20ca8b |
@ -17,7 +17,9 @@
|
||||
android:usesCleartextTraffic="true"
|
||||
tools:targetApi="m">
|
||||
<activity android:name=".MainActivity"/>
|
||||
<activity android:name=".FloatingAction" />
|
||||
<activity android:name=".FloatingAction"
|
||||
android:parentActivityName=".MainActivity"
|
||||
/>
|
||||
<activity android:name=".LoginActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
@ -26,7 +28,8 @@
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity android:name=".RegisterActivity"/>
|
||||
<activity android:name=".UserAreaActivity"/>
|
||||
<activity android:name=".UserAreaActivity"
|
||||
android:parentActivityName=".MainActivity"/>
|
||||
<receiver android:name=".ReminderReciver"/>
|
||||
|
||||
</application>
|
||||
|
@ -1,39 +0,0 @@
|
||||
package michalpawlaczyk.shoplist;
|
||||
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
|
||||
public class EditFragment extends Fragment {
|
||||
private Button addListBtn;
|
||||
EditText listNameText;
|
||||
String text;
|
||||
|
||||
|
||||
@Override
|
||||
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.popup_dialog, container, false);
|
||||
addListBtn = view.findViewById(R.id.addListBtn);
|
||||
listNameText = view.findViewById(R.id.listNameText);
|
||||
btnClick();
|
||||
return view;
|
||||
|
||||
}
|
||||
public void btnClick(){
|
||||
addListBtn.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
text = listNameText.getText().toString();
|
||||
}
|
||||
});
|
||||
}
|
||||
public String getText(){
|
||||
return text;
|
||||
}
|
||||
}
|
@ -27,6 +27,7 @@ public class FloatingAction extends AppCompatActivity {
|
||||
ArrayList<String> productItem;
|
||||
ListView listProductView;
|
||||
MainActivity mainActivity;
|
||||
private static final String URL = "http://150.254.129.198:8080";
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
@ -36,9 +37,10 @@ public class FloatingAction extends AppCompatActivity {
|
||||
listNameText = findViewById(R.id.listNameText);
|
||||
listProductView = findViewById(R.id.productsListView);
|
||||
productItem = new ArrayList<>();
|
||||
mainActivity = new MainActivity();
|
||||
listNameText.setHint("Podaj nazwę produktu");
|
||||
AddData();
|
||||
|
||||
|
||||
}
|
||||
public void AddData(){
|
||||
addListBtn.setOnClickListener(new View.OnClickListener() {
|
||||
@ -54,7 +56,7 @@ public class FloatingAction extends AppCompatActivity {
|
||||
postParam.put("rowData", text);
|
||||
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
|
||||
Request.Method.POST,
|
||||
"http://192.168.0.115:8080/addData?"+settings.getString("token",""),
|
||||
URL + "/addData?"+settings.getString("token",""),
|
||||
new JSONObject(postParam),
|
||||
new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
|
@ -28,7 +28,7 @@ import java.util.Map;
|
||||
|
||||
|
||||
public class LoginActivity extends AppCompatActivity {
|
||||
private static final String LOGIN_REQUEST_URL = "http://192.168.0.115:8080/login";
|
||||
private static final String URL = "http://150.254.129.198:8080/login";
|
||||
|
||||
|
||||
@Override
|
||||
@ -67,7 +67,7 @@ public class LoginActivity extends AppCompatActivity {
|
||||
postParam.put("password", password);
|
||||
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
|
||||
Request.Method.POST,
|
||||
LOGIN_REQUEST_URL,
|
||||
URL,
|
||||
new JSONObject(postParam),
|
||||
new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
|
@ -7,6 +7,7 @@ import android.content.SharedPreferences;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.design.widget.FloatingActionButton;
|
||||
import android.support.design.widget.NavigationView;
|
||||
import android.support.v4.app.Fragment;
|
||||
import android.support.v4.view.GravityCompat;
|
||||
import android.support.v4.widget.DrawerLayout;
|
||||
import android.support.v7.app.ActionBarDrawerToggle;
|
||||
@ -62,6 +63,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
String chosenTableID;
|
||||
ImageView accountImage;
|
||||
TextView accountHeader;
|
||||
private static final String URL = "http://150.254.129.198:8080";
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@ -87,14 +89,11 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
|
||||
final SharedPreferences settings = getApplicationContext().getSharedPreferences("userInfo", MODE_PRIVATE);
|
||||
final SharedPreferences lastTable = getApplicationContext().getSharedPreferences("userData", MODE_PRIVATE);
|
||||
|
||||
chosenTableID = lastTable.getString("lastTable","");
|
||||
Toast.makeText(getBaseContext(), chosenListID, Toast.LENGTH_LONG).show();
|
||||
if(chosenTableID != null)
|
||||
viewTableData(chosenTableID);
|
||||
if(settings.getString("username","") != null){
|
||||
accountHeader.setText(settings.getString("username",""));
|
||||
accountHeader.setText(settings.getString("username",""));
|
||||
}
|
||||
|
||||
fab.setOnClickListener(new View.OnClickListener() {
|
||||
@ -109,14 +108,9 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
accountImage.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
final SharedPreferences settings = getApplicationContext().getSharedPreferences("userInfo", MODE_PRIVATE);
|
||||
if(settings.getString("username","") == null) {
|
||||
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
|
||||
getApplicationContext().startActivity(intent);
|
||||
} else {
|
||||
Intent intent = new Intent(getApplicationContext(), UserAreaActivity.class);
|
||||
getApplicationContext().startActivity(intent);
|
||||
}
|
||||
Intent intent = new Intent(getApplicationContext(), UserAreaActivity.class);
|
||||
getApplicationContext().startActivity(intent);
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
@ -131,7 +125,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
postParam.put("isCheck", isCheck.toString());
|
||||
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
|
||||
Request.Method.POST,
|
||||
"http://192.168.0.115:8080/setItemCheck?" + settings.getString("token", ""),
|
||||
URL + "/setItemCheck?" + settings.getString("token", ""),
|
||||
new JSONObject(postParam),
|
||||
new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
@ -159,17 +153,20 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
|
||||
|
||||
listNameView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
|
||||
@SuppressLint("RestrictedApi")
|
||||
@Override
|
||||
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
|
||||
listProductView.setVisibility(View.VISIBLE);
|
||||
fab.setVisibility(View.VISIBLE);
|
||||
chosenListID = parent.getItemAtPosition(position).toString();
|
||||
//((FrameLayout)findViewById(R.id.content_frame)).removeAllViews();
|
||||
for (Fragment fragment:getSupportFragmentManager().getFragments()) {
|
||||
|
||||
getSupportFragmentManager().beginTransaction().remove(fragment).commit();
|
||||
}
|
||||
final SharedPreferences settings = getApplicationContext().getSharedPreferences("userData", MODE_PRIVATE);
|
||||
@SuppressLint("CommitPrefEdits") SharedPreferences.Editor editor = settings.edit();
|
||||
editor.putString("lastTable", chosenListID);
|
||||
viewTableData(chosenListID);
|
||||
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
listNameView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
|
||||
@ -183,7 +180,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
Map<String, String> postParam= new HashMap<>();
|
||||
postParam.put("tableName", deleteTableID);
|
||||
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
|
||||
"http://192.168.0.115:8080/deleteTable?" + settings.getString("token", ""),
|
||||
URL + "/deleteTable?" + settings.getString("token", ""),
|
||||
new JSONObject(postParam),
|
||||
new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
@ -223,7 +220,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
postParam.put("rowData", listProductID);
|
||||
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
|
||||
Request.Method.POST,
|
||||
"http://192.168.0.115:8080/deleteData?" + settings.getString("token", ""),
|
||||
URL + "/deleteData?" + settings.getString("token", ""),
|
||||
new JSONObject(postParam),
|
||||
new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
@ -255,7 +252,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
postParam.put("rowData", listProductID);
|
||||
postParam.put("newRow", editText.getText().toString());
|
||||
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
|
||||
"http://192.168.0.115:8080/editData?" + settings.getString("token", ""),
|
||||
URL + "/editData?" + settings.getString("token", ""),
|
||||
new JSONObject(postParam),
|
||||
new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
@ -288,10 +285,13 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
}
|
||||
|
||||
//Navigation items
|
||||
@SuppressLint("RestrictedApi")
|
||||
@Override
|
||||
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
|
||||
switch (menuItem.getItemId()){
|
||||
case R.id.AddList:
|
||||
listProductView.setVisibility(View.GONE);
|
||||
fab.setVisibility(View.GONE);
|
||||
((FrameLayout)findViewById(R.id.content_frame)).removeAllViews();
|
||||
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame,
|
||||
new MenuFragment()).commit();
|
||||
@ -301,6 +301,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
getTableName();
|
||||
break;
|
||||
case R.id.Reminder:
|
||||
listProductView.setVisibility(View.GONE);
|
||||
fab.setVisibility(View.GONE);
|
||||
((FrameLayout)findViewById(R.id.content_frame)).removeAllViews();
|
||||
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame,
|
||||
new ReminderFragment()).commit();
|
||||
@ -316,7 +318,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
|
||||
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(
|
||||
Request.Method.POST,
|
||||
"http://192.168.0.115:8080/getTableRows?" + settings.getString("token", "")+":"+tableName, null,
|
||||
URL + "/getTableRows?" + settings.getString("token", "")+":"+tableName, null,
|
||||
new Response.Listener<JSONArray>() {
|
||||
@Override
|
||||
public void onResponse(JSONArray response) {
|
||||
@ -375,12 +377,12 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
|
||||
queue.add(jsonObjectRequest);
|
||||
}
|
||||
|
||||
private void getTableName() {
|
||||
public void getTableName() {
|
||||
listItem = new ArrayList<>();
|
||||
final SharedPreferences settings = getApplicationContext().getSharedPreferences("userInfo", MODE_PRIVATE);
|
||||
JsonArrayRequest jsonObjectRequest = new JsonArrayRequest(
|
||||
Request.Method.POST,
|
||||
"http://192.168.0.115:8080/getTableName?"+settings.getString("token",""), null,
|
||||
URL + "/getTableName?"+settings.getString("token",""), null,
|
||||
new Response.Listener<JSONArray>() {
|
||||
@Override
|
||||
public void onResponse(JSONArray response) {
|
||||
|
@ -28,6 +28,7 @@ public class MenuFragment extends Fragment{
|
||||
|
||||
private Button addListBtn;
|
||||
EditText listNameText;
|
||||
private static final String URL = "http://150.254.129.198:8080";
|
||||
|
||||
|
||||
@Override
|
||||
@ -37,6 +38,7 @@ public class MenuFragment extends Fragment{
|
||||
listNameText = view.findViewById(R.id.listNameText);
|
||||
listNameText.setHint("Podaj nazwę listy");
|
||||
AddTable();
|
||||
((MainActivity) Objects.requireNonNull(getActivity())).getTableName();
|
||||
return view;
|
||||
|
||||
}
|
||||
@ -50,7 +52,7 @@ public class MenuFragment extends Fragment{
|
||||
postParam.put("tableName", tableName);
|
||||
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
|
||||
Request.Method.POST,
|
||||
"http://192.168.0.115:8080/addTable?"+settings.getString("token",""),
|
||||
URL + "/addTable?"+settings.getString("token",""),
|
||||
new JSONObject(postParam),
|
||||
new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
|
@ -30,7 +30,7 @@ import java.util.Map;
|
||||
|
||||
public class RegisterActivity extends AppCompatActivity {
|
||||
|
||||
private static final String REGISTER_REQUEST_URL = "http://192.168.0.115:8080/register";
|
||||
private static final String URL = "http://150.254.129.198:8080/register";
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
@ -64,7 +64,7 @@ public class RegisterActivity extends AppCompatActivity {
|
||||
postParam.put("password", password);
|
||||
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
|
||||
Request.Method.POST,
|
||||
REGISTER_REQUEST_URL,
|
||||
URL,
|
||||
new JSONObject(postParam),
|
||||
new Response.Listener<JSONObject>() {
|
||||
@Override
|
||||
|
@ -8,31 +8,36 @@
|
||||
android:fitsSystemWindows="true"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/content_frame"
|
||||
<android.support.design.widget.CoordinatorLayout
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/content_frame"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"/>
|
||||
|
||||
|
||||
<android.support.design.widget.FloatingActionButton
|
||||
android:id="@+id/fab"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginEnd="25dp"
|
||||
android:layout_marginBottom="25dp"
|
||||
android:layout_gravity="end|bottom"
|
||||
android:src="@drawable/ic_baseline_add_24px"
|
||||
android:layout_margin="30dp"
|
||||
app:backgroundTint="@color/colorPrimary" />
|
||||
|
||||
<ListView
|
||||
android:id="@+id/productsListView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/productsListView"
|
||||
android:layout_marginStart="0dp"
|
||||
android:layout_marginTop="0dp"
|
||||
android:layout_marginEnd="0dp"
|
||||
android:layout_marginBottom="0dp"
|
||||
android:choiceMode="singleChoice" />
|
||||
|
||||
|
||||
</FrameLayout>
|
||||
|
||||
</android.support.design.widget.CoordinatorLayout>
|
||||
|
||||
<android.support.design.widget.NavigationView
|
||||
android:layout_width="wrap_content"
|
||||
|
32
app/src/main/res/layout/product_listview.xml
Normal file
32
app/src/main/res/layout/product_listview.xml
Normal file
@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<android.support.constraint.ConstraintLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:id="@+id/productListView">
|
||||
|
||||
<android.support.design.widget.FloatingActionButton
|
||||
android:id="@+id/fab"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_gravity="end|bottom"
|
||||
android:layout_margin="30dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:layout_marginBottom="8dp"
|
||||
android:backgroundTint="@color/colorPrimary"
|
||||
android:src="@drawable/ic_baseline_add_24px"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent" />
|
||||
|
||||
<ListView
|
||||
android:id="@+id/productsListView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:layout_marginStart="0dp"
|
||||
android:layout_marginTop="0dp"
|
||||
android:layout_marginEnd="0dp"
|
||||
android:layout_marginBottom="0dp"
|
||||
android:choiceMode="singleChoice" />
|
||||
|
||||
</android.support.constraint.ConstraintLayout>
|
@ -7,7 +7,7 @@ buildscript {
|
||||
jcenter()
|
||||
}
|
||||
dependencies {
|
||||
classpath 'com.android.tools.build:gradle:3.2.1'
|
||||
classpath 'com.android.tools.build:gradle:3.3.0'
|
||||
|
||||
|
||||
// NOTE: Do not place your application dependencies here; they belong
|
||||
|
3
gradle/wrapper/gradle-wrapper.properties
vendored
3
gradle/wrapper/gradle-wrapper.properties
vendored
@ -1,5 +1,6 @@
|
||||
#Thu Jan 17 17:33:40 CET 2019
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
|
||||
|
405
main.go
Normal file
405
main.go
Normal file
@ -0,0 +1,405 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/elgs/gosqljson"
|
||||
_ "github.com/lib/pq"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type registerStruct struct {
|
||||
Login string `json:"login"`
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
|
||||
}
|
||||
|
||||
type loginStruct struct{
|
||||
Login string `json:"login"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
type dataStruct struct{
|
||||
TableName string `json:"tableName"`
|
||||
RowData string `json:"rowData"`
|
||||
}
|
||||
type itemCheckStruct struct{
|
||||
TableName string `json:"tableName"`
|
||||
TableRow string `json:"productName"`
|
||||
IsCheck string `json:"isCheck"`
|
||||
}
|
||||
type tableNameStruct struct{
|
||||
TableName string `json:"tableName"`
|
||||
}
|
||||
type editRowStruct struct {
|
||||
TableName string `json:"tableName"`
|
||||
TableRow string `json:"rowData"`
|
||||
NewTableRow string `json:"newRow"`
|
||||
}
|
||||
|
||||
func checkPassword (username string, password string) bool{
|
||||
|
||||
database, _ := sql.Open("sqlite3", "./users.db")
|
||||
result := database.QueryRow("SELECT password FROM users WHERE login=?", username)
|
||||
dbLogin := &loginStruct{}
|
||||
err := result.Scan(&dbLogin.Password)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
return false
|
||||
}
|
||||
|
||||
if err = bcrypt.CompareHashAndPassword([]byte(dbLogin.Password), []byte(password)); err != nil{
|
||||
return false
|
||||
} else{
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
func register (writer http.ResponseWriter, request *http.Request) {
|
||||
register := ®isterStruct{}
|
||||
database, _ := sql.Open("sqlite3", "./users.db")
|
||||
if request.Body == nil {
|
||||
writer.WriteHeader(http.StatusBadRequest)
|
||||
fmt.Println("Empty JSON")
|
||||
return
|
||||
}
|
||||
err := json.NewDecoder(request.Body).Decode(register)
|
||||
if err != nil {
|
||||
writer.WriteHeader(http.StatusBadRequest)
|
||||
fmt.Println("JSON Parse fail")
|
||||
return
|
||||
}
|
||||
fmt.Println(register)
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(register.Password), 8)
|
||||
if _, err = database.Exec("INSERT INTO users VALUES (?, ?, ?)",register.Login, register.Email, string(hashedPassword)); err != nil {
|
||||
writer.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
_, _ = sql.Open("sqlite3", "./"+register.Login+".db")
|
||||
|
||||
}
|
||||
|
||||
func login (writer http.ResponseWriter, request *http.Request) {
|
||||
database, _ := sql.Open("sqlite3", "./users.db")
|
||||
login := &loginStruct{}
|
||||
err := json.NewDecoder(request.Body).Decode(login)
|
||||
if err != nil {
|
||||
writer.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
result := database.QueryRow("SELECT password FROM users WHERE login=?", login.Login)
|
||||
dbLogin := &loginStruct{}
|
||||
err = result.Scan(&dbLogin.Password)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows{
|
||||
writer.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
writer.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err = bcrypt.CompareHashAndPassword([]byte(dbLogin.Password), []byte(login.Password)); err != nil {
|
||||
writer.WriteHeader(http.StatusUnauthorized)
|
||||
} else{
|
||||
writer.WriteHeader(http.StatusOK)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
database, _ := sql.Open("sqlite3", "./users.db")
|
||||
statement, _ := database.Prepare("CREATE TABLE IF NOT EXISTS users (login TEXT PRIMARY KEY, email TEXT, password TEXT)")
|
||||
statement.Exec()
|
||||
http.HandleFunc("/login", login)
|
||||
http.HandleFunc("/register", register)
|
||||
|
||||
http.HandleFunc("/addData", func(writer http.ResponseWriter, request *http.Request) {
|
||||
|
||||
token, err := base64.StdEncoding.DecodeString(request.URL.RawQuery)
|
||||
if err != nil{
|
||||
panic(err)
|
||||
}
|
||||
|
||||
decodeToken := strings.Split(string(token), ":")
|
||||
username := decodeToken[0]
|
||||
password := decodeToken[1]
|
||||
|
||||
if checkPassword(username, password) {
|
||||
database, _ = sql.Open("sqlite3", "./"+username+".db")
|
||||
data := &dataStruct{}
|
||||
err = json.NewDecoder(request.Body).Decode(data)
|
||||
if err != nil {
|
||||
writer.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
fmt.Println(data)
|
||||
_, err = database.Exec("CREATE TABLE IF NOT EXISTS " + data.TableName + " (product TEXT, isCheck TEXT)")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
_, err = database.Exec("INSERT INTO "+data.TableName+" VALUES (?,?)", data.RowData, "false")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else{
|
||||
writer.WriteHeader(http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
|
||||
})
|
||||
|
||||
http.HandleFunc("/deleteData", func(writer http.ResponseWriter, request *http.Request) {
|
||||
token, err := base64.StdEncoding.DecodeString(request.URL.RawQuery)
|
||||
if err != nil{
|
||||
panic(err)
|
||||
}
|
||||
|
||||
decodeToken := strings.Split(string(token), ":")
|
||||
username := decodeToken[0]
|
||||
password := decodeToken[1]
|
||||
|
||||
if checkPassword(username, password) {
|
||||
database, _ = sql.Open("sqlite3", "./"+username+".db")
|
||||
data := &dataStruct{}
|
||||
err := json.NewDecoder(request.Body).Decode(data)
|
||||
if err != nil {
|
||||
writer.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
fmt.Println(data)
|
||||
|
||||
_, err = database.Exec("DELETE FROM "+data.TableName+" WHERE product = ?", data.RowData)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
writer.WriteHeader(http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
http.HandleFunc("/updateData", func(writer http.ResponseWriter, request *http.Request) {
|
||||
token, err := base64.StdEncoding.DecodeString(request.URL.RawQuery)
|
||||
if err != nil{
|
||||
panic(err)
|
||||
}
|
||||
|
||||
decodeToken := strings.Split(string(token), ":")
|
||||
username := decodeToken[0]
|
||||
password := decodeToken[1]
|
||||
|
||||
if checkPassword(username, password) {
|
||||
database, _ = sql.Open("sqlite3", "./"+username+".db")
|
||||
data := &dataStruct{}
|
||||
err := json.NewDecoder(request.Body).Decode(data)
|
||||
if err != nil {
|
||||
writer.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
_, err = database.Exec("UPDATE "+data.TableName+" SET product = ? WHERE product = ?", data.RowData, data.RowData)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else{
|
||||
writer.WriteHeader(http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
})
|
||||
http.HandleFunc("/deleteTable", func(writer http.ResponseWriter, request *http.Request) {
|
||||
token, err := base64.StdEncoding.DecodeString(request.URL.RawQuery)
|
||||
if err != nil{
|
||||
panic(err)
|
||||
}
|
||||
|
||||
decodeToken := strings.Split(string(token), ":")
|
||||
username := decodeToken[0]
|
||||
password := decodeToken[1]
|
||||
|
||||
if checkPassword(username, password) {
|
||||
database, _ = sql.Open("sqlite3", "./"+username+".db")
|
||||
data := &tableNameStruct{}
|
||||
err := json.NewDecoder(request.Body).Decode(data)
|
||||
if err != nil {
|
||||
writer.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
fmt.Println(data)
|
||||
_, err = database.Exec("DROP TABLE "+data.TableName)
|
||||
if err != nil{
|
||||
panic(err)
|
||||
}
|
||||
} else{
|
||||
writer.WriteHeader(http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
})
|
||||
http.HandleFunc("/addTable", func(writer http.ResponseWriter, request *http.Request) {
|
||||
token, err := base64.StdEncoding.DecodeString(request.URL.RawQuery)
|
||||
if err != nil{
|
||||
panic(err)
|
||||
}
|
||||
|
||||
decodeToken := strings.Split(string(token), ":")
|
||||
username := decodeToken[0]
|
||||
password := decodeToken[1]
|
||||
|
||||
if checkPassword(username, password) {
|
||||
database, _ = sql.Open("sqlite3", "./"+username+".db")
|
||||
data := &tableNameStruct{}
|
||||
err := json.NewDecoder(request.Body).Decode(data)
|
||||
if err != nil {
|
||||
writer.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
fmt.Println(data)
|
||||
|
||||
|
||||
_, err = database.Exec("CREATE TABLE IF NOT EXISTS " + data.TableName + " (product TEXT, isCheck TEXT)")
|
||||
if err != nil{
|
||||
panic(err)
|
||||
}
|
||||
} else{
|
||||
writer.WriteHeader(http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
|
||||
})
|
||||
|
||||
http.HandleFunc("/getTableName", func(writer http.ResponseWriter, request *http.Request) {
|
||||
|
||||
|
||||
token, err := base64.StdEncoding.DecodeString(request.URL.RawQuery)
|
||||
decodeToken := strings.Split(string(token), ":")
|
||||
username := decodeToken[0]
|
||||
password := decodeToken[1]
|
||||
|
||||
fmt.Println(username +":"+password)
|
||||
|
||||
if checkPassword(username, password) {
|
||||
database, err = sql.Open("sqlite3", "./"+username+".db")
|
||||
|
||||
if err != nil {
|
||||
writer.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
m, _ := gosqljson.QueryDbToMapJSON(database, "lower", "SELECT name FROM sqlite_master WHERE type='table'")
|
||||
writer.Header().Set("Content-Type", "application/json")
|
||||
_, err = writer.Write([]byte(m))
|
||||
if err != nil{
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("Json send")
|
||||
fmt.Println(m)
|
||||
|
||||
} else{
|
||||
writer.WriteHeader(http.StatusUnauthorized)
|
||||
}
|
||||
})
|
||||
|
||||
http.HandleFunc("/getTableRows", func(writer http.ResponseWriter, request *http.Request) {
|
||||
|
||||
urlTable := strings.Split(request.URL.RawQuery,":")
|
||||
url := urlTable[0]
|
||||
tableName := urlTable[1]
|
||||
token, err := base64.StdEncoding.DecodeString(url)
|
||||
decodeToken := strings.Split(string(token), ":")
|
||||
username := decodeToken[0]
|
||||
password := decodeToken[1]
|
||||
|
||||
fmt.Println(username +":"+password)
|
||||
|
||||
if checkPassword(username, password) {
|
||||
database, err = sql.Open("sqlite3", "./"+username+".db")
|
||||
if err != nil {
|
||||
writer.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
m, _ := gosqljson.QueryDbToMapJSON(database, "lower", "SELECT * FROM "+tableName)
|
||||
writer.Header().Set("Content-Type", "application/json")
|
||||
_, err = writer.Write([]byte(m))
|
||||
if err != nil{
|
||||
panic(err)
|
||||
}
|
||||
fmt.Println("Json send")
|
||||
fmt.Println(m)
|
||||
|
||||
} else{
|
||||
writer.WriteHeader(http.StatusUnauthorized)
|
||||
}
|
||||
})
|
||||
|
||||
http.HandleFunc("/setItemCheck", func(writer http.ResponseWriter, request *http.Request) {
|
||||
token, err := base64.StdEncoding.DecodeString(request.URL.RawQuery)
|
||||
if err != nil{
|
||||
panic(err)
|
||||
}
|
||||
|
||||
decodeToken := strings.Split(string(token), ":")
|
||||
username := decodeToken[0]
|
||||
password := decodeToken[1]
|
||||
|
||||
if checkPassword(username, password) {
|
||||
database, _ = sql.Open("sqlite3", "./"+username+".db")
|
||||
data := &itemCheckStruct{}
|
||||
err := json.NewDecoder(request.Body).Decode(data)
|
||||
if err != nil {
|
||||
writer.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
fmt.Println(data)
|
||||
|
||||
_, err = database.Exec("UPDATE "+data.TableName+" SET isCheck = ? WHERE product = ?", data.IsCheck, data.TableRow)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
writer.WriteHeader(http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
http.HandleFunc("/editData", func(writer http.ResponseWriter, request *http.Request) {
|
||||
token, err := base64.StdEncoding.DecodeString(request.URL.RawQuery)
|
||||
if err != nil{
|
||||
panic(err)
|
||||
}
|
||||
|
||||
decodeToken := strings.Split(string(token), ":")
|
||||
username := decodeToken[0]
|
||||
password := decodeToken[1]
|
||||
|
||||
if checkPassword(username, password) {
|
||||
database, _ = sql.Open("sqlite3", "./"+username+".db")
|
||||
data := &editRowStruct{}
|
||||
err := json.NewDecoder(request.Body).Decode(data)
|
||||
if err != nil {
|
||||
writer.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
fmt.Println(data)
|
||||
|
||||
_, err = database.Exec("UPDATE "+data.TableName+" SET product = ? WHERE product = ?", data.NewTableRow, data.TableRow)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
} else {
|
||||
writer.WriteHeader(http.StatusUnauthorized)
|
||||
}
|
||||
|
||||
})
|
||||
|
||||
|
||||
http.ListenAndServe(":8080", nil)
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user