Skip to content

Commit 030e3dd

Browse files
committed
Simple structure of api, add and edit functionality
1 parent 06833dd commit 030e3dd

File tree

6 files changed

+492
-90
lines changed

6 files changed

+492
-90
lines changed

cmd/main.go

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package main
22

33
import (
4+
"fmt"
5+
"log"
46
"net/http"
57
"time"
68

@@ -10,13 +12,16 @@ import (
1012
func main() {
1113
mux := routes.Init()
1214

13-
server := http.Server{
14-
Addr: ":8080",
15+
srv := http.Server{
16+
Addr: ":8081",
1517
Handler: mux,
1618
ReadTimeout: 5 * time.Second,
1719
WriteTimeout: 10 * time.Second,
1820
MaxHeaderBytes: 8175,
1921
}
20-
21-
server.ListenAndServe()
22+
fmt.Println("Listen on port " + srv.Addr)
23+
err := srv.ListenAndServe()
24+
if err != nil {
25+
log.Println(err)
26+
}
2227
}

pkg/routes/api.go

+22-24
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
11
package routes
22

3-
import "net/http"
4-
5-
func Api(mux *http.ServeMux) *http.ServeMux {
6-
mux.HandleFunc("/edit", edit)
7-
mux.HandleFunc("/add", edit)
8-
mux.HandleFunc("/delete", edit)
9-
return mux
3+
import (
4+
"net/http"
5+
)
6+
7+
type Item struct {
8+
Name string `json:"name"`
9+
Quantity int `json:"quantity"`
10+
Price int `json:"price"`
1011
}
1112

12-
func edit(w http.ResponseWriter, r *http.Request) {
13-
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
14-
15-
// ..
16-
17-
w.WriteHeader(http.StatusOK)
13+
type EditItem struct {
14+
Name string `json:"name"`
15+
Quantity int `json:"quantity"`
16+
Price int `json:"price"`
17+
Query string `json:"query"`
1818
}
1919

20-
func delete(w http.ResponseWriter, r *http.Request) {
21-
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
22-
23-
// ..
24-
25-
w.WriteHeader(http.StatusOK)
20+
type ItemListPrice struct {
21+
Items []Item `json:"item"`
22+
Total int `json:"total"`
2623
}
2724

28-
func add(w http.ResponseWriter, r *http.Request) {
29-
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
30-
31-
// ..
25+
var itemsBunch ItemListPrice
3226

33-
w.WriteHeader(http.StatusOK)
27+
func Api(mux *http.ServeMux) *http.ServeMux {
28+
mux.HandleFunc("/edit", edit)
29+
mux.HandleFunc("/add", add)
30+
mux.HandleFunc("/delete", delete)
31+
return mux
3432
}

pkg/routes/middleware.go

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package routes
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"log"
8+
"net/http"
9+
)
10+
11+
func add(w http.ResponseWriter, r *http.Request) {
12+
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
13+
14+
if r.Method == "POST" {
15+
w.WriteHeader(http.StatusOK)
16+
bodyBytes, _ := ioutil.ReadAll(r.Body)
17+
var element Item
18+
err := json.Unmarshal(bodyBytes, &element)
19+
if err != nil {
20+
log.Println("error:", err)
21+
}
22+
itemsBunch.Items = append(itemsBunch.Items, element)
23+
totalProduct := productTotalPrice(element.Price, element.Quantity)
24+
itemsBunch.Total = newTotal(itemsBunch.Total, totalProduct)
25+
if err := json.NewEncoder(w).Encode(itemsBunch); err != nil {
26+
panic(err)
27+
}
28+
} else {
29+
w.WriteHeader(http.StatusBadRequest)
30+
}
31+
32+
}
33+
34+
func edit(w http.ResponseWriter, r *http.Request) {
35+
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
36+
if r.Method == "POST" {
37+
w.WriteHeader(http.StatusOK)
38+
bodyBytes, _ := ioutil.ReadAll(r.Body)
39+
var elementEdit EditItem
40+
err := json.Unmarshal(bodyBytes, &elementEdit)
41+
if err != nil {
42+
log.Println("error:", err)
43+
}
44+
itemsBunch.Total = 0
45+
for index, element := range itemsBunch.Items {
46+
if element.Name == elementEdit.Query {
47+
itemsBunch.Items[index].Name = elementEdit.Name
48+
itemsBunch.Items[index].Price = elementEdit.Price
49+
itemsBunch.Items[index].Quantity = elementEdit.Quantity
50+
}
51+
totalProduct := productTotalPrice(itemsBunch.Items[index].Price, itemsBunch.Items[index].Quantity)
52+
itemsBunch.Total = newTotal(itemsBunch.Total, totalProduct)
53+
}
54+
elementEdit.Query = ""
55+
if err := json.NewEncoder(w).Encode(itemsBunch); err != nil {
56+
panic(err)
57+
}
58+
} else {
59+
w.WriteHeader(http.StatusBadRequest)
60+
}
61+
}
62+
63+
func delete(w http.ResponseWriter, r *http.Request) {
64+
w.Header().Set("Content-Type", "application/json; charset=UTF-8")
65+
66+
if r.Method == "POST" {
67+
w.WriteHeader(http.StatusOK)
68+
bodyBytes, _ := ioutil.ReadAll(r.Body)
69+
var element EditItem
70+
err := json.Unmarshal(bodyBytes, &element)
71+
if err != nil {
72+
log.Println("error:", err)
73+
}
74+
fmt.Println("delete: " + element.Query)
75+
76+
if err := json.NewEncoder(w).Encode(itemsBunch); err != nil {
77+
panic(err)
78+
}
79+
} else {
80+
w.WriteHeader(http.StatusBadRequest)
81+
}
82+
83+
}
84+
85+
func newTotal(total, newElement int) int {
86+
return total + newElement
87+
}
88+
89+
func productTotalPrice(productPrice, quantity int) int {
90+
return productPrice * quantity
91+
}

web/app/css/style.css

-27
Original file line numberDiff line numberDiff line change
@@ -1,27 +0,0 @@
1-
ul {
2-
list-style-type: none;
3-
}
4-
5-
li {
6-
margin-top: 5px;
7-
}
8-
a {
9-
margin-left: 5px;
10-
margin-right: 5px;
11-
}
12-
13-
table{
14-
margin-top: 20px;
15-
}
16-
17-
th, td{
18-
text-align: center;
19-
}
20-
21-
.name{
22-
width: 50%;
23-
}
24-
25-
.balance{
26-
width: 30%;
27-
}

web/app/index.html

+127-35
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,130 @@
1-
<!DOCTYPE html>
2-
<html>
3-
<body>
4-
<head>
5-
<link rel="stylesheet" type="text/css" href="css/style.css">
6-
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
7-
<script src="js/app.js"></script>
8-
</head>
9-
<h1>Workshop Fundamentos</h1>
10-
11-
<div ng-app="bankApp">
12-
<div align="center" ng-controller="bankCtrl">
13-
<table border="1">
14-
<tr ng-repeat="user in users | orderBy: balance ">
15-
<td class="name"><label></label></td>
16-
<td class="balance"><label></label></td>
17-
<td><a href="#" ng-click="remove(user)">Delete</a></td>
18-
<td><a href="#" ng-click="edit(user)">Edit</a></td>
19-
</tr>
20-
</table>
21-
<hr>
22-
<ul>
23-
<li>
24-
Name:<input type="text" id="name" ng-model="current.name" value="" />
25-
<br>
26-
Balance: <input type="text" id="balance" ng-model="current.balance" value="" />
27-
<br>
28-
</li>
29-
<li>
30-
<button ng-click="save(current) "> Save</button>
31-
<button ng-click="addNew(current) "> Add New User</button>
32-
</li>
33-
</ul>
1+
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
2+
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
3+
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
4+
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
5+
<script src="https://unpkg.com/[email protected]/dist/axios.min.js"></script>
6+
<script src="http://getbootstrap.com/dist/js/bootstrap.min.js"></script>
7+
<script src="./js/app.js"></script>
8+
9+
10+
<!------ Table list ---------->
11+
<div class="container">
12+
<div class="row">
13+
<div class="col-md-12">
14+
<h4>Item list</h4>
15+
<div class="table-responsive">
16+
<table id="mytable" class="table table-bordred table-striped">
17+
<thead>
18+
<th>Item</th>
19+
<th>Quantity</th>
20+
<th>Price</th>
21+
<th>Edit</th>
22+
<th>Delete</th>
23+
</thead>
24+
<tbody id="tableBody">
25+
<tr><td>
26+
<p data-placement="top" data-toggle="tooltip" title="Add"><button class="btn btn-secondary btn-xs" data-title="Add"
27+
data-toggle="modal" data-target="#add"><span class="glyphicon glyphicon-plus"></span></button></p>
28+
</td>
29+
</tr>
30+
</tbody>
31+
</table>
32+
<div class="clearfix"></div>
33+
</div>
3434
</div>
3535
</div>
36+
</div>
37+
38+
<!------ Total ---------->
39+
<div class="container">
40+
<div class="row">
41+
<p id="total">Total:0</p>
42+
</div>
43+
</div>
44+
45+
46+
<!------ Modal add ---------->
47+
<div class="modal fade" id="add" tabindex="-1" role="dialog" aria-labelledby="edit" aria-hidden="true">
48+
<div class="modal-dialog">
49+
<div class="modal-content">
50+
<div class="modal-header">
51+
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove"
52+
aria-hidden="true"></span></button>
53+
<h4 class="modal-title custom_align" id="Heading">Add Item</h4>
54+
</div>
55+
<div class="modal-body">
56+
<div class="form-group">
57+
<input id="addName" class="form-control " type="text" placeholder="item">
58+
</div>
59+
<div class="form-group">
60+
61+
<input id="addQuantity" class="form-control " type="text" placeholder="quantity">
62+
</div>
63+
<div class="form-group">
64+
<input id="addPrice" class="form-control" type="text" placeholder="price">
65+
</div>
66+
</div>
67+
<div class="modal-footer ">
68+
<button type="button" onclick="add(document.getElementById('addName').value,document.getElementById('addQuantity').value,document.getElementById('addPrice').value)" class="btn btn-warning btn-lg" style="width: 100%;"><span class="glyphicon glyphicon-ok-sign"></span> Create</button>
69+
</div>
70+
</div>
71+
<!-- /.modal-content -->
72+
</div>
73+
<!-- /.modal-dialog -->
74+
</div>
75+
3676

37-
</body>
38-
</html>
77+
78+
<!------ Modal edit ---------->
79+
<div class="modal fade" id="edit" tabindex="-1" role="dialog" aria-labelledby="edit" aria-hidden="true">
80+
<div class="modal-dialog">
81+
<div class="modal-content">
82+
<div class="modal-header">
83+
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove"
84+
aria-hidden="true"></span></button>
85+
<h4 class="modal-title custom_align" id="Heading">Edit Item</h4>
86+
</div>
87+
<div class="modal-body">
88+
<div class="form-group">
89+
<input id="editName" class="form-control " type="text" placeholder="item">
90+
</div>
91+
<div class="form-group">
92+
93+
<input id="editQuantity" class="form-control " type="text" placeholder="quantity">
94+
</div>
95+
<div class="form-group">
96+
<input id="editPrice" class="form-control" placeholder="price">
97+
</div>
98+
</div>
99+
<div class="modal-footer ">
100+
<button type="button" onclick="edit(document.getElementById('editName').value,document.getElementById('editQuantity').value,document.getElementById('editPrice').value)" class="btn btn-warning btn-lg" style="width: 100%;"><span class="glyphicon glyphicon-ok-sign"></span> Update</button>
101+
</div>
102+
</div>
103+
<!-- /.modal-content -->
104+
</div>
105+
<!-- /.modal-dialog -->
106+
</div>
107+
108+
109+
<!------ Modal delete ---------->
110+
<div class="modal fade" id="delete" tabindex="-1" role="dialog" aria-labelledby="edit" aria-hidden="true">
111+
<div class="modal-dialog">
112+
<div class="modal-content">
113+
<div class="modal-header">
114+
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span class="glyphicon glyphicon-remove"
115+
aria-hidden="true"></span></button>
116+
<h4 class="modal-title custom_align" id="Heading">Delete this entry</h4>
117+
</div>
118+
<div class="modal-body">
119+
<div class="alert alert-danger"><span class="glyphicon glyphicon-warning-sign"></span> Are you sure you want to delete
120+
this Record?</div>
121+
</div>
122+
<div class="modal-footer ">
123+
<button type="button" onclick="deleteItem()" class="btn btn-success"><span class="glyphicon glyphicon-ok-sign"></span> Yes</button>
124+
<button type="button" class="btn btn-default" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> No</button>
125+
</div>
126+
</div>
127+
<!-- /.modal-content -->
128+
</div>
129+
<!-- /.modal-dialog -->
130+
</div>

0 commit comments

Comments
 (0)