-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added second try for make simple app on golang
- Loading branch information
Showing
11 changed files
with
357 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
examples/golang/cource/06h_http/internals/app/handlers/cars.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"golang/cource/06h_http/internals/app/processors" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
type CarsHandler struct { | ||
processor *processors.CarsProcessor | ||
} | ||
|
||
func NewCarsHandler(processor *processors.CarsProcessor) *CarsHandler { | ||
handler := new(CarsHandler) | ||
handler.processor = processor | ||
return handler | ||
} | ||
|
||
func (handler *CarsHandler) Create(response http.ResponseWriter, request *http.Request) { | ||
var newCar models.Cars | ||
err := json.NewDecoder(request.Body).Decode(&newCar) | ||
if err != nil { | ||
WrapError(response, err) | ||
return | ||
} | ||
|
||
err = handler.processor.CreateCar(newCar) | ||
if err != nil { | ||
WrapError(response, err) | ||
return | ||
} | ||
|
||
var data = map[string]interface{}{ | ||
"result": "OK", | ||
"data": "", | ||
} | ||
|
||
WrapOK(response, data) | ||
} | ||
|
||
func (handler *CarsHandler) List(response http.ResponseWriter, request *http.Request) { | ||
vars := request.URL.Query() | ||
var userIdFilter int64 = 0 | ||
if vars.Get("userid") != "" { | ||
var err error | ||
userIdFilter, err := strconv.ParseInt(vars.Get("userid"), 10, 64) | ||
if err != nil { | ||
WrapError(response, err) | ||
return | ||
} | ||
} | ||
list, err := handler.processor.ListCars(userIdFilter, strings.Trim(vars.Get("brand"), "\""), | ||
strings.Trim(vars.Get("colour"), "\""), strings.Trim(vars.Get("license_plate"), "\"")) | ||
if err != nil { | ||
WrapError(response, err) | ||
} | ||
|
||
var data = map[string]interface{}{ | ||
"result": "OK", | ||
"data": list, | ||
} | ||
|
||
WrapOK(response, data) | ||
} | ||
|
||
func (handler *CarsHandler) Find(response http.ResponseWriter, request *http.Request) { | ||
vars := mux.Vars(request) | ||
if vars["id"] == "" { | ||
WrapError(response, errors.New("Missing ID.")) | ||
return | ||
} | ||
|
||
id, err := strconv.ParseInt(vars["id"], 10, 64) | ||
if err != nil { | ||
WrapError(response, err) | ||
return | ||
} | ||
|
||
user, err := handler.processor.FindCar(id) | ||
if err != nil { | ||
WrapError(response, err) | ||
return | ||
} | ||
|
||
var data = map[string]interface{}{ | ||
"result": "OK", | ||
"data": user, | ||
} | ||
|
||
WrapOK(response, data) | ||
} |
31 changes: 31 additions & 0 deletions
31
examples/golang/cource/06h_http/internals/app/handlers/handler.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
func WrapError(response http.ResponseWriter, err error) { | ||
WrapErrorWithStatus(response, err, http.StatusBadRequest) | ||
} | ||
|
||
func WrapErrorWithStatus(response http.ResponseWriter, err error, httpStatus int) { | ||
var data = map[string]string{ | ||
"result": "error", | ||
"data": err.Error(), | ||
} | ||
|
||
res, _ := json.Marshal(data) | ||
response.Header().Set("Content-Type", "application/json; charset=utf-8") | ||
response.Header().Set("X-Content-Type-Options", "nosniff") | ||
response.WriteHeader(httpStatus) | ||
fmt.Fprintln(response, string(res)) | ||
} | ||
|
||
func WrapOK(response http.ResponseWriter, data map[string]interface{}) { | ||
res, _ := json.Marshal(data) | ||
response.Header().Set("Content-Type", "application/json; charset=utf-8") | ||
response.WriteHeader(http.StatusOK) | ||
fmt.Fprintln(response, string(res)) | ||
} |
10 changes: 10 additions & 0 deletions
10
examples/golang/cource/06h_http/internals/app/handlers/not_found_handler.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package handlers | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
) | ||
|
||
func NotFound(response http.ResponseWriter, request *http.Request) { | ||
WrapErrorWithStatus(response, errors.New("Not found Handler"), http.StatusNotFound) | ||
} |
86 changes: 86 additions & 0 deletions
86
examples/golang/cource/06h_http/internals/app/handlers/users.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package handlers | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"golang/cource/06h_http/internals/app/processors" | ||
"net/http" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/gorilla/mux" | ||
) | ||
|
||
type UsersHandler struct { | ||
processor *processors.UsersProcessor | ||
} | ||
|
||
func NewUsersHandler(processor *processors.UsersProcessor) *UsersHandler { | ||
handler := new(UsersHandler) | ||
handler.processor = processor | ||
return handler | ||
} | ||
|
||
func (handler *UsersHandler) Create(response http.ResponseWriter, request *http.Request) { | ||
var newUser models.User | ||
err := json.NewDecoder(request.Body).Decode(&newUser) | ||
if err != nil { | ||
WrapError(response, err) | ||
return | ||
} | ||
|
||
err = handler.processor.CreateUser(newUser) | ||
if err != nil { | ||
WrapError(response, err) | ||
return | ||
} | ||
|
||
var data = map[string]interface{}{ | ||
"result": "OK", | ||
"data": "", | ||
} | ||
|
||
WrapOK(response, data) | ||
} | ||
|
||
func (handler *UsersHandler) List(response http.ResponseWriter, request *http.Request) { | ||
vars := request.URL.Query() | ||
list, err := handler.processor.ListUsers(strings.Trim(vars.Get("name"), "\"")) | ||
if err != nil { | ||
WrapError(response, err) | ||
} | ||
|
||
var data = map[string]interface{}{ | ||
"result": "OK", | ||
"data": list, | ||
} | ||
|
||
WrapOK(response, data) | ||
} | ||
|
||
func (handler *UsersHandler) Find(response http.ResponseWriter, request *http.Request) { | ||
vars := mux.Vars(request) | ||
if vars["id"] == "" { | ||
WrapError(response, errors.New("Missing ID.")) | ||
return | ||
} | ||
|
||
id, err := strconv.ParseInt(vars["id"], 10, 64) | ||
if err != nil { | ||
WrapError(response, err) | ||
return | ||
} | ||
|
||
user, err := handler.processor.FindUser(id) | ||
if err != nil { | ||
WrapError(response, err) | ||
return | ||
} | ||
|
||
var data = map[string]interface{}{ | ||
"result": "OK", | ||
"data": user, | ||
} | ||
|
||
WrapOK(response, data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
examples/golang/cource/06h_http/internals/app/processors/cars_processor.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package processors | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
type CarsProcessor struct { | ||
storage *db.CarsStorage | ||
} | ||
|
||
func NewCarsProcessor(storage *db.CarsStorage) *CarsProcessor { | ||
processor := new(CarsProcessor) | ||
processor.storage = storage | ||
return processor | ||
} | ||
|
||
func (processor *CarsProcessor) CreateCar(car models.Car) error { | ||
if car.Colour == "" { | ||
return errors.New("Colour should not be empty.") | ||
} | ||
if car.Brand == "" { | ||
return errors.New("Brand should not be empty.") | ||
} | ||
if car.LicensePlate == "" { | ||
return errors.New("License place should not be empty.") | ||
} | ||
if car.Owner.Id <= 0 { | ||
return errors.New("Owner id shall be filled") | ||
} | ||
|
||
return processor.storage.CreateCar(car) | ||
} | ||
|
||
func (processor *CarsProcessor) FindCar(id int64) (models.Car, error) { | ||
user := processor.storage.GetCarById(id) | ||
if car.Id != id { | ||
return car, errors.New("Car not found.") | ||
} | ||
|
||
return car, nil | ||
} | ||
|
||
func (processor *CarsProcessor) ListCars(userId int64, brandFilter string, colourFilter string, licenseFilter string) ([]models.Car, error) { | ||
return processor.storage.GetCarsList(userId, brandFilter, colourFilter, licenseFilter), nil | ||
} |
Oops, something went wrong.