Skip to content

Commit fef4315

Browse files
author
oguzhan.onder
committed
post,get,delete,put methods moved in service object.
1 parent ffa07a2 commit fef4315

File tree

6 files changed

+74
-35
lines changed

6 files changed

+74
-35
lines changed

src/components/Login.vue

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@
3939
login() {
4040
this.$store.dispatch("login", this.loginRequest)
4141
.then(res => {
42-
this.$store.dispatch("getTradeResult");
43-
this.$store.dispatch("initApp");
44-
this.$router.push("/");
42+
if (res) {
43+
this.$store.dispatch("getTradeResult");
44+
this.$store.dispatch("initApp");
45+
this.$router.push("/");
46+
}
4547
})
4648
},
4749
},

src/components/ProductList.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
<thead>
1010
<th>id</th>
1111
<th>Ürün Adı</th>
12-
<th>Adet</th>
12+
<th>Kalan Adet</th>
13+
<th>Satılan Adet</th>
1314
<th>Fiyat</th>
1415
<th>Açıklama</th>
1516
</thead>
@@ -18,6 +19,7 @@
1819
<td class="align-middle text-center"><span class="badge badge-info"> {{product.id}} </span></td>
1920
<td class="align-middle text-center"> {{product.title}}</td>
2021
<td class="align-middle text-center" :class="getCountClass(product.count)"> {{product.count}}</td>
22+
<td class="align-middle text-center"> {{product.sellCount}}</td>
2123
<td style="width: 120px;"> {{product.price | currency}}</td>
2224
<td class="align-middle"> {{product.description}}</td>
2325
</tr>

src/components/Register.vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@
5353
createUser() {
5454
this.$store.dispatch("userRegister", this.user)
5555
.then(res => {
56-
this.$router.push("/login");
56+
if (res) {
57+
this.$router.push("/login");
58+
}
5759
})
5860
}
5961
}

src/store/actions.js

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,64 @@ import * as util from '../util/util'
33
export const setTradeResult = ({state, commit}, tradeResult) => {
44
util.service.post("trade/create", tradeResult)
55
.then(response => {
6-
commit("updateTradeResult", response.data);
7-
});
6+
if (response) {
7+
commit("updateTradeResult", response.data);
8+
}
9+
});
810
};
911

1012
export const getTradeResult = ({commit}) => {
1113
util.service.get("trade/result")
1214
.then(response => {
13-
commit("updateTradeResult", response.data);
15+
if (response) {
16+
commit("updateTradeResult", response.data);
17+
}
1418
}).catch(error => {
1519
util.notify.control(commit, error)
1620
})
1721
};
1822

1923

2024
export const login = (vueContext, loginData) => {
21-
return util.service.post("auth/login",loginData)
25+
let password = loginData.password;
26+
let username = loginData.username;
27+
let rndUser = Math.random().toString(36).substr(2, 6);
28+
let rndPass = Math.random().toString(36).substr(2, 6);
29+
loginData.password = btoa(btoa(password + util.rndData + rndPass));
30+
loginData.username = btoa(btoa(username + util.rndData + rndUser));
31+
32+
return util.service.post("auth/login", loginData)
2233
.then(response => {
23-
vueContext.commit("setIsLogin", true);
24-
localStorage.setItem(util.token, response.data.token);
34+
if (response) {
35+
vueContext.commit("setIsLogin", true);
36+
localStorage.setItem(util.token, response.data.token);
37+
return response;
38+
}
39+
}).catch(error => {
40+
loginData.password = null;
41+
loginData.username = null;
2542
});
2643
};
2744

2845
export const logout = (vueContext) => {
2946
return util.service.post("auth/logout")
30-
.then(res=>{
31-
vueContext.commit("setIsLogin", false);
32-
localStorage.removeItem(util.token);
47+
.then(res => {
48+
if (res) {
49+
vueContext.commit("setIsLogin", false);
50+
localStorage.removeItem(util.token);
51+
return res;
52+
}
3353
});
3454

3555
};
3656

3757
export const userRegister = (vueContext, registerData) => {
38-
return util.service.post("user/registration",registerData);
58+
return util.service.post("user/registration", registerData)
59+
.then(response => {
60+
if (response) {
61+
return response;
62+
}
63+
})
3964
};
4065

4166
export const initIsLogin = (vueContext) => {

src/store/modules/product.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import Vue from 'vue';
21
import router from '../../util/router';
32
import * as util from "../../util/util";
43

@@ -34,37 +33,45 @@ const actions = {
3433
initApp({commit}) {
3534
util.service.get("product/all")
3635
.then(response => {
37-
commit("refreshProducts", response.data);
36+
if (response) {
37+
commit("refreshProducts", response.data);
38+
}
3839
}).catch(error => {
3940
util.notify.control(commit, error)
4041
})
4142
},
4243
saveProduct({dispatch, commit}, product) {
43-
util.service.post("product/create",product)
44+
util.service.post("product/create", product)
4445
.then((response) => {
45-
commit("updateProductList", response.data);
46-
let tradeResult = {
47-
purchase: response.data.price,
48-
sale: 0,
49-
count: response.data.count
50-
};
51-
dispatch("setTradeResult", tradeResult);
52-
router.push("/");
46+
if (response) {
47+
commit("updateProductList", response.data);
48+
let tradeResult = {
49+
purchase: response.data.price,
50+
sale: 0,
51+
count: response.data.count
52+
};
53+
dispatch("setTradeResult", tradeResult);
54+
router.push("/");
55+
}
56+
5357
}).catch(error => {
5458
util.notify.control(commit, error)
5559
})
5660
},
5761
sellProduct({commit, dispatch}, product) {
58-
util.service.put("product/update",product)
62+
util.service.put("product/update", product)
5963
.then(response => {
60-
let tradeResult = {
61-
purchase: 0,
62-
sale: response.data.price,
63-
count: response.data.count
64-
};
65-
product.count = response.data.count;
66-
dispatch("setTradeResult", tradeResult);
67-
router.push("/");
64+
if (response) {
65+
let tradeResult = {
66+
purchase: 0,
67+
sale: response.data.price,
68+
count: response.data.sellCount
69+
};
70+
product.count = response.data.count;
71+
dispatch("setTradeResult", tradeResult);
72+
router.push("/");
73+
}
74+
6875
}).catch(error => {
6976
util.notify.control(commit, error)
7077
});

src/util/util.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import router from '../util/router';
33
import Vue from "vue";
44

55
export const token = "mytoken";
6+
export const rndData = "ond75";
67

78
export const notify = {
89
control(commit, error) {

0 commit comments

Comments
 (0)