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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.idea
.idea
.vscode
5 changes: 4 additions & 1 deletion cmd/rest/main.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
package main

import (
"github.com/wshaman/course-rest/handlers/user"
"log"
"net/http"

"github.com/gorilla/mux"
"github.com/wshaman/course-rest/handlers/user"
)


func main() {
router := mux.NewRouter()
router.HandleFunc("/api/v0/user", user.List).Methods(http.MethodGet)
router.HandleFunc("/api/v0/search", user.Search).Methods(http.MethodGet)
router.HandleFunc("/api/v0/user", user.Create).Methods(http.MethodPost)
router.HandleFunc("/api/v0/user/{id}", user.Delete).Methods(http.MethodDelete)
router.HandleFunc("/api/v0/user/{id}", user.View).Methods(http.MethodGet)
router.HandleFunc("/api/v0/user/{id}", user.Update).Methods(http.MethodPut)

log.Println("Starting API server on 8080")
if err := http.ListenAndServe(":8080", router); err != nil {
log.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/wshaman/course-rest
go 1.14

require (
github.com/gorilla/mux v1.7.4
github.com/gorilla/mux v1.8.0
github.com/stretchr/testify v1.7.0 // indirect
github.com/wshaman/contacts-stub v0.0.0-20210724212216-60df974b9b84
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gorilla/mux v1.7.4 h1:VuZ8uybHlWmqV03+zRzdwKL4tUnIp1MAQtp1mIFE1bc=
github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
Expand Down
1 change: 0 additions & 1 deletion handlers/user/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"net/http"

contacts "github.com/wshaman/contacts-stub"

"github.com/wshaman/course-rest/lib"
)

Expand Down
22 changes: 22 additions & 0 deletions handlers/user/search.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package user

import (
"net/http"

contacts "github.com/wshaman/contacts-stub"
"github.com/wshaman/course-rest/lib"
)

func Search(w http.ResponseWriter, r *http.Request) {
// url transfer as a parameter
query := r.URL.Query()
number := query.Get("number")
p := contacts.LoadPersistent()
// find by phone
res, err := p.FindByPhone(number)
if err != nil {
lib.ReturnInternalError(w, err)
return
}
lib.ReturnJSON(w, res)
}